8#include <QtGui/qstylehints.h>
9#include <QtGui/qguiapplication.h>
10#include <QtQuick/private/qquickwindow_p.h>
11#include <QtQuick/private/qquickevents_p_p.h>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
58class QQuickSwitchPrivate :
public QQuickAbstractButtonPrivate
60 Q_DECLARE_PUBLIC(QQuickSwitch)
63 qreal positionAt(
const QPointF &point)
const;
65 bool canDrag(
const QPointF &movePoint)
const;
66 bool handleMove(
const QPointF &point, ulong timestamp) override;
67 bool handleRelease(
const QPointF &point, ulong timestamp) override;
69 QPalette defaultPalette()
const override {
return QQuickTheme::palette(QQuickTheme::Switch); }
74qreal QQuickSwitchPrivate::positionAt(
const QPointF &point)
const
76 Q_Q(
const QQuickSwitch);
79 pos = indicator->mapFromItem(q, point).x() / indicator->width();
85bool QQuickSwitchPrivate::canDrag(
const QPointF &movePoint)
const
90 const qreal pressPos = positionAt(pressPoint);
91 const qreal movePos = positionAt(movePoint);
92 return (pressPos >= 0.0 && pressPos <= 1.0) || (movePos >= 0.0 && movePos <= 1.0);
95bool QQuickSwitchPrivate::handleMove(
const QPointF &point, ulong timestamp)
98 QQuickAbstractButtonPrivate::handleMove(point, timestamp);
99 if (q->keepMouseGrab() || q->keepTouchGrab())
100 q->setPosition(positionAt(point));
104bool QQuickSwitchPrivate::handleRelease(
const QPointF &point, ulong timestamp)
107 QQuickAbstractButtonPrivate::handleRelease(point, timestamp);
108 q->setKeepMouseGrab(
false);
109 q->setKeepTouchGrab(
false);
113QQuickSwitch::QQuickSwitch(QQuickItem *parent)
114 : QQuickAbstractButton(*(
new QQuickSwitchPrivate), parent)
117 d->keepPressed =
true;
122
123
124
125
126
127qreal QQuickSwitch::position()
const
129 Q_D(
const QQuickSwitch);
133void QQuickSwitch::setPosition(qreal position)
136 position = std::clamp(position, qreal(0.0), qreal(1.0));
137 if (qFuzzyCompare(d->position, position))
140 d->position = position;
141 emit positionChanged();
142 emit visualPositionChanged();
146
147
148
149
150
151qreal QQuickSwitch::visualPosition()
const
153 Q_D(
const QQuickSwitch);
155 return 1.0 - d->position;
159void QQuickSwitch::mouseMoveEvent(QMouseEvent *event)
162 if (!keepMouseGrab()) {
163 const QPointF movePoint = event->position();
164 if (d->canDrag(movePoint)) {
165 setKeepMouseGrab(QQuickDeliveryAgentPrivate::dragOverThreshold(movePoint.x() - d->pressPoint.x(),
169 QQuickAbstractButton::mouseMoveEvent(event);
172#if QT_CONFIG(quicktemplates2_multitouch)
173void QQuickSwitch::touchEvent(QTouchEvent *event)
176 if (!keepTouchGrab() && event->type() == QEvent::TouchUpdate) {
177 for (
const QTouchEvent::TouchPoint &point : event->points()) {
178 if (point.id() != d->touchId || point.state() != QEventPoint::Updated)
180 if (d->canDrag(point.position())) {
181 setKeepTouchGrab(QQuickDeliveryAgentPrivate::dragOverThreshold(point.position().x() - d->pressPoint.x(),
186 QQuickAbstractButton::touchEvent(event);
190void QQuickSwitch::mirrorChange()
192 QQuickAbstractButton::mirrorChange();
193 emit visualPositionChanged();
196void QQuickSwitch::nextCheckState()
199 if (keepMouseGrab() || keepTouchGrab()) {
200 d->toggle(d->position > 0.5);
203 setPosition(d->checked ? 1.0 : 0.0);
205 QQuickAbstractButton::nextCheckState();
209void QQuickSwitch::buttonChange(ButtonChange change)
212 if (change == ButtonCheckedChange)
213 setPosition(d->checked ? 1.0 : 0.0);
215 QQuickAbstractButton::buttonChange(change);
218QFont QQuickSwitch::defaultFont()
const
220 return QQuickTheme::font(QQuickTheme::Switch);
225#include "moc_qquickswitch_p.cpp"