diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-09-19 16:39:41 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-09-19 16:37:11 +0000 |
commit | b3983692680a91023dc5f22608b4e84b46fae883 (patch) | |
tree | 6f25d5f3f84deabab3aacefd03fef020a196af96 /src | |
parent | 6ed4918f131ff235bcab3efe849f5b67406ef350 (diff) |
QQuickSwitchDelegate: make the handle draggable
Sync the implementation with QQuickSwitch to make it behave exactly
the same way.
Change-Id: I59d08f68f87d8776e4012da880ac57a99950dfe8
Task-number: QTBUG-55686
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/quicktemplates2/qquickswitchdelegate.cpp | 57 | ||||
-rw-r--r-- | src/quicktemplates2/qquickswitchdelegate_p.h | 7 |
2 files changed, 52 insertions, 12 deletions
diff --git a/src/quicktemplates2/qquickswitchdelegate.cpp b/src/quicktemplates2/qquickswitchdelegate.cpp index fbdae418..e22d163a 100644 --- a/src/quicktemplates2/qquickswitchdelegate.cpp +++ b/src/quicktemplates2/qquickswitchdelegate.cpp @@ -80,22 +80,17 @@ public: { } - void updatePosition(); - qreal positionAt(const QPoint &point) const; + qreal positionAt(const QPointF &point) const; qreal position; }; -void QQuickSwitchDelegatePrivate::updatePosition() -{ - Q_Q(QQuickSwitchDelegate); - q->setPosition(checked ? 1.0 : 0.0); -} - -qreal QQuickSwitchDelegatePrivate::positionAt(const QPoint &point) const +qreal QQuickSwitchDelegatePrivate::positionAt(const QPointF &point) const { Q_Q(const QQuickSwitchDelegate); - qreal pos = point.x() / indicator->width(); + qreal pos = 0.0; + if (indicator) + pos = indicator->mapFromItem(q, point).x() / indicator->width(); if (q->isMirrored()) return 1.0 - pos; return pos; @@ -104,9 +99,9 @@ qreal QQuickSwitchDelegatePrivate::positionAt(const QPoint &point) const QQuickSwitchDelegate::QQuickSwitchDelegate(QQuickItem *parent) : QQuickItemDelegate(*(new QQuickSwitchDelegatePrivate), parent) { + Q_D(QQuickSwitchDelegate); + d->keepPressed = true; setCheckable(true); - - QObjectPrivate::connect(this, &QQuickAbstractButton::checkedChanged, d_func(), &QQuickSwitchDelegatePrivate::updatePosition); } /*! @@ -147,6 +142,29 @@ qreal QQuickSwitchDelegate::visualPosition() const return d->position; } +void QQuickSwitchDelegate::mousePressEvent(QMouseEvent *event) +{ + QQuickItemDelegate::mousePressEvent(event); +} + +void QQuickSwitchDelegate::mouseMoveEvent(QMouseEvent *event) +{ + Q_D(QQuickSwitchDelegate); + QQuickItemDelegate::mouseMoveEvent(event); + + const QPointF movePoint = event->localPos(); + if (!keepMouseGrab()) + setKeepMouseGrab(QQuickWindowPrivate::dragOverThreshold(movePoint.x() - d->pressPoint.x(), Qt::XAxis, event)); + if (keepMouseGrab()) + setPosition(d->positionAt(movePoint)); +} + +void QQuickSwitchDelegate::mouseReleaseEvent(QMouseEvent *event) +{ + QQuickItemDelegate::mouseReleaseEvent(event); + setKeepMouseGrab(false); +} + QFont QQuickSwitchDelegate::defaultFont() const { return QQuickControlPrivate::themeFont(QPlatformTheme::ListViewFont); @@ -158,4 +176,19 @@ void QQuickSwitchDelegate::mirrorChange() emit visualPositionChanged(); } +void QQuickSwitchDelegate::nextCheckState() +{ + Q_D(QQuickSwitchDelegate); + if (keepMouseGrab()) + setChecked(d->position > 0.5); + else + QQuickItemDelegate::nextCheckState(); +} + +void QQuickSwitchDelegate::checkStateSet() +{ + Q_D(QQuickSwitchDelegate); + setPosition(d->checked ? 1.0 : 0.0); +} + QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquickswitchdelegate_p.h b/src/quicktemplates2/qquickswitchdelegate_p.h index 5126f643..c0cc21ac 100644 --- a/src/quicktemplates2/qquickswitchdelegate_p.h +++ b/src/quicktemplates2/qquickswitchdelegate_p.h @@ -73,9 +73,16 @@ Q_SIGNALS: void visualPositionChanged(); protected: + void mousePressEvent(QMouseEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + QFont defaultFont() const override; void mirrorChange() override; + void nextCheckState() override; + void checkStateSet() override; + private: Q_DISABLE_COPY(QQuickSwitchDelegate) Q_DECLARE_PRIVATE(QQuickSwitchDelegate) |