Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickswitchdelegate.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
7
9
14
45{
46 Q_DECLARE_PUBLIC(QQuickSwitchDelegate)
47
48public:
49 qreal positionAt(const QPointF &point) const;
50
51 bool canDrag(const QPointF &movePoint) const;
52 bool handleMove(const QPointF &point, ulong timestamp) override;
53 bool handleRelease(const QPointF &point, ulong timestamp) override;
54
56
58};
59
61{
62 Q_Q(const QQuickSwitchDelegate);
63 qreal pos = 0.0;
64 if (indicator)
65 pos = indicator->mapFromItem(q, point).x() / indicator->width();
66 if (q->isMirrored())
67 return 1.0 - pos;
68 return pos;
69}
70
72{
73 // don't start dragging the handle unless the initial press was at the indicator,
74 // or the drag has reached the indicator area. this prevents unnatural jumps when
75 // dragging far outside the indicator.
76 const qreal pressPos = positionAt(pressPoint);
77 const qreal movePos = positionAt(movePoint);
78 return (pressPos >= 0.0 && pressPos <= 1.0) || (movePos >= 0.0 && movePos <= 1.0);
79}
80
82{
85 if (q->keepMouseGrab() || q->keepTouchGrab())
86 q->setPosition(positionAt(point));
87 return true;
88}
89
91{
94 q->setKeepMouseGrab(false);
95 q->setKeepTouchGrab(false);
96 return true;
97}
98
101{
103 d->keepPressed = true;
104 setCheckable(true);
105}
106
114{
115 Q_D(const QQuickSwitchDelegate);
116 return d->position;
117}
118
120{
122 position = qBound<qreal>(0.0, position, 1.0);
123 if (qFuzzyCompare(d->position, position))
124 return;
125
126 d->position = position;
129}
130
138{
139 Q_D(const QQuickSwitchDelegate);
140 if (isMirrored())
141 return 1.0 - d->position;
142 return d->position;
143}
144
146{
148 if (!keepMouseGrab()) {
149 const QPointF movePoint = event->position();
150 if (d->canDrag(movePoint))
152 }
154}
155
156#if QT_CONFIG(quicktemplates2_multitouch)
158{
160 if (!keepTouchGrab() && event->type() == QEvent::TouchUpdate) {
161 for (const QTouchEvent::TouchPoint &point : event->points()) {
162 if (point.id() != d->touchId || point.state() != QEventPoint::Updated)
163 continue;
164 if (d->canDrag(point.position()))
165 setKeepTouchGrab(QQuickWindowPrivate::dragOverThreshold(point.position().x() - d->pressPoint.x(), Qt::XAxis, &point));
166 }
167 }
169}
170#endif
171
176
182
184{
186 if (keepMouseGrab() || keepTouchGrab()) {
187 d->toggle(d->position > 0.5);
188 // the checked state might not change => force a position update to
189 // avoid that the handle is left somewhere in the middle (QTBUG-57944)
190 setPosition(d->checked ? 1.0 : 0.0);
191 } else {
193 }
194}
195
197{
199 if (change == ButtonCheckedChange)
200 setPosition(d->checked ? 1.0 : 0.0);
201 else
203}
204
206
207#include "moc_qquickswitchdelegate_p.cpp"
The QEventPoint class provides information about a point in a QPointerEvent.
Definition qeventpoint.h:20
@ TouchUpdate
Definition qcoreevent.h:242
\reentrant
Definition qfont.h:22
\inmodule QtGui
Definition qevent.h:196
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
QQuickDeferredPointer< QQuickItem > indicator
bool handleRelease(const QPointF &point, ulong timestamp) override
bool handleMove(const QPointF &point, ulong timestamp) override
void setCheckable(bool checkable)
virtual void buttonChange(ButtonChange change)
bool isMirrored() const
\qmlproperty bool QtQuick.Controls::Control::mirrored \readonly
virtual void mirrorChange()
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
void setKeepTouchGrab(bool)
Sets whether the touch points grabbed by this item should remain exclusively with this item.
Q_INVOKABLE QPointF mapFromItem(const QQuickItem *item, const QPointF &point) const
Maps the given point in item's coordinate system to the equivalent point within this item's coordinat...
bool keepTouchGrab() const
Returns whether the touch points grabbed by this item should exclusively remain with this item.
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
bool keepMouseGrab() const
Returns whether mouse input should exclusively remain with this item.
void setKeepMouseGrab(bool)
Sets whether the mouse input should remain exclusively with this item.
virtual void touchEvent(QTouchEvent *event)
This event handler can be reimplemented in a subclass to receive touch events for an item.
virtual void mouseMoveEvent(QMouseEvent *event)
This event handler can be reimplemented in a subclass to receive mouse move events for an item.
Item delegate with a switch indicator that can be toggled on or off.
QPalette defaultPalette() const override
bool handleRelease(const QPointF &point, ulong timestamp) override
bool handleMove(const QPointF &point, ulong timestamp) override
bool canDrag(const QPointF &movePoint) const
qreal positionAt(const QPointF &point) const
void mouseMoveEvent(QMouseEvent *event) override
This event handler can be reimplemented in a subclass to receive mouse move events for an item.
void buttonChange(ButtonChange change) override
void setPosition(qreal position)
QFont defaultFont() const override
QQuickSwitchDelegate(QQuickItem *parent=nullptr)
static QPalette palette(Scope scope)
static QFont font(Scope scope)
static bool dragOverThreshold(qreal d, Qt::Axis axis, const QEventPoint *tp, int startDragThreshold=-1)
The QTouchEvent class contains parameters that describe a touch event.
Definition qevent.h:917
Combined button and popup list for selecting options.
@ XAxis
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
n void setPosition(void) \n\
struct _cl_event * event
GLfixed GLfixed GLint GLint GLfixed points
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define emit
unsigned long ulong
Definition qtypes.h:35
double qreal
Definition qtypes.h:187