Debian Codegraph @

kysdk-qtwidgets/src/kpressbutton.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: Zhenyu Wang <[email protected]>
20 *
21 */
22
23 #include "kpressbutton.h"
24 #include "themeController.h"
25 #include <QDebug>
26 #include <QIcon>
27 #include <QPainter>
28 #include <QPainterPath>
29 #include <QStyleOptionButton>
30 #include <QApplication>
31 #include <QTimer>
32
33 namespace kdk
34 {
35
36 class KPressButtonPrivate : public QObject, public ThemeController
37 {
38 Q_OBJECT
39 Q_DECLARE_PUBLIC(KPressButton)
40 public:
41 KPressButtonPrivate(KPressButton *parent);
42 void changeTheme();
43
44 private:
45 KPressButton *q_ptr;
46 QColor m_pbkgColor;
47 QTimer *m_pTimer;
48 KPressButton::ButtonType m_pButtonType;
49 QIcon m_pIcon;
50 int m_flashState;
51 int m_pTopLeftRadius;
52 int m_pTopRightRadius;
53 int m_pBottomLeftRadius;
54 int m_pBottomRightRadius;
55 bool m_pLoading; // loafing标志位
56 bool m_pCheackable; // 开启标志位
57 bool m_pTranslucent;
58 bool m_isTranslucentFlag;
59 };
60
61 KPressButtonPrivate::KPressButtonPrivate(KPressButton *parent)
62 : q_ptr(parent)
63 {
64 Q_Q(KPressButton);
65 }
66
67 void KPressButtonPrivate::changeTheme()
68 {
69 Q_Q(KPressButton);
70 if (m_isTranslucentFlag) {
71 if (ThemeController::widgetTheme() == ClassicTheme)
72 m_pTranslucent = false;
73 else
74 m_pTranslucent = true;
75 } else {
76 m_pTranslucent = false;
77 }
78 initThemeStyle();
79 }
80
81 KPressButton::KPressButton(QWidget *parent)
82 : QPushButton(parent)
83 , d_ptr(new KPressButtonPrivate(this))
84 {
85 Q_D(KPressButton);
86 int radius = ThemeController::getRadiusFromDT("kradius-normal");
87 if (radius == -1) {
88 d->m_pTopLeftRadius = 6;
89 d->m_pTopRightRadius = 6;
90 d->m_pBottomLeftRadius = 6;
91 d->m_pBottomRightRadius = 6;
92 } else {
93 d->m_pTopLeftRadius = radius;
94 d->m_pTopRightRadius = radius;
95 d->m_pBottomLeftRadius = radius;
96 d->m_pBottomRightRadius = radius;
97 }
98 d->m_pButtonType = KPressButton::NormalType;
99 d->m_pCheackable = false;
100 d->m_pTranslucent = false;
101 d->m_isTranslucentFlag = false;
102 d->m_pTimer = new QTimer(this);
103 d->m_pTimer->setInterval(100);
104 d->m_flashState = 0;
105 setCheckable(true);
106 connect(d->m_pTimer, &QTimer::timeout, this, [=]() {
107 if (d->m_flashState < 7)
108 d->m_flashState++;
109 else
110 d->m_flashState = 0;
111 setIcon(QIcon::fromTheme(QString("ukui-loading-%1.symbolic").arg(d->m_flashState)));
112 });
113 connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){
114 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
115 {
116 d->changeTheme();
117 }
118 });
119 }
120
121 void KPressButton::setBorderRadius(int radius)
122 {
123 Q_D(KPressButton);
124 d->m_pTopLeftRadius = radius;
125 d->m_pTopRightRadius = radius;
126 d->m_pBottomLeftRadius = radius;
127 d->m_pBottomRightRadius = radius;
128 }
129
130 void KPressButton::setBorderRadius(int bottomLeft, int topLeft, int topRight, int bottomRight)
131 {
132 Q_D(KPressButton);
133 d->m_pBottomLeftRadius = bottomLeft;
134 d->m_pTopLeftRadius = topLeft;
135 d->m_pTopRightRadius = topRight;
136 d->m_pBottomRightRadius = bottomRight;
137 }
138
139 void KPressButton::setCheckable(bool flag)
140 {
141 QPushButton::setCheckable(flag);
142 }
143
144 bool KPressButton::isCheckable() const
145 {
146 return QPushButton::isCheckable();
147 }
148
149 void KPressButton::setChecked(bool flag)
150 {
151 QPushButton::setChecked(flag);
152 }
153
154 bool KPressButton::isChecked() const
155 {
156 return QPushButton::isChecked();
157 }
158
159 void KPressButton::setButtonType(ButtonType type)
160 {
161 Q_D(KPressButton);
162 d->m_pButtonType = type;
163 }
164
165 KPressButton::ButtonType KPressButton::buttonType()
166 {
167 Q_D(KPressButton);
168 return d->m_pButtonType;
169 }
170
171 void KPressButton::setLoaingStatus(bool flag)
172 {
173 Q_D(KPressButton);
174 d->m_pLoading = flag;
175 if (flag)
176 d->m_pTimer->start();
177 else
178 d->m_pTimer->stop();
179 update();
180 }
181
182 bool KPressButton::isLoading()
183 {
184 Q_D(KPressButton);
185 return d->m_pLoading;
186 }
187
188 void KPressButton::setTranslucent(bool flag)
189 {
190 Q_D(KPressButton);
191 d->m_isTranslucentFlag = flag;
192 d->m_pTranslucent = flag;
193 d->changeTheme();
194 }
195
196 bool KPressButton::isTranslucent()
197 {
198 Q_D(KPressButton);
199 return d->m_pTranslucent;
200 }
201
202 void KPressButton::paintEvent(QPaintEvent *event)
203 {
204 Q_D(KPressButton);
205 QStyleOptionButton opt;
206 initStyleOption(&opt);
207 QPainter p(this);
208 QColor borderColor;
209 p.setRenderHint(QPainter::Antialiasing);
210 p.setRenderHint(QPainter::TextAntialiasing);
211 p.setRenderHint(QPainter::SmoothPixmapTransform);
212 if (ThemeController::widgetTheme() == ClassicTheme) {
213 d->m_pTranslucent = false;
214 if (p.pen().width() == 1)
215 p.translate(0.5, 0.5);
216 }
217
218 QColor startColor;
219 QColor endColor;
220 QLinearGradient m_linearGradient = QLinearGradient(rect().left() + rect().width()/2, rect().top(),rect().left() + rect().width()/2, rect().bottom());
221 if (d->m_pTranslucent)
222 {
223 if (isChecked() /*&& !isLoading()*/)
224 {
225 d->m_pbkgColor = ThemeController::getCustomColorFromDT("highlight-active");
226 m_linearGradient.setColorAt(0,d->m_pbkgColor);
227 m_linearGradient.setColorAt(1,d->m_pbkgColor);
228 }
229 else
230 {
231 d->m_pbkgColor = ThemeController::getCustomColorFromDT("kcomponent-alpha-normal");
232 m_linearGradient.setColorAt(0,d->m_pbkgColor);
233 m_linearGradient.setColorAt(1,d->m_pbkgColor);
234 }
235
236 if (opt.state.testFlag(QStyle::State_MouseOver) && isCheckable() /*&& !isLoading()*/) {
237 if (isChecked()) {
238 if (opt.state & QStyle::State_Sunken) {
239 d->m_pbkgColor = ThemeController::mixColor(d->m_pbkgColor, ThemeController::getCustomColorFromDT("brighttext-active"), 0.2); // 点击变深
240 m_linearGradient.setColorAt(0,d->m_pbkgColor);
241 m_linearGradient.setColorAt(1,d->m_pbkgColor);
242 } else {
243 d->m_pbkgColor = ThemeController::mixColor(d->m_pbkgColor, ThemeController::getCustomColorFromDT("brighttext-active"), 0.05); // 悬浮变浅
244 m_linearGradient.setColorAt(0,d->m_pbkgColor);
245 m_linearGradient.setColorAt(1,d->m_pbkgColor);
246 }
247 }
248 else
249 {
250 if (opt.state & QStyle::State_Sunken)
251 {
252 ThemeController::getGradientFromDT("kcomponent-alpha-click",startColor,endColor); // 点击变深
253 m_linearGradient.setColorAt(0,startColor);
254 m_linearGradient.setColorAt(1,endColor);
255 }
256 else
257 {
258 ThemeController::getGradientFromDT("kcomponent-alpha-hover",startColor,endColor); // 悬浮变浅
259 m_linearGradient.setColorAt(0,startColor);
260 m_linearGradient.setColorAt(1,endColor);
261 }
262 }
263 } else if (opt.state.testFlag(QStyle::State_Selected)) {
264 if (isChecked())
265 {
266 d->m_pbkgColor = ThemeController::getCustomColorFromDT("highlight-active");
267 m_linearGradient.setColorAt(0,d->m_pbkgColor);
268 m_linearGradient.setColorAt(1,d->m_pbkgColor);
269 }
270 else
271 {
272 ThemeController::getGradientFromDT("kcomponent-alpha-normal",startColor,endColor);
273 m_linearGradient.setColorAt(0,startColor);
274 m_linearGradient.setColorAt(1,endColor);
275 }
276 }
277 } else // 关闭透明度
278 {
279 if (isChecked() /*&& !isLoading()*/)
280 {
281 d->m_pbkgColor = ThemeController::getCustomColorFromDT("highlight-active");
282 m_linearGradient.setColorAt(0,d->m_pbkgColor);
283 m_linearGradient.setColorAt(1,d->m_pbkgColor);
284 }
285 else
286 {
287 d->m_pbkgColor = ThemeController::getCustomColorFromDT("button-active");
288 m_linearGradient.setColorAt(0,d->m_pbkgColor);
289 m_linearGradient.setColorAt(1,d->m_pbkgColor);
290 }
291
292
293 if (opt.state.testFlag(QStyle::State_MouseOver) && isCheckable() /*&& !isLoading()*/)
294 {
295 if (isChecked())
296 {
297 if (opt.state & QStyle::State_Sunken)
298 {
299 d->m_pbkgColor = ThemeController::mixColor(d->m_pbkgColor, ThemeController::getCustomColorFromDT("brighttext-active"), 0.2); // 点击变深
300 m_linearGradient.setColorAt(0,d->m_pbkgColor);
301 m_linearGradient.setColorAt(1,d->m_pbkgColor);
302 }
303 else
304 {
305 d->m_pbkgColor = ThemeController::mixColor(d->m_pbkgColor, ThemeController::getCustomColorFromDT("brighttext-active"), 0.05); // 悬浮变浅
306 m_linearGradient.setColorAt(0,d->m_pbkgColor);
307 m_linearGradient.setColorAt(1,d->m_pbkgColor);
308 }
309 }
310 else
311 {
312 if (opt.state & QStyle::State_Sunken)
313 {
314 ThemeController::getGradientFromDT("kcomponent-click",startColor,endColor);
315 m_linearGradient.setColorAt(0,startColor);
316 m_linearGradient.setColorAt(1,endColor);
317 }
318 else
319 {
320 ThemeController::getGradientFromDT("kcomponent-hover",startColor,endColor);
321 m_linearGradient.setColorAt(0,startColor);
322 m_linearGradient.setColorAt(1,endColor);
323 }
324
325 }
326 }
327 else if (opt.state.testFlag(QStyle::State_Selected))
328 {
329 if (isChecked())
330 {
331 d->m_pbkgColor = ThemeController::getCustomColorFromDT("highlight-active");
332 m_linearGradient.setColorAt(0,d->m_pbkgColor);
333 m_linearGradient.setColorAt(1,d->m_pbkgColor);
334 }
335 else
336 {
337 d->m_pbkgColor = ThemeController::getCustomColorFromDT("button-active");
338 m_linearGradient.setColorAt(0,d->m_pbkgColor);
339 m_linearGradient.setColorAt(1,d->m_pbkgColor);
340 }
341 }
342 }
343
344 if (opt.icon.name() != "ukui-loading-0.symbolic" && opt.icon.name() != "ukui-loading-1.symbolic" && opt.icon.name() != "ukui-loading-2.symbolic" && opt.icon.name() != "ukui-loading-3.symbolic" && opt.icon.name() != "ukui-loading-4.symbolic" && opt.icon.name() != "ukui-loading-5.symbolic" && opt.icon.name() != "ukui-loading-6.symbolic" && opt.icon.name() != "ukui-loading-7.symbolic")
345 d->m_pIcon = opt.icon;
346
347 QRect rect = this->rect();
348 QPainterPath path;
349 switch (d->m_pButtonType) {
350 case NormalType:
351 if (ThemeController::widgetTheme() == ClassicTheme) {
352 path.addRect(rect.adjusted(0, 0, -1, -1));
353 p.setPen(borderColor);
354 } else {
355 path.moveTo(rect.topLeft() + QPointF(0, d->m_pTopLeftRadius));
356 path.lineTo(rect.bottomLeft() - QPointF(0, d->m_pBottomLeftRadius));
357 path.quadTo(rect.bottomLeft(), rect.bottomLeft() + QPointF(d->m_pBottomLeftRadius, 0));
358 path.lineTo(rect.bottomRight() - QPointF(d->m_pBottomRightRadius, 0));
359 path.quadTo(rect.bottomRight(), rect.bottomRight() - QPointF(0, d->m_pBottomRightRadius));
360 path.lineTo(rect.topRight() + QPointF(0, d->m_pTopRightRadius));
361 path.quadTo(rect.topRight(), rect.topRight() - QPointF(d->m_pTopRightRadius, 0));
362 path.lineTo(rect.topLeft() + QPointF(d->m_pTopLeftRadius, 0));
363 path.quadTo(rect.topLeft(), rect.topLeft() + QPointF(0, d->m_pTopLeftRadius));
364 p.setPen(Qt::NoPen);
365 }
366 // if(ThemeController::widgetTheme() == FashionTheme && (opt.state & QStyle::State_Sunken || opt.state & QStyle::State_MouseOver) && !isChecked())
367 p.setBrush(m_linearGradient);
368 // else
369 // p.setBrush(d->m_pbkgColor);
370 p.drawPath(path);
371 break;
372 case CircleType:
373 if (ThemeController::widgetTheme() == ClassicTheme)
374 p.setPen(borderColor);
375 else
376 p.setPen(Qt::NoPen);
377 // if(ThemeController::widgetTheme() == FashionTheme && (opt.state & QStyle::State_Sunken || opt.state & QStyle::State_MouseOver) && !isChecked())
378 p.setBrush(m_linearGradient);
379 // else
380 // p.setBrush(d->m_pbkgColor);
381 p.drawEllipse(rect.adjusted(1, 1, -1, -1));
382 default:
383 break;
384 }
385
386 int iconSize = opt.iconSize.width();
387
388 if(!isEnabled())
389 {
390 opt.icon = ThemeController::drawColoredPixmap(opt.icon.pixmap(iconSize, iconSize), ThemeController::getCustomColorFromDT("buttontext-disable"));
391 d->m_pIcon = ThemeController::drawColoredPixmap(d->m_pIcon.pixmap(iconSize, iconSize), ThemeController::getCustomColorFromDT("buttontext-disable"));
392 }
393 else if (isChecked() || ThemeController::themeMode() == DarkTheme) {
394 opt.icon = ThemeController::drawColoredPixmap(opt.icon.pixmap(iconSize, iconSize), ThemeController::getCustomColorFromDT("kwhite"));
395 d->m_pIcon = ThemeController::drawColoredPixmap(d->m_pIcon.pixmap(iconSize, iconSize), ThemeController::getCustomColorFromDT("kwhite"));
396 } else {
397 opt.icon = ThemeController::drawColoredPixmap(opt.icon.pixmap(iconSize, iconSize), ThemeController::getCustomColorFromDT("kblack"));
398 d->m_pIcon = ThemeController::drawColoredPixmap(d->m_pIcon.pixmap(iconSize, iconSize), ThemeController::getCustomColorFromDT("kblack"));
399 }
400
401 if (isLoading())
402 p.drawPixmap(rect.center().x() - iconSize / 2, rect.center().y() - iconSize / 2, iconSize, iconSize, opt.icon.pixmap(iconSize, iconSize)); // opt.icon loading
403 else
404 p.drawPixmap(rect.center().x() - iconSize / 2, rect.center().y() - iconSize / 2, iconSize, iconSize, d->m_pIcon.pixmap(iconSize, iconSize));
405 }
406
407 }
408
409 #include "kpressbutton.moc"
410 #include "moc_kpressbutton.cpp"