Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / examples / declarative / screenorientation / screenorientation.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 examples of the Qt Toolkit.
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.0
42 import "Core"
43 import "Core/screenorientation.js" as ScreenOrientation
44
45 Rectangle {
46     id: window
47     width: 360  
48     height: 640
49     color: "white"
50
51     Rectangle {
52         id: main
53         clip: true
54         property variant selectedOrientation: Orientation.UnknownOrientation
55         property variant activeOrientation: selectedOrientation == Orientation.UnknownOrientation ? runtime.orientation : selectedOrientation
56         state: "orientation " + activeOrientation
57         property bool inPortrait: (activeOrientation == Orientation.Portrait || activeOrientation == Orientation.PortraitInverted);
58
59         // rotation correction for landscape devices like N900
60         property bool landscapeWindow: window.width > window.height 
61         property variant rotationDelta: landscapeWindow ? -90 : 0
62         rotation: rotationDelta
63
64         // initial state is portrait
65         property real baseWidth: landscapeWindow ? window.height-10 : window.width-10
66         property real baseHeight: landscapeWindow ? window.width-10 : window.height-10
67
68         width: baseWidth
69         height: baseHeight
70         anchors.centerIn: parent
71
72         color: "black"
73         gradient: Gradient {
74             GradientStop { position: 0.0; color: Qt.rgba(0.5,0.5,0.5,0.5) }
75             GradientStop { position: 0.8; color: "black" }
76             GradientStop { position: 1.0; color: "black" }
77         }
78         Item {
79             id: bubbles
80             property bool rising: false
81             anchors.fill: parent
82             property variant gravityPoint: ScreenOrientation.calculateGravityPoint(main.activeOrientation, runtime.orientation)
83             Repeater {
84                 model: 24
85                 Bubble {
86                     rising: bubbles.rising
87                     verticalRise: ScreenOrientation.parallel(main.activeOrientation, runtime.orientation)
88                     xAttractor: parent.gravityPoint.x
89                     yAttractor: parent.gravityPoint.y
90                 }
91             }
92             Component.onCompleted: bubbles.rising = true;
93         }
94
95         Column {
96             width: centeredText.width
97             anchors.verticalCenter: parent.verticalCenter
98             anchors.horizontalCenter: parent.horizontalCenter 
99             anchors.verticalCenterOffset: 30
100             Text {
101                 text: "Orientation"
102                 color: "white"
103                 font.pixelSize: 22
104                 anchors.horizontalCenter: parent.horizontalCenter
105             }
106             Text {
107                 id: centeredText
108                 text: ScreenOrientation.printOrientation(main.activeOrientation)
109                 color: "white"
110                 font.pixelSize: 40
111                 anchors.horizontalCenter: parent.horizontalCenter
112             }
113             Text {
114                 text: "sensor: " + ScreenOrientation.printOrientation(runtime.orientation)
115                 color: "white"
116                 font.pixelSize: 14
117                 anchors.horizontalCenter: parent.horizontalCenter
118             }
119         }
120         Flow {
121             anchors.top: parent.top
122             anchors.left: parent.left
123             anchors.right: parent.right
124             anchors.margins: 10
125             spacing: 4
126             Button {
127                 width: main.inPortrait ? (parent.width-4)/2 : (parent.width-8)/3
128                 text: "Portrait"
129                 onClicked: main.selectedOrientation = Orientation.Portrait
130                 toggled: main.selectedOrientation == Orientation.Portrait
131             }
132             Button {
133                 width: main.inPortrait ? (parent.width-4)/2 : (parent.width-8)/3
134                 text: "Portrait inverted"
135                 onClicked: main.selectedOrientation = Orientation.PortraitInverted
136                 toggled: main.selectedOrientation == Orientation.PortraitInverted
137             }
138             Button {
139                 width: main.inPortrait ? (parent.width-4)/2 : (parent.width-8)/3
140                 text: "Landscape"
141                 onClicked: main.selectedOrientation = Orientation.Landscape
142                 toggled: main.selectedOrientation == Orientation.Landscape
143             }
144             Button {
145                 width: main.inPortrait ? (parent.width-4)/2 : (parent.width-8)/3
146                 text: "Landscape inverted"
147                 onClicked: main.selectedOrientation = Orientation.LandscapeInverted
148                 toggled: main.selectedOrientation == Orientation.LandscapeInverted
149             }
150             Button {
151                 width: main.inPortrait ? parent.width : 2*(parent.width-2)/3
152                 text: "From runtime.orientation"
153                 onClicked: main.selectedOrientation = Orientation.UnknownOrientation
154                 toggled: main.selectedOrientation == Orientation.UnknownOrientation
155             }
156         }
157         states: [
158             State {
159                 name: "orientation " + Orientation.Landscape
160                 PropertyChanges { 
161                     target: main
162                     rotation: ScreenOrientation.getAngle(Orientation.Landscape)+rotationDelta
163                     width: baseHeight
164                     height: baseWidth
165                 }
166             },
167             State {
168                 name: "orientation " + Orientation.PortraitInverted
169                 PropertyChanges { 
170                     target: main
171                     rotation: ScreenOrientation.getAngle(Orientation.PortraitInverted)+rotationDelta
172                     width: baseWidth
173                     height: baseHeight
174                 }
175             },
176             State {
177                 name: "orientation " + Orientation.LandscapeInverted
178                 PropertyChanges { 
179                     target: main
180                     rotation: ScreenOrientation.getAngle(Orientation.LandscapeInverted)+rotationDelta
181                     width: baseHeight
182                     height: baseWidth
183                 }
184             }
185         ]
186         transitions: Transition {
187             ParallelAnimation {
188                 RotationAnimation { 
189                     direction: RotationAnimation.Shortest
190                     duration: 300
191                     easing.type: Easing.InOutQuint 
192                 }
193                 NumberAnimation { 
194                     properties: "x,y,width,height"
195                     duration: 300
196                     easing.type: Easing.InOutQuint 
197                 }
198             }
199         }
200     }
201 }