| 1 |
/* |
| 2 |
* Copyright 2013 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU 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 |
import QtQuick 2.4 |
| 18 |
import QtQuick.Window 2.2 |
| 19 |
import Lomiri.Components |
| 20 |
|
| 21 |
/*! |
| 22 |
\qmltype OrientationHelper |
| 23 |
\inqmlmodule Lomiri.Components |
| 24 |
\ingroup lomiri |
| 25 |
\brief The OrientationHelper automatically rotates its children following the |
| 26 |
orientation of the device. |
| 27 |
|
| 28 |
Any Item placed inside an Orientation Helper will be automatically rotated |
| 29 |
following the orientation of the device. |
| 30 |
|
| 31 |
Note that OrientationHelper is always filling its parent (anchors.parent: fill). |
| 32 |
|
| 33 |
Example: |
| 34 |
\qml |
| 35 |
Item { |
| 36 |
OrientationHelper { |
| 37 |
Label { |
| 38 |
text: "Automatically rotated" |
| 39 |
} |
| 40 |
Button { |
| 41 |
text: "Automatically rotated" |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
\endqml |
| 46 |
*/ |
| 47 |
Item { |
| 48 |
id: orientationHelper |
| 49 |
|
| 50 |
/*! |
| 51 |
Sets whether it will be automatically rotating when the device is. |
| 52 |
|
| 53 |
The default value is true. |
| 54 |
|
| 55 |
\qmlproperty bool automaticOrientation |
| 56 |
*/ |
| 57 |
property bool automaticOrientation: true |
| 58 |
/*! |
| 59 |
Sets whether the rotation transition is performed. |
| 60 |
|
| 61 |
The default value is true. |
| 62 |
|
| 63 |
\qmlproperty bool transitionEnabled |
| 64 |
*/ |
| 65 |
property bool transitionEnabled: true |
| 66 |
/*! |
| 67 |
Exposes whether the orientationTransition is running. |
| 68 |
|
| 69 |
\qmlproperty alias rotating |
| 70 |
*/ |
| 71 |
readonly property bool rotating: d.transitioning |
| 72 |
|
| 73 |
/*! |
| 74 |
\qmlproperty int __orientationAngle |
| 75 |
\deprecated |
| 76 |
|
| 77 |
Use orientationAngle instead. |
| 78 |
*/ |
| 79 |
property alias __orientationAngle: orientationHelper.orientationAngle |
| 80 |
|
| 81 |
/*! |
| 82 |
\qmlproperty int orientationAngle |
| 83 |
|
| 84 |
Calculates the current orientation angle. |
| 85 |
*/ |
| 86 |
property int orientationAngle: automaticOrientation ? Screen.angleBetween(Screen.primaryOrientation, Screen.orientation) : 0 |
| 87 |
// Screen.primaryOrientation and Screen.orientation can change one right after the other, |
| 88 |
// causing orientationAngle to momentarily change. To avoid responding to such |
| 89 |
// intermediate states, wait for its value to stabilize before rotating to it. |
| 90 |
onOrientationAngleChanged: { automaticOrientation ? stableOrientationAngleTimer.restart() : d.tryUpdateState(); } |
| 91 |
|
| 92 |
/*! |
| 93 |
The property holds if the OrientationHelper should automatically resize the |
| 94 |
contents when the input method appears |
| 95 |
|
| 96 |
The default value is false. |
| 97 |
*/ |
| 98 |
property bool anchorToKeyboard: false |
| 99 |
|
| 100 |
x: parent ? (parent.width - width) / 2 : 0 |
| 101 |
y: parent ? (d.availableParentHeight - height) / 2 : 0 |
| 102 |
width: parent ? (d.flipDimensions ? d.availableParentHeight : parent.width) : 0 |
| 103 |
height: parent ? (d.flipDimensions ? parent.width : d.availableParentHeight) : 0 |
| 104 |
|
| 105 |
/* |
| 106 |
The attached property Screen.orientation is only valid inside Item or |
| 107 |
derived components. Inside Object it evaluates to 0 with no error. |
| 108 |
Also be aware that some apps eg. webbrowser-app set window.contentOrientation |
| 109 |
and thus can hide failure to update it from this code. |
| 110 |
See http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick-window2-screen.html |
| 111 |
*/ |
| 112 |
Item { |
| 113 |
id: d |
| 114 |
|
| 115 |
property real availableParentHeight: { |
| 116 |
if (!orientationHelper.parent) |
| 117 |
return 0; |
| 118 |
|
| 119 |
var availableHeight = orientationHelper.parent.height; |
| 120 |
if (d.stateAngle === 0 && LomiriApplication.inputMethod.visible && anchorToKeyboard) |
| 121 |
availableHeight -= LomiriApplication.inputMethod.keyboardRectangle.height; |
| 122 |
return availableHeight; |
| 123 |
} |
| 124 |
|
| 125 |
/*! |
| 126 |
'window' is defined by QML between startup and showing on the screen. |
| 127 |
There is no signal for when it becomes available and re-declaring it is not safe. |
| 128 |
|
| 129 |
http://qt-project.org/doc/qt-5.1/qtqml/qml-qtqml2-qt.html |
| 130 |
http://qt-project.org/doc/qt-5.1/qtquick/qmlmodule-qtquick-window2-qtquick-window-2.html |
| 131 |
*/ |
| 132 |
property bool windowActive: window !== null |
| 133 |
|
| 134 |
/*! |
| 135 |
Report the current orientation of the application via QWindow::contentOrientation. |
| 136 |
http://qt-project.org/doc/qt-5.0/qtgui/qwindow.html#contentOrientation-prop |
| 137 |
*/ |
| 138 |
function applyOrientation() { |
| 139 |
if (window) { |
| 140 |
window.contentOrientation = Screen.orientation; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
onWindowActiveChanged: { |
| 145 |
if (automaticOrientation) { |
| 146 |
applyOrientation(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
Timer { |
| 151 |
id: stableOrientationAngleTimer |
| 152 |
interval: 250 |
| 153 |
onTriggered: { d.tryUpdateState(); } |
| 154 |
} |
| 155 |
|
| 156 |
readonly property bool flipDimensions: d.stateAngle === 90 || d.stateAngle === 270 |
| 157 |
|
| 158 |
property bool transitioning: false |
| 159 |
onTransitioningChanged: { d.tryUpdateState(); } |
| 160 |
|
| 161 |
// NB: Using a binding would cause QML to detect a |
| 162 |
// binding loop and break the binding, thus the imperative code to update the state. |
| 163 |
function tryUpdateState() { |
| 164 |
if (!d.transitioning) { |
| 165 |
d.oldAngle = d.stateAngle; |
| 166 |
d.stateAngle = orientationHelper.orientationAngle; |
| 167 |
d.state = orientationHelper.orientationAngle.toString(); |
| 168 |
d.applyOrientation(); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
state: "0" |
| 173 |
states: [ |
| 174 |
State { name: "0" }, |
| 175 |
State { name: "90" }, |
| 176 |
State { name: "180" }, |
| 177 |
State { name: "270" } |
| 178 |
] |
| 179 |
property int stateAngle: 0 |
| 180 |
property int oldAngle: 0 |
| 181 |
|
| 182 |
transitions: [ |
| 183 |
Transition { |
| 184 |
id: animatedTransition |
| 185 |
enabled: orientationHelper.transitionEnabled |
| 186 |
SequentialAnimation { |
| 187 |
|
| 188 |
ScriptAction { script: { d.transitioning = true; } } |
| 189 |
RotationAnimator { |
| 190 |
target: orientationHelper |
| 191 |
duration: LomiriAnimation.FastDuration |
| 192 |
easing: LomiriAnimation.StandardEasing |
| 193 |
direction: RotationAnimation.Shortest |
| 194 |
from: d.oldAngle |
| 195 |
to: d.stateAngle |
| 196 |
} |
| 197 |
ScriptAction { script: { d.transitioning = false; } } |
| 198 |
} |
| 199 |
}, |
| 200 |
Transition { |
| 201 |
enabled: !orientationHelper.transitionEnabled |
| 202 |
ScriptAction { script: { orientationHelper.rotation = d.stateAngle; } } |
| 203 |
} |
| 204 |
] |
| 205 |
} |
| 206 |
} |