| 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 "kprogresscircle.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QDebug> |
| 26 |
#include <QIcon> |
| 27 |
#include <QLocale> |
| 28 |
#include <QPainter> |
| 29 |
|
| 30 |
namespace kdk |
| 31 |
{ |
| 32 |
class KProgressCirclePrivate : public QObject, public ThemeController |
| 33 |
{ |
| 34 |
Q_OBJECT |
| 35 |
Q_DECLARE_PUBLIC(KProgressCircle) |
| 36 |
|
| 37 |
public: |
| 38 |
KProgressCirclePrivate(KProgressCircle *parent); |
| 39 |
void initDefaultFormat(); |
| 40 |
void drawBaseCircle(QPainter *painter); |
| 41 |
void dawColoredCircle(QPainter *painter); |
| 42 |
void drawText(QPainter *painter); |
| 43 |
void calculateCircleRect(); |
| 44 |
void changeTheme(); |
| 45 |
|
| 46 |
private: |
| 47 |
KProgressCircle *q_ptr; |
| 48 |
int m_minimum; |
| 49 |
int m_maximum; |
| 50 |
int m_value; |
| 51 |
QString m_format; |
| 52 |
QString m_text; |
| 53 |
bool m_defaultFormat; |
| 54 |
bool m_isTextVisiable; |
| 55 |
ProgressBarState m_state; |
| 56 |
|
| 57 |
QColor m_color; |
| 58 |
QColor m_baseColor; |
| 59 |
QColor m_baseCircleColor; |
| 60 |
QRect m_baseRect; |
| 61 |
QRect m_contentRect; |
| 62 |
int m_cirleWidth; |
| 63 |
int m_startAngel; |
| 64 |
int m_pixWidth; |
| 65 |
int m_textWidth; |
| 66 |
}; |
| 67 |
|
| 68 |
KProgressCircle::KProgressCircle(QWidget *parent) |
| 69 |
: QWidget(parent) |
| 70 |
, d_ptr(new KProgressCirclePrivate(this)) |
| 71 |
{ |
| 72 |
setFixedSize(60, 60); |
| 73 |
connect(this, &KProgressCircle::valueChanged, this, [=]() { |
| 74 |
if (this->value() == this->maximum()) |
| 75 |
setState(SuccessProgress); |
| 76 |
}); |
| 77 |
} |
| 78 |
|
| 79 |
int KProgressCircle::minimum() const |
| 80 |
{ |
| 81 |
Q_D(const KProgressCircle); |
| 82 |
return d->m_minimum; |
| 83 |
} |
| 84 |
|
| 85 |
int KProgressCircle::maximum() const |
| 86 |
{ |
| 87 |
Q_D(const KProgressCircle); |
| 88 |
return d->m_maximum; |
| 89 |
} |
| 90 |
|
| 91 |
int KProgressCircle::value() const |
| 92 |
{ |
| 93 |
Q_D(const KProgressCircle); |
| 94 |
return d->m_value; |
| 95 |
} |
| 96 |
|
| 97 |
QString KProgressCircle::text() const |
| 98 |
{ |
| 99 |
Q_D(const KProgressCircle); |
| 100 |
if ((d->m_maximum == 0 && d->m_minimum == 0) || d->m_value < d->m_minimum |
| 101 |
|| (d->m_value == INT_MIN && d->m_minimum == INT_MIN)) |
| 102 |
return QString(); |
| 103 |
|
| 104 |
qint64 totalSteps = qint64(d->m_maximum) - d->m_minimum; |
| 105 |
|
| 106 |
QString result = d->m_format; |
| 107 |
QLocale locale = QWidget::locale(); |
| 108 |
locale.setNumberOptions(locale.numberOptions() | QLocale::OmitGroupSeparator); |
| 109 |
result.replace(QLatin1String("%m"), QString::number(totalSteps)); |
| 110 |
result.replace(QLatin1String("%v"), QString::number(d->m_value)); |
| 111 |
|
| 112 |
if (totalSteps == 0) { |
| 113 |
result.replace(QLatin1String("%p"), locale.toString(100)); |
| 114 |
return result; |
| 115 |
} |
| 116 |
|
| 117 |
const auto progress = static_cast<int>((qint64(d->m_value) - d->m_minimum) * 100.0 / totalSteps); |
| 118 |
result.replace(QLatin1String("%p"), QString::number(progress)); |
| 119 |
return result; |
| 120 |
} |
| 121 |
|
| 122 |
void KProgressCircle::setTextVisible(bool visible) |
| 123 |
{ |
| 124 |
Q_D(KProgressCircle); |
| 125 |
d->m_isTextVisiable = visible; |
| 126 |
update(); |
| 127 |
} |
| 128 |
|
| 129 |
bool KProgressCircle::isTextVisible() const |
| 130 |
{ |
| 131 |
Q_D(const KProgressCircle); |
| 132 |
return d->m_isTextVisiable; |
| 133 |
} |
| 134 |
|
| 135 |
ProgressBarState KProgressCircle::state() |
| 136 |
{ |
| 137 |
Q_D(const KProgressCircle); |
| 138 |
return d->m_state; |
| 139 |
} |
| 140 |
|
| 141 |
void KProgressCircle::setState(ProgressBarState state) |
| 142 |
{ |
| 143 |
Q_D(KProgressCircle); |
| 144 |
d->m_state = state; |
| 145 |
switch (d->m_state) { |
| 146 |
case FailedProgress: |
| 147 |
d->m_color = "#FF4D4F"; |
| 148 |
break; |
| 149 |
case SuccessProgress: |
| 150 |
d->m_color = "#52C429"; |
| 151 |
break; |
| 152 |
case NormalProgress: |
| 153 |
default: |
| 154 |
d->m_color = "#3790FA"; |
| 155 |
break; |
| 156 |
} |
| 157 |
update(); |
| 158 |
} |
| 159 |
|
| 160 |
void KProgressCircle::reset() |
| 161 |
{ |
| 162 |
Q_D(KProgressCircle); |
| 163 |
if (d->m_minimum == INT_MIN) |
| 164 |
d->m_value = INT_MIN; |
| 165 |
else |
| 166 |
d->m_value = d->m_minimum - 1; |
| 167 |
repaint(); |
| 168 |
} |
| 169 |
|
| 170 |
void KProgressCircle::setRange(int minimum, int maximum) |
| 171 |
{ |
| 172 |
Q_D(KProgressCircle); |
| 173 |
if (minimum != d->m_minimum || maximum != d->m_maximum) { |
| 174 |
d->m_minimum = minimum; |
| 175 |
d->m_maximum = qMax(minimum, maximum); |
| 176 |
|
| 177 |
if (d->m_value < qint64(d->m_minimum) - 1 || d->m_value > d->m_maximum) |
| 178 |
reset(); |
| 179 |
else |
| 180 |
update(); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void KProgressCircle::setMinimum(int minimum) |
| 185 |
{ |
| 186 |
Q_D(KProgressCircle); |
| 187 |
setRange(minimum, qMax(d->m_maximum, minimum)); |
| 188 |
} |
| 189 |
|
| 190 |
void KProgressCircle::setMaximum(int maximum) |
| 191 |
{ |
| 192 |
Q_D(KProgressCircle); |
| 193 |
setRange(qMin(d->m_minimum, maximum), maximum); |
| 194 |
} |
| 195 |
|
| 196 |
void KProgressCircle::setValue(int value) |
| 197 |
{ |
| 198 |
Q_D(KProgressCircle); |
| 199 |
if (d->m_value == value |
| 200 |
|| ((value > d->m_maximum || value < d->m_minimum) |
| 201 |
&& (d->m_maximum != 0 || d->m_minimum != 0))) |
| 202 |
return; |
| 203 |
d->m_value = value; |
| 204 |
Q_EMIT valueChanged(value); |
| 205 |
repaint(); |
| 206 |
} |
| 207 |
|
| 208 |
void KProgressCircle::paintEvent(QPaintEvent *) |
| 209 |
{ |
| 210 |
Q_D(KProgressCircle); |
| 211 |
QPainter painter(this); |
| 212 |
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
| 213 |
if (painter.pen().width() == 1) |
| 214 |
painter.translate(0.5, 0.5); |
| 215 |
d->calculateCircleRect(); |
| 216 |
d->drawBaseCircle(&painter); |
| 217 |
d->dawColoredCircle(&painter); |
| 218 |
d->drawText(&painter); |
| 219 |
} |
| 220 |
|
| 221 |
KProgressCirclePrivate::KProgressCirclePrivate(KProgressCircle *parent) |
| 222 |
: q_ptr(parent) |
| 223 |
, m_minimum(0) |
| 224 |
, m_maximum(100) |
| 225 |
, m_value(0) |
| 226 |
, m_cirleWidth(6) |
| 227 |
, m_isTextVisiable(true) |
| 228 |
, m_startAngel(90) |
| 229 |
, m_pixWidth(20) |
| 230 |
, m_textWidth(30) |
| 231 |
, m_state(NormalProgress) |
| 232 |
, m_color("#3790FA") |
| 233 |
, m_defaultFormat(true) |
| 234 |
{ |
| 235 |
initDefaultFormat(); |
| 236 |
changeTheme(); |
| 237 |
connect(m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 238 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 239 |
{ |
| 240 |
changeTheme(); |
| 241 |
} |
| 242 |
}); |
| 243 |
} |
| 244 |
|
| 245 |
void KProgressCirclePrivate::initDefaultFormat() |
| 246 |
{ |
| 247 |
Q_Q(KProgressCircle); |
| 248 |
if (m_defaultFormat) { |
| 249 |
QLocale locale = q->locale(); |
| 250 |
m_format = QLatin1String("%p") + locale.percent(); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
void KProgressCirclePrivate::drawBaseCircle(QPainter *painter) |
| 255 |
{ |
| 256 |
Q_Q(KProgressCircle); |
| 257 |
QPen pen; |
| 258 |
if (m_isTextVisiable) { |
| 259 |
if (ThemeController::widgetTheme() == ClassicTheme) { |
| 260 |
if (ThemeController::themeMode() == LightTheme) |
| 261 |
pen.setColor(ThemeController::lanhuHSLToQtHsl(0, 0, 73)); |
| 262 |
else |
| 263 |
pen.setColor(ThemeController::lanhuHSLToQtHsl(0, 0, 35)); |
| 264 |
pen.setWidth(8); |
| 265 |
painter->setPen(pen); |
| 266 |
painter->setBrush(Qt::NoBrush); |
| 267 |
painter->setRenderHint(QPainter::Antialiasing); |
| 268 |
painter->drawEllipse(m_baseRect.adjusted(1, 1, -1, -1)); |
| 269 |
} |
| 270 |
pen.setColor(m_baseColor); |
| 271 |
pen.setWidth(m_cirleWidth); |
| 272 |
painter->setPen(pen); |
| 273 |
painter->setBrush(Qt::NoBrush); |
| 274 |
painter->setRenderHint(QPainter::Antialiasing); |
| 275 |
painter->drawEllipse(m_baseRect.adjusted(1, 1, -1, -1)); |
| 276 |
} else { |
| 277 |
QPen penColor; |
| 278 |
QBrush outBrush, inBrush; |
| 279 |
if (ThemeController::widgetTheme() == ClassicTheme) { |
| 280 |
if (ThemeController::themeMode() == LightTheme) { |
| 281 |
outBrush = QBrush(ThemeController::lanhuHSLToQtHsl(0, 0, 98)); |
| 282 |
inBrush = QBrush(ThemeController::lanhuHSLToQtHsl(0, 0, 98)); |
| 283 |
penColor.setColor(ThemeController::lanhuHSLToQtHsl(0, 0, 73)); |
| 284 |
} else { |
| 285 |
outBrush = QBrush(ThemeController::lanhuHSLToQtHsl(0, 0, 27)); |
| 286 |
inBrush = QBrush(ThemeController::lanhuHSLToQtHsl(0, 0, 27)); |
| 287 |
penColor.setColor(ThemeController::lanhuHSLToQtHsl(0, 0, 35)); |
| 288 |
} |
| 289 |
} else { |
| 290 |
outBrush = m_baseColor; |
| 291 |
inBrush = QBrush(Qt::NoBrush); |
| 292 |
penColor = QPen(Qt::NoPen); |
| 293 |
} |
| 294 |
|
| 295 |
painter->save(); |
| 296 |
painter->setPen(penColor); |
| 297 |
painter->setBrush(outBrush); |
| 298 |
painter->setRenderHint(QPainter::Antialiasing); |
| 299 |
painter->drawEllipse(m_baseRect); |
| 300 |
painter->restore(); |
| 301 |
pen.setColor(m_baseCircleColor); |
| 302 |
pen.setWidth(m_cirleWidth); |
| 303 |
painter->setPen(pen); |
| 304 |
painter->setBrush(inBrush); |
| 305 |
painter->setRenderHint(QPainter::Antialiasing); |
| 306 |
painter->drawEllipse(m_contentRect); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
void KProgressCirclePrivate::dawColoredCircle(QPainter *painter) |
| 311 |
{ |
| 312 |
Q_Q(KProgressCircle); |
| 313 |
QPen pen; |
| 314 |
int angleSpan = m_value * 360 / m_maximum; |
| 315 |
|
| 316 |
if (m_state == ProgressBarState::NormalProgress) { |
| 317 |
m_color = ThemeController::getCustomColorFromDT("kbrand-normal"); |
| 318 |
} |
| 319 |
|
| 320 |
if (m_isTextVisiable) { |
| 321 |
pen.setColor(m_color); |
| 322 |
pen.setWidth(m_cirleWidth); |
| 323 |
if (ThemeController::widgetTheme() == ClassicTheme) |
| 324 |
pen.setCapStyle(Qt::SquareCap); |
| 325 |
else |
| 326 |
pen.setCapStyle(Qt::RoundCap); |
| 327 |
painter->setPen(pen); |
| 328 |
painter->setBrush(Qt::NoBrush); |
| 329 |
painter->setRenderHint(QPainter::Antialiasing); |
| 330 |
if (ThemeController::widgetTheme() == ClassicTheme) |
| 331 |
painter->drawArc(m_baseRect.adjusted(1, 1, -1, -1), m_startAngel * 16, -angleSpan * 16); |
| 332 |
else |
| 333 |
painter->drawArc(m_baseRect, m_startAngel * 16, -angleSpan * 16); |
| 334 |
} else { |
| 335 |
pen.setColor(m_color); |
| 336 |
pen.setWidth(m_cirleWidth); |
| 337 |
if (ThemeController::widgetTheme() == ClassicTheme) |
| 338 |
pen.setCapStyle(Qt::SquareCap); |
| 339 |
else |
| 340 |
pen.setCapStyle(Qt::RoundCap); |
| 341 |
painter->setPen(pen); |
| 342 |
painter->setBrush(Qt::NoBrush); |
| 343 |
painter->setRenderHint(QPainter::Antialiasing); |
| 344 |
painter->drawArc(m_contentRect, m_startAngel * 16, -angleSpan * 16); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
void KProgressCirclePrivate::drawText(QPainter *painter) |
| 349 |
{ |
| 350 |
Q_Q(KProgressCircle); |
| 351 |
QRect pixRect; |
| 352 |
pixRect.setLeft(q->rect().center().x() - m_pixWidth / 2); |
| 353 |
pixRect.setTop(q->rect().center().y() - m_pixWidth / 2); |
| 354 |
pixRect.setRight(q->rect().center().x() + m_pixWidth / 2); |
| 355 |
pixRect.setBottom(q->rect().center().y() + m_pixWidth / 2); |
| 356 |
QRect textRect; |
| 357 |
textRect.setLeft(q->rect().center().x() - m_textWidth / 2); |
| 358 |
textRect.setTop(q->rect().center().y() - m_textWidth / 2); |
| 359 |
textRect.setRight(q->rect().center().x() + m_textWidth / 2); |
| 360 |
textRect.setBottom(q->rect().center().y() + m_textWidth / 2); |
| 361 |
if (m_isTextVisiable) { |
| 362 |
switch (m_state) { |
| 363 |
case NormalProgress: { |
| 364 |
painter->setRenderHint(QPainter::Antialiasing); |
| 365 |
painter->drawText(textRect, Qt::AlignCenter, q->text()); |
| 366 |
} break; |
| 367 |
case SuccessProgress: { |
| 368 |
QColor green = ThemeController::getCustomColorFromDT("ksuccess-normal"); |
| 369 |
QPixmap pixmap = ThemeController::drawColoredPixmap(QIcon::fromTheme("object-select-symbolic").pixmap(m_pixWidth, m_pixWidth), green); |
| 370 |
painter->setRenderHint(QPainter::Antialiasing); |
| 371 |
painter->drawPixmap(pixRect, pixmap); |
| 372 |
break; |
| 373 |
} |
| 374 |
case FailedProgress: { |
| 375 |
QColor red = ThemeController::getCustomColorFromDT("kerror-normal");; |
| 376 |
QPixmap pixmap = ThemeController::drawColoredPixmap(QIcon::fromTheme("window-close-symbolic").pixmap(20, 20), red); |
| 377 |
painter->setRenderHint(QPainter::Antialiasing); |
| 378 |
painter->drawPixmap(pixRect, pixmap); |
| 379 |
break; |
| 380 |
} |
| 381 |
default: |
| 382 |
break; |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
void KProgressCirclePrivate::calculateCircleRect() |
| 388 |
{ |
| 389 |
Q_Q(KProgressCircle); |
| 390 |
|
| 391 |
m_baseRect.setTop(1 + m_cirleWidth / 2); |
| 392 |
m_baseRect.setLeft(1 + m_cirleWidth / 2); |
| 393 |
m_baseRect.setBottom(q->rect().height() - 1 - m_cirleWidth / 2); |
| 394 |
m_baseRect.setRight(q->rect().right() - 1 - m_cirleWidth / 2); |
| 395 |
|
| 396 |
m_contentRect.setTop(1 + m_cirleWidth * 2); |
| 397 |
m_contentRect.setLeft(1 + m_cirleWidth * 2); |
| 398 |
m_contentRect.setBottom(q->rect().height() - 1 - m_cirleWidth * 2); |
| 399 |
m_contentRect.setRight(q->rect().right() - 1 - m_cirleWidth * 2); |
| 400 |
} |
| 401 |
|
| 402 |
void KProgressCirclePrivate::changeTheme() |
| 403 |
{ |
| 404 |
Q_Q(KProgressCircle); |
| 405 |
initThemeStyle(); |
| 406 |
if (ThemeController::themeMode() == LightTheme) { |
| 407 |
m_baseColor = ThemeController::getCustomColorFromDT("kgray-6"); |
| 408 |
m_baseCircleColor = ThemeController::getCustomColorFromDT("kgray-10"); |
| 409 |
} else { |
| 410 |
m_baseColor = ThemeController::getCustomColorFromDT("kgray-16"); |
| 411 |
m_baseCircleColor = ThemeController::getCustomColorFromDT("kgray-17"); |
| 412 |
} |
| 413 |
q->update(); |
| 414 |
} |
| 415 |
|
| 416 |
} |
| 417 |
#include "kprogresscircle.moc" |
| 418 |
#include "moc_kprogresscircle.cpp" |