Debian Codegraph @

kysdk-qtwidgets/src/kborderbutton.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhen Sun <[email protected]>
20 *
21 */
22
23 #include "kborderbutton.h"
24 #include "parmscontroller.h"
25 #include "themeController.h"
26 #include <QDebug>
27 #include <QIcon>
28 #include <QPainter>
29 #include <QStyleOptionButton>
30
31 namespace kdk
32 {
33 class KBorderButtonPrivate : public QObject, public ThemeController
34 {
35 Q_DECLARE_PUBLIC(KBorderButton)
36 Q_OBJECT
37 public:
38 KBorderButtonPrivate(KBorderButton *parent);
39
40 protected:
41 void changeTheme();
42
43 private:
44 KBorderButton *q_ptr;
45 bool m_hoverd;
46 int m_radius;
47 };
48 KBorderButton::KBorderButton(QWidget *parent)
49 : QPushButton(parent)
50 , d_ptr(new KBorderButtonPrivate(this))
51 {
52 Q_D(KBorderButton);
53 connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=]() {
54 updateGeometry();
55 });
56 }
57
58 KBorderButton::~KBorderButton()
59 {
60 }
61
62 QSize KBorderButton::sizeHint() const
63 {
64 auto size = QPushButton::sizeHint();
65 size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_PushButtonHeight));
66 return size;
67 }
68 bool KBorderButton::eventFilter(QObject *watched, QEvent *event)
69 {
70 Q_D(KBorderButton);
71 return QPushButton::eventFilter(watched, event);
72 }
73
74 void KBorderButton::paintEvent(QPaintEvent *event)
75 {
76 Q_D(KBorderButton);
77 QStyleOptionButton option;
78 initStyleOption(&option);
79 QPainter p(this);
80 QColor borderColor;
81 QColor fontColor;
82 int borderWidth = 1;
83 QColor startColor;
84 QColor endColor;
85 if (!option.state.testFlag(QStyle::State_Enabled)) {
86 borderColor = ThemeController::getCustomColorFromDT("kfont-primary-disable");
87 fontColor = ThemeController::getCustomColorFromDT("kfont-primary-disable");
88 } else if (option.state.testFlag(QStyle::State_MouseOver)) {
89 if (option.state.testFlag(QStyle::State_Sunken))
90 {
91 ThemeController::getGradientFromDT("kbrand-click",startColor,endColor);
92 fontColor = endColor;
93 borderColor = endColor;
94 }
95 else
96 {
97 ThemeController::getGradientFromDT("kbrand-hover",startColor,endColor);
98 fontColor = endColor;
99 borderColor = endColor;
100 }
101 }
102 else
103 {
104 fontColor = ThemeController::getCustomColorFromDT("buttontext-active");
105 borderColor = ThemeController::getCustomColorFromDT("button-active");
106 }
107 p.setBrush(Qt::NoBrush);
108 p.setRenderHint(QPainter::Antialiasing);
109 p.setRenderHint(QPainter::TextAntialiasing);
110 p.setRenderHint(QPainter::SmoothPixmapTransform);
111 QPen pen;
112 pen.setCapStyle(Qt::RoundCap);
113 pen.setJoinStyle(Qt::RoundJoin);
114
115 p.save();
116 pen.setWidth(borderWidth);
117 pen.setColor(borderColor);
118 p.setPen(pen);
119 p.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), d->m_radius, d->m_radius);
120 p.restore();
121 pen.setWidth(1);
122 pen.setColor(fontColor);
123 p.setPen(pen);
124 QPoint point;
125 QRect ir = option.rect;
126 uint tf = Qt::AlignVCenter;
127 if (!option.icon.isNull()) {
128 // Center both icon and text
129
130 QIcon::Mode mode = option.state & QStyle::State_Enabled ? QIcon::Normal
131 : QIcon::Disabled;
132 if (mode == QIcon::Normal && option.state & QStyle::State_HasFocus)
133 mode = QIcon::Active;
134 QIcon::State state = QIcon::Off;
135 if (option.state & QStyle::State_On)
136 state = QIcon::On;
137
138 QPixmap pixmap = option.icon.pixmap(option.iconSize, mode, state);
139 pixmap = ThemeController::drawColoredPixmap(pixmap, fontColor);
140 int w = pixmap.width() / pixmap.devicePixelRatio();
141 int h = pixmap.height() / pixmap.devicePixelRatio();
142
143 if (!option.text.isEmpty())
144 w += option.fontMetrics.boundingRect(option.rect, tf, option.text).width() + 2;
145
146 point = QPoint(ir.x() + ir.width() / 2 - w / 2,
147 ir.y() + ir.height() / 2 - h / 2);
148
149 w = pixmap.width() / pixmap.devicePixelRatio();
150
151 if (option.direction == Qt::RightToLeft)
152 point.rx() += w;
153
154 p.drawPixmap(this->style()->visualPos(option.direction, option.rect, point), pixmap);
155
156 if (option.direction == Qt::RightToLeft)
157 ir.translate(-point.x() - 2, 0);
158 else
159 ir.translate(point.x() + w + 4, 0);
160
161 // left-align text if there is
162 if (!option.text.isEmpty())
163 tf |= Qt::AlignLeft;
164 } else {
165 tf |= Qt::AlignHCenter;
166 }
167 p.drawText(ir, tf, option.text);
168 }
169
170 KBorderButton::KBorderButton(const QString &text, QWidget *parent)
171 : KBorderButton(parent)
172 {
173 setText(text);
174 }
175
176 KBorderButton::KBorderButton(const QIcon &icon, const QString &text, QWidget *parent)
177 : KBorderButton(parent)
178 {
179 setIcon(icon);
180 setText(text);
181 }
182
183 void KBorderButton::setIcon(const QIcon &icon)
184 {
185 Q_D(KBorderButton);
186 QPushButton::setIcon(icon);
187 }
188
189 KBorderButtonPrivate::KBorderButtonPrivate(KBorderButton *parent)
190 : q_ptr(parent)
191 , m_hoverd(false)
192 {
193 setParent(parent);
194 m_radius = ThemeController::getRadiusFromDT("kradius-normal");
195 if (m_radius == -1)
196 m_radius = 6;
197 }
198
199 void KBorderButtonPrivate::changeTheme()
200 {
201 Q_Q(KBorderButton);
202 initThemeStyle();
203 }
204
205 }
206 #include "kborderbutton.moc"
207 #include "moc_kborderbutton.cpp"