| 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 <QDir> |
| 41 | #include <QPluginLoader> |
| 42 | #include <QDebug> |
| 43 | |
| 44 | #include "qsensorgesture.h" |
| 45 | #include "qsensorgesture_p.h" |
| 46 | #include "qsensorgesturemanager.h" |
| 47 | |
| 48 | #include <private/qmetaobjectbuilder_p.h> |
| 49 | |
| 50 | /*! |
| 51 | \class QSensorGesture |
| 52 | \ingroup sensorgestures_main |
| 53 | \inmodule QtSensors |
| 54 | \since 5.1 |
| 55 | |
| 56 | \brief The QSensorGesture class represents one or more sensor gesture recognizers. |
| 57 | |
| 58 | In addition to the QSensorGesture::detected() signal, Sensor Gesture Recognizers can |
| 59 | have their own specific signals, and may be discovered through |
| 60 | QSensorGesture::gestureSignals(). |
| 61 | |
| 62 | \b {Note that QSensorGesture uses a custom meta-object in order to provide |
| 63 | recognizer-specific signals. This means it is not possible to sub-class |
| 64 | QSensorGesture and use Q_OBJECT. Also qobject_cast<QSensorGesture*>(ptr) will |
| 65 | not work.} |
| 66 | |
| 67 | \sa QSensorGestureRecognizer |
| 68 | |
| 69 | You may use QSensorGestureManager to obtain the systems known sensor gesture ids. |
| 70 | |
| 71 | \sa QSensorGestureManager |
| 72 | */ |
| 73 | |
| 74 | /*! |
| 75 | \fn void QSensorGesture::detected(QString) |
| 76 | Signals when the gesture has been recognized. |
| 77 | */ |
| 78 | |
| 79 | /*! |
| 80 | Constructs the sensor gesture, and initializes the \a ids list of recognizers, |
| 81 | with parent \a parent |
| 82 | */ |
| 83 | QSensorGesture::QSensorGesture(const QStringList &ids, QObject *parent) : |
| 84 | QObject(parent) |
| 85 | { |
| 86 | d_ptr = new QSensorGesturePrivate(); |
| 87 | Q_FOREACH (const QString &id, ids) { |
| 88 | QSensorGestureRecognizer * rec = QSensorGestureManager::sensorGestureRecognizer(id); |
| 89 | if (rec != 0) { |
| 90 | d_ptr->m_sensorRecognizers.append(t: rec); |
| 91 | d_ptr->availableIds.append(t: id); |
| 92 | } else { |
| 93 | d_ptr->invalidIds.append(t: id); |
| 94 | //add to not available things |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | d_ptr->meta = 0; |
| 99 | |
| 100 | QMetaObjectBuilder builder; |
| 101 | builder.setSuperClass(&QObject::staticMetaObject); |
| 102 | builder.setClassName("QSensorGesture" ); |
| 103 | |
| 104 | Q_FOREACH (QSensorGestureRecognizer *recognizer, d_ptr->m_sensorRecognizers) { |
| 105 | Q_FOREACH (const QString &gesture, recognizer->gestureSignals()) { |
| 106 | QMetaMethodBuilder b = builder.addSignal(signature: gesture.toLatin1()); |
| 107 | if (!d_ptr->localGestureSignals.contains(str: QLatin1String(b.signature()))) |
| 108 | d_ptr->localGestureSignals.append(t: QLatin1String(b.signature())); |
| 109 | } |
| 110 | recognizer->createBackend(); |
| 111 | } |
| 112 | d_ptr->meta = builder.toMetaObject(); |
| 113 | |
| 114 | if (d_ptr->m_sensorRecognizers.count() > 0) { |
| 115 | d_ptr->valid = true; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /*! |
| 120 | Destroy the QSensorGesture |
| 121 | */ |
| 122 | QSensorGesture::~QSensorGesture() |
| 123 | { |
| 124 | stopDetection(); |
| 125 | if (d_ptr->meta) |
| 126 | free(ptr: d_ptr->meta); |
| 127 | delete d_ptr; |
| 128 | } |
| 129 | |
| 130 | /*! |
| 131 | Returns the gesture recognizer ids that were found. |
| 132 | */ |
| 133 | QStringList QSensorGesture::validIds() const |
| 134 | { |
| 135 | return d_ptr->availableIds; |
| 136 | } |
| 137 | |
| 138 | /*! |
| 139 | Returns the gesture recognizer ids that were not found. |
| 140 | */ |
| 141 | QStringList QSensorGesture::invalidIds() const |
| 142 | { |
| 143 | return d_ptr->invalidIds; |
| 144 | } |
| 145 | |
| 146 | /*! |
| 147 | Starts the gesture detection routines in the recognizer. |
| 148 | */ |
| 149 | void QSensorGesture::startDetection() |
| 150 | { |
| 151 | if (d_ptr->m_sensorRecognizers.count() < 1) |
| 152 | return; |
| 153 | if (d_ptr->isActive) |
| 154 | return; |
| 155 | |
| 156 | Q_FOREACH (QSensorGestureRecognizer *recognizer, d_ptr->m_sensorRecognizers) { |
| 157 | |
| 158 | Q_ASSERT(recognizer !=0); |
| 159 | |
| 160 | connect(sender: recognizer,SIGNAL(detected(QString)), |
| 161 | receiver: this,SIGNAL(detected(QString)),Qt::UniqueConnection); |
| 162 | |
| 163 | //connect recognizer signals |
| 164 | Q_FOREACH (QString method, recognizer->gestureSignals()) { |
| 165 | method.prepend(s: QLatin1String("2" )); |
| 166 | connect(sender: recognizer, signal: method.toLatin1(), |
| 167 | receiver: this, member: method.toLatin1(), Qt::UniqueConnection); |
| 168 | } |
| 169 | |
| 170 | recognizer->startBackend(); |
| 171 | } |
| 172 | d_ptr->isActive = true; |
| 173 | } |
| 174 | |
| 175 | /*! |
| 176 | Stops the gesture detection routines. |
| 177 | */ |
| 178 | void QSensorGesture::stopDetection() |
| 179 | { |
| 180 | if (d_ptr->m_sensorRecognizers.count() < 1) |
| 181 | return; |
| 182 | |
| 183 | if (!d_ptr->isActive) |
| 184 | return; |
| 185 | |
| 186 | Q_FOREACH (QSensorGestureRecognizer *recognizer, d_ptr->m_sensorRecognizers) { |
| 187 | disconnect(sender: recognizer,SIGNAL(detected(QString)), |
| 188 | receiver: this,SIGNAL(detected(QString))); |
| 189 | //disconnect recognizer signals |
| 190 | Q_FOREACH (QString method,recognizer->gestureSignals()) { |
| 191 | method.prepend(s: QLatin1String("2" )); |
| 192 | disconnect(sender: recognizer, signal: method.toLatin1(), |
| 193 | receiver: this, member: method.toLatin1()); |
| 194 | } |
| 195 | |
| 196 | recognizer->stopBackend(); |
| 197 | } |
| 198 | d_ptr->isActive = false; |
| 199 | } |
| 200 | |
| 201 | /*! |
| 202 | Returns all the possible gestures signals that may be emitted. |
| 203 | */ |
| 204 | QStringList QSensorGesture::gestureSignals() const |
| 205 | { |
| 206 | if (d_ptr->m_sensorRecognizers.count() > 0) { |
| 207 | return d_ptr->localGestureSignals; |
| 208 | } |
| 209 | return QStringList(); |
| 210 | } |
| 211 | |
| 212 | /*! |
| 213 | Returns whether this gesture is active or not. |
| 214 | */ |
| 215 | |
| 216 | bool QSensorGesture::isActive() |
| 217 | { |
| 218 | return d_ptr->isActive; |
| 219 | } |
| 220 | |
| 221 | /*! |
| 222 | \internal |
| 223 | */ |
| 224 | const QMetaObject* QSensorGesture::metaObject() const |
| 225 | { |
| 226 | return d_ptr->meta; |
| 227 | } |
| 228 | /*! |
| 229 | \internal |
| 230 | */ |
| 231 | int QSensorGesture::qt_metacall(QMetaObject::Call c, int id, void **a) |
| 232 | { |
| 233 | id = QObject::qt_metacall(c, id, a); |
| 234 | |
| 235 | if (id < 0 || !d_ptr->meta) |
| 236 | return id; |
| 237 | |
| 238 | QMetaObject::activate(sender: this, d_ptr->meta, local_signal_index: id, argv: a); |
| 239 | return id; |
| 240 | } |
| 241 | |
| 242 | QSensorGesturePrivate::QSensorGesturePrivate(QObject *parent) |
| 243 | : QObject(parent),isActive(0), valid(0) |
| 244 | { |
| 245 | } |
| 246 | |
| 247 | QSensorGesturePrivate::~QSensorGesturePrivate() |
| 248 | { |
| 249 | |
| 250 | } |
| 251 | |