]> git.sur5r.net Git - minitube/blob - src/thlibrary/thblackbar.cpp
0043fc57c54db995ab4910d62d21915382feae3d
[minitube] / src / thlibrary / thblackbar.cpp
1 #include <QPainter>
2 #include <QPaintEvent>
3 #include <QList>
4 #include <QtGui>
5
6 #include "thblackbar.h"
7
8 /* ============================================================================
9  *  PRIVATE Class
10  */
11 class THBlackBar::Private {
12         public:
13     QList<QAction *> actionList;
14     QAction *checkedAction;
15     QAction *hoveredAction;
16 };
17
18 /* ============================================================================
19  *  PUBLIC Constructor/Destructors
20  */
21 THBlackBar::THBlackBar (QWidget *parent)
22         : QWidget(parent), d(new THBlackBar::Private)
23 {
24     // Setup Widget Options
25     setMouseTracking(true);
26
27     // Setup Members
28     d->hoveredAction = NULL;
29     d->checkedAction = NULL;
30 }
31
32 THBlackBar::~THBlackBar() {
33     delete d;
34 }
35
36 /* ============================================================================
37  *  PUBLIC Methods
38  */
39 QAction *THBlackBar::addAction (QAction *action) {
40     action->setCheckable(true);
41     d->actionList.append(action);
42     return(action);
43 }
44
45 QAction *THBlackBar::addAction (const QString& text) {
46     QAction *action = new QAction(text, this);
47     action->setCheckable(true);
48     d->actionList.append(action);
49     return(action);
50 }
51
52 void THBlackBar::setCheckedAction(int index) {
53     if (d->checkedAction)
54         d->checkedAction->setChecked(false);
55     d->checkedAction = d->actionList.at(index);
56     d->checkedAction->setChecked(true);
57     update();
58 }
59
60 QSize THBlackBar::minimumSizeHint (void) const {        
61     int itemsWidth = calculateButtonWidth() * d->actionList.size();
62     return(QSize(100 + itemsWidth, 32));
63 }
64
65 /* ============================================================================
66  *  PROTECTED Methods
67  */
68 void THBlackBar::paintEvent (QPaintEvent *event) {
69     int height = event->rect().height();
70     int width = event->rect().width();
71     // int mh = (height / 2);
72
73     // THPainter p(this);
74     QPainter p(this);
75
76     /*
77     // Draw Background
78     QLinearGradient linearGradUp(QPointF(0, 0), QPointF(0, mh));
79     linearGradUp.setColorAt(0, QColor(0x97, 0x97, 0x97));
80     linearGradUp.setColorAt(1, QColor(0x4d, 0x4d, 0x4d));
81     p.fillRect(0, 0, width, mh, QBrush(linearGradUp));
82
83     QLinearGradient linearGradDw(QPointF(0, mh), QPointF(0, height));
84     linearGradDw.setColorAt(0, QColor(0x3a, 0x3a, 0x3a));
85     linearGradDw.setColorAt(1, QColor(0x42, 0x42, 0x42));
86     p.fillRect(0, mh, width, mh, QBrush(linearGradDw));
87     */
88     
89     // Calculate Buttons Size & Location
90     int buttonWidth = width / d->actionList.size(); // calculateButtonWidth();
91     // int buttonsWidth = width; // buttonWidth * d->actionList.size();
92     int buttonsX = 0; // (width / 2) - (buttonsWidth / 2);
93
94     // Draw Buttons
95     // p.translate(0, 4);
96     QRect rect(buttonsX, 0, buttonWidth, height);
97     foreach (QAction *action, d->actionList) {
98         drawButton(&p, rect, action);
99         rect.moveLeft(rect.x() + rect.width());
100     }
101     // p.translate(0, -4);
102
103     // Draw Buttons Shadow
104     // p.fillRect(buttonsX, height - 4, buttonsWidth, 1, QColor(0x6d, 0x6d, 0x6d));
105
106     p.end();
107 }
108
109 void THBlackBar::mouseMoveEvent (QMouseEvent *event) {
110     QWidget::mouseMoveEvent(event);
111
112     QAction *action = hoveredAction(event->pos());
113
114     if (action == NULL && d->hoveredAction != NULL) {
115         // d->hoveredAction->hover(false);
116         d->hoveredAction = NULL;
117         update();
118     } else if (action != NULL) {
119         d->hoveredAction = action;
120         action->hover();
121         update();
122
123         // status tip
124         QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
125         if (mainWindow) mainWindow->statusBar()->showMessage(action->statusTip());
126     }
127 }
128
129 void THBlackBar::mousePressEvent (QMouseEvent *event) {
130     QWidget::mousePressEvent(event);
131
132     if (d->hoveredAction != NULL) {
133
134         if (d->checkedAction != NULL) {
135             // already checked
136             if (d->checkedAction == d->hoveredAction) return;
137             d->checkedAction->setChecked(false);
138         }
139
140         d->checkedAction = d->hoveredAction;
141         d->hoveredAction->setChecked(true);
142         d->hoveredAction->trigger();
143
144         update();
145     }
146 }
147
148 void THBlackBar::leaveEvent(QEvent *event) {
149     // status tip
150     QMainWindow* mainWindow = static_cast<QMainWindow*>(window());
151     if (mainWindow) mainWindow->statusBar()->clearMessage();
152 }
153
154 QAction *THBlackBar::hoveredAction (const QPoint& pos) const {
155     if (pos.y() <= 0 || pos.y() >= height())
156         return(NULL);
157
158     /*
159     int buttonWidth = calculateButtonWidth();
160     int buttonsWidth = buttonWidth * d->actionList.size();
161     int buttonsX = (width() / 2) - (buttonsWidth / 2);
162     */
163     
164     int buttonWidth = width() / d->actionList.size(); // calculateButtonWidth();
165     int buttonsWidth = width(); // buttonWidth * d->actionList.size();
166     int buttonsX = 0; // (width / 2) - (buttonsWidth / 2);
167     
168     if (pos.x() <= buttonsX || pos.x() >= (buttonsX + buttonsWidth))
169         return(NULL);
170
171     int buttonIndex = (pos.x() - buttonsX) / buttonWidth;
172
173     if (buttonIndex >= d->actionList.size())
174         return(NULL);
175     return(d->actionList[buttonIndex]);
176 }
177
178 int THBlackBar::calculateButtonWidth (void) const {
179     QFont smallerBoldFont;
180     smallerBoldFont.setBold(true);
181     smallerBoldFont.setPointSize(smallerBoldFont.pointSize()*.85);
182     QFontMetrics fontMetrics(smallerBoldFont);
183     int tmpItemWidth, itemWidth = 0;
184     foreach (QAction *action, d->actionList) {
185         tmpItemWidth = fontMetrics.width(action->text());
186         if (itemWidth < tmpItemWidth) itemWidth = tmpItemWidth;
187     }
188     return itemWidth;
189 }
190
191
192 /* ============================================================================
193  *  PRIVATE Methods
194  */
195 void THBlackBar::drawUnselectedButton ( QPainter *painter,
196                                         const QRect& rect,
197                                         const QAction *action)
198 {
199     QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, rect.height() / 2));    
200     linearGrad.setColorAt(0, QColor(0x8e, 0x8e, 0x8e));
201     linearGrad.setColorAt(1, QColor(0x5c, 0x5c, 0x5c));
202     /*
203     QPalette palette;
204     linearGrad.setColorAt(0, palette.color(QPalette::Dark));
205     linearGrad.setColorAt(1, palette.color(QPalette::Midlight));
206 */
207     drawButton(painter, rect, linearGrad, QColor(0x41, 0x41, 0x41), action);
208     // drawButton(painter, rect, linearGrad, palette.color(QPalette::Shadow), action);
209 }
210
211 void THBlackBar::drawSelectedButton (   QPainter *painter,
212                                         const QRect& rect,
213                                         const QAction *action)
214 {
215     QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, rect.height() / 2));
216     linearGrad.setColorAt(0, QColor(0x6d, 0x6d, 0x6d));
217     linearGrad.setColorAt(1, QColor(0x25, 0x25, 0x25));
218     drawButton(painter, rect, linearGrad, QColor(0x00, 0x00, 0x00), action);
219 }
220
221 void THBlackBar::drawButton (   QPainter *painter,
222                                 const QRect& rect,
223                                 const QAction *action)
224 {
225     if (action->isChecked())
226         drawSelectedButton(painter, rect, action);
227     else
228         drawUnselectedButton(painter, rect, action);
229 }
230
231 void THBlackBar::drawButton (   QPainter *painter, 
232                                 const QRect& rect,
233                                 const QLinearGradient& gradient,
234                                 const QColor& color,
235                                 const QAction *action)
236 {
237     painter->save();
238
239     int height = rect.height();
240     int width = rect.width();
241     int mh = (height / 2);
242
243     painter->translate(rect.x(), rect.y());
244     painter->setPen(QColor(0x28, 0x28, 0x28));
245
246     painter->fillRect(0, 0, width, mh, QBrush(gradient));
247     painter->fillRect(0, mh, width, mh, color);
248     painter->drawRect(0, 0, width, height);
249
250     QFont smallerBoldFont;
251     smallerBoldFont.setBold(true);
252     smallerBoldFont.setPointSize(smallerBoldFont.pointSize()*.85);
253     painter->setFont(smallerBoldFont);
254     painter->setPen(QPen(QColor(0xff, 0xff, 0xff), 1));
255     painter->drawText(0, 1, width, height, Qt::AlignCenter, action->text());
256
257     painter->restore();
258 }
259