summaryrefslogtreecommitdiffstats
path: root/src/imports/sensors2/qsensor2gesture.cpp
blob: 14690f357aef25ebc523c139bd8eef08b9c27581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtSensors module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qsensor2gesture.h"
#include <qsensorgesture.h>
#include <qsensorgesturemanager.h>

QT_BEGIN_NAMESPACE

//#define LOGGESTURQMLAPI

/*!
    \qmlclass SensorGesture QSensor2Gesture
    \inqmlmodule QtSensors 5
    \since QtSensors 5.0
    \brief Provides notifications when sensor-based gestures are detected.

    This element provides notification when sensor gestures are triggered.

    This element is part of the \b{QtSensors 5} module.

    The following QML code creates a "shake" and "template" SensorGesture QML element, and
    displays the detected gesture in a text element.

    QtSensors.shake gesture is available with the Qt Sensors API, but the QtSensors.SecondCounter
    sensor gesture is provided as example code for the \l {SensorGesture QML Element example}

    \qml
    Item {
       SensorGesture {
           id: sensorGesture
           enabled: false
           gestures : ["QtSensors.shake", "QtSensors.SecondCounter"]
           onDetected:{
               detectedText.text = gesture
           }
       }
       Text {
           id: detectedText
           x:5
           y:160
           text: ""
       }
    }
    \endqml

    \l {Qt Sensor Gestures} contains a list of currently supported sensor gestures and their
    descriptions.


*/
QSensor2Gesture::QSensor2Gesture(QObject* parent)
    : QObject(parent)
    , _enabled(false)
    , _oldEnabled(false)
    , _init(true)
    , _sensorGesture(0)
    , _sensorGestureManager(0)
{
    _sensorGestureManager = new QSensorGestureManager(this);
    connect(_sensorGestureManager, SIGNAL(newSensorGestureAvailable()), SIGNAL(availableGesturesChanged()));
}

QSensor2Gesture::~QSensor2Gesture()
{
}

/*
  QQmlParserStatus interface implementation
*/
void QSensor2Gesture::classBegin()
{
}

void QSensor2Gesture::componentComplete()
{
    /*
      this is needed in the case the customer defines the type(s) and set it enabled = true
    */
    _init = false;
    setEnabled(_enabled);
}
/*
  End of QQmlParserStatus interface implementation
*/

/*!
    \qmlproperty stringlist QtSensors5::SensorGesture::availableGestures
    This property can be used to determine all available gestures on the system.
*/
QStringList QSensor2Gesture::availableGestures()
{
    return _sensorGestureManager->gestureIds();
}

/*!
    \qmlproperty stringlist QtSensors5::SensorGesture::gestures
    Set this property to a list of the gestures that the application is interested in detecting.
    This property cannot be changed while the element is enabled.

    The properties validGestures and invalidGestures will be set as appropriate immediately.
    To determine all available getures on the system please use the
    \l {QtSensors5::SensorGesture::availableGestures} {availableGestures} property.

    \sa {QtSensorGestures Plugins}
*/
QStringList QSensor2Gesture::gestures() const
{
    return _gestures;
}

void QSensor2Gesture::setGestures(const QStringList& value)
{
    if (_gestures == value)
        return;

    if (!_init && enabled()){
        qWarning() << "Cannot change gestures while running.";
        return;
    }
    _gestures.clear();
    _gestures = value;
    createGesture();
    emit gesturesChanged();
}


/*!
    \qmlproperty stringlist QtSensors5::SensorGesture::validGestures
    This property holds the requested gestures that were found on the system.
*/
QStringList QSensor2Gesture::validGestures() const
{
    if (_sensorGesture)
        return _sensorGesture->validIds();
    return QStringList();
}

/*!
    \qmlproperty stringlist QtSensors5::SensorGesture::invalidGestures
    This property holds the requested gestures that were not found on the system.
*/
QStringList QSensor2Gesture::invalidGestures() const
{
    if (_sensorGesture)
        return _sensorGesture->invalidIds();
    return QStringList();
}

/*!
    \qmlproperty bool QtSensors5::SensorGesture::enabled
    This property can be used to activate or deactivate the sensor gesture.
    Default value is false;
    \sa {QtSensors5::SensorGesture::detected} {detected}
*/
bool QSensor2Gesture::enabled() const
{
    return _enabled;
}

void QSensor2Gesture::setEnabled(bool value)
{
    _enabled = value;
    if (_init)
        return;

    if (_enabled != _oldEnabled){
        _oldEnabled = _enabled;
        if (_sensorGesture){
            if (_enabled){
#ifdef LOGGESTURQMLAPI
                qDebug() << "start detection" << _gestureIds;
#endif
                _sensorGesture->startDetection();
            }
            else {
#ifdef LOGGESTURQMLAPI
                qDebug() << "stop detection" << _gestureIds;
#endif
                _sensorGesture->stopDetection();
            }
        }
        emit enabledChanged();
    }
}

/*!
    \qmlsignal QtSensors5::SensorGesture::detected(string gesture)
    This signal is emitted whenever a gesture is detected.
    The gesture parameter contains the gesture that was detected.
*/

/*
  private funtion implementation
*/
void QSensor2Gesture::deleteGesture()
{
    if (_sensorGesture){
        bool emitinvalidchange = !invalidGestures().isEmpty();
        bool emitvalidchange = !validGestures().isEmpty();

        if (_sensorGesture->isActive()) {
            _sensorGesture->stopDetection();
        }
        delete _sensorGesture;
        _sensorGesture = 0;

        if (emitinvalidchange){
            emit invalidGesturesChanged();
        }
        if (emitvalidchange){
            emit validGesturesChanged();
        }
    }
}

void QSensor2Gesture::createGesture()
{
    deleteGesture();
#ifdef LOGGESTURQMLAPI
    qDebug() << "Create Gestrues:";
    for (int i = 0; i < _gestures.count(); i++){
        qDebug() << " -" << _gestures[i];
    }
#endif
    _sensorGesture = new QSensorGesture(_gestures, this);
    if (!validGestures().isEmpty()){
        QObject::connect(_sensorGesture
                         , SIGNAL(detected(QString))
                         , this
                         , SIGNAL(detected(QString)));
        emit validGesturesChanged();
    }
    if (!invalidGestures().isEmpty())
        emit invalidGesturesChanged();
}

/*
  End of private funtion implementation
*/

QT_END_NAMESPACE