summaryrefslogtreecommitdiffstats
path: root/src/sensors/qorientablesensorbase.cpp
blob: 84eaf55d081661f1d6f8cbdef9cbfd6f23d97d14 (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
/****************************************************************************
**
** Copyright (C) 2012 Research In Motion <blackberry-qt@qnx.com>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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, Digia gives you certain additional
** rights.  These rights are described in the Digia 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qorientablesensorbase.h"
#include <QObject>

QTM_BEGIN_NAMESPACE

class QOrientableSensorBasePrivate
{
public:
    QOrientableSensorBasePrivate()
        : axesOrientationMode(QOrientableSensorBase::FixedOrientation),
          currentOrientation(0),
          userOrientation(0)
    {
    }

    QOrientableSensorBase::AxesOrientationMode axesOrientationMode;
    int currentOrientation;
    int userOrientation;
};

/*!
    \class QOrientableSensorBase
    \ingroup sensors_main
    \inmodule QtSensors

    \brief The QOrientableSensorBase class is the base class for all sensors that react to screen
           orientation changes.

    All sensors based on this class support changing the reporting of the reading values based
    on the orientation of the screen. This is controlled by the axesOrientationMode property, which
    controls how the orientation affects the reading values.

    In the default mode, QOrientableSensorBase::FixedOrientation, the reading values remain
    unaffected by the orientation. In the QOrientableSensorBase::AutomaticOrientation mode, the reading
    values are automatically rotated by taking the current screen orientation into account. And
    finally, in the QOrientableSensorBase::UserOrientation mode, the reading values are rotated
    according to a user-specified orientation.

    The functionality of this is only available if it is supported by the backend, which can be
    checked by calling QSensor::isFeatureSupported() with the QSensor::AxesOrientation flag.

    The orientation here is always the screen orientation, not the device orientation. The orientation
    values range from 0 to 270 degrees. The orientation is applied in clockwise direction.
    Note that when rotating a device by 90 degrees counter-clockwise, the screen orientation compensates for
    that by rotating 90 degrees to the right.
*/

/*!
    \enum QOrientableSensorBase::AxesOrientationMode

    Describes how reading values are affected by the screen orientation.

    \value FixedOrientation No automatic rotation is applied to the reading values.

    \value AutomaticOrientation The reading values are automatically rotated based on the screen
                                orientation.

    \value UserOrientation The reading values are rotated based on the angle of the userOrientation property.

    \sa QOrientableSensorBase::axesOrientationMode
*/

/*!
   Constructs a QOrientableSensorBase.
 */
QOrientableSensorBase::QOrientableSensorBase()
    : d(new QOrientableSensorBasePrivate)
{
}

/*!
   Destroys a QOrientableSensorBase.
 */
QOrientableSensorBase::~QOrientableSensorBase()
{
    delete d;
    d = 0;
}

/*!
    \property QOrientableSensorBase::axesOrientationMode
    \brief The mode that affects how the screen orientation changes reading values.

    When set to FixedOrientation, which is the default mode, no automatic rotation is applied to
    the reading. This is the only mode available for backends that do not support the
    QSensor::AxesOrientation feature.

    When set to AutomaticOrientation, the reading values are automatically rotated when the
    screen orientation changes. In effect, the screen orientation is canceled out.

    As an example, assume the device is rotated by 180 degrees and therefore the screen orientation
    also is rotated by 180 degrees from the native orientation. Without automatic axes orientation,
    the reading values would now be changed: Both the X and the Y values would be negated, forcing
    an application developer to manually cancel out the negation in application code. Automatic
    axes orientation does this automatically, in this mode the X and Y values would be the same as
    with the default screen orientation.

    This automatic rotation of the axes is handy is some usecases, for example in a bubble level
    application that measures how level a surface is by looking at the X axis value of an
    accelerometer. When the device and screen orientation change by 90 degrees, an application
    developer does not need to change anything, he can continue using the X axis value even though
    the device is rotated. Without automatic axes orientation, the application developer would need
    to look at the Y values instead, thereby adding code to the application that reads from a
    different axis depending on the screen orientation.

    The UserOrientation mode is quite similar to AutomaticOrientation, only that the screen orientation
    is manually controlled instead of automatically determined. The angle of the userOrientation
    property is then used for rotating the reading values.

    Since the rotation of the reading values is based on the screen orientation, Z values will never
    change, as the Z axis is perpendicular to the screen.
    As screen orientation changes in 90 degree steps, rotating the reading values is also done in
    steps of 90 degrees.
*/

void QOrientableSensorBase::setAxesOrientationMode(QOrientableSensorBase::AxesOrientationMode axesOrientationMode)
{
    if (d->axesOrientationMode != axesOrientationMode) {
        d->axesOrientationMode = axesOrientationMode;
        emit axesOrientationModeChanged(axesOrientationMode);
    }
}

QOrientableSensorBase::AxesOrientationMode QOrientableSensorBase::axesOrientationMode() const
{
    return d->axesOrientationMode;
}

/*!
    \property QOrientableSensorBase::currentOrientation
    \brief The current orientation that is used for rotating the reading values.

    This might not be the same as the screen orientation. For example, in the FixedOrientation mode,
    the reading values are not rotated, and therefore the property is 0.

    In the UserOrientation mode, the readings are rotated based on the userOrientation property,
    and therefore this property is equal to the userOrientation property.

    In the AutomaticOrientation mode, the readings are rotated based on the screen orientation,
    and therefore this property will be equal to the current screen orientation.

    This property is set by the backend.
*/

int QOrientableSensorBase::currentOrientation() const
{
    return d->currentOrientation;
}

/*!
   Sets the current screen orientation to \a currentOrientation. This is to be called from the
   backend whenever the screen orientation or the userOrientation property changes.
 */
void QOrientableSensorBase::setCurrentOrientation(int currentOrientation)
{
    if (d->currentOrientation != currentOrientation) {
        d->currentOrientation = currentOrientation;
        emit currentOrientationChanged(currentOrientation);
    }
}

/*!
    \property QOrientableSensorBase::userOrientation
    \brief The angle used for rotating the reading values in the UserOrientation mode.

    When the axesOrientationMode property is set to UserOrientation, the angle for rotating the
    reading values is taken from this property. In other modes, the property has no effect.

    The default is 0. The only valid values are 0, 90, 180 and 270, as those are the only possible
    screen orientations.
*/

void QOrientableSensorBase::setUserOrientation(int userOrientation)
{
    if (d->userOrientation != userOrientation) {
        d->userOrientation = userOrientation;
        emit userOrientationChanged(userOrientation);
    }
}

int QOrientableSensorBase::userOrientation() const
{
    return d->userOrientation;
}

QTM_END_NAMESPACE