6 #include "thblackbar.h"
8 /* ============================================================================
11 class THBlackBar::Private {
13 QList<QAction *> actionList;
14 QAction *checkedAction;
15 QAction *hoveredAction;
18 /* ============================================================================
19 * PUBLIC Constructor/Destructors
21 THBlackBar::THBlackBar (QWidget *parent)
22 : QWidget(parent), d(new THBlackBar::Private)
24 // Setup Widget Options
25 setMouseTracking(true);
28 d->hoveredAction = NULL;
29 d->checkedAction = NULL;
32 THBlackBar::~THBlackBar() {
36 /* ============================================================================
39 QAction *THBlackBar::addAction (QAction *action) {
40 action->setCheckable(true);
41 d->actionList.append(action);
45 QAction *THBlackBar::addAction (const QString& text) {
46 QAction *action = new QAction(text, this);
47 action->setCheckable(true);
48 d->actionList.append(action);
52 void THBlackBar::setCheckedAction(int index) {
54 d->checkedAction->setChecked(false);
55 d->checkedAction = d->actionList.at(index);
56 d->checkedAction->setChecked(true);
60 QSize THBlackBar::minimumSizeHint (void) const {
61 int itemsWidth = calculateButtonWidth() * d->actionList.size();
62 return(QSize(100 + itemsWidth, 32));
65 /* ============================================================================
68 void THBlackBar::paintEvent (QPaintEvent *event) {
69 int height = event->rect().height();
70 int width = event->rect().width();
71 // int mh = (height / 2);
78 QLinearGradient linearGradUp(QPointF(0, 0), QPointF(0, mh));
79 linearGradUp.setColorAt(0, QColor(0x97, 0x97, 0x97));
80 linearGradUp.setColorAt(1, QColor(0x4d, 0x4d, 0x4d));
81 p.fillRect(0, 0, width, mh, QBrush(linearGradUp));
83 QLinearGradient linearGradDw(QPointF(0, mh), QPointF(0, height));
84 linearGradDw.setColorAt(0, QColor(0x3a, 0x3a, 0x3a));
85 linearGradDw.setColorAt(1, QColor(0x42, 0x42, 0x42));
86 p.fillRect(0, mh, width, mh, QBrush(linearGradDw));
89 // Calculate Buttons Size & Location
90 int buttonWidth = width / d->actionList.size(); // calculateButtonWidth();
91 // int buttonsWidth = width; // buttonWidth * d->actionList.size();
92 int buttonsX = 0; // (width / 2) - (buttonsWidth / 2);
96 QRect rect(buttonsX, 0, buttonWidth, height);
97 foreach (QAction *action, d->actionList) {
98 drawButton(&p, rect, action);
99 rect.moveLeft(rect.x() + rect.width());
101 // p.translate(0, -4);
103 // Draw Buttons Shadow
104 // p.fillRect(buttonsX, height - 4, buttonsWidth, 1, QColor(0x6d, 0x6d, 0x6d));
109 void THBlackBar::mouseMoveEvent (QMouseEvent *event) {
110 QWidget::mouseMoveEvent(event);
112 QAction *action = hoveredAction(event->pos());
114 if (action == NULL && d->hoveredAction != NULL) {
115 // d->hoveredAction->hover(false);
116 d->hoveredAction = NULL;
118 } else if (action != NULL) {
119 d->hoveredAction = action;
124 QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(window());
125 if (mainWindow) mainWindow->statusBar()->showMessage(action->statusTip());
129 void THBlackBar::mousePressEvent (QMouseEvent *event) {
130 QWidget::mousePressEvent(event);
132 if (d->hoveredAction != NULL) {
134 if (d->checkedAction != NULL) {
136 if (d->checkedAction == d->hoveredAction) return;
137 d->checkedAction->setChecked(false);
140 d->checkedAction = d->hoveredAction;
141 d->hoveredAction->setChecked(true);
142 d->hoveredAction->trigger();
148 void THBlackBar::leaveEvent(QEvent *event) {
150 QMainWindow* mainWindow = static_cast<QMainWindow*>(window());
151 if (mainWindow) mainWindow->statusBar()->clearMessage();
154 QAction *THBlackBar::hoveredAction (const QPoint& pos) const {
155 if (pos.y() <= 0 || pos.y() >= height())
159 int buttonWidth = calculateButtonWidth();
160 int buttonsWidth = buttonWidth * d->actionList.size();
161 int buttonsX = (width() / 2) - (buttonsWidth / 2);
164 int buttonWidth = width() / d->actionList.size(); // calculateButtonWidth();
165 int buttonsWidth = width(); // buttonWidth * d->actionList.size();
166 int buttonsX = 0; // (width / 2) - (buttonsWidth / 2);
168 if (pos.x() <= buttonsX || pos.x() >= (buttonsX + buttonsWidth))
171 int buttonIndex = (pos.x() - buttonsX) / buttonWidth;
173 if (buttonIndex >= d->actionList.size())
175 return(d->actionList[buttonIndex]);
178 int THBlackBar::calculateButtonWidth (void) const {
179 QFont smallerBoldFont;
180 smallerBoldFont.setBold(true);
181 smallerBoldFont.setPointSize(smallerBoldFont.pointSize()*.85);
182 QFontMetrics fontMetrics(smallerBoldFont);
183 int tmpItemWidth, itemWidth = 0;
184 foreach (QAction *action, d->actionList) {
185 tmpItemWidth = fontMetrics.width(action->text());
186 if (itemWidth < tmpItemWidth) itemWidth = tmpItemWidth;
192 /* ============================================================================
195 void THBlackBar::drawUnselectedButton ( QPainter *painter,
197 const QAction *action)
199 QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, rect.height() / 2));
200 linearGrad.setColorAt(0, QColor(0x8e, 0x8e, 0x8e));
201 linearGrad.setColorAt(1, QColor(0x5c, 0x5c, 0x5c));
204 linearGrad.setColorAt(0, palette.color(QPalette::Dark));
205 linearGrad.setColorAt(1, palette.color(QPalette::Midlight));
207 drawButton(painter, rect, linearGrad, QColor(0x41, 0x41, 0x41), action);
208 // drawButton(painter, rect, linearGrad, palette.color(QPalette::Shadow), action);
211 void THBlackBar::drawSelectedButton ( QPainter *painter,
213 const QAction *action)
215 QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, rect.height() / 2));
216 linearGrad.setColorAt(0, QColor(0x6d, 0x6d, 0x6d));
217 linearGrad.setColorAt(1, QColor(0x25, 0x25, 0x25));
218 drawButton(painter, rect, linearGrad, QColor(0x00, 0x00, 0x00), action);
221 void THBlackBar::drawButton ( QPainter *painter,
223 const QAction *action)
225 if (action->isChecked())
226 drawSelectedButton(painter, rect, action);
228 drawUnselectedButton(painter, rect, action);
231 void THBlackBar::drawButton ( QPainter *painter,
233 const QLinearGradient& gradient,
235 const QAction *action)
239 int height = rect.height();
240 int width = rect.width();
241 int mh = (height / 2);
243 painter->translate(rect.x(), rect.y());
244 painter->setPen(QColor(0x28, 0x28, 0x28));
246 painter->fillRect(0, 0, width, mh, QBrush(gradient));
247 painter->fillRect(0, mh, width, mh, color);
248 painter->drawRect(0, 0, width, height);
250 QFont smallerBoldFont;
251 smallerBoldFont.setBold(true);
252 smallerBoldFont.setPointSize(smallerBoldFont.pointSize()*.85);
253 painter->setFont(smallerBoldFont);
254 painter->setPen(QPen(QColor(0xff, 0xff, 0xff), 1));
255 painter->drawText(0, 1, width, height, Qt::AlignCenter, action->text());