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