1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "progressbar.h" |
6 | |
7 | #include <QPaintEvent> |
8 | #include <QPainter> |
9 | |
10 | class ProgressBarPrivate |
11 | { |
12 | friend class ProgressBar; |
13 | int percentageCache = 0; |
14 | QColor defaultGreen = QColor(0, 0x99, 0); |
15 | QColor brushColor = defaultGreen; |
16 | }; |
17 | |
18 | ProgressBar::ProgressBar(QWidget *parent) |
19 | : QWidget(parent) |
20 | , d(new ProgressBarPrivate) |
21 | { |
22 | setObjectName("ProgressBar" ); |
23 | } |
24 | |
25 | ProgressBar::~ProgressBar() |
26 | { |
27 | if (d) { |
28 | delete d; |
29 | } |
30 | } |
31 | |
32 | void ProgressBar::setPercentage(int percentage) |
33 | { |
34 | d->percentageCache = percentage; |
35 | setToolTip(QString::number(percentage) + "%" ); |
36 | } |
37 | |
38 | void ProgressBar::setColor(const QColor &color) |
39 | { |
40 | d->brushColor = color; |
41 | } |
42 | |
43 | void ProgressBar::paintEvent(QPaintEvent *event) |
44 | { |
45 | Q_UNUSED(event); |
46 | QPainter painter(this); |
47 | painter.setPen(this->palette().color(QPalette::Text)); |
48 | QRect rect = this->rect().adjusted(0, 0, -1, -1); |
49 | auto topLeft = rect.topLeft(); |
50 | auto topRight = rect.topRight(); |
51 | auto bottomLeft = rect.bottomLeft(); |
52 | auto bottomRight = rect.bottomRight(); |
53 | |
54 | painter.drawLine(topLeft, topRight); |
55 | painter.drawLine(topLeft, bottomLeft); |
56 | painter.drawLine(topRight, bottomRight); |
57 | painter.drawLine(bottomLeft, bottomRight); |
58 | |
59 | painter.setPen(Qt::NoPen); |
60 | double rectWidth = rect.width(); |
61 | double drawWidth = rectWidth / 100 * d->percentageCache; |
62 | QRect drawPercRect(1, 1, drawWidth, rect.height() - 2); |
63 | painter.setBrush(d->brushColor); |
64 | painter.drawRect(drawPercRect); |
65 | } |
66 | |