6 #include "thblackbar.h"
7 #include "../fontutils.h"
9 /* ============================================================================
12 class THBlackBar::Private {
14 QList<QAction *> actionList;
15 QAction *checkedAction;
16 QAction *hoveredAction;
19 /* ============================================================================
20 * PUBLIC Constructor/Destructors
22 THBlackBar::THBlackBar (QWidget *parent)
23 : QWidget(parent), d(new THBlackBar::Private)
25 // Setup Widget Options
26 setMouseTracking(true);
29 d->hoveredAction = NULL;
30 d->checkedAction = NULL;
33 THBlackBar::~THBlackBar() {
37 /* ============================================================================
40 QAction *THBlackBar::addAction (QAction *action) {
41 action->setCheckable(true);
42 d->actionList.append(action);
46 QAction *THBlackBar::addAction (const QString& text) {
47 QAction *action = new QAction(text, this);
48 action->setCheckable(true);
49 d->actionList.append(action);
53 void THBlackBar::setCheckedAction(int index) {
55 d->checkedAction->setChecked(false);
56 d->checkedAction = d->actionList.at(index);
57 d->checkedAction->setChecked(true);
61 QSize THBlackBar::minimumSizeHint (void) const {
62 int itemsWidth = calculateButtonWidth() * d->actionList.size();
63 return(QSize(100 + itemsWidth, 32));
66 /* ============================================================================
69 void THBlackBar::paintEvent (QPaintEvent *event) {
70 int height = rect().height();
71 int width = rect().width();
72 // int mh = (height / 2);
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));
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));
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);
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());
102 // p.translate(0, -4);
104 // Draw Buttons Shadow
105 // p.fillRect(buttonsX, height - 4, buttonsWidth, 1, QColor(0x6d, 0x6d, 0x6d));
110 void THBlackBar::mouseMoveEvent (QMouseEvent *event) {
111 QWidget::mouseMoveEvent(event);
113 QAction *action = hoveredAction(event->pos());
115 if (action == NULL && d->hoveredAction != NULL) {
116 // d->hoveredAction->hover(false);
117 d->hoveredAction = NULL;
119 } else if (action != NULL) {
120 d->hoveredAction = action;
125 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
126 if (mainWindow) mainWindow->statusBar()->showMessage(action->statusTip());
130 void THBlackBar::mousePressEvent (QMouseEvent *event) {
131 QWidget::mousePressEvent(event);
133 if (d->hoveredAction != NULL) {
135 if (d->checkedAction != NULL) {
137 if (d->checkedAction == d->hoveredAction) return;
138 d->checkedAction->setChecked(false);
141 d->checkedAction = d->hoveredAction;
142 d->hoveredAction->setChecked(true);
143 d->hoveredAction->trigger();
149 void THBlackBar::leaveEvent(QEvent *event) {
151 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
152 if (mainWindow) mainWindow->statusBar()->clearMessage();
155 QAction *THBlackBar::hoveredAction (const QPoint& pos) const {
156 if (pos.y() <= 0 || pos.y() >= height())
160 int buttonWidth = calculateButtonWidth();
161 int buttonsWidth = buttonWidth * d->actionList.size();
162 int buttonsX = (width() / 2) - (buttonsWidth / 2);
165 int buttonWidth = width() / d->actionList.size(); // calculateButtonWidth();
166 int buttonsWidth = width(); // buttonWidth * d->actionList.size();
167 int buttonsX = 0; // (width / 2) - (buttonsWidth / 2);
169 if (pos.x() <= buttonsX || pos.x() >= (buttonsX + buttonsWidth))
172 int buttonIndex = (pos.x() - buttonsX) / buttonWidth;
174 if (buttonIndex >= d->actionList.size())
176 return(d->actionList[buttonIndex]);
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;
191 /* ============================================================================
194 void THBlackBar::drawUnselectedButton ( QPainter *painter,
196 const QAction *action)
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));
203 linearGrad.setColorAt(0, palette.color(QPalette::Dark));
204 linearGrad.setColorAt(1, palette.color(QPalette::Midlight));
206 drawButton(painter, rect, linearGrad, QColor(0x41, 0x41, 0x41), action);
207 // drawButton(painter, rect, linearGrad, palette.color(QPalette::Shadow), action);
210 void THBlackBar::drawSelectedButton ( QPainter *painter,
212 const QAction *action)
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);
220 void THBlackBar::drawButton ( QPainter *painter,
222 const QAction *action)
224 if (action->isChecked())
225 drawSelectedButton(painter, rect, action);
227 drawUnselectedButton(painter, rect, action);
230 void THBlackBar::drawButton ( QPainter *painter,
232 const QLinearGradient& gradient,
234 const QAction *action)
238 int height = rect.height();
239 int width = rect.width();
240 int mh = (height / 2);
242 painter->translate(rect.x(), rect.y());
243 painter->setPen(QColor(0x28, 0x28, 0x28));
245 painter->fillRect(0, 0, width, mh, QBrush(gradient));
246 painter->fillRect(0, mh, width, mh, color);
247 #if defined(APP_MAC) | defined(APP_WIN)
248 painter->drawRect(-1, -1, width+1, height);
250 painter->drawRect(0, 0, width, height);
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());