improved desktop UI
[mardrone] / mardrone / imports / com / nokia / meego / Window.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Components project.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 1.1
42 import "." 1.0
43
44 Item {
45     id: root
46     width: screen.displayWidth
47     height: screen.displayHeight
48
49     property alias color: background.color
50
51     default property alias content: windowContent.data
52
53     // Read only property true if window is in portrait
54     property alias inPortrait: window.portrait
55
56     signal orientationChangeAboutToStart
57     signal orientationChangeStarted
58     signal orientationChangeFinished
59
60     Rectangle {
61         id: background
62         anchors.fill: parent
63         color: "black"
64     }
65
66     Item {
67         id: window
68         property bool portrait
69
70         width: window.portrait ? screen.displayHeight : screen.displayWidth
71         height: window.portrait ? screen.displayWidth : screen.displayHeight
72
73         anchors.centerIn : parent
74         transform: Rotation { id: windowRotation;
75                                 origin.x: 0;
76                                 origin.y: 0;
77                                 angle: 0
78                             }
79
80         Item {
81             id: windowContent
82             width: parent.width
83             height: parent.height - heightDelta
84
85             // Used for resizing windowContent when virtual keyboard appears
86             property int heightDelta: 0
87
88             objectName: "windowContent"
89             clip: true
90
91             Connections {
92                 id: inputContextConnection
93                 target: inputContext
94                 onSoftwareInputPanelVisibleChanged: inputContextConnection.updateWindowContentHeightDelta();
95
96                 onSoftwareInputPanelRectChanged: inputContextConnection.updateWindowContentHeightDelta();
97
98                 function updateWindowContentHeightDelta() {
99                     if(inputContext.customSoftwareInputPanelVisible)
100                         return
101
102                     if (root.inPortrait)
103                         windowContent.heightDelta = inputContext.softwareInputPanelRect.width
104                     else
105                         windowContent.heightDelta = inputContext.softwareInputPanelRect.height
106                 }
107             }
108         }
109
110         SoftwareInputPanel {
111             id: softwareInputPanel
112             active: inputContext.customSoftwareInputPanelVisible
113             anchors.bottom: parent.bottom
114
115             onHeightChanged: {
116                 windowContent.heightDelta = height
117             }
118
119             Loader {
120                 id: softwareInputPanelLoader
121                 width: parent.width
122                 sourceComponent: inputContext.customSoftwareInputPanelComponent
123             }
124         }
125
126         Snapshot {
127             id: snapshot
128             anchors.top: parent.top
129             anchors.left: parent.left
130             width: screen.displayWidth
131             height: screen.displayHeight
132             snapshotWidth: screen.displayWidth
133             snapshotHeight: screen.displayHeight
134             opacity: 0
135             transform: Rotation { id: snapshotRotation;
136                                   origin.x: screen.displayHeight / 2; origin.y: screen.displayHeight / 2;
137                                   angle: 0 }
138         }
139
140         state: screen.orientationString
141
142         states: [
143             State {
144                 name: "Landscape"
145                 PropertyChanges { target: window; rotation: 0; portrait: false; }
146                 PropertyChanges { target: windowRotation;
147                                   origin.x: root.height / 2;
148                                   origin.y: root.height / 2; }
149                 PropertyChanges { target: snapshot; anchors.leftMargin: 0; anchors.topMargin: 0 }
150             },
151             State {
152                 name: "Portrait"
153                 PropertyChanges { target: window; rotation: 270; portrait: true; }
154                 PropertyChanges { target: windowRotation;
155                                   origin.x: root.height - root.width / 2;
156                                   origin.y: root.width / 2; }
157                 PropertyChanges { target: snapshot; anchors.leftMargin: 0; anchors.topMargin: 0 }
158             },
159             State {
160                 name: "LandscapeInverted"
161                 PropertyChanges { target: window; rotation: 180; portrait: false; }
162                 PropertyChanges { target: windowRotation;
163                                   origin.x: root.height / 2;
164                                   origin.y: root.height / 2; }
165                 PropertyChanges { target: snapshot; anchors.leftMargin: 374; anchors.topMargin: 0 }
166             },
167             State {
168                 name: "PortraitInverted"
169                 PropertyChanges { target: window; rotation: 90; portrait: true; }
170                 PropertyChanges { target: windowRotation;
171                                   origin.x: root.height - root.width / 2;
172                                   origin.y: root.width / 2; }
173                 PropertyChanges { target: snapshot; anchors.leftMargin: 0; anchors.topMargin: 374 }
174             }
175         ]
176
177         transitions: [
178         Transition {
179             // use this transition when sip is visible
180             from: (inputContext.softwareInputPanelVisible ?  "*" : "disabled")
181             to:   (inputContext.softwareInputPanelVisible ?  "*" : "disabled")
182             PropertyAction { target: window; properties: "rotation"; }
183             ScriptAction {
184                 script: {
185                     root.orientationChangeAboutToStart();
186                     platformWindow.startSipOrientationChange(window.rotation);
187                     // note : we should really connect these signals to MInputMethodState
188                     // signals so that they are emitted at the appropriate time
189                     // but there aren't any
190                     root.orientationChangeStarted();
191                     root.orientationChangeFinished();
192                 }
193             }
194         },
195         Transition {
196             // use this transition when sip is not visible
197             from: (screen.minimized ? "disabled" : (inputContext.softwareInputPanelVisible ? "disabled" : "*"))
198             to:   (screen.minimized ? "disabled" : (inputContext.softwareInputPanelVisible ? "disabled" : "*"))
199             SequentialAnimation {
200                 alwaysRunToEnd: true
201
202                 ScriptAction {
203                     script: {
204                         snapshot.take();
205                         snapshot.opacity = 1.0;
206                         snapshotRotation.angle = -window.rotation;
207                         snapshot.smooth = false; // Quick & coarse rotation consistent with MTF
208                         platformWindow.animating = true;
209                         root.orientationChangeAboutToStart();
210                     }
211                 }
212                 PropertyAction { target: window; properties: "portrait"; }
213                 ScriptAction {
214                     script: {
215                         windowContent.opacity = 0.0;
216                         root.orientationChangeStarted();
217                     }
218                 }
219                 ParallelAnimation {
220                     NumberAnimation { target: windowContent; property: "opacity";
221                                       to: 1.0; easing.type: Easing.InOutExpo; duration: 600; }
222                     NumberAnimation { target: snapshot; property: "opacity";
223                                       to: 0.0; easing.type: Easing.InOutExpo; duration: 600; }
224                     PropertyAction { target: windowRotation; properties: "origin.x"; }
225                     PropertyAction { target: windowRotation; properties: "origin.y"; }
226                     RotationAnimation { target: windowRotation; property: "angle";
227                                         from: -screen.rotationDirection * 90;
228                                         to: 0;
229                                         direction: RotationAnimation.Shortest;
230                                         easing.type: Easing.InOutExpo; duration: 600; }
231                 }
232                 ScriptAction {
233                     script: {
234                         windowRotation.angle = 0
235                         snapshot.free();
236                         root.orientationChangeFinished();
237                         platformWindow.animating = false;
238                     }
239                 }
240             }
241         }
242         ]
243
244         focus: true
245         Keys.onReleased: {
246             if (event.key == Qt.Key_I && event.modifiers == Qt.AltModifier) {
247                 theme.inverted = !theme.inverted;
248             }
249             if (event.key == Qt.Key_E && event.modifiers == Qt.AltModifier) {
250                 if(screen.currentOrientation == Screen.Landscape) {
251                     screen.allowedOrientations = Screen.Portrait;
252                 } else if(screen.currentOrientation == Screen.Portrait) {
253                     screen.allowedOrientations = Screen.LandscapeInverted;
254                 } else if(screen.currentOrientation == Screen.LandscapeInverted) {
255                     screen.allowedOrientations = Screen.PortraitInverted;
256                 } else if(screen.currentOrientation == Screen.PortraitInverted) {
257                     screen.allowedOrientations = Screen.Landscape;
258                 }
259             }
260             if (event.key == Qt.Key_E && event.modifiers == Qt.ControlModifier ) {
261                 if(screen.currentOrientation == Screen.Portrait) {
262                     screen.allowedOrientations = Screen.Landscape;
263                 } else if(screen.currentOrientation == Screen.LandscapeInverted) {
264                     screen.allowedOrientations = Screen.Portrait;
265                 } else if(screen.currentOrientation == Screen.PortraitInverted) {
266                     screen.allowedOrientations = Screen.LandscapeInverted;
267                 } else if(screen.currentOrientation == Screen.Landscape) {
268                     screen.allowedOrientations = Screen.PortraitInverted;
269                 }
270             }
271         }
272     }
273 }