5 #include "thblackbar.h"
7 /* ============================================================================
10 class THBlackBar::Private {
12 QList<QAction *> actionList;
13 QAction *checkedAction;
14 QAction *hoveredAction;
17 /* ============================================================================
18 * PUBLIC Constructor/Destructors
20 THBlackBar::THBlackBar (QWidget *parent)
21 : QWidget(parent), d(new THBlackBar::Private)
23 // Setup Widget Options
24 setMouseTracking(true);
27 d->hoveredAction = NULL;
28 d->checkedAction = NULL;
31 THBlackBar::~THBlackBar() {
35 /* ============================================================================
38 QAction *THBlackBar::addAction (QAction *action) {
39 action->setCheckable(true);
40 d->actionList.append(action);
44 QAction *THBlackBar::addAction (const QString& text) {
45 QAction *action = new QAction(text, this);
46 action->setCheckable(true);
47 d->actionList.append(action);
51 void THBlackBar::setCheckedAction(int index) {
53 d->checkedAction->setChecked(false);
54 d->checkedAction = d->actionList.at(index);
55 d->checkedAction->setChecked(true);
59 QSize THBlackBar::minimumSizeHint (void) const {
60 int itemsWidth = calculateButtonWidth() * d->actionList.size();
61 return(QSize(100 + itemsWidth, 32));
64 /* ============================================================================
67 void THBlackBar::paintEvent (QPaintEvent *event) {
68 int height = event->rect().height();
69 int width = event->rect().width();
70 // int mh = (height / 2);
77 QLinearGradient linearGradUp(QPointF(0, 0), QPointF(0, mh));
78 linearGradUp.setColorAt(0, QColor(0x97, 0x97, 0x97));
79 linearGradUp.setColorAt(1, QColor(0x4d, 0x4d, 0x4d));
80 p.fillRect(0, 0, width, mh, QBrush(linearGradUp));
82 QLinearGradient linearGradDw(QPointF(0, mh), QPointF(0, height));
83 linearGradDw.setColorAt(0, QColor(0x3a, 0x3a, 0x3a));
84 linearGradDw.setColorAt(1, QColor(0x42, 0x42, 0x42));
85 p.fillRect(0, mh, width, mh, QBrush(linearGradDw));
88 // Calculate Buttons Size & Location
89 int buttonWidth = width / d->actionList.size(); // calculateButtonWidth();
90 // int buttonsWidth = width; // buttonWidth * d->actionList.size();
91 int buttonsX = 0; // (width / 2) - (buttonsWidth / 2);
95 QRect rect(buttonsX, 0, buttonWidth, height);
96 foreach (QAction *action, d->actionList) {
97 drawButton(&p, rect, action);
98 rect.moveLeft(rect.x() + rect.width());
100 // p.translate(0, -4);
102 // Draw Buttons Shadow
103 // p.fillRect(buttonsX, height - 4, buttonsWidth, 1, QColor(0x6d, 0x6d, 0x6d));
108 void THBlackBar::mouseMoveEvent (QMouseEvent *event) {
109 QWidget::mouseMoveEvent(event);
111 QAction *action = hoveredAction(event->pos());
113 if (action == NULL && d->hoveredAction != NULL) {
114 // d->hoveredAction->hover(false);
115 d->hoveredAction = NULL;
117 } else if (action != NULL) {
118 d->hoveredAction = action;
124 void THBlackBar::mousePressEvent (QMouseEvent *event) {
125 QWidget::mousePressEvent(event);
127 if (d->hoveredAction != NULL) {
129 if (d->checkedAction != NULL) {
131 if (d->checkedAction == d->hoveredAction) return;
132 d->checkedAction->setChecked(false);
135 d->checkedAction = d->hoveredAction;
136 d->hoveredAction->setChecked(true);
137 d->hoveredAction->trigger();
143 QAction *THBlackBar::hoveredAction (const QPoint& pos) const {
144 if (pos.y() <= 0 || pos.y() >= height())
148 int buttonWidth = calculateButtonWidth();
149 int buttonsWidth = buttonWidth * d->actionList.size();
150 int buttonsX = (width() / 2) - (buttonsWidth / 2);
153 int buttonWidth = width() / d->actionList.size(); // calculateButtonWidth();
154 int buttonsWidth = width(); // buttonWidth * d->actionList.size();
155 int buttonsX = 0; // (width / 2) - (buttonsWidth / 2);
157 if (pos.x() <= buttonsX || pos.x() >= (buttonsX + buttonsWidth))
160 int buttonIndex = (pos.x() - buttonsX) / buttonWidth;
162 if (buttonIndex >= d->actionList.size())
164 return(d->actionList[buttonIndex]);
167 int THBlackBar::calculateButtonWidth (void) const {
168 QFont smallerBoldFont;
169 smallerBoldFont.setBold(true);
170 smallerBoldFont.setPointSize(smallerBoldFont.pointSize()*.85);
171 QFontMetrics fontMetrics(smallerBoldFont);
172 int tmpItemWidth, itemWidth = 0;
173 foreach (QAction *action, d->actionList) {
174 tmpItemWidth = fontMetrics.width(action->text());
175 if (itemWidth < tmpItemWidth) itemWidth = tmpItemWidth;
181 /* ============================================================================
184 void THBlackBar::drawUnselectedButton ( QPainter *painter,
186 const QAction *action)
188 QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, rect.height() / 2));
189 linearGrad.setColorAt(0, QColor(0x8e, 0x8e, 0x8e));
190 linearGrad.setColorAt(1, QColor(0x5c, 0x5c, 0x5c));
193 linearGrad.setColorAt(0, palette.color(QPalette::Dark));
194 linearGrad.setColorAt(1, palette.color(QPalette::Midlight));
196 drawButton(painter, rect, linearGrad, QColor(0x41, 0x41, 0x41), action);
197 // drawButton(painter, rect, linearGrad, palette.color(QPalette::Shadow), action);
200 void THBlackBar::drawSelectedButton ( QPainter *painter,
202 const QAction *action)
204 QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, rect.height() / 2));
205 linearGrad.setColorAt(0, QColor(0x6d, 0x6d, 0x6d));
206 linearGrad.setColorAt(1, QColor(0x25, 0x25, 0x25));
207 drawButton(painter, rect, linearGrad, QColor(0x00, 0x00, 0x00), action);
210 void THBlackBar::drawButton ( QPainter *painter,
212 const QAction *action)
214 if (action->isChecked())
215 drawSelectedButton(painter, rect, action);
217 drawUnselectedButton(painter, rect, action);
220 void THBlackBar::drawButton ( QPainter *painter,
222 const QLinearGradient& gradient,
224 const QAction *action)
228 int height = rect.height();
229 int width = rect.width();
230 int mh = (height / 2);
232 painter->translate(rect.x(), rect.y());
233 painter->setPen(QColor(0x28, 0x28, 0x28));
235 painter->fillRect(0, 0, width, mh, QBrush(gradient));
236 painter->fillRect(0, mh, width, mh, color);
237 painter->drawRect(0, 0, width, height);
239 QFont smallerBoldFont;
240 smallerBoldFont.setBold(true);
241 smallerBoldFont.setPointSize(smallerBoldFont.pointSize()*.85);
242 painter->setFont(smallerBoldFont);
243 painter->setPen(QPen(QColor(0xff, 0xff, 0xff), 1));
244 painter->drawText(0, 1, width, height, Qt::AlignCenter, action->text());