| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 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 "qmlsensorgesture.h" |
| 41 | #include <QtSensors/qsensorgesture.h> |
| 42 | #include <QtSensors/qsensorgesturemanager.h> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | /*! |
| 47 | \qmltype SensorGesture |
| 48 | \instantiates QmlSensorGesture |
| 49 | \inqmlmodule QtSensors |
| 50 | \since QtSensors 5.0 |
| 51 | \brief Provides notifications when sensor-based gestures are detected. |
| 52 | |
| 53 | This type provides notification when sensor gestures are triggered. |
| 54 | |
| 55 | The following QML code creates a "shake" and "SecondCounter" SensorGesture QML type, and |
| 56 | displays the detected gesture in a text type. |
| 57 | |
| 58 | QtSensors.shake gesture is available with the Qt Sensors API, but the QtSensors.SecondCounter |
| 59 | sensor gesture is provided as example code for the \l {Qt Sensors - SensorGesture QML Type example} |
| 60 | |
| 61 | \qml |
| 62 | Item { |
| 63 | SensorGesture { |
| 64 | id: sensorGesture |
| 65 | enabled: false |
| 66 | gestures : ["QtSensors.shake", "QtSensors.SecondCounter"] |
| 67 | onDetected:{ |
| 68 | detectedText.text = gesture |
| 69 | } |
| 70 | } |
| 71 | Text { |
| 72 | id: detectedText |
| 73 | x:5 |
| 74 | y:160 |
| 75 | } |
| 76 | } |
| 77 | \endqml |
| 78 | |
| 79 | \l {Qt Sensor Gestures} contains a list of currently supported sensor gestures and their |
| 80 | descriptions. |
| 81 | |
| 82 | |
| 83 | */ |
| 84 | QmlSensorGesture::QmlSensorGesture(QObject* parent) |
| 85 | : QObject(parent) |
| 86 | , isEnabled(false) |
| 87 | , initDone(false) |
| 88 | , sensorGesture(0) |
| 89 | , sensorGestureManager(new QSensorGestureManager(this)) |
| 90 | { |
| 91 | connect(asender: sensorGestureManager, SIGNAL(newSensorGestureAvailable()), SIGNAL(availableGesturesChanged())); |
| 92 | } |
| 93 | |
| 94 | QmlSensorGesture::~QmlSensorGesture() |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | QQmlParserStatus interface implementation |
| 100 | */ |
| 101 | void QmlSensorGesture::classBegin() |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | void QmlSensorGesture::componentComplete() |
| 106 | { |
| 107 | /* |
| 108 | this is needed in the case the customer defines the type(s) and set it enabled = true |
| 109 | */ |
| 110 | initDone = true; |
| 111 | setEnabled(isEnabled); |
| 112 | } |
| 113 | /* |
| 114 | End of QQmlParserStatus interface implementation |
| 115 | */ |
| 116 | |
| 117 | /*! |
| 118 | \qmlproperty stringlist SensorGesture::availableGestures |
| 119 | This property can be used to determine all available gestures on the system. |
| 120 | */ |
| 121 | QStringList QmlSensorGesture::availableGestures() |
| 122 | { |
| 123 | return sensorGestureManager->gestureIds(); |
| 124 | } |
| 125 | |
| 126 | /*! |
| 127 | \qmlproperty stringlist SensorGesture::gestures |
| 128 | Set this property to a list of the gestures that the application is interested in detecting. |
| 129 | This property cannot be changed while the type is enabled. |
| 130 | |
| 131 | The properties validGestures and invalidGestures will be set as appropriate immediately. |
| 132 | To determine all available getures on the system please use the |
| 133 | \l {SensorGesture::availableGestures} {availableGestures} property. |
| 134 | |
| 135 | \sa {QtSensorGestures Plugins} |
| 136 | */ |
| 137 | QStringList QmlSensorGesture::gestures() const |
| 138 | { |
| 139 | return gestureList; |
| 140 | } |
| 141 | |
| 142 | void QmlSensorGesture::setGestures(const QStringList& value) |
| 143 | { |
| 144 | if (gestureList == value) |
| 145 | return; |
| 146 | |
| 147 | if (initDone && enabled()) { |
| 148 | qWarning() << "Cannot change gestures while running." ; |
| 149 | return; |
| 150 | } |
| 151 | gestureList = value; |
| 152 | createGesture(); |
| 153 | Q_EMIT gesturesChanged(); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /*! |
| 158 | \qmlproperty stringlist SensorGesture::validGestures |
| 159 | This property holds the requested gestures that were found on the system. |
| 160 | */ |
| 161 | QStringList QmlSensorGesture::validGestures() const |
| 162 | { |
| 163 | if (sensorGesture) |
| 164 | return sensorGesture->validIds(); |
| 165 | return QStringList(); |
| 166 | } |
| 167 | |
| 168 | /*! |
| 169 | \qmlproperty stringlist SensorGesture::invalidGestures |
| 170 | This property holds the requested gestures that were not found on the system. |
| 171 | */ |
| 172 | QStringList QmlSensorGesture::invalidGestures() const |
| 173 | { |
| 174 | if (sensorGesture) |
| 175 | return sensorGesture->invalidIds(); |
| 176 | return QStringList(); |
| 177 | } |
| 178 | |
| 179 | /*! |
| 180 | \qmlproperty bool SensorGesture::enabled |
| 181 | This property can be used to activate or deactivate the sensor gesture. |
| 182 | Default value is false; |
| 183 | \sa {SensorGesture::detected}, {detected} |
| 184 | */ |
| 185 | bool QmlSensorGesture::enabled() const |
| 186 | { |
| 187 | return isEnabled; |
| 188 | } |
| 189 | |
| 190 | void QmlSensorGesture::setEnabled(bool value) |
| 191 | { |
| 192 | bool hasChanged = false; |
| 193 | if (isEnabled != value) { |
| 194 | isEnabled = value; |
| 195 | hasChanged = true; |
| 196 | } |
| 197 | if (!initDone) |
| 198 | return; |
| 199 | |
| 200 | if (sensorGesture) { |
| 201 | if (value) { |
| 202 | sensorGesture->startDetection(); |
| 203 | } else { |
| 204 | sensorGesture->stopDetection(); |
| 205 | } |
| 206 | } |
| 207 | if (hasChanged) |
| 208 | Q_EMIT enabledChanged(); |
| 209 | } |
| 210 | |
| 211 | /*! |
| 212 | \qmlsignal SensorGesture::detected(string gesture) |
| 213 | This signal is emitted whenever a gesture is detected. |
| 214 | The \a gesture parameter contains the gesture that was detected. |
| 215 | |
| 216 | The corresponding handler is \c onDetected. |
| 217 | */ |
| 218 | |
| 219 | /* |
| 220 | private function implementation |
| 221 | */ |
| 222 | void QmlSensorGesture::deleteGesture() |
| 223 | { |
| 224 | if (sensorGesture) { |
| 225 | bool emitInvalidChange = !invalidGestures().isEmpty(); |
| 226 | bool emitValidChange = !validGestures().isEmpty(); |
| 227 | |
| 228 | if (sensorGesture->isActive()) { |
| 229 | sensorGesture->stopDetection(); |
| 230 | } |
| 231 | delete sensorGesture; |
| 232 | sensorGesture = 0; |
| 233 | |
| 234 | if (emitInvalidChange) { |
| 235 | Q_EMIT invalidGesturesChanged(); |
| 236 | } |
| 237 | if (emitValidChange) { |
| 238 | Q_EMIT validGesturesChanged(); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void QmlSensorGesture::createGesture() |
| 244 | { |
| 245 | deleteGesture(); |
| 246 | sensorGesture = new QSensorGesture(gestureList, this); |
| 247 | if (!validGestures().isEmpty()) { |
| 248 | QObject::connect(sender: sensorGesture |
| 249 | , SIGNAL(detected(QString)) |
| 250 | , receiver: this |
| 251 | , SIGNAL(detected(QString))); |
| 252 | Q_EMIT validGesturesChanged(); |
| 253 | } |
| 254 | if (!invalidGestures().isEmpty()) |
| 255 | Q_EMIT invalidGesturesChanged(); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | End of private function implementation |
| 260 | */ |
| 261 | |
| 262 | QT_END_NAMESPACE |
| 263 | |