Changed app to use Qt Mobility in screen rotation instead of default Qt screen rotation.
[jspeed] / src / orientation.cpp
diff --git a/src/orientation.cpp b/src/orientation.cpp
new file mode 100644 (file)
index 0000000..0452cf1
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * This file is part of jSpeed.
+ *
+ * jSpeed is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * jSpeed is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "orientation.h"
+
+Orientation::Orientation(QMainWindow* window): QtMobility::QOrientationSensor(window),
+current_(QtMobility::QOrientationReading::TopUp), orientations_(0), window_(window)
+{
+    connect(this, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));
+}
+
+void Orientation::setSupportedOrientations(int orientations)
+{
+    orientations_ = orientations;
+}
+
+void Orientation::update()
+{
+    using QtMobility::QOrientationReading;
+
+    OrientationName orientation = LANDSCAPE;
+
+    QOrientationReading::Orientation current = reading()->orientation();
+
+    switch(current)
+    {
+    case QOrientationReading::LeftUp:
+        orientation = PORTRAIT;
+        break;
+    case QOrientationReading::FaceUp:
+        if(current_ == QOrientationReading::LeftUp)
+        {
+            orientation = PORTRAIT;
+        }
+        else
+        {
+            orientation = LANDSCAPE;
+        }
+        break;
+    default:
+        orientation = LANDSCAPE;
+        break;
+    }
+
+    current_ = current;
+
+    if(orientations_ & orientation)
+    {
+        switch(orientation)
+        {
+        case LANDSCAPE:
+            window_->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
+            break;
+        case PORTRAIT:
+            window_->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
+            break;
+        }
+    }
+    else
+    {
+        if(orientations_ & LANDSCAPE)
+        {
+            window_->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
+        }
+        else if(orientations_ & PORTRAIT)
+        {
+            window_->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
+        }
+    }
+}
+
+void Orientation::onReadingChanged()
+{
+    if(reading()->orientation() != current_)
+    {
+        update();
+        emit changed();
+    }
+}