1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Extras module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qquickcircularprogressbar_p.h"
41
42#include <QtGui/QPen>
43#include <QtGui/QPainter>
44#include <QtMath>
45
46QQuickCircularProgressBar::QQuickCircularProgressBar(QQuickItem *parent) :
47 QQuickPaintedItem(parent),
48 mProgress(0),
49 mBarWidth(1),
50 mInset(0),
51 mBackgroundColor(QColor(Qt::transparent)),
52 mMinimumValueAngle(-90 * 16),
53 mMaximumValueAngle(-450 * 16)
54{
55 setFlag(flag: ItemHasContents, enabled: true);
56}
57
58QQuickCircularProgressBar::~QQuickCircularProgressBar()
59{
60}
61
62void QQuickCircularProgressBar::paint(QPainter *painter)
63{
64 QPen pen(Qt::red);
65 pen.setWidthF(mBarWidth);
66
67 const QRectF bounds = boundingRect();
68 const qreal smallest = qMin(a: bounds.width(), b: bounds.height());
69 QRectF rect = QRectF(pen.widthF() / 2.0, pen.widthF() / 2.0, smallest - pen.widthF(), smallest - pen.widthF());
70 rect.adjust(xp1: mInset, yp1: mInset, xp2: -mInset, yp2: -mInset);
71
72 // Make sure the arc is aligned to whole pixels.
73 if (rect.x() - int(rect.x()) > 0) {
74 rect.setX(qCeil(v: rect.x()));
75 }
76 if (rect.y() - int(rect.y()) > 0) {
77 rect.setY(qCeil(v: rect.y()));
78 }
79 if (rect.width() - int(rect.width()) > 0) {
80 rect.setWidth(qFloor(v: rect.width()));
81 }
82 if (rect.height() - int(rect.height()) > 0) {
83 rect.setHeight(qFloor(v: rect.height()));
84 }
85
86 painter->setRenderHint(hint: QPainter::Antialiasing);
87
88 // QPainter::drawArc uses positive values for counter clockwise - the opposite of our API -
89 // so we must reverse the angles with * -1. Also, our angle origin is at 12 o'clock, whereas
90 // QPainter's is 3 o'clock, hence - 90.
91 const qreal startAngle = ((mMinimumValueAngle - 90) * -1) * 16;
92 if (mBackgroundColor != Qt::transparent) {
93 QBrush bgBrush(mBackgroundColor);
94 QPen bgPen;
95 bgPen.setWidthF(mBarWidth);
96 bgPen.setBrush(bgBrush);
97 painter->setPen(bgPen);
98 painter->drawArc(rect, a: startAngle, alen: (((mMaximumValueAngle - 90) * -1 * 16) - startAngle));
99 }
100
101 QLinearGradient gradient;
102 gradient.setStart(x: bounds.width() / 2, y: mInset);
103 gradient.setFinalStop(x: bounds.width() / 2, y: bounds.height() - mInset);
104 gradient.setStops(mGradientStops);
105
106 QBrush brush(gradient);
107 pen.setBrush(brush);
108
109 painter->setPen(pen);
110
111 const qreal spanAngle = progress() * (((mMaximumValueAngle - 90) * -1 * 16) - startAngle);
112 painter->drawArc(rect, a: startAngle, alen: spanAngle);
113}
114
115qreal QQuickCircularProgressBar::progress() const
116{
117 return mProgress;
118}
119
120void QQuickCircularProgressBar::setProgress(qreal progress)
121{
122 if (mProgress != progress) {
123 mProgress = progress;
124 emit progressChanged(progress: mProgress);
125 update();
126 }
127}
128
129qreal QQuickCircularProgressBar::barWidth() const
130{
131 return mBarWidth;
132}
133
134void QQuickCircularProgressBar::setBarWidth(qreal barWidth)
135{
136 if (mBarWidth != barWidth) {
137 mBarWidth = barWidth;
138 emit barWidthChanged(barWidth: mBarWidth);
139 update();
140 }
141}
142
143qreal QQuickCircularProgressBar::inset() const
144{
145 return mInset;
146}
147
148void QQuickCircularProgressBar::setInset(qreal inset)
149{
150 if (mInset != inset) {
151 mInset = inset;
152 emit insetChanged(inset: mInset);
153 update();
154 }
155}
156
157qreal QQuickCircularProgressBar::minimumValueAngle() const
158{
159 return mMinimumValueAngle;
160}
161
162void QQuickCircularProgressBar::setMinimumValueAngle(qreal minimumValueAngle)
163{
164 if (mMinimumValueAngle != minimumValueAngle) {
165 mMinimumValueAngle = minimumValueAngle;
166 emit minimumValueAngleChanged(minimumValueAngle: mMinimumValueAngle);
167 update();
168 }
169}
170
171qreal QQuickCircularProgressBar::maximumValueAngle() const
172{
173 return mMinimumValueAngle;
174}
175
176void QQuickCircularProgressBar::setMaximumValueAngle(qreal maximumValueAngle)
177{
178 if (mMaximumValueAngle != maximumValueAngle) {
179 mMaximumValueAngle = maximumValueAngle;
180 emit maximumValueAngleChanged(maximumValueAngle: mMaximumValueAngle);
181 update();
182 }
183}
184
185void QQuickCircularProgressBar::clearStops()
186{
187 mGradientStops.clear();
188}
189
190void QQuickCircularProgressBar::addStop(qreal position, const QColor &color)
191{
192 mGradientStops.append(t: QGradientStop(position, color));
193}
194
195void QQuickCircularProgressBar::redraw()
196{
197 update();
198}
199
200QColor QQuickCircularProgressBar::backgroundColor() const
201{
202 return mBackgroundColor;
203}
204
205void QQuickCircularProgressBar::setBackgroundColor(const QColor &backgroundColor)
206{
207 if (mBackgroundColor != backgroundColor) {
208 mBackgroundColor = backgroundColor;
209 emit backgroundColorChanged(backgroundColor);
210 update();
211 }
212}
213

source code of qtquickcontrols/src/extras/Private/qquickcircularprogressbar.cpp