Add (better) portrait mode support. This commit will temporarily break
[groove] / qmaemo5rotator.cpp
diff --git a/qmaemo5rotator.cpp b/qmaemo5rotator.cpp
new file mode 100644 (file)
index 0000000..78cbe6a
--- /dev/null
@@ -0,0 +1,106 @@
+
+#include "qmaemo5rotator.h"
+#if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
+#include <mce/dbus-names.h>
+#include <mce/mode-names.h>
+#include <QtDBus/QDBusConnection>
+#include <QtDBus/QDBusMessage>
+#endif
+
+QMaemo5Rotator::QMaemo5Rotator(RotationBehavior behavior, QWidget *parent)
+    : QObject(parent),
+    isSetUp(false)
+{
+    setCurrentBehavior(behavior);
+}
+
+QMaemo5Rotator::~QMaemo5Rotator()
+{
+#if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
+    QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
+#endif
+}
+
+const QMaemo5Rotator::RotationBehavior QMaemo5Rotator::currentBehavior()
+{
+    return _currentBehavior;
+}
+
+const QMaemo5Rotator::Orientation QMaemo5Rotator::currentOrientation()
+{
+    return _currentOrientation;
+}
+
+void QMaemo5Rotator::setCurrentBehavior(QMaemo5Rotator::RotationBehavior value)
+{
+#if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
+    if (value == _currentBehavior && isSetUp)
+        return;
+
+    isSetUp = true;
+    _currentBehavior = value;
+
+    if (value == QMaemo5Rotator::AutomaticBehavior)
+    {
+        QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_ENABLE_REQ));
+        QDBusConnection::systemBus().connect(QString(), MCE_SIGNAL_PATH, MCE_SIGNAL_IF, MCE_DEVICE_ORIENTATION_SIG, this, SLOT(on_orientation_changed(QString)));
+    }
+    else
+    {
+        QDBusConnection::systemBus().call(QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
+
+        if (value == QMaemo5Rotator::PortraitBehavior)
+        {
+            setCurrentOrientation(QMaemo5Rotator::PortraitOrientation);
+        }
+        else
+        {
+            setCurrentOrientation(QMaemo5Rotator::LandscapeOrientation);
+        }
+    }
+#endif
+}
+
+void QMaemo5Rotator::setCurrentOrientation(QMaemo5Rotator::Orientation value)
+{
+    _currentOrientation = value;
+    QWidget *par = (QWidget*)parent();
+
+    switch (value)
+    {
+    case QMaemo5Rotator::PortraitOrientation:
+        if (par != NULL)
+        {
+#if defined(Q_WS_MAEMO_5)
+            par->setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
+            par->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
+#endif
+        }
+        orientationChanged(QMaemo5Rotator::PortraitOrientation);
+        break;
+    case QMaemo5Rotator::LandscapeOrientation:
+        if (par != NULL)
+        {
+#if defined(Q_WS_MAEMO_5)
+            par->setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
+            par->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
+#endif
+        }
+        orientationChanged(QMaemo5Rotator::LandscapeOrientation);
+
+        break;
+    }
+}
+
+void QMaemo5Rotator::on_orientation_changed(const QString& newOrientation)
+{
+    if (newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT) || newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT_INVERTED))
+    {
+        setCurrentOrientation(QMaemo5Rotator::PortraitOrientation);
+    }
+    else
+    {
+        setCurrentOrientation(QMaemo5Rotator::LandscapeOrientation);
+    }
+    QApplication::desktop()->updateGeometry();
+}