Debian Codegraph @

kysdk-qtwidgets/src/kcolorbutton.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 "kcolorbutton.h"
24 #include "themeController.h"
25 #include <QDebug>
26 #include <QPainter>
27 #include <QStyleOptionButton>
28
29 namespace kdk
30 {
31
32 static const int circleButtonSize = 20;
33 static const int normalButtonSize = 24;
34
35 class KColorButtonPrivate : public QObject, public ThemeController
36 {
37 Q_OBJECT
38 Q_DECLARE_PUBLIC(KColorButton)
39 public:
40 KColorButtonPrivate(KColorButton *parent);
41
42 protected:
43 void changeTheme();
44
45 private:
46 KColorButton *const q_ptr;
47 bool m_backgroundFlag;
48 QColor m_backgroundColor;
49 int m_radius;
50 KColorButton::ButtonType m_buttonType;
51 };
52
53 KColorButton::KColorButton(QWidget *parent)
54 : QPushButton(parent)
55 , d_ptr(new KColorButtonPrivate(this))
56 {
57 Q_D(KColorButton);
58 setCheckable(true);
59 }
60
61 void KColorButton::setBackgroundColor(QColor color)
62 {
63 Q_D(KColorButton);
64 d->m_backgroundFlag = true;
65 d->m_backgroundColor = color;
66 update();
67 }
68
69 QColor KColorButton::backgroundColor()
70 {
71 Q_D(KColorButton);
72 return d->m_backgroundColor;
73 }
74
75 void KColorButton::setBorderRadius(int radious)
76 {
77 Q_D(KColorButton);
78 d->m_radius = radious;
79 }
80
81 int KColorButton::borderRadius()
82 {
83 Q_D(KColorButton);
84 return d->m_radius;
85 }
86
87 void KColorButton::setButtonType(KColorButton::ButtonType type)
88 {
89 Q_D(KColorButton);
90 d->m_buttonType = type;
91 }
92
93 KColorButton::ButtonType KColorButton::buttonType()
94 {
95 Q_D(KColorButton);
96 return d->m_buttonType;
97 }
98
99 void KColorButton::paintEvent(QPaintEvent *)
100 {
101 Q_D(KColorButton);
102 QStyleOptionButton option;
103 initStyleOption(&option);
104
105 QRect rect = option.rect;
106
107 QPainter painter(this);
108 painter.setRenderHint(QPainter::Antialiasing);
109 painter.setRenderHint(QPainter::SmoothPixmapTransform);
110 painter.setPen(Qt::NoPen);
111
112 if (!option.state.testFlag(QStyle::State_Enabled)) {
113 painter.save();
114 painter.setBrush(ThemeController::getCustomColorFromDT("buttontext-disable"));
115 painter.drawRoundedRect(rect, d->m_radius, d->m_radius);
116 painter.restore();
117 } else if (d->m_buttonType == RoundedRect) {
118 painter.save();
119 if (d->m_backgroundFlag) {
120 painter.setBrush(d->m_backgroundColor);
121 } else {
122 painter.setBrush(ThemeController::getCustomColorFromDT("highlight-active"));
123 }
124 if (isChecked() || option.state.testFlag(QStyle::State_MouseOver)) {
125 painter.save();
126 painter.setBrush(Qt::white);
127 painter.drawRoundedRect(rect, d->m_radius, d->m_radius); // 绘制边框
128 rect = rect.adjusted(2, 2, -2, -2);
129 painter.restore();
130
131 painter.drawRoundedRect(rect, d->m_radius / 2, d->m_radius / 2);
132 } else
133 painter.drawRoundedRect(rect, d->m_radius, d->m_radius);
134 painter.restore();
135 } else if (d->m_buttonType == Circle) {
136 painter.save();
137 QRect rect = option.rect.adjusted(1, 1, 0, 0);
138 if (d->m_backgroundFlag) {
139 painter.setBrush(d->m_backgroundColor);
140 } else {
141 painter.setBrush(ThemeController::getCustomColorFromDT("highlight-active"));
142 }
143
144 painter.drawEllipse(rect);
145
146 QRect rect1 = rect.adjusted(rect.width() / 4, rect.height() / 4, -rect.width() / 4, -rect.height() / 4);
147 if (option.state.testFlag(QStyle::State_MouseOver) || isChecked()) {
148 painter.save();
149 painter.setBrush(Qt::white);
150 painter.drawEllipse(rect1);
151 painter.restore();
152 }
153 painter.restore();
154 } else {
155 painter.save();
156 if (d->m_backgroundFlag) {
157 painter.setBrush(d->m_backgroundColor);
158 } else {
159 painter.setBrush(ThemeController::getCustomColorFromDT("highlight-active"));
160 }
161 if (isChecked()) {
162 QPointF points[3] = {
163 QPointF(rect.x() + rect.width() * 2 / 11, rect.y() + rect.height() * 6 / 11),
164 QPointF(rect.x() + rect.width() * 5 / 11, rect.y() + rect.height() * 8 / 11),
165 QPointF(rect.x() + rect.width() * 9 / 11, rect.y() + rect.height() * 4 / 11),
166 };
167 painter.drawRoundedRect(rect, d->m_radius, d->m_radius);
168 painter.setPen(QPen(Qt::white, 2));
169 painter.drawPolyline(points, 3);
170 } else {
171 painter.drawRoundedRect(rect, d->m_radius, d->m_radius);
172 }
173 painter.restore();
174 }
175 }
176
177 QSize KColorButton::sizeHint() const
178 {
179 Q_D(const KColorButton);
180 if (d->m_buttonType == Circle)
181 return QSize(circleButtonSize, circleButtonSize);
182 else
183 return QSize(normalButtonSize, normalButtonSize);
184 }
185
186 KColorButtonPrivate::KColorButtonPrivate(KColorButton *parent)
187 : q_ptr(parent)
188 {
189 Q_Q(KColorButton);
190 m_backgroundFlag = false;
191 m_radius = ThemeController::getRadiusFromDT("kradius-normal");
192 m_buttonType = KColorButton::RoundedRect;
193 m_backgroundColor = ThemeController::getCustomColorFromDT("highlight-active");
194 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
195 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
196 {
197 changeTheme();
198 q->update();
199 }
200 });
201 }
202
203 void KColorButtonPrivate::changeTheme()
204 {
205 Q_Q(KColorButton);
206 initThemeStyle();
207 }
208
209 }
210
211 #include "kcolorbutton.moc"
212 #include "moc_kcolorbutton.cpp"