Debian Codegraph @

kysdk-qtwidgets/src/kprogressbar.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 "kprogressbar.h"
24 #include "themeController.h"
25 #include <QApplication>
26 #include <QDebug>
27 #include <QFontMetrics>
28 #include <QLinearGradient>
29 #include <QPainter>
30 #include <QRect>
31 #include <QStyleOptionProgressBar>
32 #include <QVariantAnimation>
33
34 namespace kdk
35 {
36 class KProgressBarPrivate : public QObject, public ThemeController
37 {
38 Q_OBJECT
39 Q_DECLARE_PUBLIC(KProgressBar)
40
41 public:
42 KProgressBarPrivate(KProgressBar *parent);
43 void calculateTextRect();
44 void calculateRect();
45 void calculateContenteRect();
46 void changeTheme();
47 int computeHardLightChannel(int base, int blend);
48 QColor hardLight(QColor blendColor, QColor baseColor = Qt::white);
49
50 Q_SIGNALS:
51 void progressStateChanged();
52
53 private:
54 KProgressBar *q_ptr;
55 ProgressBarState m_state;
56 int m_contentMargin;
57 QRect m_textRect;
58 QRect m_contentRect;
59 QRect m_rect;
60 int m_bodyWidth;
61 QLinearGradient m_linearGradient;
62 bool isLinearGradient;
63 QColor m_color1;
64 QColor m_color2;
65 QColor m_backgroundColor;
66 bool isBackgroundColor;
67 QColor m_fillColor;
68 bool isFillColor;
69 QVariantAnimation *m_pAnimation;
70 qreal m_animationPos;
71 bool linearGradient;
72 bool animationsFlag;
73 };
74
75 KProgressBar::KProgressBar(QWidget *parent)
76 : QProgressBar(parent)
77 , d_ptr(new KProgressBarPrivate(this))
78 {
79 Q_D(KProgressBar);
80 d->changeTheme();
81 connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){
82 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
83 {
84 d->changeTheme();
85 }
86 });
87 connect(this, &KProgressBar::valueChanged, this, [=]() {
88 if (this->value() == this->maximum()) {
89 setState(SuccessProgress);
90 }
91 });
92
93 connect(d->m_pAnimation, &QVariantAnimation::valueChanged, this, [=](const QVariant &value) {
94 if (orientation() == Qt::Horizontal)
95 d->m_animationPos = value.toDouble() / d->m_contentRect.width();
96 else {
97 d->m_animationPos = (value.toDouble() - d->m_contentRect.top()) / d->m_contentRect.height();
98 }
99 update();
100 });
101
102 setContentsMargins(6, 6, 6, 6);
103 setValue(0);
104 }
105
106 ProgressBarState KProgressBar::state()
107 {
108 Q_D(KProgressBar);
109 return d->m_state;
110 }
111
112 void KProgressBar::setState(ProgressBarState state)
113 {
114 Q_D(KProgressBar);
115 d->m_state = state;
116 if (d->animationsFlag && state == ProgressBarState::NormalProgress) {
117 d->m_pAnimation->start();
118 } else {
119 d->m_pAnimation->stop();
120 }
121 update();
122 }
123
124 void KProgressBar::paintEvent(QPaintEvent *event)
125 {
126 Q_D(KProgressBar);
127 d->calculateTextRect();
128 d->calculateRect();
129 d->calculateContenteRect();
130 // 以上三个函数有逻辑关系,相对位置不能改变
131
132 QPainter painter(this);
133 painter.setRenderHint(QPainter::Antialiasing);
134 painter.setPen(Qt::NoPen);
135
136 int radius = ThemeController::getRadiusFromDT("kradius-normal");
137 if (radius == -1)
138 radius = 6;
139
140 if (this->orientation() == Qt::Horizontal)
141 {
142 if (d->isBackgroundColor)
143 painter.setBrush(d->m_backgroundColor);
144 else
145 painter.setBrush(ThemeController::getCustomColorFromDT("kcomponent-alpha-disable"));
146 painter.setPen(ThemeController::getCustomColorFromDT("kline-component-normal"));
147 painter.drawRoundedRect(d->m_rect, radius, radius);
148 // QLinearGradient linear(this->rect().topLeft(), this->rect().bottomRight());
149 QLinearGradient linear(d->m_contentRect.left(), 0, d->m_contentRect.right(), 0);
150 d->m_linearGradient = QLinearGradient(d->m_contentRect.topLeft(), d->m_contentRect.bottomRight());
151 QColor color = ThemeController::getCustomColorFromDT("highlight-active");
152 switch (d->m_state) {
153 case NormalProgress:
154 if (d->animationsFlag) {
155 linear.setColorAt(0, color);
156 linear.setColorAt(d->m_animationPos, d->hardLight(color));
157 linear.setColorAt(1, color);
158 } else {
159 QColor startColor;
160 QColor endColor;
161 ThemeController::getGradientFromDT("kbrand-normal",startColor,endColor);
162 linear.setColorAt(0, startColor);
163 linear.setColorAt(1, endColor);
164 }
165 linear.setSpread(QGradient::PadSpread);
166 if (d->isLinearGradient) {
167 if (!d->linearGradient) {
168 d->m_color1 = ThemeController::mixColor(color, Qt::white, 0.2);
169 d->m_color2 = ThemeController::mixColor(color, Qt::white, 0.05);
170 }
171 d->m_linearGradient.setColorAt(0, d->m_color1);
172 d->m_linearGradient.setColorAt(1, d->m_color2);
173 painter.setBrush(d->m_linearGradient);
174 } else if (d->isFillColor)
175 painter.setBrush(d->m_fillColor);
176 else
177 painter.setBrush(linear);
178 painter.drawRoundedRect(d->m_contentRect, 6, 6);
179 if (isTextVisible()) {
180 painter.setPen(ThemeController::getCustomColorFromDT("kfont-primary"));
181 painter.drawText(d->m_textRect, Qt::AlignCenter, text());
182 }
183 break;
184 case FailedProgress:
185 {
186 QColor startColor;
187 QColor endColor;
188 ThemeController::getGradientFromDT("kerror-normal",startColor,endColor);
189 linear.setColorAt(0,startColor);
190 linear.setColorAt(1,endColor);
191 linear.setSpread(QGradient::PadSpread);
192 painter.setBrush(linear);
193 painter.drawRoundedRect(d->m_contentRect, 6, 6);
194 if (isTextVisible()) {
195 QPixmap pixmap = QIcon::fromTheme("dialog-error").pixmap(16, 16);
196 QRect rect(0, 0, 16, 16);
197 rect.moveCenter(d->m_textRect.center());
198 painter.drawPixmap(rect, pixmap);
199 }
200 break;
201 }
202 case SuccessProgress:
203 {
204 QColor startColor;
205 QColor endColor;
206 ThemeController::getGradientFromDT("ksuccess-normal",startColor,endColor);
207 linear.setColorAt(0,startColor);
208 linear.setColorAt(1,endColor);
209 linear.setSpread(QGradient::PadSpread);
210 painter.setBrush(linear);
211 painter.drawRoundedRect(d->m_contentRect, 6, 6);
212 if (isTextVisible()) {
213 QPixmap pixmap = QIcon::fromTheme("ukui-dialog-success").pixmap(16, 16);
214 QRect rect(0, 0, 16, 16);
215 rect.moveCenter(d->m_textRect.center());
216 painter.drawPixmap(rect, pixmap);
217 }
218 break;
219 }
220 default:
221 break;
222 }
223 } else {
224 if (d->isBackgroundColor)
225 painter.setBrush(d->m_backgroundColor);
226 else
227 {
228 painter.setBrush(ThemeController::getCustomColorFromDT("kcomponent-alpha-disable"));
229 }
230 painter.setPen(ThemeController::getCustomColorFromDT("kline-component-normal"));
231 painter.drawRoundedRect(d->m_rect, 6, 6);
232 QLinearGradient linear(0, d->m_contentRect.top(), 0, d->m_contentRect.bottom());
233
234 d->m_linearGradient = QLinearGradient(d->m_contentRect.topLeft(), d->m_contentRect.bottomRight());
235 QColor color = ThemeController::getCustomColorFromDT("highlight-active");
236 switch (d->m_state) {
237 case NormalProgress:
238 if (d->animationsFlag) {
239 linear.setColorAt(0, color);
240 linear.setColorAt(d->m_animationPos, d->hardLight(color));
241 linear.setColorAt(1, color);
242 } else {
243 QColor startColor;
244 QColor endColor;
245 ThemeController::getGradientFromDT("kbrand-normal",startColor,endColor);
246 linear.setColorAt(0, startColor);
247 linear.setColorAt(1, endColor);
248 }
249 linear.setSpread(QGradient::PadSpread);
250 if (d->isLinearGradient)
251 {
252 if (!d->linearGradient) {
253 d->m_color1 = ThemeController::mixColor(color, Qt::white, 0.2);
254 d->m_color2 = ThemeController::mixColor(color, Qt::white, 0.05);
255 }
256 d->m_linearGradient.setColorAt(0, d->m_color1);
257 d->m_linearGradient.setColorAt(1, d->m_color2);
258 painter.setBrush(d->m_linearGradient);
259 } else if (d->isFillColor)
260 painter.setBrush(d->m_fillColor);
261 else
262 painter.setBrush(linear);
263 painter.drawRoundedRect(d->m_contentRect, 6, 6);
264 if (isTextVisible()) {
265 painter.setPen(ThemeController::getCustomColorFromDT("kfont-primary"));
266 painter.drawText(d->m_textRect, Qt::AlignCenter, text());
267 }
268 break;
269 case FailedProgress: {
270 QColor startColor;
271 QColor endColor;
272 ThemeController::getGradientFromDT("kerror-normal",startColor,endColor);
273 linear.setColorAt(0,startColor);
274 linear.setColorAt(1,endColor);
275 linear.setSpread(QGradient::PadSpread);
276 painter.setBrush(linear);
277 painter.drawRoundedRect(d->m_contentRect, 6, 6);
278 if (isTextVisible()) {
279 QPixmap pixmap = QIcon::fromTheme("dialog-error").pixmap(16, 16);
280 QRect rect(0, 0, 16, 16);
281 rect.moveCenter(d->m_textRect.center());
282 painter.drawPixmap(rect, pixmap);
283 }
284 break;
285 }
286 case SuccessProgress: {
287 QColor startColor;
288 QColor endColor;
289 ThemeController::getGradientFromDT("ksuccess-normal",startColor,endColor);
290 linear.setColorAt(0,startColor);
291 linear.setColorAt(1,endColor);
292 linear.setSpread(QGradient::PadSpread);
293 painter.setBrush(linear);
294 painter.drawRoundedRect(d->m_contentRect, 6, 6);
295 if (isTextVisible()) {
296 QPixmap pixmap = QIcon::fromTheme("ukui-dialog-success").pixmap(16, 16);
297 // painter.drawPixmap(d->m_textRect,pixmap);
298 QRect rect(0, 0, 16, 16);
299 rect.moveCenter(d->m_textRect.center());
300 painter.drawPixmap(rect, pixmap);
301 }
302 break;
303 }
304 default:
305 break;
306 }
307 }
308 }
309
310 QSize KProgressBar::sizeHint() const
311 {
312 QSize size = QProgressBar::sizeHint();
313 if (this->orientation() == Qt::Horizontal)
314 size.setHeight(30);
315 else
316 size.setWidth(30);
317 return size;
318 }
319
320 QString KProgressBar::text() const
321 {
322 Q_D(const KProgressBar);
323 if ((maximum() == 0 && minimum() == 0) || value() < minimum()
324 || (minimum() == INT_MIN && minimum() == INT_MIN))
325 return QString();
326 qint64 totalSteps = qint64(maximum()) - minimum();
327
328 QString result = format();
329 QLocale locale = this->locale(); // Omit group separators for compatibility with previous versions that were non-localized.
330 locale.setNumberOptions(locale.numberOptions() | QLocale::OmitGroupSeparator);
331 result.replace(QLatin1String("%m"), locale.toString(totalSteps));
332 result.replace(QLatin1String("%v"), locale.toString(value()));
333
334 // If max and min are equal and we get this far, it means that the
335 // progress bar has one step and that we are on that step. Return
336 // 100% here in order to avoid division by zero further down.
337 if (totalSteps == 0) {
338 result.replace(QLatin1String("%p"), locale.toString(100));
339 return result;
340 }
341
342 const auto progress = static_cast<int>((qint64(value()) - minimum()) * 100.0 / totalSteps);
343 result.replace(QLatin1String("%p"), locale.toString(progress));
344 return result;
345 }
346
347 void KProgressBar::setOrientation(Qt::Orientation orientation)
348 {
349 if (orientation == Qt::Vertical)
350 this->setMinimumHeight(200);
351 QProgressBar::setOrientation(orientation);
352 }
353
354 void KProgressBar::setBodyWidth(int width)
355 {
356 Q_D(KProgressBar);
357 d->m_bodyWidth = width;
358 update();
359 }
360
361 void KProgressBar::setGradationColor(QColor color1, QColor color2)
362 {
363 Q_D(KProgressBar);
364 d->linearGradient = true;
365 d->m_color1 = color1;
366 d->m_color2 = color2;
367 }
368
369 void KProgressBar::setBackgroundColor(QColor color)
370 {
371 Q_D(KProgressBar);
372 d->m_backgroundColor = color;
373 d->isBackgroundColor = true;
374 }
375
376 void KProgressBar::setFillColor(QColor color)
377 {
378 Q_D(KProgressBar);
379 d->m_fillColor = color;
380 d->isFillColor = true;
381 }
382
383 void KProgressBar::setGradation(bool flag)
384 {
385 Q_D(KProgressBar);
386 d->isLinearGradient = flag;
387 }
388
389 bool KProgressBar::gradation()
390 {
391 Q_D(KProgressBar);
392 return d->isLinearGradient;
393 }
394
395 void KProgressBar::setAnimation(bool flag)
396 {
397 Q_D(KProgressBar);
398 d->animationsFlag = flag;
399
400 if (!flag) {
401 d->m_pAnimation->stop();
402 } else {
403 d->m_pAnimation->start();
404 }
405 }
406
407 bool KProgressBar::animation()
408 {
409 Q_D(KProgressBar);
410 return d->animationsFlag;
411 }
412
413 KProgressBarPrivate::KProgressBarPrivate(KProgressBar *parent)
414 : q_ptr(parent)
415 {
416 Q_Q(KProgressBar);
417 m_contentMargin = 2;
418 m_state = NormalProgress;
419 setParent(parent);
420 m_bodyWidth = 0;
421 isLinearGradient = false;
422 isBackgroundColor = false;
423 isFillColor = false;
424 linearGradient = false;
425 animationsFlag = false;
426
427 m_animationPos = 0.00;
428 m_pAnimation = new QVariantAnimation();
429 m_pAnimation->setDuration(1000);
430 m_pAnimation->setEasingCurve(QEasingCurve::Linear);
431 m_pAnimation->setLoopCount(-1);
432 }
433
434 void KProgressBarPrivate::calculateTextRect()
435 {
436 Q_Q(KProgressBar);
437 if (!q->isTextVisible())
438 m_textRect = QRect();
439 else {
440 QFont font(QApplication::font());
441 QFontMetrics fm(font);
442 m_textRect = QRect(0, 0, fm.horizontalAdvance(q->text()), fm.height());
443 m_textRect.moveCenter(q->rect().center());
444 if (q->orientation() == Qt::Horizontal) {
445 if (q->alignment() & Qt::AlignCenter)
446 return;
447 else {
448 if (ThemeController::systemLang())
449 m_textRect.moveLeft(q->rect().left());
450 else
451 m_textRect.moveRight(q->rect().right());
452 }
453 } else {
454 if (q->alignment() & Qt::AlignCenter)
455 return;
456 else
457 m_textRect.moveTop(q->rect().top());
458 }
459 }
460 }
461
462 void KProgressBarPrivate::calculateRect() // 背景矩形
463 {
464 Q_Q(KProgressBar);
465 QMargins margins = q->contentsMargins();
466 m_rect = q->rect();
467 if (q->orientation() == Qt::Horizontal) {
468 if (m_bodyWidth != 0)
469 m_rect.setHeight(m_bodyWidth);
470 if (!q->isTextVisible())
471 return;
472 else {
473 m_rect.moveCenter(q->rect().center());
474 if (q->alignment() & Qt::AlignCenter)
475 return;
476 else {
477 if (ThemeController::systemLang())
478 m_rect.setLeft(margins.right() + m_textRect.width() /*-gSpace*/);
479 else
480 m_rect.setRight(q->rect().width() - margins.right() - m_textRect.width() /*-gSpace*/);
481 }
482 }
483 } else {
484 if (m_bodyWidth != 0)
485 m_rect.setWidth(m_bodyWidth);
486 if (!q->isTextVisible())
487 return;
488 else {
489 m_rect.moveCenter(q->rect().center());
490 if (q->alignment() & Qt::AlignCenter)
491 return;
492 else {
493 m_rect.setTop(margins.top() + m_textRect.height() /* + gSpace*/);
494 }
495 }
496 }
497 }
498
499 void KProgressBarPrivate::calculateContenteRect() // 填充矩形
500 {
501 Q_Q(KProgressBar);
502 m_contentRect = m_rect;
503 if (q->orientation() == Qt::Horizontal) {
504 int width;
505 qint64 totalSteps = qint64(q->maximum()) - q->minimum();
506 width = m_rect.width() * (q->value() - q->minimum()) / totalSteps;
507 if (!width) {
508 m_contentRect = QRect();
509 }
510 // 没设置反方向的情况下,即普通情况下
511 if (!q->invertedAppearance()) {
512 m_contentRect.setRight(width + m_rect.left());
513 } else {
514 m_contentRect.setLeft(m_rect.width() - width);
515 }
516 m_pAnimation->setStartValue(m_contentRect.x());
517 m_pAnimation->setEndValue(m_contentRect.width());
518 } else {
519 int height;
520 qint64 totalSteps = q->maximum() - q->minimum();
521 height = m_rect.height() * (q->value() - q->minimum()) / totalSteps;
522 if (!height) {
523 m_contentRect = QRect();
524 }
525 // 没设置反方向的情况下,即普通情况下
526 if (!q->invertedAppearance()) {
527 m_contentRect.setTop(m_rect.top() + m_rect.height() - height);
528 } else {
529 m_contentRect.setBottom(m_rect.top() + height);
530 }
531 m_pAnimation->setStartValue(m_contentRect.bottom());
532 m_pAnimation->setEndValue(m_contentRect.top());
533 }
534 }
535
536 void KProgressBarPrivate::changeTheme()
537 {
538 Q_Q(KProgressBar);
539 initThemeStyle();
540 q->repaint();
541 }
542
543 int KProgressBarPrivate::computeHardLightChannel(int base, int blend)
544 {
545 if (blend <= 128) {
546 return (base * blend) / 128;
547 }
548 return 255 - ((255 - blend) * (255 - base)) / 128;
549 }
550
551 QColor KProgressBarPrivate::hardLight(QColor blendColor, QColor baseColor)
552 {
553 int r = computeHardLightChannel(baseColor.red(), blendColor.red());
554 int g = computeHardLightChannel(baseColor.green(), blendColor.green());
555 int b = computeHardLightChannel(baseColor.blue(), blendColor.blue());
556
557 return QColor(r, g, b, blendColor.alpha());
558 }
559
560
561 }
562
563 #include "kprogressbar.moc"
564 #include "moc_kprogressbar.cpp"