diff options
Diffstat (limited to 'src/quicktemplates2/qquickslider.cpp')
-rw-r--r-- | src/quicktemplates2/qquickslider.cpp | 84 |
1 files changed, 69 insertions, 15 deletions
diff --git a/src/quicktemplates2/qquickslider.cpp b/src/quicktemplates2/qquickslider.cpp index c0680725..09a3a59c 100644 --- a/src/quicktemplates2/qquickslider.cpp +++ b/src/quicktemplates2/qquickslider.cpp @@ -75,12 +75,20 @@ QT_BEGIN_NAMESPACE \sa {Customizing Slider}, {Input Controls} */ +/*! + \since QtQuick.Controls 2.2 + \qmlsignal QtQuick.Controls::Slider::moved() + + This signal is emitted when the slider has been interactively moved + by the user by using either touch, mouse, wheel, or keys. +*/ + class QQuickSliderPrivate : public QQuickControlPrivate { Q_DECLARE_PUBLIC(QQuickSlider) public: - QQuickSliderPrivate() : from(0), to(1), value(0), position(0), stepSize(0), pressed(false), + QQuickSliderPrivate() : from(0), to(1), value(0), position(0), stepSize(0), live(false), pressed(false), orientation(Qt::Horizontal), snapMode(QQuickSlider::NoSnap), handle(nullptr) { @@ -96,6 +104,7 @@ public: qreal value; qreal position; qreal stepSize; + bool live; bool pressed; QPoint pressPoint; Qt::Orientation orientation; @@ -119,23 +128,25 @@ qreal QQuickSliderPrivate::snapPosition(qreal position) const qreal QQuickSliderPrivate::positionAt(const QPoint &point) const { Q_Q(const QQuickSlider); + qreal pos = 0.0; if (orientation == Qt::Horizontal) { const qreal hw = handle ? handle->width() : 0; const qreal offset = hw / 2; const qreal extent = q->availableWidth() - hw; if (!qFuzzyIsNull(extent)) { if (q->isMirrored()) - return (q->width() - point.x() - q->rightPadding() - offset) / extent; - return (point.x() - q->leftPadding() - offset) / extent; + pos = (q->width() - point.x() - q->rightPadding() - offset) / extent; + else + pos = (point.x() - q->leftPadding() - offset) / extent; } } else { const qreal hh = handle ? handle->height() : 0; const qreal offset = hh / 2; const qreal extent = q->availableHeight() - hh; if (!qFuzzyIsNull(extent)) - return (q->height() - point.y() - q->bottomPadding() - offset) / extent; + pos = (q->height() - point.y() - q->bottomPadding() - offset) / extent; } - return 0; + return qBound<qreal>(0.0, pos, 1.0); } void QQuickSliderPrivate::setPosition(qreal pos) @@ -225,12 +236,12 @@ void QQuickSlider::setTo(qreal to) This property holds the value in the range \c from - \c to. The default value is \c 0.0. - Unlike the \l position property, the \c value is not updated while the - handle is dragged, but only after the value has been chosen and the slider - has been released. The \l valueAt() method can be used to get continuous - updates. + Unlike the \l position property, the \c value is not updated by default + while the handle is dragged, but only after the value has been chosen and + the slider has been released. The \l live property can be used to make the + slider provide live updates for the \c value property. - \sa position, valueAt() + \sa position, live, valueAt() */ qreal QQuickSlider::value() const { @@ -259,9 +270,9 @@ void QQuickSlider::setValue(qreal value) This property holds the logical position of the handle. The position is expressed as a fraction of the control's size, in the range - \c {0.0 - 1.0}. Unlike the \l value property, the \c position is - continuously updated while the handle is dragged. For visualizing a - slider, the right-to-left aware \l visualPosition should be used instead. + \c {0.0 - 1.0}. The \c position is continuously updated while the + handle is dragged. For visualizing a slider, the right-to-left aware + \l visualPosition should be used instead. \sa value, visualPosition, valueAt() */ @@ -356,6 +367,33 @@ void QQuickSlider::setSnapMode(SnapMode mode) } /*! + \since QtQuick.Controls 2.2 + \qmlproperty bool QtQuick.Controls::Slider::live + + This property holds whether the slider provides live updates for the \l value + property while the handle is dragged. + + The default value is \c false. + + \sa value +*/ +bool QQuickSlider::live() const +{ + Q_D(const QQuickSlider); + return d->live; +} + +void QQuickSlider::setLive(bool live) +{ + Q_D(QQuickSlider); + if (d->live == live) + return; + + d->live = live; + emit liveChanged(); +} + +/*! \qmlproperty bool QtQuick.Controls::Slider::pressed This property holds whether the slider is pressed. @@ -479,6 +517,8 @@ void QQuickSlider::keyPressEvent(QKeyEvent *event) { Q_D(QQuickSlider); QQuickControl::keyPressEvent(event); + + const qreal oldValue = d->value; if (d->orientation == Qt::Horizontal) { if (event->key() == Qt::Key_Left) { setPressed(true); @@ -506,6 +546,8 @@ void QQuickSlider::keyPressEvent(QKeyEvent *event) event->accept(); } } + if (!qFuzzyCompare(d->value, oldValue)) + emit moved(); } void QQuickSlider::keyReleaseEvent(QKeyEvent *event) @@ -533,10 +575,16 @@ void QQuickSlider::mouseMoveEvent(QMouseEvent *event) setKeepMouseGrab(QQuickWindowPrivate::dragOverThreshold(event->pos().y() - d->pressPoint.y(), Qt::YAxis, event)); } if (keepMouseGrab()) { + const qreal oldPos = d->position; qreal pos = d->positionAt(event->pos()); if (d->snapMode == SnapAlways) pos = d->snapPosition(pos); - d->setPosition(pos); + if (d->live) + setValue(valueAt(pos)); + else + d->setPosition(pos); + if (!qFuzzyCompare(pos, oldPos)) + emit moved(); } } @@ -545,6 +593,7 @@ void QQuickSlider::mouseReleaseEvent(QMouseEvent *event) Q_D(QQuickSlider); QQuickControl::mouseReleaseEvent(event); d->pressPoint = QPoint(); + const qreal oldPos = d->position; qreal pos = d->positionAt(event->pos()); if (d->snapMode != NoSnap) pos = d->snapPosition(pos); @@ -553,6 +602,8 @@ void QQuickSlider::mouseReleaseEvent(QMouseEvent *event) setValue(val); else if (d->snapMode != NoSnap) d->setPosition(pos); + if (!qFuzzyCompare(pos, oldPos)) + emit moved(); setKeepMouseGrab(false); setPressed(false); } @@ -575,7 +626,10 @@ void QQuickSlider::wheelEvent(QWheelEvent *event) const qreal delta = (qFuzzyIsNull(angle.y()) ? angle.x() : angle.y()) / QWheelEvent::DefaultDeltasPerStep; const qreal step = qFuzzyIsNull(d->stepSize) ? 0.1 : d->stepSize; setValue(oldValue + step * delta); - event->setAccepted(!qFuzzyCompare(d->value, oldValue)); + const bool wasMoved = !qFuzzyCompare(d->value, oldValue); + if (wasMoved) + emit moved(); + event->setAccepted(wasMoved); } } |