1// Copyright (C) 2016 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 "qtapsensor.h"
5#include "qtapsensor_p.h"
6
7QT_BEGIN_NAMESPACE
8
9IMPLEMENT_READING(QTapReading)
10
11/*!
12 \class QTapReading
13 \ingroup sensors_reading
14 \inmodule QtSensors
15 \since 5.1
16 \internal
17
18 \brief The QTapReading class represents one reading from the
19 tap sensor.
20
21 \section2 QTapReading Units
22 The tap sensor registers tap events along the 3 axes that originate from the phone.
23 The axes are arranged as follows.
24
25 \image sensors-coordinates2.jpg
26
27 By default it returns only double tap events. The QTapSensor::returnDoubleTapEvents property
28 must be set to false to return individual tap events.
29*/
30
31/*!
32 \enum QTapReading::TapDirection
33
34 The tap direction is indicated using flags. Applications should check for the presence of
35 a particular flag as multiple flags may be set at once.
36
37 The X, Y and Z flags allow an app to check for taps along an axis without caring about the
38 direction.
39
40 \code
41 if (reading->tapDirection()&QTapReading::X) {
42 ...
43 }
44 \endcode
45
46 The *_Pos and *_Neg flags allow checking for taps in a specific direction. Note that some
47 devices cannot determine the direction of a tap and will set both the _Pos and _Neg flag for
48 the detected axis. Previous versions of the API did not allow this. Applications that check
49 for the _Pos and _Neg flags as values should be updated so they can work with all devices.
50
51 For example, if you have code like
52
53 \code
54 if (reading->tapDirection() == QTapReading::X_Pos) {
55 ...
56 }
57 \endcode
58
59 you can rewrite it as
60
61 \code
62 if (reading->tapDirection()&QTapReading::X_Pos) {
63 ...
64 }
65 \endcode
66
67 \value Undefined This value means that the direction is unknown.
68 \value X This flag is set if the tap was along the X axis.
69 \value Y This flag is set if the tap was along the Y axis.
70 \value Z This flag is set if the tap was along the Z axis.
71 \value X_Pos This flag is set if the tap was towards the positive X direction.
72 \value Y_Pos This flag is set if the tap was towards the positive Y direction.
73 \value Z_Pos This flag is set if the tap was towards the positive Z direction.
74 \value X_Neg This flag is set if the tap was towards the negative X direction.
75 \value Y_Neg This flag is set if the tap was towards the negative Y direction.
76 \value Z_Neg This flag is set if the tap was towards the negative Z direction.
77 \value X_Both Equivalent to \c{X_Pos|X_Neg}. Returned by devices that cannot detect the direction of a tap.
78 \value Y_Both Equivalent to \c{Y_Pos|Y_Neg}. Returned by devices that cannot detect the direction of a tap.
79 \value Z_Both Equivalent to \c{Z_Pos|Z_Neg}. Returned by devices that cannot detect the direction of a tap.
80*/
81
82/*!
83 \property QTapReading::tapDirection
84 \brief the direction of the tap.
85
86 \sa {QTapReading Units}
87*/
88
89QTapReading::TapDirection QTapReading::tapDirection() const
90{
91 return static_cast<QTapReading::TapDirection>(d->tapDirection);
92}
93
94/*!
95 Sets the tap direction to \a tapDirection.
96*/
97void QTapReading::setTapDirection(QTapReading::TapDirection tapDirection)
98{
99 switch (tapDirection) {
100 case X_Pos:
101 case Y_Pos:
102 case Z_Pos:
103 case X_Neg:
104 case Y_Neg:
105 case Z_Neg:
106 case X_Both:
107 case Y_Both:
108 case Z_Both:
109 d->tapDirection = tapDirection;
110 break;
111 default:
112 d->tapDirection = Undefined;
113 break;
114 }
115}
116
117/*!
118 \property QTapReading::doubleTap
119 \brief a value indicating if there was a single or double tap.
120
121 \list
122 \li true - double tap
123 \li false - single tap
124 \endlist
125 \sa {QTapReading Units}
126*/
127
128bool QTapReading::isDoubleTap() const
129{
130 return d->doubleTap;
131}
132
133/*!
134 Sets the double tap status of the reading to \a doubleTap.
135*/
136void QTapReading::setDoubleTap(bool doubleTap)
137{
138 d->doubleTap = doubleTap;
139}
140
141// =====================================================================
142
143/*!
144 \class QTapFilter
145 \ingroup sensors_filter
146 \inmodule QtSensors
147 \since 5.1
148 \internal
149
150 \brief The QTapFilter class is a convenience wrapper around QSensorFilter.
151
152 The only difference is that the filter() method features a pointer to QTapReading
153 instead of QSensorReading.
154*/
155
156/*!
157 \fn QTapFilter::filter(QTapReading *reading)
158
159 Called when \a reading changes. Returns false to prevent the reading from propagating.
160
161 \sa QSensorFilter::filter()
162*/
163
164bool QTapFilter::filter(QSensorReading *reading)
165{
166 return filter(reading: static_cast<QTapReading*>(reading));
167}
168
169char const * const QTapSensor::sensorType("QTapSensor");
170
171/*!
172 \class QTapSensor
173 \ingroup sensors_type
174 \inmodule QtSensors
175 \since 5.1
176 \internal
177
178 \brief The QTapSensor class is a convenience wrapper around QSensor.
179
180 The only behavioural difference is that this class sets the type properly.
181
182 This class also features a reading() function that returns a QTapReading instead of a QSensorReading.
183
184 For details about how the sensor works, see \l QTapReading.
185
186 \sa QTapReading
187*/
188
189/*!
190 Construct the sensor as a child of \a parent.
191*/
192QTapSensor::QTapSensor(QObject *parent)
193 : QSensor(QTapSensor::sensorType, *new QTapSensorPrivate, parent)
194{
195}
196
197/*!
198 Destroy the sensor. Stops the sensor if it has not already been stopped.
199*/
200QTapSensor::~QTapSensor()
201{
202}
203
204/*!
205 \fn QTapSensor::reading() const
206
207 Returns the reading class for this sensor.
208
209 \sa QSensor::reading()
210*/
211
212QTapReading *QTapSensor::reading() const
213{
214 return static_cast<QTapReading*>(QSensor::reading());
215}
216
217/*!
218 \property QTapSensor::returnDoubleTapEvents
219 \brief a value indicating if double tap events should be reported.
220
221 Set to true (the default) to have the sensor report only on double tap events.
222 Set to false to have the sensor report only on individual tap events.
223
224 It is not possible to have the sensor report both single and double tap events.
225 If both are needed the app should create 2 sensor objects.
226
227 The property must be set before calling start().
228*/
229
230bool QTapSensor::returnDoubleTapEvents() const
231{
232 Q_D(const QTapSensor);
233 return d->returnDoubleTapEvents;
234}
235
236void QTapSensor::setReturnDoubleTapEvents(bool returnDoubleTapEvents)
237{
238 Q_D(QTapSensor);
239 if (d->returnDoubleTapEvents != returnDoubleTapEvents) {
240 d->returnDoubleTapEvents = returnDoubleTapEvents;
241 emit returnDoubleTapEventsChanged(returnDoubleTapEvents);
242 }
243}
244
245QT_END_NAMESPACE
246
247#include "moc_qtapsensor.cpp"
248

source code of qtsensors/src/sensors/qtapsensor.cpp