QtCanpool 3.3.0
载入中...
搜索中...
未找到
ribboncolorbutton.cpp
浏览该文件的文档.
1
5#include "ribboncolorbutton.h"
7
8#include <QPainter>
9#include <QStyleOptionToolButton>
10#include <QStyle>
11
13
14const int c_colorHeight = 5;
15
16RibbonColorButtonPrivate::RibbonColorButtonPrivate()
18 , m_color(QColor())
19{
20}
21
22QPixmap RibbonColorButtonPrivate::createIconPixmap(const QStyleOptionToolButton &opt, const QSize &iconsize)
23{
25 if (opt.icon.isNull()) {
26 return QPixmap();
27 }
28 QIcon::State state = opt.state & QStyle::State_On ? QIcon::On : QIcon::Off;
29 QIcon::Mode mode;
30 if (!(opt.state & QStyle::State_Enabled)) {
31 mode = QIcon::Disabled;
32 } else if ((opt.state & QStyle::State_MouseOver) && (opt.state & QStyle::State_AutoRaise)) {
33 mode = QIcon::Active;
34 } else {
35 mode = QIcon::Normal;
36 }
37 QSize realIconSize = iconsize - QSize(0, c_colorHeight + 1);
38#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
39 QPixmap pixmap = opt.icon.pixmap(q->window()->windowHandle(), realIconSize, mode, state);
40#else
41 QPixmap pixmap = opt.icon.pixmap(realIconSize, q->window()->devicePixelRatio(), mode, state);
42#endif
43 QPixmap res(pixmap.size() + QSize(4, c_colorHeight + 4)); // 宽度上,颜色块多出2px
44 res.fill(Qt::transparent);
45 QPainter painter(&res);
46 int xPixmap = (res.width() - pixmap.width()) / 2;
47 int yPixmap =
48 (res.height() - c_colorHeight - 2 - pixmap.height()) / 2; // 这里要减去2而不是1,这样奇数偶数都不会影响
49 QRect rPixmap = QRect(xPixmap, yPixmap, pixmap.width(), pixmap.height());
50 painter.drawPixmap(rPixmap, pixmap);
51 QRect colorRect = rPixmap.adjusted(0, pixmap.height() + 1, 0, c_colorHeight + 1);
52 if (m_color.isValid()) {
53 painter.fillRect(colorRect, m_color);
54 } else {
55 QPen pen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap);
56 painter.setPen(pen);
57 painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
58 painter.setRenderHint(QPainter::Antialiasing, true);
59 int ss = colorRect.width() / 3;
60 painter.drawLine(QPoint(colorRect.x() + ss, colorRect.bottom()),
61 QPoint(colorRect.right() - ss, colorRect.top()));
62 pen.setColor(Qt::black);
63 painter.setPen(pen);
64 painter.drawRect(colorRect);
65 }
66 return res;
67}
68
71{
73 d->setPublic(this);
74
75 connect(this, &QAbstractButton::clicked, this, &RibbonColorButton::onButtonClicked);
76}
77
78RibbonColorButton::RibbonColorButton(QAction *defaultAction, QWidget *parent)
79 : RibbonColorButton(parent)
80{
81 setDefaultAction(defaultAction);
82}
83
87
88const QColor &RibbonColorButton::color() const
89{
90 Q_D(const RibbonColorButton);
91 return d->m_color;
92}
93
94void RibbonColorButton::setColor(const QColor &color)
95{
97 if (d->m_color == color) {
98 return;
99 }
100 d->m_color = color;
101 update();
102 Q_EMIT colorChanged(color);
103}
104
106{
108 Q_EMIT colorClicked(d->m_color, checked);
109}
110
Ribbon 界面适用的 toolButton
QPixmap createIconPixmap(const QStyleOptionToolButton &opt, const QSize &iconsize) override
void colorClicked(const QColor &color, bool checked=false)
const QColor & color() const
void onButtonClicked(bool checked=false)
void setColor(const QColor &color)
RibbonColorButton(QWidget *parent=nullptr)
void colorChanged(const QColor &color)
const int c_colorHeight
颜色块的高度
#define QX_RIBBON_END_NAMESPACE
#define QX_RIBBON_BEGIN_NAMESPACE