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