3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "segmentedcontrol.h"
22 #include "mainwindow.h"
23 #include "painterutils.h"
25 static const QColor borderColor = QColor(0x26, 0x26, 0x26);
27 class SegmentedControl::Private {
29 QList<QAction *> actionList;
30 QAction *checkedAction;
31 QAction *hoveredAction;
32 QAction *pressedAction;
35 SegmentedControl::SegmentedControl (QWidget *parent)
36 : QWidget(parent), d(new SegmentedControl::Private) {
38 setMouseTracking(true);
45 SegmentedControl::~SegmentedControl() {
49 QAction *SegmentedControl::addAction(QAction *action) {
50 QWidget::addAction(action);
51 action->setCheckable(true);
52 d->actionList.append(action);
56 bool SegmentedControl::setCheckedAction(int index) {
61 QAction* newCheckedAction = d->actionList.at(index);
62 return setCheckedAction(newCheckedAction);
65 bool SegmentedControl::setCheckedAction(QAction *action) {
66 if (d->checkedAction == action) {
70 d->checkedAction->setChecked(false);
71 d->checkedAction = action;
72 d->checkedAction->setChecked(true);
77 QSize SegmentedControl::minimumSizeHint (void) const {
78 int itemsWidth = calculateButtonWidth() * d->actionList.size() * 1.2;
79 return(QSize(itemsWidth, QFontMetrics(font()).height() * 1.9));
82 void SegmentedControl::paintEvent (QPaintEvent * /*event*/) {
83 int height = rect().height();
84 int width = rect().width();
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));
93 // Calculate Buttons Size & Location
94 const int buttonWidth = width / d->actionList.size();
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);
102 if (i + 1 == actionCount) {
103 rect.setWidth(width - buttonWidth * (actionCount-1));
104 drawButton(&p, rect, action);
106 drawButton(&p, rect, action);
107 rect.moveLeft(rect.x() + rect.width());
114 void SegmentedControl::mouseMoveEvent (QMouseEvent *event) {
115 QWidget::mouseMoveEvent(event);
117 QAction *action = hoveredAction(event->pos());
119 if (!action && d->hoveredAction) {
120 d->hoveredAction = 0;
122 } else if (action && action != d->hoveredAction) {
123 d->hoveredAction = action;
128 MainWindow::instance()->statusBar()->showMessage(action->statusTip());
132 void SegmentedControl::mousePressEvent(QMouseEvent *event) {
133 QWidget::mousePressEvent(event);
134 if (d->hoveredAction) {
135 d->pressedAction = d->hoveredAction;
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();
149 void SegmentedControl::leaveEvent(QEvent *event) {
150 QWidget::leaveEvent(event);
152 MainWindow::instance()->statusBar()->clearMessage();
153 d->hoveredAction = 0;
154 d->pressedAction = 0;
158 QAction *SegmentedControl::hoveredAction(const QPoint& pos) const {
159 if (pos.y() <= 0 || pos.y() >= height())
162 int buttonWidth = width() / d->actionList.size();
163 int buttonsWidth = width();
166 if (pos.x() <= buttonsX || pos.x() >= (buttonsX + buttonsWidth))
169 int buttonIndex = (pos.x() - buttonsX) / buttonWidth;
171 if (buttonIndex >= d->actionList.size())
173 return(d->actionList[buttonIndex]);
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;
186 void SegmentedControl::drawButton (QPainter *painter,
188 const QAction *action) {
189 if (action == d->checkedAction)
190 drawSelectedButton(painter, rect, action);
192 drawUnselectedButton(painter, rect, action);
195 void SegmentedControl::drawUnselectedButton (QPainter *painter,
197 const QAction *action) {
198 paintButton(painter, rect, action);
201 void SegmentedControl::drawSelectedButton (QPainter *painter,
203 const QAction *action) {
205 painter->translate(rect.topLeft());
207 const int width = rect.width();
208 const int height = rect.height();
209 const int hCenter = width * .5;
210 QRadialGradient gradient(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));
218 paintButton(painter, rect, action);
221 void SegmentedControl::paintButton(QPainter *painter, const QRect& rect, const QAction *action) {
223 painter->translate(rect.topLeft());
225 const int height = rect.height();
226 const int width = rect.width();
228 if (action == d->pressedAction && action != d->checkedAction) {
229 const int hCenter = width * .5;
230 QRadialGradient gradient(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,
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));
246 painter->setPen(borderColor);
247 #if defined(APP_MAC) | defined(APP_WIN)
248 painter->drawRect(-1, -1, width, height);
250 painter->drawRect(0, 0, width, height - 1);
253 const QString text = action->text();
256 painter->setPen(QColor(0, 0, 0, 128));
257 painter->drawText(0, -1, width, height, Qt::AlignCenter, text);
259 painter->setPen(QPen(Qt::white, 1));
260 painter->drawText(0, 0, width, height, Qt::AlignCenter, text);
262 if (action->property("notifyCount").isValid()) {
263 QRect textBox = painter->boundingRect(rect,
266 painter->translate((width + textBox.width()) / 2 + 10, (height - 20) / 2);
267 PainterUtils::paintBadge(painter, action->property("notifyCount").toString());