Merge branch 'qml' of https://vcs.maemo.org/git/situare into qml
[situare] / src / qmlui / rotation.cpp
1 #include "rotation.h"
2 #include <QOrientationReading>
3 #include <QOrientationSensor>
4 #include <QTimer>
5
6 QTM_USE_NAMESPACE
7
8 Rotation::Rotation(QObject *parent) :
9     QObject(parent)
10 {
11     startReading();
12 }
13
14 void Rotation::onReadingChanged()
15 {
16     if (m_sensor) {
17         switch (m_sensor->reading()->orientation()) {
18         case QOrientationReading::TopUp:
19         case QOrientationReading::TopDown:
20             if( m_orientation == "potrait") {
21                 m_orientation = "";
22                 emit orientationChanged();
23             }
24             break;
25         case QOrientationReading::LeftUp:
26         case QOrientationReading::RightUp:
27             if (m_orientation == "") {
28                 m_orientation = "potrait";
29                 emit orientationChanged();
30             }
31             break;
32         default:
33             break;
34         }
35
36         delete m_sensor;
37         m_sensor = 0;
38
39         QTimer::singleShot(1000, this, SLOT(startReading()));
40     }
41 }
42
43 const QString& Rotation::orientation() const
44 {
45     return m_orientation;
46 }
47
48 void Rotation::startReading()
49 {
50     m_sensor = new QOrientationSensor(this);
51     connect(m_sensor, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));
52     m_sensor->start();
53 }