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
57class QQuickSwitchPrivate :
public QQuickAbstractButtonPrivate
59 Q_DECLARE_PUBLIC(QQuickSwitch)
62 qreal positionAt(
const QPointF &point)
const;
64 bool canDrag(
const QPointF &movePoint)
const;
65 bool handleMove(
const QPointF &point, ulong timestamp) override;
66 bool handleRelease(
const QPointF &point, ulong timestamp) override;
68 QPalette defaultPalette()
const override {
return QQuickTheme::palette(QQuickTheme::Switch); }
73qreal QQuickSwitchPrivate::positionAt(
const QPointF &point)
const
75 Q_Q(
const QQuickSwitch);
78 pos = indicator->mapFromItem(q, point).x() / indicator->width();
84bool QQuickSwitchPrivate::canDrag(
const QPointF &movePoint)
const
89 const qreal pressPos = positionAt(pressPoint);
90 const qreal movePos = positionAt(movePoint);
91 return (pressPos >= 0.0 && pressPos <= 1.0) || (movePos >= 0.0 && movePos <= 1.0);
94bool QQuickSwitchPrivate::handleMove(
const QPointF &point, ulong timestamp)
97 QQuickAbstractButtonPrivate::handleMove(point, timestamp);
98 if (q->keepMouseGrab() || q->keepTouchGrab())
99 q->setPosition(positionAt(point));
103bool QQuickSwitchPrivate::handleRelease(
const QPointF &point, ulong timestamp)
106 QQuickAbstractButtonPrivate::handleRelease(point, timestamp);
107 q->setKeepMouseGrab(
false);
108 q->setKeepTouchGrab(
false);
112QQuickSwitch::QQuickSwitch(QQuickItem *parent)
113 : QQuickAbstractButton(*(
new QQuickSwitchPrivate), parent)
116 d->keepPressed =
true;
121
122
123
124
125
126qreal QQuickSwitch::position()
const
128 Q_D(
const QQuickSwitch);
132void QQuickSwitch::setPosition(qreal position)
135 position = std::clamp(position, qreal(0.0), qreal(1.0));
136 if (qFuzzyCompare(d->position, position))
139 d->position = position;
140 emit positionChanged();
141 emit visualPositionChanged();
145
146
147
148
149
150qreal QQuickSwitch::visualPosition()
const
152 Q_D(
const QQuickSwitch);
154 return 1.0 - d->position;
158void QQuickSwitch::mouseMoveEvent(QMouseEvent *event)
161 if (!keepMouseGrab()) {
162 const QPointF movePoint = event->position();
163 if (d->canDrag(movePoint)) {
164 setKeepMouseGrab(QQuickDeliveryAgentPrivate::dragOverThreshold(movePoint.x() - d->pressPoint.x(),
168 QQuickAbstractButton::mouseMoveEvent(event);
171#if QT_CONFIG(quicktemplates2_multitouch)
172void QQuickSwitch::touchEvent(QTouchEvent *event)
175 if (!keepTouchGrab() && event->type() == QEvent::TouchUpdate) {
176 for (
const QTouchEvent::TouchPoint &point : event->points()) {
177 if (point.id() != d->touchId || point.state() != QEventPoint::Updated)
179 if (d->canDrag(point.position())) {
180 setKeepTouchGrab(QQuickDeliveryAgentPrivate::dragOverThreshold(point.position().x() - d->pressPoint.x(),
185 QQuickAbstractButton::touchEvent(event);
189void QQuickSwitch::mirrorChange()
191 QQuickAbstractButton::mirrorChange();
192 emit visualPositionChanged();
195void QQuickSwitch::nextCheckState()
198 if (keepMouseGrab() || keepTouchGrab()) {
199 d->toggle(d->position > 0.5);
202 setPosition(d->checked ? 1.0 : 0.0);
204 QQuickAbstractButton::nextCheckState();
208void QQuickSwitch::buttonChange(ButtonChange change)
211 if (change == ButtonCheckedChange)
212 setPosition(d->checked ? 1.0 : 0.0);
214 QQuickAbstractButton::buttonChange(change);
217QFont QQuickSwitch::defaultFont()
const
219 return QQuickTheme::font(QQuickTheme::Switch);
224#include "moc_qquickswitch_p.cpp"