diff options
-rw-r--r-- | src/controls/qquickprogressbar.cpp | 111 | ||||
-rw-r--r-- | src/controls/qquickprogressbar_p.h | 14 | ||||
-rw-r--r-- | src/imports/controls/ProgressBar.qml | 2 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_progressbar.qml | 66 |
4 files changed, 179 insertions, 14 deletions
diff --git a/src/controls/qquickprogressbar.cpp b/src/controls/qquickprogressbar.cpp index c02e2c4b..9b77f949 100644 --- a/src/controls/qquickprogressbar.cpp +++ b/src/controls/qquickprogressbar.cpp @@ -68,8 +68,10 @@ QT_BEGIN_NAMESPACE class QQuickProgressBarPrivate : public QQuickControlPrivate { public: - QQuickProgressBarPrivate() : value(0), indeterminate(false), indicator(Q_NULLPTR) { } + QQuickProgressBarPrivate() : from(0), to(1.0), value(0), indeterminate(false), indicator(Q_NULLPTR) { } + qreal from; + qreal to; qreal value; bool indeterminate; QQuickItem *indicator; @@ -81,11 +83,63 @@ QQuickProgressBar::QQuickProgressBar(QQuickItem *parent) : } /*! + \qmlproperty real QtQuickControls2::ProgressBar::from + + This property holds the starting value for the progress. The default value is \c 0.0. + + \sa to, value +*/ +qreal QQuickProgressBar::from() const +{ + Q_D(const QQuickProgressBar); + return d->from; +} + +void QQuickProgressBar::setFrom(qreal from) +{ + Q_D(QQuickProgressBar); + if (!qFuzzyCompare(d->from, from)) { + d->from = from; + emit fromChanged(); + emit positionChanged(); + emit visualPositionChanged(); + if (isComponentComplete()) + setValue(d->value); + } +} + +/*! + \qmlproperty real QtQuickControls2::ProgressBar::to + + This property holds the end value for the progress. The default value is \c 1.0. + + \sa from, value +*/ +qreal QQuickProgressBar::to() const +{ + Q_D(const QQuickProgressBar); + return d->to; +} + +void QQuickProgressBar::setTo(qreal to) +{ + Q_D(QQuickProgressBar); + if (!qFuzzyCompare(d->to, to)) { + d->to = to; + emit toChanged(); + emit positionChanged(); + emit visualPositionChanged(); + if (isComponentComplete()) + setValue(d->value); + } +} + +/*! \qmlproperty real QtQuickControls2::ProgressBar::value - This property holds the value in the range \c 0.0 - \c 1.0. The default value is \c 0.0. + This property holds the progress value. The default value is \c 0.0. - \sa visualPosition + \sa from, to, position */ qreal QQuickProgressBar::value() const { @@ -96,33 +150,56 @@ qreal QQuickProgressBar::value() const void QQuickProgressBar::setValue(qreal value) { Q_D(QQuickProgressBar); - value = qBound(0.0, value, 1.0); + if (isComponentComplete()) + value = d->from > d->to ? qBound(d->to, value, d->from) : qBound(d->from, value, d->to); + if (!qFuzzyCompare(d->value, value)) { d->value = value; emit valueChanged(); + emit positionChanged(); emit visualPositionChanged(); } } /*! + \qmlproperty real QtQuickControls2::ProgressBar::position + \readonly + + This property holds the logical position of the progress. + + The position is defined as a percentage of the value, scaled to + \c {0.0 - 1.0}. For visualizing the progress, the right-to-left + aware \l visualPosition should be used instead. + + \sa value, visualPosition +*/ +qreal QQuickProgressBar::position() const +{ + Q_D(const QQuickProgressBar); + if (qFuzzyCompare(d->from, d->to)) + return 0; + return (d->value - d->from) / (d->to - d->from); +} + +/*! \qmlproperty real QtQuickControls2::ProgressBar::visualPosition \readonly This property holds the visual position of the progress. - The position is defined as a percentage of the control's size, scaled to - \c 0.0 - \c 1.0. When the control is \l mirrored, the value is equal to - \c {1.0 - value}. This makes the value suitable for visualizing the - progress, taking right-to-left support into account. + The position is defined as a percentage of the value, scaled to + \c {0.0 - 1.0}. When the control is \l mirrored, \c visuaPosition + is equal to \c {1.0 - position}. This makes \c visualPosition + suitable for visualizing the progress, taking right-to-left + support into account. - \sa value + \sa position, value */ qreal QQuickProgressBar::visualPosition() const { - Q_D(const QQuickProgressBar); if (isMirrored()) - return 1.0 - d->value; - return d->value; + return 1.0 - position(); + return position(); } /*! @@ -173,7 +250,15 @@ void QQuickProgressBar::setIndicator(QQuickItem *indicator) void QQuickProgressBar::mirrorChange() { QQuickControl::mirrorChange(); - emit visualPositionChanged(); + if (!qFuzzyCompare(position(), 0.5)) + emit visualPositionChanged(); +} + +void QQuickProgressBar::componentComplete() +{ + Q_D(QQuickProgressBar); + QQuickControl::componentComplete(); + setValue(d->value); } QT_END_NAMESPACE diff --git a/src/controls/qquickprogressbar_p.h b/src/controls/qquickprogressbar_p.h index 263e3186..cf87e2cf 100644 --- a/src/controls/qquickprogressbar_p.h +++ b/src/controls/qquickprogressbar_p.h @@ -57,7 +57,10 @@ class QQuickProgressBarPrivate; class Q_QUICKCONTROLS_EXPORT QQuickProgressBar : public QQuickControl { Q_OBJECT + Q_PROPERTY(qreal from READ from WRITE setFrom NOTIFY fromChanged FINAL) + Q_PROPERTY(qreal to READ to WRITE setTo NOTIFY toChanged FINAL) Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged FINAL) + Q_PROPERTY(qreal position READ position NOTIFY positionChanged FINAL) Q_PROPERTY(qreal visualPosition READ visualPosition NOTIFY visualPositionChanged FINAL) Q_PROPERTY(bool indeterminate READ isIndeterminate WRITE setIndeterminate NOTIFY indeterminateChanged FINAL) Q_PROPERTY(QQuickItem *indicator READ indicator WRITE setIndicator NOTIFY indicatorChanged FINAL) @@ -65,9 +68,16 @@ class Q_QUICKCONTROLS_EXPORT QQuickProgressBar : public QQuickControl public: explicit QQuickProgressBar(QQuickItem *parent = Q_NULLPTR); + qreal from() const; + void setFrom(qreal from); + + qreal to() const; + void setTo(qreal to); + qreal value() const; void setValue(qreal value); + qreal position() const; qreal visualPosition() const; bool isIndeterminate() const; @@ -77,13 +87,17 @@ public: void setIndicator(QQuickItem *indicator); Q_SIGNALS: + void fromChanged(); + void toChanged(); void valueChanged(); + void positionChanged(); void visualPositionChanged(); void indeterminateChanged(); void indicatorChanged(); protected: void mirrorChange() Q_DECL_OVERRIDE; + void componentComplete() Q_DECL_OVERRIDE; private: Q_DISABLE_COPY(QQuickProgressBar) diff --git a/src/imports/controls/ProgressBar.qml b/src/imports/controls/ProgressBar.qml index e71e2de8..6a6dfc11 100644 --- a/src/imports/controls/ProgressBar.qml +++ b/src/imports/controls/ProgressBar.qml @@ -62,7 +62,7 @@ AbstractProgressBar { model: indeterminate ? 2 : 1 Rectangle { - property real offset: indeterminate ? 0 : control.value + property real offset: indeterminate ? 0 : control.position x: 2 + (indeterminate ? offset * parent.width - 4 : 0) y: (parent.height - height) / 2 diff --git a/tests/auto/controls/data/tst_progressbar.qml b/tests/auto/controls/data/tst_progressbar.qml index a542fd46..7958c96c 100644 --- a/tests/auto/controls/data/tst_progressbar.qml +++ b/tests/auto/controls/data/tst_progressbar.qml @@ -75,6 +75,72 @@ TestCase { control.destroy() } + function test_range() { + var control = progressBar.createObject(testCase, {from: 0, to: 100, value: 50}) + compare(control.from, 0) + compare(control.to, 100) + compare(control.value, 50) + compare(control.position, 0.5) + + control.value = 1000 + compare(control.value, 100) + compare(control.position, 1) + + control.value = -1 + compare(control.value, 0) + compare(control.position, 0) + + control.from = 25 + compare(control.from, 25) + compare(control.value, 25) + compare(control.position, 0) + + control.to = 75 + compare(control.to, 75) + compare(control.value, 25) + compare(control.position, 0) + + control.value = 50 + compare(control.value, 50) + compare(control.position, 0.5) + + control.destroy() + } + + function test_inverted() { + var control = progressBar.createObject(testCase, {from: 1.0, to: -1.0}) + compare(control.from, 1.0) + compare(control.to, -1.0) + compare(control.value, 0.0) + compare(control.position, 0.5) + + control.value = 2.0 + compare(control.value, 1.0) + compare(control.position, 0.0) + + control.value = -2.0 + compare(control.value, -1.0) + compare(control.position, 1.0) + + control.value = 0.0 + compare(control.value, 0.0) + compare(control.position, 0.5) + + control.destroy() + } + + function test_position() { + var control = progressBar.createObject(testCase, {value: 0.25}) + compare(control.value, 0.25) + compare(control.position, 0.25) + + control.value = 0.75 + compare(control.value, 0.75) + compare(control.position, 0.75) + + control.destroy() + } + function test_visualPosition() { var control = progressBar.createObject(testCase, {value: 0.25}) compare(control.value, 0.25) |