]> git.sur5r.net Git - minitube/blob - src/segmentedcontrol.cpp
Clear status tip
[minitube] / src / segmentedcontrol.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "segmentedcontrol.h"
22 #include "mainwindow.h"
23 #include "painterutils.h"
24
25 static const QColor borderColor = QColor(0x26, 0x26, 0x26);
26
27 class SegmentedControl::Private {
28 public:
29     QList<QAction *> actionList;
30     QAction *checkedAction;
31     QAction *hoveredAction;
32     QAction *pressedAction;
33 };
34
35 SegmentedControl::SegmentedControl (QWidget *parent)
36     : QWidget(parent), d(new SegmentedControl::Private) {
37
38     setMouseTracking(true);
39
40     d->hoveredAction = 0;
41     d->checkedAction = 0;
42     d->pressedAction = 0;
43 }
44
45 SegmentedControl::~SegmentedControl() {
46     delete d;
47 }
48
49 QAction *SegmentedControl::addAction(QAction *action) {
50     QWidget::addAction(action);
51     action->setCheckable(true);
52     d->actionList.append(action);
53     return action;
54 }
55
56 bool SegmentedControl::setCheckedAction(int index) {
57     if (index < 0) {
58         d->checkedAction = 0;
59         return true;
60     }
61     QAction* newCheckedAction = d->actionList.at(index);
62     return setCheckedAction(newCheckedAction);
63 }
64
65 bool SegmentedControl::setCheckedAction(QAction *action) {
66     if (d->checkedAction == action) {
67         return false;
68     }
69     if (d->checkedAction)
70         d->checkedAction->setChecked(false);
71     d->checkedAction = action;
72     d->checkedAction->setChecked(true);
73     update();
74     return true;
75 }
76
77 QSize SegmentedControl::minimumSizeHint (void) const {
78     int itemsWidth = calculateButtonWidth() * d->actionList.size() * 1.2;
79     return(QSize(itemsWidth, QFontMetrics(font()).height() * 1.9));
80 }
81
82 void SegmentedControl::paintEvent (QPaintEvent * /*event*/) {
83     int height = rect().height();
84     int width = rect().width();
85
86     QPainter p(this);
87
88     QLinearGradient linearGrad(rect().topLeft(), rect().bottomLeft());
89     linearGrad.setColorAt(0, borderColor);
90     linearGrad.setColorAt(1, QColor(0x3c, 0x3c, 0x3c));
91     p.fillRect(rect(), QBrush(linearGrad));
92
93     // Calculate Buttons Size & Location
94     const int buttonWidth = width / d->actionList.size();
95
96     // Draw Buttons
97     QRect rect(0, 0, buttonWidth, height);
98     const int actionCount = d->actionList.size();
99     for (int i = 0; i < actionCount; i++) {
100         QAction *action = d->actionList.at(i);
101
102         if (i + 1 == actionCount) {
103             rect.setWidth(width - buttonWidth * (actionCount-1));
104             drawButton(&p, rect, action);
105         } else {
106             drawButton(&p, rect, action);
107             rect.moveLeft(rect.x() + rect.width());
108         }
109
110     }
111
112 }
113
114 void SegmentedControl::mouseMoveEvent (QMouseEvent *event) {
115     QWidget::mouseMoveEvent(event);
116
117     QAction *action = hoveredAction(event->pos());
118
119     if (!action && d->hoveredAction) {
120         d->hoveredAction = 0;
121         update();
122     } else if (action && action != d->hoveredAction) {
123         d->hoveredAction = action;
124         action->hover();
125         update();
126
127         // status tip
128         MainWindow::instance()->statusBar()->showMessage(action->statusTip());
129     }
130 }
131
132 void SegmentedControl::mousePressEvent(QMouseEvent *event) {
133     QWidget::mousePressEvent(event);
134     if (d->hoveredAction) {
135         d->pressedAction = d->hoveredAction;
136         update();
137     }
138 }
139
140 void SegmentedControl::mouseReleaseEvent(QMouseEvent *event) {
141     QWidget::mouseReleaseEvent(event);
142     d->pressedAction = 0;
143     if (d->hoveredAction) {
144         bool changed = setCheckedAction(d->hoveredAction);
145         if (changed) d->hoveredAction->trigger();
146     }
147 }
148
149 void SegmentedControl::leaveEvent(QEvent *event) {
150     QWidget::leaveEvent(event);
151     // status tip
152     MainWindow::instance()->statusBar()->clearMessage();
153     d->hoveredAction = 0;
154     d->pressedAction = 0;
155     update();
156 }
157
158 QAction *SegmentedControl::hoveredAction(const QPoint& pos) const {
159     if (pos.y() <= 0 || pos.y() >= height())
160         return 0;
161
162     int buttonWidth = width() / d->actionList.size();
163     int buttonsWidth = width();
164     int buttonsX = 0;
165
166     if (pos.x() <= buttonsX || pos.x() >= (buttonsX + buttonsWidth))
167         return 0;
168
169     int buttonIndex = (pos.x() - buttonsX) / buttonWidth;
170
171     if (buttonIndex >= d->actionList.size())
172         return 0;
173     return(d->actionList[buttonIndex]);
174 }
175
176 int SegmentedControl::calculateButtonWidth (void) const {
177     QFontMetrics fontMetrics(font());
178     int tmpItemWidth, itemWidth = 0;
179     foreach (QAction *action, d->actionList) {
180         tmpItemWidth = fontMetrics.width(action->text());
181         if (itemWidth < tmpItemWidth) itemWidth = tmpItemWidth;
182     }
183     return itemWidth;
184 }
185
186 void SegmentedControl::drawButton (QPainter *painter,
187                               const QRect& rect,
188                               const QAction *action) {
189     if (action == d->checkedAction)
190         drawSelectedButton(painter, rect, action);
191     else
192         drawUnselectedButton(painter, rect, action);
193 }
194
195 void SegmentedControl::drawUnselectedButton (QPainter *painter,
196                                         const QRect& rect,
197                                         const QAction *action) {
198     paintButton(painter, rect, action);
199 }
200
201 void SegmentedControl::drawSelectedButton (QPainter *painter,
202                                       const QRect& rect,
203                                       const QAction *action) {
204     painter->save();
205     painter->translate(rect.topLeft());
206
207     const int width = rect.width();
208     const int height = rect.height();
209     const int hCenter = width * .5;
210     QRadialGradient gradient(hCenter, 0,
211                              width,
212                              hCenter, 0);
213     gradient.setColorAt(1, Qt::black);
214     gradient.setColorAt(0, QColor(0x33, 0x33, 0x33));
215     painter->fillRect(0, 0, width, height, QBrush(gradient));
216
217     painter->restore();
218     paintButton(painter, rect, action);
219 }
220
221 void SegmentedControl::paintButton(QPainter *painter, const QRect& rect, const QAction *action) {
222     painter->save();
223     painter->translate(rect.topLeft());
224
225     const int height = rect.height();
226     const int width = rect.width();
227
228     if (action == d->pressedAction && action != d->checkedAction) {
229         const int hCenter = width * .5;
230         QRadialGradient gradient(hCenter, 0,
231                                  width,
232                                  hCenter, 0);
233         gradient.setColorAt(1, QColor(0x00, 0x00, 0x00, 0));
234         gradient.setColorAt(0, QColor(0x00, 0x00, 0x00, 16));
235         painter->fillRect(0, 0, width, height, QBrush(gradient));
236     } else if (action == d->hoveredAction && action != d->checkedAction) {
237         const int hCenter = width * .5;
238         QRadialGradient gradient(hCenter, 0,
239                                  width,
240                                  hCenter, 0);
241         gradient.setColorAt(1, QColor(0xff, 0xff, 0xff, 0));
242         gradient.setColorAt(0, QColor(0xff, 0xff, 0xff, 16));
243         painter->fillRect(0, 0, width, height, QBrush(gradient));
244     }
245
246     painter->setPen(borderColor);
247 #if defined(APP_MAC) | defined(APP_WIN)
248     painter->drawRect(-1, -1, width, height);
249 #else
250     painter->drawRect(0, 0, width, height - 1);
251 #endif
252
253     const QString text = action->text();
254
255     // text shadow
256     painter->setPen(QColor(0, 0, 0, 128));
257     painter->drawText(0, -1, width, height, Qt::AlignCenter, text);
258
259     painter->setPen(QPen(Qt::white, 1));
260     painter->drawText(0, 0, width, height, Qt::AlignCenter, text);
261
262     if (action->property("notifyCount").isValid()) {
263         QRect textBox = painter->boundingRect(rect,
264                                               Qt::AlignCenter,
265                                               text);
266         painter->translate((width + textBox.width()) / 2 + 10, (height - 20) / 2);
267         PainterUtils::paintBadge(painter, action->property("notifyCount").toString());
268     }
269
270     painter->restore();
271 }