diff options
author | Kevin Ottens <kevin.ottens.qnx@kdab.com> | 2012-05-24 11:38:43 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-06-19 02:26:25 +0200 |
commit | a42fd9d05a8f8db8a79fc8da920fa917188b8e9b (patch) | |
tree | 6a048369935054a6913b84f73fa394b5d11f2e83 /src | |
parent | 440bc188c8f0f427ef979b8e9cff8d39fdaa8af7 (diff) | |
download | qtsensors-a42fd9d05a8f8db8a79fc8da920fa917188b8e9b.tar.gz |
BlackBerry backend: Automatic Axis Remapping
Add a backend specific property named "automaticAxisRemapping" to force
the sensor to modify its coordinate system based on the device native
orientation.
So usable from C++ using 'setProperty("automaticAxisRemapping", true)'
or from QML using 'property var automaticAxisRemapping: true'.
Note it requires QPlatformScreen::nativeOrientation() private API to
find out the device native orientation.
Change-Id: If4c5622fd42c74a55fe6d36966d7f589e612c117
Reviewed-by: Adam Parco <aparco@rim.com>
Reviewed-by: Lincoln Ramsay <lincoln.ramsay@nokia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/sensors/blackberry/bbrotationsensor.cpp | 70 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/bbrotationsensor.h | 9 | ||||
-rw-r--r-- | src/plugins/sensors/blackberry/blackberry.pro | 2 |
3 files changed, 78 insertions, 3 deletions
diff --git a/src/plugins/sensors/blackberry/bbrotationsensor.cpp b/src/plugins/sensors/blackberry/bbrotationsensor.cpp index fe34ea4..28dc49c 100644 --- a/src/plugins/sensors/blackberry/bbrotationsensor.cpp +++ b/src/plugins/sensors/blackberry/bbrotationsensor.cpp @@ -42,10 +42,16 @@ #include "bbutil.h" #include <QtCore/qmath.h> +#include <QGuiApplication> +#include <QScreen> +#include <qpa/qplatformscreen.h> BbRotationSensor::BbRotationSensor(QSensor *sensor) - : BbSensorBackend<QRotationReading>(devicePath(), SENSOR_TYPE_ROTATION_MATRIX, sensor) + : BbSensorBackend<QRotationReading>(devicePath(), SENSOR_TYPE_ROTATION_MATRIX, sensor), + m_orientation(Qt::PrimaryOrientation), + m_nativeOrientation(Qt::PrimaryOrientation) { + updateOrientation(); setDescription(QLatin1String("Device rotation in degrees")); } @@ -95,16 +101,46 @@ static void matrixToEulerZXY(const float matrix[3*3], } } +static void remapMatrix(const float inputMatrix[3*3], + const float mappingMatrix[4], + float outputMatrix[3*3]) +{ + int i,j,k; + + for (i = 0; i < 3; i++) { + for (j = 0; j < 2; j++) { //only goto 2 because last column stays unchanged + + outputMatrix[i*3+j] = 0; + + for (k = 0; k < 2; k++) { //only goto 2 because we know rotation matrix is zero in bottom row + outputMatrix[i*3+j] += inputMatrix[i*3+k] * mappingMatrix[k*2+j]; + } + } + + outputMatrix[i*3+2] = inputMatrix[i*3+2]; + } +} + + bool BbRotationSensor::updateReadingFromEvent(const sensor_event_t &event, QRotationReading *reading) { // sensor_event_t has euler angles for a Z-Y'-X'' system, but the QtMobility API // uses Z-X'-Y''. // So extract the euler angles using the Z-X'-Y'' system from the matrix. float xRad, yRad, zRad; - matrixToEulerZXY(event.rotation_matrix, xRad, yRad, zRad); + + if (isAutoAxisRemappingEnabled() && m_orientation!=m_nativeOrientation) { + float mappedRotationMatrix[3*3]; + remapMatrix(event.rotation_matrix, m_mappingMatrix, mappedRotationMatrix); + matrixToEulerZXY(mappedRotationMatrix, xRad, yRad, zRad); + } else { + matrixToEulerZXY(event.rotation_matrix, xRad, yRad, zRad); + } + reading->setX(radiansToDegrees(xRad)); reading->setY(radiansToDegrees(yRad)); reading->setZ(radiansToDegrees(zRad)); + return true; } @@ -112,3 +148,33 @@ qreal BbRotationSensor::convertValue(float bbValueInRad) { return radiansToDegrees(bbValueInRad); } + +bool BbRotationSensor::isAutoAxisRemappingEnabled() const +{ + return sensor()->property("automaticAxisRemapping").toBool(); +} + +bool BbRotationSensor::eventFilter(QObject *object, QEvent *event) +{ + if (object == QCoreApplication::instance() && event->type() == QEvent::OrientationChange) + updateOrientation(); + + return BbSensorBackend<QRotationReading>::eventFilter(object, event); +} + +void BbRotationSensor::updateOrientation() +{ + QScreen *screen = QGuiApplication::primaryScreen(); + if (screen) { + const QPlatformScreen * const platformScreen = screen->handle(); + m_nativeOrientation = platformScreen->nativeOrientation(); + m_orientation = screen->orientation(); + + const int rotationAngle = screen->angleBetween(m_nativeOrientation, m_orientation); + + m_mappingMatrix[0] = cos(rotationAngle*M_PI/180); + m_mappingMatrix[1] = sin(rotationAngle*M_PI/180); + m_mappingMatrix[2] = -sin(rotationAngle*M_PI/180); + m_mappingMatrix[3] = cos(rotationAngle*M_PI/180); + } +} diff --git a/src/plugins/sensors/blackberry/bbrotationsensor.h b/src/plugins/sensors/blackberry/bbrotationsensor.h index 475ad22..d2fd8a0 100644 --- a/src/plugins/sensors/blackberry/bbrotationsensor.h +++ b/src/plugins/sensors/blackberry/bbrotationsensor.h @@ -58,7 +58,16 @@ protected: bool addDefaultRange(); qreal convertValue(float bbValue); bool updateReadingFromEvent(const sensor_event_t &event, QRotationReading *reading); + bool isAutoAxisRemappingEnabled() const; + bool eventFilter(QObject *object, QEvent *event); + +private: + void updateOrientation(); + + Qt::ScreenOrientation m_orientation; + Qt::ScreenOrientation m_nativeOrientation; + float m_mappingMatrix[4]; }; #endif diff --git a/src/plugins/sensors/blackberry/blackberry.pro b/src/plugins/sensors/blackberry/blackberry.pro index c2efb36..1f81e06 100644 --- a/src/plugins/sensors/blackberry/blackberry.pro +++ b/src/plugins/sensors/blackberry/blackberry.pro @@ -1,7 +1,7 @@ load(qt_module) TARGET = qtsensors_blackberry -QT = sensors core gui +QT = sensors core gui gui-private DEFINES += QT_NO_CAST_FROM_ASCII QT_NO_CAST_TO_ASCII load(qt_plugin) |