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
47class QQuickSwitchDelegatePrivate :
public QQuickItemDelegatePrivate
49 Q_DECLARE_PUBLIC(QQuickSwitchDelegate)
52 qreal positionAt(
const QPointF &point)
const;
54 bool canDrag(
const QPointF &movePoint)
const;
55 bool handleMove(
const QPointF &point, ulong timestamp) override;
56 bool handleRelease(
const QPointF &point, ulong timestamp) override;
58 QPalette defaultPalette()
const override {
return QQuickTheme::palette(QQuickTheme::ListView); }
63qreal QQuickSwitchDelegatePrivate::positionAt(
const QPointF &point)
const
65 Q_Q(
const QQuickSwitchDelegate);
68 pos = indicator->mapFromItem(q, point).x() / indicator->width();
74bool QQuickSwitchDelegatePrivate::canDrag(
const QPointF &movePoint)
const
79 const qreal pressPos = positionAt(pressPoint);
80 const qreal movePos = positionAt(movePoint);
81 return (pressPos >= 0.0 && pressPos <= 1.0) || (movePos >= 0.0 && movePos <= 1.0);
84bool QQuickSwitchDelegatePrivate::handleMove(
const QPointF &point, ulong timestamp)
86 Q_Q(QQuickSwitchDelegate);
87 QQuickItemDelegatePrivate::handleMove(point, timestamp);
88 if (q->keepMouseGrab() || q->keepTouchGrab())
89 q->setPosition(positionAt(point));
93bool QQuickSwitchDelegatePrivate::handleRelease(
const QPointF &point, ulong timestamp)
95 Q_Q(QQuickSwitchDelegate);
96 QQuickItemDelegatePrivate::handleRelease(point, timestamp);
97 q->setKeepMouseGrab(
false);
98 q->setKeepTouchGrab(
false);
102QQuickSwitchDelegate::QQuickSwitchDelegate(QQuickItem *parent)
103 : QQuickItemDelegate(*(
new QQuickSwitchDelegatePrivate), parent)
105 Q_D(QQuickSwitchDelegate);
106 d->keepPressed =
true;
111
112
113
114
115
116qreal QQuickSwitchDelegate::position()
const
118 Q_D(
const QQuickSwitchDelegate);
122void QQuickSwitchDelegate::setPosition(qreal position)
124 Q_D(QQuickSwitchDelegate);
125 position = std::clamp(position, qreal(0.0), qreal(1.0));
126 if (qFuzzyCompare(d->position, position))
129 d->position = position;
130 emit positionChanged();
131 emit visualPositionChanged();
135
136
137
138
139
140qreal QQuickSwitchDelegate::visualPosition()
const
142 Q_D(
const QQuickSwitchDelegate);
144 return 1.0 - d->position;
148void QQuickSwitchDelegate::mouseMoveEvent(QMouseEvent *event)
150 Q_D(QQuickSwitchDelegate);
151 if (!keepMouseGrab()) {
152 const QPointF movePoint = event->position();
153 if (d->canDrag(movePoint)) {
154 setKeepMouseGrab(QQuickDeliveryAgentPrivate::dragOverThreshold(movePoint.x() - d->pressPoint.x(),
158 QQuickItemDelegate::mouseMoveEvent(event);
161#if QT_CONFIG(quicktemplates2_multitouch)
162void QQuickSwitchDelegate::touchEvent(QTouchEvent *event)
164 Q_D(QQuickSwitchDelegate);
165 if (!keepTouchGrab() && event->type() == QEvent::TouchUpdate) {
166 for (
const QTouchEvent::TouchPoint &point : event->points()) {
167 if (point.id() != d->touchId || point.state() != QEventPoint::Updated)
169 if (d->canDrag(point.position())) {
170 setKeepTouchGrab(QQuickDeliveryAgentPrivate::dragOverThreshold(point.position().x() - d->pressPoint.x(),
175 QQuickItemDelegate::touchEvent(event);
179QFont QQuickSwitchDelegate::defaultFont()
const
181 return QQuickTheme::font(QQuickTheme::ListView);
184void QQuickSwitchDelegate::mirrorChange()
186 QQuickItemDelegate::mirrorChange();
187 emit visualPositionChanged();
190void QQuickSwitchDelegate::nextCheckState()
192 Q_D(QQuickSwitchDelegate);
193 if (keepMouseGrab() || keepTouchGrab()) {
194 d->toggle(d->position > 0.5);
197 setPosition(d->checked ? 1.0 : 0.0);
199 QQuickItemDelegate::nextCheckState();
203void QQuickSwitchDelegate::buttonChange(ButtonChange change)
205 Q_D(QQuickSwitchDelegate);
206 if (change == ButtonCheckedChange)
207 setPosition(d->checked ? 1.0 : 0.0);
209 QQuickAbstractButton::buttonChange(change);
214#include "moc_qquickswitchdelegate_p.cpp"