Debian Codegraph @

kysdk-qtwidgets/src/kprogresslabel.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 "kprogresslabel.h"
24 #include "themeController.h"
25 #include <QDebug>
26 #include <QGSettings/QGSettings>
27 #include <QIcon>
28 #include <QVariant>
29
30 namespace kdk
31 {
32
33 class Q_DECL_HIDDEN LineLabel : public QLabel
34 {
35 Q_OBJECT
36 public:
37 LineLabel(QWidget *parent = nullptr);
38 virtual ~LineLabel();
39
40 void setBackgroundHighlight(bool flag);
41
42 protected:
43 virtual void paintEvent(QPaintEvent *);
44
45 private:
46 QColor m_backgroundColor;
47 bool colorFlag;
48 };
49
50 class Q_DECL_HIDDEN CircleLable : public QLabel
51 {
52 Q_OBJECT
53 public:
54 CircleLable(const QString &text, QWidget *parent = nullptr);
55 virtual ~CircleLable();
56 void setText(const QString &text);
57 void setBackgroundHighlight(bool flag);
58 void setSucceed(bool flag);
59
60 protected:
61 virtual void paintEvent(QPaintEvent *);
62
63 private:
64 QString m_text;
65 QColor m_textColor;
66 QColor m_backgroundColor;
67 bool colorFlag;
68 bool isSucceed;
69 int m_radius;
70 };
71
72 class Q_DECL_HIDDEN TextLabel : public QLabel, public ThemeController
73 {
74 Q_OBJECT
75 public:
76 TextLabel(QString str, QWidget *parent = nullptr);
77 virtual ~TextLabel();
78
79 void setTextHighlight(bool flag);
80 void followPalette(bool flag);
81
82 protected:
83 virtual void paintEvent(QPaintEvent *);
84
85 private:
86 bool isPalette;
87 };
88
89 class Q_DECL_HIDDEN KProgressLabelPrivate : public QObject, public ThemeController
90 {
91 Q_OBJECT
92 Q_DECLARE_PUBLIC(KProgressLabel)
93 public:
94 KProgressLabelPrivate(KProgressLabel *parent);
95
96 protected:
97 void changeTheme();
98
99 private:
100 KProgressLabel *const q_ptr;
101
102 QVBoxLayout *mainLayout;
103 QHBoxLayout *circleLayout;
104 QHBoxLayout *textLayout;
105
106 int m_count;
107 QSize m_circleSize;
108 QList<CircleLable *> circleList;
109 QList<LineLabel *> lineList;
110 QList<TextLabel *> textList;
111 };
112
113 KProgressLabel::KProgressLabel(QWidget *parent)
114 : QWidget(parent)
115 , d_ptr(new KProgressLabelPrivate(this))
116 {
117 Q_D(KProgressLabel);
118 d->mainLayout = new QVBoxLayout(this);
119 QWidget *wid1 = new QWidget();
120 QWidget *wid2 = new QWidget();
121 d->circleLayout = new QHBoxLayout(wid1);
122 d->circleLayout->setContentsMargins(0, 0, 0, 0);
123 d->textLayout = new QHBoxLayout(wid2);
124 d->textLayout->setContentsMargins(8, 0, 8, 0);
125 d->mainLayout->setSpacing(0);
126 d->mainLayout->addWidget(wid1);
127 d->mainLayout->addSpacing(8);
128 d->mainLayout->addWidget(wid2);
129 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
130 }
131
132 KProgressLabel::~KProgressLabel()
133 {
134 }
135
136 void KProgressLabel::setStringList(QStringList list1, QStringList list2)
137 {
138 Q_D(KProgressLabel);
139 // 若布局不为空,则清空布局
140 if (!d->circleLayout->isEmpty() || !d->textLayout->isEmpty()) {
141 // 先清除list
142 d->circleList.clear();
143 d->lineList.clear();
144 d->textList.clear();
145
146 QLayoutItem *child;
147 while ((child = d->circleLayout->takeAt(0)) != 0) {
148 // 删除Stretch(弹簧)等布局
149 if (child->spacerItem()) {
150 d->circleLayout->removeItem(child);
151 continue;
152 }
153 // 删除布局
154 d->circleLayout->removeWidget(child->widget());
155 child->widget()->setParent(nullptr);
156 delete child;
157 child = nullptr;
158 }
159 while ((child = d->textLayout->takeAt(0)) != 0) {
160 if (child->spacerItem()) {
161 d->textLayout->removeItem(child);
162 continue;
163 }
164 d->textLayout->removeWidget(child->widget());
165 child->widget()->setParent(nullptr);
166 delete child;
167 child = nullptr;
168 }
169 d->circleLayout->addSpacing(80);
170 d->textLayout->addSpacing(11);
171 }
172 for (int i = 0; i < list1.count(); i++) {
173 // 第一行圆形label
174 QString str;
175 if (i < list1.count())
176 str = list1.at(i);
177 CircleLable *circlelabel = new CircleLable(str, this);
178 circlelabel->setFixedSize(d->m_circleSize);
179 d->circleList.append(circlelabel);
180 d->circleLayout->addWidget(circlelabel);
181
182 if (i < list1.count() - 1) {
183 // 第一行线条label
184 LineLabel *lineLabel;
185 lineLabel = new LineLabel(this);
186 d->lineList.append(lineLabel);
187 d->circleLayout->addWidget(lineLabel);
188 }
189 }
190 // 第二行文本label
191 for (int i = 0; i < list2.count(); i++) {
192 TextLabel *textLabel = new TextLabel(list2.at(i), this);
193 textLabel->setWordWrap(true);
194 textLabel->setAlignment(Qt::AlignCenter);
195 textLabel->setScaledContents(true);
196 d->textList.append(textLabel);
197 d->textLayout->addWidget(textLabel);
198 if (i < list2.count() - 1)
199 d->textLayout->addStretch();
200 }
201 }
202
203 void KProgressLabel::setStringList(QStringList list1)
204 {
205 Q_D(KProgressLabel);
206 if (!d->circleLayout->isEmpty() || !d->textLayout->isEmpty()) {
207 // 先清除list
208 d->circleList.clear();
209 d->lineList.clear();
210 d->textList.clear();
211
212 QLayoutItem *child;
213 while ((child = d->circleLayout->takeAt(0)) != 0) {
214 // 删除Stretch(弹簧)等布局
215 if (child->spacerItem()) {
216 d->circleLayout->removeItem(child);
217 continue;
218 }
219 // 删除布局
220 d->circleLayout->removeWidget(child->widget());
221 child->widget()->setParent(nullptr);
222 delete child;
223 child = nullptr;
224 }
225 while ((child = d->textLayout->takeAt(0)) != 0) {
226 if (child->spacerItem()) {
227 d->textLayout->removeItem(child);
228 continue;
229 }
230 d->textLayout->removeWidget(child->widget());
231 child->widget()->setParent(nullptr);
232 delete child;
233 child = nullptr;
234 }
235 d->circleLayout->addSpacing(80);
236 // d->textLayout->addSpacing(11);
237 }
238 for (int i = 0; i < list1.count(); i++) {
239 // 第一行圆形label
240 QString str;
241 CircleLable *circlelabel = new CircleLable(str, this);
242 circlelabel->setFixedSize(d->m_circleSize);
243 d->circleList.append(circlelabel);
244 d->circleLayout->addWidget(circlelabel);
245
246 if (i < list1.count() - 1) {
247 // 第一行线条label
248 LineLabel *lineLabel;
249 lineLabel = new LineLabel(this);
250 d->lineList.append(lineLabel);
251 d->circleLayout->addWidget(lineLabel);
252 }
253 }
254 // 第二行文本label
255 for (int i = 0; i < list1.count(); i++) {
256 TextLabel *textLabel = new TextLabel(list1.at(i), this);
257 textLabel->setWordWrap(true);
258 textLabel->setAlignment(Qt::AlignCenter);
259 textLabel->setScaledContents(true);
260 d->textList.append(textLabel);
261 d->textLayout->addWidget(textLabel);
262 if (i < list1.count() - 1)
263 d->textLayout->addStretch();
264 }
265 }
266
267 void KProgressLabel::setCurrentIndex(int index)
268 {
269 Q_D(KProgressLabel);
270 if (index < -1)
271 return;
272 if (index > d->circleList.count() - 1)
273 return;
274 for (int i = 0; i <= index; i++) {
275 if (i < index)
276 d->circleList.at(i)->setBackgroundHighlight(true);
277 if (i < d->lineList.count())
278 d->lineList.at(i)->setBackgroundHighlight(true);
279 if (i < index)
280 d->textList.at(i)->setTextHighlight(false);
281 if (i == index) {
282 d->circleList.at(i)->setBackgroundHighlight(false);
283 d->circleList.at(i)->setSucceed(true);
284 d->textList.at(i)->setTextHighlight(true);
285 }
286 }
287 if (d->m_count > index) {
288 for (int j = index + 1; j < d->circleList.count(); j++) {
289 if (j < d->circleList.count()) {
290 d->circleList.at(j)->setSucceed(false);
291 d->circleList.at(j)->setBackgroundHighlight(false);
292 }
293 if (j < d->lineList.count())
294 d->lineList.at(j)->setBackgroundHighlight(false);
295 if (j < d->textList.count())
296 d->textList.at(j)->followPalette(false);
297 }
298 }
299 d->m_count = index;
300 repaint();
301 }
302
303 int KProgressLabel::currentIndex()
304 {
305 Q_D(KProgressLabel);
306 return d->m_count;
307 }
308
309 void KProgressLabel::setCircleSize(QSize size)
310 {
311 Q_D(KProgressLabel);
312 for (int count = 0; count < d->circleList.count(); count++) {
313 d->m_circleSize = size;
314 d->circleList.at(count)->setFixedSize(size);
315 d->textLayout->setContentsMargins(size.width() / 2 - 6, 0, size.width() / 2 - 6, 0);
316 }
317 }
318
319 LineLabel::LineLabel(QWidget *parent)
320 : QLabel(parent)
321 , colorFlag(false)
322 {
323 setMaximumHeight(30);
324 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
325 }
326
327 LineLabel::~LineLabel()
328 {
329 }
330
331 void LineLabel::setBackgroundHighlight(bool flag)
332 {
333 colorFlag = flag;
334 }
335
336 void LineLabel::paintEvent(QPaintEvent *event)
337 {
338 Q_UNUSED(event)
339 QPainter painter(this);
340 painter.setRenderHint(QPainter::Antialiasing);
341 painter.save();
342 if (!colorFlag) {
343 m_backgroundColor = ThemeController::getCustomColorFromDT("midlight-active");
344 } else {
345 m_backgroundColor = ThemeController::getCustomColorFromDT("highlight-active");
346 }
347 QPen pen = painter.pen();
348 pen.setColor(m_backgroundColor);
349 pen.setWidth(2);
350 painter.setPen(pen);
351 QRect rect = this->rect();
352 painter.drawLine(0, rect.height() / 2, rect.width(), rect.height() / 2);
353
354 painter.restore();
355 QLabel::paintEvent(event);
356 }
357
358 CircleLable::CircleLable(const QString &text, QWidget *parent)
359 : QLabel(parent)
360 , m_text(text)
361 , m_radius(24)
362 , colorFlag(false)
363 , isSucceed(false)
364 , m_textColor(Qt::white)
365 {
366 setFixedSize(QSize(m_radius, m_radius));
367 }
368
369 CircleLable::~CircleLable()
370 {
371 }
372
373 void CircleLable::setText(const QString &text)
374 {
375 m_text = text;
376 repaint();
377 }
378
379 void CircleLable::setBackgroundHighlight(bool flag)
380 {
381 colorFlag = flag;
382 }
383
384 void CircleLable::setSucceed(bool flag)
385 {
386 isSucceed = flag;
387 }
388
389 void CircleLable::paintEvent(QPaintEvent *event)
390 {
391 QPainter painter(this);
392 painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
393 // 深浅主题切换
394 if (!colorFlag && !isSucceed) {
395 m_backgroundColor = ThemeController::getCustomColorFromDT("midlight-active");
396 m_textColor = ThemeController::getCustomColorFromDT("kwhite");
397 painter.setBrush(QBrush(m_backgroundColor));
398 } else if (isSucceed || colorFlag) {
399 painter.setBrush(ThemeController::getCustomColorFromDT("highlight-active"));
400 }
401
402 painter.setPen(Qt::NoPen);
403 QRect rect = this->rect();
404 painter.drawRoundedRect(rect, rect.width() / 2, rect.width() / 2);
405
406 painter.setPen(m_textColor);
407 if (colorFlag) {
408 QPixmap pixmap = QIcon::fromTheme("dialog-apply").pixmap(16, 16);
409 pixmap = ThemeController::drawSymbolicColoredPixmap(pixmap);
410 QRect pixRect((rect.width() - 16) / 2, (rect.height() - 16) / 2, 16, 16);
411 painter.drawPixmap(pixRect, pixmap);
412 } else
413 painter.drawText(rect, Qt::AlignCenter, m_text);
414
415 QLabel::paintEvent(event);
416 }
417
418 TextLabel::TextLabel(QString str, QWidget *parent)
419 : QLabel(parent)
420 {
421 setText(str);
422 isPalette = false;
423 }
424
425 TextLabel::~TextLabel()
426 {
427 }
428
429 void TextLabel::setTextHighlight(bool flag)
430 {
431 isPalette = flag;
432 if (flag) {
433 QPalette palette = this->palette();
434 palette.setColor(QPalette::WindowText, ThemeController::getCustomColorFromDT("highlight-active"));
435 this->setPalette(palette);
436 } else {
437 QPalette palette;
438 this->setPalette(palette);
439 }
440 }
441
442 void TextLabel::followPalette(bool flag)
443 {
444 isPalette = flag;
445 }
446
447 void TextLabel::paintEvent(QPaintEvent *e)
448 {
449 if (!isPalette) {
450 QPalette palette = this->palette();
451 QColor textColor = ThemeController::getCustomColorFromDT("brighttext-active");
452 textColor.setAlphaF(0.35);
453 palette.setColor(QPalette::WindowText, textColor);
454 this->setPalette(palette);
455 } else {
456 QPalette palette = this->palette();
457 palette.setColor(QPalette::WindowText, ThemeController::getCustomColorFromDT("highlight-active"));
458 this->setPalette(palette);
459 }
460 QLabel::paintEvent(e);
461 }
462
463 KProgressLabelPrivate::KProgressLabelPrivate(KProgressLabel *parent)
464 : q_ptr(parent)
465 , m_count(-1)
466 , m_circleSize(24, 24)
467 {
468 Q_Q(KProgressLabel);
469 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
470 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
471 {
472 changeTheme();
473 }
474 });
475 }
476
477 void KProgressLabelPrivate::changeTheme()
478 {
479 Q_Q(KProgressLabel);
480 initThemeStyle();
481 }
482
483 }
484
485 #include "kprogresslabel.moc"
486 #include "moc_kprogresslabel.cpp"