1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickprogressbar_p.h"
5#include "qquickcontrol_p_p.h"
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \qmltype ProgressBar
11 \inherits Control
12//! \nativetype QQuickProgressBar
13 \inqmlmodule QtQuick.Controls
14 \since 5.7
15 \ingroup qtquickcontrols-indicators
16 \brief Indicates the progress of an operation.
17
18 \image qtquickcontrols-progressbar.gif
19
20 ProgressBar indicates the progress of an operation. The value should be updated
21 regularly. The range is defined by \l from and \l to, which both can contain any value.
22
23 \code
24 ProgressBar {
25 value: 0.5
26 }
27 \endcode
28
29 ProgressBar also supports a special \l indeterminate mode, which is useful,
30 for example, when unable to determine the size of the item being downloaded,
31 or if the download progress gets interrupted due to a network disconnection.
32
33 \image qtquickcontrols-progressbar-indeterminate.gif
34
35 \code
36 ProgressBar {
37 indeterminate: true
38 }
39 \endcode
40
41 The indeterminate mode is similar to a \l BusyIndicator. Both can be used
42 to indicate background activity. The main difference is visual, and that
43 ProgressBar can also present a concrete amount of progress (when it can be
44 determined). Due to the visual difference, indeterminate progress bars and
45 busy indicators fit different places in user interfaces. Typical places for
46 an indeterminate progress bar:
47 \list
48 \li at the bottom of a \l ToolBar
49 \li inline within the content of a \l Page
50 \li in an \l ItemDelegate to show the progress of a particular item
51 \endlist
52
53 \sa {Customizing ProgressBar}, BusyIndicator, {Indicator Controls}
54*/
55
56class QQuickProgressBarPrivate : public QQuickControlPrivate
57{
58public:
59 qreal from = 0;
60 qreal to = 1;
61 qreal value = 0;
62 bool indeterminate = false;
63};
64
65QQuickProgressBar::QQuickProgressBar(QQuickItem *parent)
66 : QQuickControl(*(new QQuickProgressBarPrivate), parent)
67{
68 Q_D(QQuickProgressBar);
69 d->setSizePolicy(horizontalPolicy: QLayoutPolicy::Expanding, verticalPolicy: QLayoutPolicy::Fixed);
70}
71
72/*!
73 \qmlproperty real QtQuick.Controls::ProgressBar::from
74
75 This property holds the starting value for the progress. The default value is \c 0.0.
76
77 \sa to, value
78*/
79qreal QQuickProgressBar::from() const
80{
81 Q_D(const QQuickProgressBar);
82 return d->from;
83}
84
85void QQuickProgressBar::setFrom(qreal from)
86{
87 Q_D(QQuickProgressBar);
88 if (qFuzzyCompare(p1: d->from, p2: from))
89 return;
90
91 d->from = from;
92 emit fromChanged();
93 emit positionChanged();
94 emit visualPositionChanged();
95 if (isComponentComplete())
96 setValue(d->value);
97}
98
99/*!
100 \qmlproperty real QtQuick.Controls::ProgressBar::to
101
102 This property holds the end value for the progress. The default value is \c 1.0.
103
104 \sa from, value
105*/
106qreal QQuickProgressBar::to() const
107{
108 Q_D(const QQuickProgressBar);
109 return d->to;
110}
111
112void QQuickProgressBar::setTo(qreal to)
113{
114 Q_D(QQuickProgressBar);
115 if (qFuzzyCompare(p1: d->to, p2: to))
116 return;
117
118 d->to = to;
119 emit toChanged();
120 emit positionChanged();
121 emit visualPositionChanged();
122 if (isComponentComplete())
123 setValue(d->value);
124}
125
126/*!
127 \qmlproperty real QtQuick.Controls::ProgressBar::value
128
129 This property holds the progress value. The default value is \c 0.0.
130
131 \sa from, to, position
132*/
133qreal QQuickProgressBar::value() const
134{
135 Q_D(const QQuickProgressBar);
136 return d->value;
137}
138
139void QQuickProgressBar::setValue(qreal value)
140{
141 Q_D(QQuickProgressBar);
142 if (isComponentComplete())
143 value = d->from > d->to ? qBound(min: d->to, val: value, max: d->from) : qBound(min: d->from, val: value, max: d->to);
144
145 if (qFuzzyCompare(p1: d->value, p2: value))
146 return;
147
148 d->value = value;
149 emit valueChanged();
150 emit positionChanged();
151 emit visualPositionChanged();
152}
153
154/*!
155 \qmlproperty real QtQuick.Controls::ProgressBar::position
156 \readonly
157
158 This property holds the logical position of the progress.
159
160 The position is expressed as a fraction of the value, in the range
161 \c {0.0 - 1.0}. For visualizing the progress, the right-to-left
162 aware \l visualPosition should be used instead.
163
164 \sa value, visualPosition
165*/
166qreal QQuickProgressBar::position() const
167{
168 Q_D(const QQuickProgressBar);
169 if (qFuzzyCompare(p1: d->from, p2: d->to))
170 return 0;
171 return (d->value - d->from) / (d->to - d->from);
172}
173
174/*!
175 \qmlproperty real QtQuick.Controls::ProgressBar::visualPosition
176 \readonly
177
178 This property holds the visual position of the progress.
179
180 The position is expressed as a fraction of the value, in the range \c {0.0 - 1.0}.
181 When the control is \l {Control::mirrored}{mirrored}, \c visuaPosition is equal
182 to \c {1.0 - position}. This makes \c visualPosition suitable for visualizing
183 the progress, taking right-to-left support into account.
184
185 \sa position, value
186*/
187qreal QQuickProgressBar::visualPosition() const
188{
189 if (isMirrored())
190 return 1.0 - position();
191 return position();
192}
193
194/*!
195 \qmlproperty bool QtQuick.Controls::ProgressBar::indeterminate
196
197 This property holds whether the progress bar is in indeterminate mode.
198 A progress bar in indeterminate mode displays that an operation is in progress, but it
199 doesn't show how much progress has been made.
200
201 \image qtquickcontrols-progressbar-indeterminate.gif
202*/
203bool QQuickProgressBar::isIndeterminate() const
204{
205 Q_D(const QQuickProgressBar);
206 return d->indeterminate;
207}
208
209void QQuickProgressBar::setIndeterminate(bool indeterminate)
210{
211 Q_D(QQuickProgressBar);
212 if (d->indeterminate == indeterminate)
213 return;
214
215 d->indeterminate = indeterminate;
216 emit indeterminateChanged();
217}
218
219void QQuickProgressBar::mirrorChange()
220{
221 QQuickControl::mirrorChange();
222 if (!qFuzzyCompare(p1: position(), p2: qreal(0.5)))
223 emit visualPositionChanged();
224}
225
226void QQuickProgressBar::componentComplete()
227{
228 Q_D(QQuickProgressBar);
229 QQuickControl::componentComplete();
230 setValue(d->value);
231}
232
233#if QT_CONFIG(accessibility)
234QAccessible::Role QQuickProgressBar::accessibleRole() const
235{
236 return QAccessible::ProgressBar;
237}
238#endif
239
240QT_END_NAMESPACE
241
242#include "moc_qquickprogressbar_p.cpp"
243

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtdeclarative/src/quicktemplates/qquickprogressbar.cpp