on xenial help sign in
Basic shell-controlled orientation support with added orientation lock
commit04b59db444378b626d207fd1a2f41c24fb893521 authorGerry Boland <gerry.boland@canonical.com> dateWed 27 Aug 2014, 00:33 (12 years ago) parentdf2d076
9 files changed+196−4
plugins/Unity/Session/CMakeLists.txt+7 plugins/Unity/Session/orientationlock.cpp+74 plugins/Unity/Session/orientationlock.h+62 plugins/Unity/Session/plugin.cpp+8−4 qml/Shell.qml+18 qml/Stages/ApplicationWindow.qml+2 qml/Stages/PhoneStage.qml+10 qml/Stages/SpreadDelegate.qml+2 qml/Stages/TabletStage.qml+13
plugins/Unity/Session/CMakeLists.txt+7
@@ -1,10 +1,16 @@
1+include(FindPkgConfig)
2+
3+pkg_search_module(GIO REQUIRED gio-2.0)
4+
15include_directories(
26 ${CMAKE_CURRENT_SOURCE_DIR}
7+ ${GIO_INCLUDE_DIRS}
38)
49
510set(QMLSESSIONPLUGIN_SRC
611 plugin.cpp
712 dbusunitysessionservice.cpp
13+ orientationlock.cpp
814 )
915
1016add_library(UnitySession-qml MODULE
@@ -12,6 +18,7 @@ add_library(UnitySession-qml MODULE
1218 )
1319
1420qt5_use_modules(UnitySession-qml DBus Qml)
21+target_link_libraries(UnitySession-qml ${GIO_LDFLAGS})
1522
1623# export the qmldir and qmltypes files
1724add_unity8_plugin(Unity.Session 0.1 Unity/Session TARGETS UnitySession-qml)
plugins/Unity/Session/orientationlock.cppnew file+74
@@ -0,0 +1,74 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it under
5+ * the terms of the GNU Lesser General Public License version 3, as published by
6+ * the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but WITHOUT
9+ * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10+ * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+ * Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include "orientationlock.h"
18+
19+#include <QDBusConnection>
20+#include <QDBusInterface>
21+
22+OrientationLock::OrientationLock(QObject *parent)
23+ : QObject(parent)
24+ , m_enabled(false)
25+ , m_savedOrientation(Qt::PortraitOrientation)
26+{
27+ systemSettings = g_settings_new("com.ubuntu.touch.system");
28+ g_signal_connect(systemSettings, "changed::rotation-lock",
29+ G_CALLBACK(OrientationLock::onEnabledChangedProxy), this);
30+ m_enabled = g_settings_get_boolean(systemSettings, "rotation-lock");
31+}
32+
33+OrientationLock::~OrientationLock()
34+{
35+ g_signal_handlers_disconnect_by_data(systemSettings, this);
36+ g_object_unref(systemSettings);
37+}
38+
39+bool OrientationLock::enabled() const
40+{
41+ return m_enabled;
42+}
43+
44+Qt::ScreenOrientation OrientationLock::savedOrientation() const
45+{
46+ return m_savedOrientation;
47+}
48+
49+void OrientationLock::onEnabledChangedProxy(GSettings */*settings*/, const gchar */*key*/, gpointer data)
50+{
51+ OrientationLock* _this = static_cast<OrientationLock*>(data);
52+ _this->onEnabledChanged();
53+}
54+
55+void OrientationLock::onEnabledChanged()
56+{
57+ const bool enabled = g_settings_get_boolean(systemSettings, "rotation-lock");
58+ if (m_enabled != enabled) {
59+ m_enabled = enabled;
60+ Q_EMIT enabledChanged();
61+ }
62+}
63+
64+void OrientationLock::setSavedOrientation(const Qt::ScreenOrientation orientation)
65+{
66+ if (orientation == m_savedOrientation) {
67+ return;
68+ }
69+
70+ m_savedOrientation = orientation;
71+
72+ //TODO - save value with dbus to persist over sessions
73+ Q_EMIT savedOrientationChanged();
74+}
plugins/Unity/Session/orientationlock.hnew file+62
@@ -0,0 +1,62 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it under
5+ * the terms of the GNU Lesser General Public License version 3, as published by
6+ * the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but WITHOUT
9+ * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10+ * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+ * Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef ORIENTATIONLOCK_H
18+#define ORIENTATIONLOCK_H
19+
20+#include <gio/gio.h>
21+#include <QtCore/QObject>
22+#include <QtDBus/QDBusInterface>
23+
24+/**
25+ * @brief The OrientationLock class exports orientation lock related properties to QML
26+ * It has two properties:
27+ * - readonly boolean with the Orientation lock property state
28+ * - Qt::ScreenOrientation to save the locked orientation state across Sessions (only relevant if lock is true)
29+ */
30+class OrientationLock : public QObject
31+{
32+ Q_OBJECT
33+
34+ Q_PROPERTY(bool enabled READ enabled NOTIFY enabledChanged)
35+ Q_PROPERTY(Qt::ScreenOrientation savedOrientation READ savedOrientation WRITE setSavedOrientation
36+ NOTIFY savedOrientationChanged)
37+
38+public:
39+ explicit OrientationLock(QObject *parent = 0);
40+ ~OrientationLock();
41+
42+ bool enabled() const;
43+ Qt::ScreenOrientation savedOrientation() const;
44+ void setSavedOrientation(const Qt::ScreenOrientation orientation);
45+
46+Q_SIGNALS:
47+ void enabledChanged();
48+ void savedOrientationChanged();
49+
50+private Q_SLOTS:
51+ static void onEnabledChangedProxy(GSettings *settings, const gchar *key, gpointer data);
52+ void onEnabledChanged();
53+
54+private:
55+ QDBusInterface *dbusInterface;
56+ GSettings *systemSettings;
57+
58+ bool m_enabled;
59+ Qt::ScreenOrientation m_savedOrientation;
60+};
61+
62+#endif // ORIENTATIONLOCK_H
plugins/Unity/Session/plugin.cpp+8−4
@@ -18,23 +18,27 @@
1818
1919#include "plugin.h"
2020#include "dbusunitysessionservice.h"
21+#include "orientationlock.h"
2122
2223#include <QAbstractItemModel>
2324#include <QDBusConnection>
2425#include <QtQml/qqml.h>
2526
26−static QObject *dbusunitysessionservice_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
27+static QObject *dbusunitysessionservice_provider(QQmlEngine */*engine*/, QJSEngine */*jsEngine*/)
2728{
28− Q_UNUSED(engine)
29− Q_UNUSED(scriptEngine)
30−
3129 return new DBusUnitySessionService();
3230}
3331
32+static QObject *orientationlock_provider(QQmlEngine */*engine*/, QJSEngine */*jsEngine*/)
33+{
34+ return new OrientationLock();
35+}
36+
3437void SessionPlugin::registerTypes(const char *uri)
3538{
3639 qmlRegisterType<QAbstractItemModel>();
3740
3841 Q_ASSERT(uri == QLatin1String("Unity.Session"));
3942 qmlRegisterSingletonType<DBusUnitySessionService>(uri, 0, 1, "DBusUnitySessionService", dbusunitysessionservice_provider);
43+ qmlRegisterSingletonType<OrientationLock>(uri, 0, 1, "OrientationLock", orientationlock_provider);
4044}
qml/Shell.qml+18
@@ -15,6 +15,7 @@
1515 */
1616
1717import QtQuick 2.0
18+import QtQuick.Window 2.0
1819import AccountsService 0.1
1920import GSettings 1.0
2021import Unity.Application 0.1
@@ -167,6 +168,18 @@ Item {
167168 }
168169 }
169170
171+ property int orientation
172+ readonly property int deviceOrientation: Screen.angleBetween(Screen.primaryOrientation, Screen.orientation)
173+ onDeviceOrientationChanged: {
174+ if (!OrientationLock.enabled)
175+ orientation = Screen.orientation;
176+ }
177+ readonly property bool orientationLockEnabled: OrientationLock.enabled
178+ onOrientationLockEnabledChanged: {
179+ if (!OrientationLock.enabled)
180+ orientation = Screen.orientation;
181+ }
182+
170183 Loader {
171184 id: applicationsDisplayLoader
172185 anchors.fill: parent
@@ -204,6 +217,11 @@ Item {
204217 property: "inverseProgress"
205218 value: launcher.progress
206219 }
220+ Binding {
221+ target: applicationsDisplayLoader.item
222+ property: "orientation"
223+ value: stages.orientation
224+ }
207225 }
208226 }
209227
qml/Stages/ApplicationWindow.qml+2
@@ -27,6 +27,7 @@ Item {
2727 // to be set from outside
2828 property bool interactive: true
2929 property QtObject application
30+ property int orientation
3031
3132 QtObject {
3233 id: d
@@ -57,6 +58,7 @@ Item {
5758 property bool surfaceInitialized: false
5859 onSurfaceChanged: {
5960 if (surface) {
61+ surface.orientation = Qt.binding( function() { return root.orientation; } );
6062 surfaceInitTimer.start();
6163 } else {
6264 hadSurface = true;
qml/Stages/PhoneStage.qml+10
@@ -18,6 +18,7 @@ import QtQuick 2.0
1818import Ubuntu.Components 0.1
1919import Ubuntu.Gestures 0.1
2020import Unity.Application 0.1
21+import Unity.Session 0.1
2122import Utils 0.1
2223import "../Components"
2324
@@ -30,6 +31,7 @@ Rectangle {
3031 property bool interactive
3132 property bool spreadEnabled: true // If false, animations and right edge will be disabled
3233 property real inverseProgress: 0 // This is the progress for left edge drags, in pixels.
34+ property int orientation
3335
3436 color: "black"
3537
@@ -389,6 +391,14 @@ Rectangle {
389391 spreadView.closingIndex = index;
390392 ApplicationManager.stopApplication(ApplicationManager.get(index).appId);
391393 }
394+
395+ onInteractiveChanged: {
396+ if (interactive) {
397+ appDelegate.orientation = Qt.binding( function() { return root.orientation; } );
398+ } else {
399+ appDelegate.orientation = root.orientation; // breaks the binding intentionally
400+ }
401+ }
392402 }
393403 }
394404 }
qml/Stages/SpreadDelegate.qml+2
@@ -36,6 +36,7 @@ Item {
3636 property alias swipeToCloseEnabled: dragArea.enabled
3737 property bool closeable
3838 property alias application: appWindow.application
39+ property int orientation
3940
4041 Item {
4142 objectName: "appWindowWithShadow"
@@ -66,6 +67,7 @@ Item {
6667 }
6768
6869 interactive: root.interactive
70+ orientation: root.orientation
6971 }
7072 }
7173
qml/Stages/TabletStage.qml+13
@@ -34,6 +34,7 @@ Rectangle {
3434 property real maximizedAppTopMargin
3535 property bool interactive
3636 property real inverseProgress: 0 // This is the progress for left edge drags, in pixels.
37+ property int orientation
3738
3839 onInverseProgressChanged: {
3940 if (inverseProgress == 0 && priv.oldInverseProgress > 0) {
@@ -549,6 +550,18 @@ Rectangle {
549550 ApplicationManager.stopApplication(ApplicationManager.get(index).appId);
550551 }
551552
553+ onInteractiveChanged: {
554+ if (isInSideStage) {
555+ appDelegate.orientation = Qt.PortraitOrientation; // FIXME! Need Proper Rotation Support
556+ } else {
557+ if (interactive) {
558+ appDelegate.orientation = Qt.binding( function() { return root.orientation; } );
559+ } else {
560+ appDelegate.orientation = root.orientation; // breaks the binding intentionally
561+ }
562+ }
563+ }
564+
552565 EasingCurve {
553566 id: snappingCurve
554567 type: EasingCurve.Linear