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
qquickswitch.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
4#include "qquickswitch_p.h"
6
7#include <QtGui/qstylehints.h>
8#include <QtGui/qguiapplication.h>
9#include <QtQuick/private/qquickwindow_p.h>
10#include <QtQuick/private/qquickevents_p_p.h>
11
13
18
48{
49 Q_DECLARE_PUBLIC(QQuickSwitch)
50
51public:
52 qreal positionAt(const QPointF &point) const;
53
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;
57
59
61};
62
64{
65 Q_Q(const QQuickSwitch);
66 qreal pos = 0.0;
67 if (indicator)
68 pos = indicator->mapFromItem(q, point).x() / indicator->width();
69 if (q->isMirrored())
70 return 1.0 - pos;
71 return pos;
72}
73
74bool QQuickSwitchPrivate::canDrag(const QPointF &movePoint) const
75{
76 // don't start dragging the handle unless the initial press was at the indicator,
77 // or the drag has reached the indicator area. this prevents unnatural jumps when
78 // dragging far outside the indicator.
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);
82}
83
84bool QQuickSwitchPrivate::handleMove(const QPointF &point, ulong timestamp)
85{
86 Q_Q(QQuickSwitch);
88 if (q->keepMouseGrab() || q->keepTouchGrab())
89 q->setPosition(positionAt(point));
90 return true;
91}
92
94{
95 Q_Q(QQuickSwitch);
97 q->setKeepMouseGrab(false);
98 q->setKeepTouchGrab(false);
99 return true;
100}
101
104{
105 Q_D(QQuickSwitch);
106 d->keepPressed = true;
107 setCheckable(true);
108}
109
117{
118 Q_D(const QQuickSwitch);
119 return d->position;
120}
121
123{
124 Q_D(QQuickSwitch);
125 position = qBound<qreal>(0.0, position, 1.0);
126 if (qFuzzyCompare(d->position, position))
127 return;
128
129 d->position = position;
132}
133
141{
142 Q_D(const QQuickSwitch);
143 if (isMirrored())
144 return 1.0 - d->position;
145 return d->position;
146}
147
149{
150 Q_D(QQuickSwitch);
151 if (!keepMouseGrab()) {
152 const QPointF movePoint = event->position();
153 if (d->canDrag(movePoint))
155 }
157}
158
159#if QT_CONFIG(quicktemplates2_multitouch)
161{
162 Q_D(QQuickSwitch);
163 if (!keepTouchGrab() && event->type() == QEvent::TouchUpdate) {
164 for (const QTouchEvent::TouchPoint &point : event->points()) {
165 if (point.id() != d->touchId || point.state() != QEventPoint::Updated)
166 continue;
167 if (d->canDrag(point.position()))
168 setKeepTouchGrab(QQuickWindowPrivate::dragOverThreshold(point.position().x() - d->pressPoint.x(), Qt::XAxis, &point));
169 }
170 }
172}
173#endif
174
180
182{
183 Q_D(QQuickSwitch);
184 if (keepMouseGrab() || keepTouchGrab()) {
185 d->toggle(d->position > 0.5);
186 // the checked state might not change => force a position update to
187 // avoid that the handle is left somewhere in the middle (QTBUG-57944)
188 setPosition(d->checked ? 1.0 : 0.0);
189 } else {
191 }
192}
193
195{
196 Q_D(QQuickSwitch);
197 if (change == ButtonCheckedChange)
198 setPosition(d->checked ? 1.0 : 0.0);
199 else
201}
202
207
209
210#include "moc_qquickswitch_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.
Switch button that can be toggled on or off.
QPalette defaultPalette() const override
qreal positionAt(const QPointF &point) const
bool canDrag(const QPointF &movePoint) const
bool handleMove(const QPointF &point, ulong timestamp) override
bool handleRelease(const QPointF &point, ulong timestamp) override
QQuickSwitch(QQuickItem *parent=nullptr)
QFont defaultFont() const override
void positionChanged()
void mouseMoveEvent(QMouseEvent *event) override
This event handler can be reimplemented in a subclass to receive mouse move events for an item.
void visualPositionChanged()
void setPosition(qreal position)
void buttonChange(ButtonChange change) override
void nextCheckState() override
void mirrorChange() override
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