QT控件大全 三十六 QBlackBarButton

来源:互联网 发布:php开发环境 编辑:程序博客网 时间:2024/06/07 02:14

效果图:


核心代码:

#ifndef _THBLACKBAR_H_#define _THBLACKBAR_H_#include <QWidget>class THAction;class THBlackBar : public QWidget {Q_OBJECTpublic:THBlackBar (QWidget *parent = 0);~THBlackBar();public:THAction *addAction (THAction *action);THAction *addAction (const QString& text);QSize minimumSizeHint (void) const;protected:void paintEvent (QPaintEvent *event);void mouseMoveEvent (QMouseEvent *event);void mouseReleaseEvent (QMouseEvent *event);private:void drawUnselectedButton (QPainter *painter,const QRect& rect,const THAction *action);void drawSelectedButton (QPainter *painter,const QRect& rect,const THAction *action);void drawButton (QPainter *painter,const QRect& rect,const THAction *action);void drawButton (QPainter *painter,const QRect& rect,const QLinearGradient& gradient,const QColor& color,const THAction *action);THAction *hoveredAction (const QPoint& pos) const;int calculateButtonWidth (void) const;private:class Private;Private *d;};#endif /* !_THBLACKBAR_H_ */
#include <QPaintEvent>#include <QList>#include "thblackbutton.h"#include "thblackbar.h"#include "thpainter.h"#include "thaction.h"/* ============================================================================ *  PRIVATE Class */class THBlackBar::Private {public:QList<THAction *> actionList;THAction *checkedAction;THAction *hoveredAction;};/* ============================================================================ *  PUBLIC Constructor/Destructors */THBlackBar::THBlackBar (QWidget *parent): QWidget(parent), d(new THBlackBar::Private){// Setup Widget OptionssetMouseTracking(true);// Setup Membersd->hoveredAction = NULL;d->checkedAction = NULL;}THBlackBar::~THBlackBar() {delete d;}/* ============================================================================ *  PUBLIC Methods */THAction *THBlackBar::addAction (THAction *action) {d->actionList.append(action);return(action);}THAction *THBlackBar::addAction (const QString& text) {THAction *action = new THAction(text, this);d->actionList.append(action);return(action);}QSize THBlackBar::minimumSizeHint (void) const {int itemsWidth = calculateButtonWidth() * d->actionList.size();return(QSize(100 + itemsWidth, 28));}/* ============================================================================ *  PROTECTED Methods */void THBlackBar::paintEvent (QPaintEvent *event) {int height = event->rect().height();int width = event->rect().width();int mh = (height / 2);THPainter p(this);// Draw BackgroundQLinearGradient linearGradUp(QPointF(0, 0), QPointF(0, mh));linearGradUp.setColorAt(0, QColor(0x97, 0x97, 0x97));linearGradUp.setColorAt(1, QColor(0x4d, 0x4d, 0x4d));p.fillRect(0, 0, width, mh, QBrush(linearGradUp));QLinearGradient linearGradDw(QPointF(0, mh), QPointF(0, height));linearGradDw.setColorAt(0, QColor(0x3a, 0x3a, 0x3a));linearGradDw.setColorAt(1, QColor(0x42, 0x42, 0x42));p.fillRect(0, mh, width, mh, QBrush(linearGradDw));// Calculate Buttons Size & Locationint buttonWidth = calculateButtonWidth();int buttonsWidth = buttonWidth * d->actionList.size();int buttonsX = (width / 2) - (buttonsWidth / 2);// Draw Buttonsp.translate(0, 4);QRect rect(buttonsX, 0, buttonWidth, 20);foreach (THAction *action, d->actionList) {drawButton(&p, rect, action);rect.moveLeft(rect.x() + rect.width());}p.translate(0, -4);// Draw Buttons Shadowp.fillRect(buttonsX, height - 4, buttonsWidth, 1, QColor(0x6d, 0x6d, 0x6d));p.end();}void THBlackBar::mouseMoveEvent (QMouseEvent *event) {QWidget::mouseMoveEvent(event);THAction *action = hoveredAction(event->pos());if (action == NULL && d->hoveredAction != NULL) {d->hoveredAction->hover(false);d->hoveredAction = NULL;update();} else if (action != NULL) {d->hoveredAction = action;action->hover(true);update();}}void THBlackBar::mouseReleaseEvent (QMouseEvent *event) {QWidget::mouseReleaseEvent(event);if (d->hoveredAction != NULL) {if (d->checkedAction != NULL)d->checkedAction->setChecked(false);d->checkedAction = d->hoveredAction;d->hoveredAction->setChecked(true);d->hoveredAction->trigger();update();}}THAction *THBlackBar::hoveredAction (const QPoint& pos) const {if (pos.y() <= 4 || pos.y() >= 24)return(NULL);int buttonWidth = calculateButtonWidth();int buttonsWidth = buttonWidth * d->actionList.size();int buttonsX = (width() / 2) - (buttonsWidth / 2);if (pos.x() <= buttonsX || pos.x() >= (buttonsX + buttonsWidth))return(NULL);int buttonIndex = (pos.x() - buttonsX) / buttonWidth;return(d->actionList[buttonIndex]);}int THBlackBar::calculateButtonWidth (void) const {QFontMetrics fontMetrics(QFont("Arial", 8, QFont::Bold));int tmpItemWidth, itemWidth = 0;foreach (THAction *action, d->actionList) {tmpItemWidth = fontMetrics.width(action->text());if (itemWidth < tmpItemWidth) itemWidth = tmpItemWidth;}return(itemWidth + 20);}/* ============================================================================ *  PRIVATE Methods */void THBlackBar::drawUnselectedButton (QPainter *painter,const QRect& rect,const THAction *action){QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, rect.height() / 2));linearGrad.setColorAt(0, QColor(0x8e, 0x8e, 0x8e));linearGrad.setColorAt(1, QColor(0x5c, 0x5c, 0x5c));drawButton(painter, rect, linearGrad, QColor(0x41, 0x41, 0x41), action);}void THBlackBar::drawSelectedButton (QPainter *painter,const QRect& rect,const THAction *action){QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, rect.height() / 2));linearGrad.setColorAt(0, QColor(0x6d, 0x6d, 0x6d));linearGrad.setColorAt(1, QColor(0x25, 0x25, 0x25));drawButton(painter, rect, linearGrad, QColor(0x00, 0x00, 0x00), action);}void THBlackBar::drawButton (QPainter *painter,const QRect& rect,const THAction *action){if (action->isChecked())drawSelectedButton(painter, rect, action);elsedrawUnselectedButton(painter, rect, action);}void THBlackBar::drawButton (QPainter *painter, const QRect& rect,const QLinearGradient& gradient,const QColor& color,const THAction *action){painter->save();int height = rect.height();int width = rect.width();int mh = (height / 2);painter->translate(rect.x(), rect.y());painter->setPen(QColor(0x28, 0x28, 0x28));painter->fillRect(0, 0, width, mh, QBrush(gradient));painter->fillRect(0, mh, width, mh, color);painter->drawRect(0, 0, width, height);painter->setFont(QFont("Arial", 8, QFont::Bold));painter->setPen(QPen(QColor(0xff, 0xff, 0xff), 1));painter->drawText(0, 1, width, height, Qt::AlignCenter, action->text());painter->restore();}







源码工程:

QQ:609162385