14
15
16
17
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
48class QQuickSwitchDelegatePrivate :
public QQuickItemDelegatePrivate
50 Q_DECLARE_PUBLIC(QQuickSwitchDelegate)
53 qreal positionAt(
const QPointF &point)
const;
55 bool canDrag(
const QPointF &movePoint)
const;
56 bool handleMove(
const QPointF &point, ulong timestamp) override;
57 bool handleRelease(
const QPointF &point, ulong timestamp) override;
59 QPalette defaultPalette()
const override {
return QQuickTheme::palette(QQuickTheme::ListView); }
64qreal QQuickSwitchDelegatePrivate::positionAt(
const QPointF &point)
const
66 Q_Q(
const QQuickSwitchDelegate);
69 pos = indicator->mapFromItem(q, point).x() / indicator->width();
75bool QQuickSwitchDelegatePrivate::canDrag(
const QPointF &movePoint)
const
80 const qreal pressPos = positionAt(pressPoint);
81 const qreal movePos = positionAt(movePoint);
82 return (pressPos >= 0.0 && pressPos <= 1.0) || (movePos >= 0.0 && movePos <= 1.0);
85bool QQuickSwitchDelegatePrivate::handleMove(
const QPointF &point, ulong timestamp)
87 Q_Q(QQuickSwitchDelegate);
88 QQuickItemDelegatePrivate::handleMove(point, timestamp);
89 if (q->keepMouseGrab() || q->keepTouchGrab())
90 q->setPosition(positionAt(point));
94bool QQuickSwitchDelegatePrivate::handleRelease(
const QPointF &point, ulong timestamp)
96 Q_Q(QQuickSwitchDelegate);
97 QQuickItemDelegatePrivate::handleRelease(point, timestamp);
98 q->setKeepMouseGrab(
false);
99 q->setKeepTouchGrab(
false);
103QQuickSwitchDelegate::QQuickSwitchDelegate(QQuickItem *parent)
104 : QQuickItemDelegate(*(
new QQuickSwitchDelegatePrivate), parent)
106 Q_D(QQuickSwitchDelegate);
107 d->keepPressed =
true;
112
113
114
115
116
117qreal QQuickSwitchDelegate::position()
const
119 Q_D(
const QQuickSwitchDelegate);
123void QQuickSwitchDelegate::setPosition(qreal position)
125 Q_D(QQuickSwitchDelegate);
126 position = std::clamp(position, qreal(0.0), qreal(1.0));
127 if (qFuzzyCompare(d->position, position))
130 d->position = position;
131 emit positionChanged();
132 emit visualPositionChanged();
136
137
138
139
140
141qreal QQuickSwitchDelegate::visualPosition()
const
143 Q_D(
const QQuickSwitchDelegate);
145 return 1.0 - d->position;
149void QQuickSwitchDelegate::mouseMoveEvent(QMouseEvent *event)
151 Q_D(QQuickSwitchDelegate);
152 if (!keepMouseGrab()) {
153 const QPointF movePoint = event->position();
154 if (d->canDrag(movePoint)) {
155 setKeepMouseGrab(QQuickDeliveryAgentPrivate::dragOverThreshold(movePoint.x() - d->pressPoint.x(),
159 QQuickItemDelegate::mouseMoveEvent(event);
162#if QT_CONFIG(quicktemplates2_multitouch)
163void QQuickSwitchDelegate::touchEvent(QTouchEvent *event)
165 Q_D(QQuickSwitchDelegate);
166 if (!keepTouchGrab() && event->type() == QEvent::TouchUpdate) {
167 for (
const QTouchEvent::TouchPoint &point : event->points()) {
168 if (point.id() != d->touchId || point.state() != QEventPoint::Updated)
170 if (d->canDrag(point.position())) {
171 setKeepTouchGrab(QQuickDeliveryAgentPrivate::dragOverThreshold(point.position().x() - d->pressPoint.x(),
176 QQuickItemDelegate::touchEvent(event);
180QFont QQuickSwitchDelegate::defaultFont()
const
182 return QQuickTheme::font(QQuickTheme::ListView);
185void QQuickSwitchDelegate::mirrorChange()
187 QQuickItemDelegate::mirrorChange();
188 emit visualPositionChanged();
191void QQuickSwitchDelegate::nextCheckState()
193 Q_D(QQuickSwitchDelegate);
194 if (keepMouseGrab() || keepTouchGrab()) {
195 d->toggle(d->position > 0.5);
198 setPosition(d->checked ? 1.0 : 0.0);
200 QQuickItemDelegate::nextCheckState();
204void QQuickSwitchDelegate::buttonChange(ButtonChange change)
206 Q_D(QQuickSwitchDelegate);
207 if (change == ButtonCheckedChange)
208 setPosition(d->checked ? 1.0 : 0.0);
210 QQuickAbstractButton::buttonChange(change);
215#include "moc_qquickswitchdelegate_p.cpp"