1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2019 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSensors module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qsensorgesturerecognizer.h" |
41 | #include "qsensorgesture_p.h" |
42 | |
43 | #include "qsensorgesturemanager.h" |
44 | #ifdef SIMULATOR_BUILD |
45 | #include "qsensorgesturemanagerprivate_p.h" |
46 | #endif |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | /*! |
50 | \class QSensorGestureRecognizer |
51 | \ingroup sensorgestures_recognizer |
52 | \inmodule QtSensors |
53 | \since 5.1 |
54 | |
55 | \brief The QSensorGestureRecognizer class is the base class for a sensor gesture |
56 | recognizer. |
57 | |
58 | QSensorGesture recognizer developers should sub-class this to implement their own recognizer. |
59 | |
60 | All sensor gesture recognizers have a detected(QString) signal. Implementors can use this |
61 | and send recognizer specific gestures, such as detected("shake_left") or implement custom signals |
62 | such as shakeLeft(). |
63 | |
64 | These custom signals will be available in the QSensorGesture object at runtime. |
65 | |
66 | \sa QSensorGestureRecognizer::gestureSignals() |
67 | |
68 | */ |
69 | |
70 | /*! |
71 | \fn void QSensorGestureRecognizer::create() |
72 | |
73 | Called by QSensorGesture object constructor to create the recognizers backend. |
74 | |
75 | Implementors would use this to instantiate QSensors and connect signals. |
76 | |
77 | */ |
78 | |
79 | /*! |
80 | \fn QString QSensorGestureRecognizer::id() const |
81 | Returns the identifier for this recognizer. |
82 | */ |
83 | /*! |
84 | \fn bool QSensorGestureRecognizer::start() |
85 | |
86 | Called by QSensorGesture::startDetection() to start this recognizer. |
87 | Implementors should start the sensors. |
88 | Returns true if the operation is successful. |
89 | |
90 | */ |
91 | /*! |
92 | \fn bool QSensorGestureRecognizer::stop() |
93 | |
94 | Called by QSensorGesture::stopDetection() to stop this recognizer. |
95 | Returns true if the call succeeds, otherwise false. |
96 | |
97 | Implementors should stop the sensors. |
98 | |
99 | */ |
100 | /*! |
101 | \fn bool QSensorGestureRecognizer::isActive() |
102 | |
103 | Returns true if this recognizer is active, otherwise false. |
104 | */ |
105 | |
106 | /*! |
107 | \fn QSensorGestureRecognizer::detected(const QString &) |
108 | Signals when a gesture is recognized. Implementors can use this signal to send |
109 | recognizer-specific gestures, such as \c detected("shake_left") or implement |
110 | custom signals such as \c shakeLeft(). |
111 | |
112 | The custom signals are available in the QSensorGesture object at runtime. |
113 | */ |
114 | |
115 | class QSensorGestureRecognizerPrivate |
116 | { |
117 | public: |
118 | bool initialized; |
119 | int count; |
120 | }; |
121 | |
122 | |
123 | /*! |
124 | Constructs the QSensorGestureRecognizer with \a parent as parent. |
125 | */ |
126 | QSensorGestureRecognizer::QSensorGestureRecognizer(QObject *parent) |
127 | :QObject(parent), |
128 | d_ptr(new QSensorGestureRecognizerPrivate()) |
129 | { |
130 | } |
131 | |
132 | /*! |
133 | Destroy the QSensorGestureRecognizer |
134 | */ |
135 | QSensorGestureRecognizer::~QSensorGestureRecognizer() |
136 | { |
137 | delete d_ptr; |
138 | } |
139 | |
140 | /*! |
141 | Returns a list of signals that this recognizer supports. |
142 | |
143 | Note that all signals declared will be exported to the QSensorGesture |
144 | object. If you need to use signals that are not exported, you should use a private class |
145 | to do so. |
146 | |
147 | */ |
148 | QStringList QSensorGestureRecognizer::gestureSignals() const |
149 | { |
150 | QStringList list; |
151 | bool ok = false; |
152 | for (int i = 0; i < this->metaObject()->methodCount(); i++) { |
153 | //weed out objectsignals and slots |
154 | const QByteArray sig(this->metaObject()->method(index: i).methodSignature()); |
155 | if (this->metaObject()->indexOfSignal(signal: sig) != -1) { |
156 | if (sig.contains(c: "detected" )) |
157 | ok = true; |
158 | if (ok) |
159 | list.append(t: QString::fromLatin1(str: sig)); |
160 | } |
161 | } |
162 | return list; |
163 | } |
164 | |
165 | /*! |
166 | Calls QSensorGestureRecognizer::create() if the recognizer is valid. |
167 | */ |
168 | void QSensorGestureRecognizer::createBackend() |
169 | { |
170 | if (d_ptr->initialized) { |
171 | return; |
172 | } |
173 | d_ptr->initialized = true; |
174 | create(); |
175 | } |
176 | |
177 | /*! |
178 | Calls QSensorGestureRecognizer::start() if the recognizer isn't already initialized. |
179 | This is called by the QSensorGesture object, so please use that instead. |
180 | |
181 | \sa QSensorGesture::startDetection() |
182 | |
183 | */ |
184 | void QSensorGestureRecognizer::startBackend() |
185 | { |
186 | if (!d_ptr->initialized) { |
187 | qWarning() << "Not starting. Gesture Recognizer not initialized" ; |
188 | return; |
189 | } |
190 | if (d_ptr->count++ == 0) { |
191 | start(); |
192 | #ifdef SIMULATOR_BUILD |
193 | QSensorGestureManagerPrivate::instance()->recognizerStarted(this); |
194 | #endif |
195 | } |
196 | } |
197 | |
198 | /*! |
199 | Calls QSensorGestureRecognizer::stop() if no other clients are using it. |
200 | This is called by the QSensorGesture object, so please use that instead. |
201 | |
202 | \sa QSensorGesture::stopDetection() |
203 | */ |
204 | void QSensorGestureRecognizer::stopBackend() |
205 | { |
206 | if (!d_ptr->initialized) { |
207 | qWarning() << "Not stopping. Gesture Recognizer not initialized" ; |
208 | return; |
209 | } |
210 | if (--d_ptr->count == 0) { |
211 | stop(); |
212 | #ifdef SIMULATOR_BUILD |
213 | QSensorGestureManagerPrivate::instance()->recognizerStopped(this); |
214 | #endif |
215 | } |
216 | } |
217 | |
218 | QT_END_NAMESPACE |
219 | |