libandroidplugin added
[mardrone] / mardrone / imports / com / nokia / android.1.1 / ToolBar.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.1
43
44 Item {
45     id: root
46
47     property Item tools: null
48
49     // The transition type. One of the following:
50     //      set         an instantaneous change (default)
51     //      push        follows page stack push animation
52     //      pop         follows page stack pop animation
53     //      replace     follows page stack replace animation
54     property string transition: "set"
55
56     // Symbian specific API
57     property bool platformInverted: false
58
59     // Sets the tools with a transition.
60     function setTools(tools, transition) {
61         stateGroup.state = tools ? "" : "Hidden"
62         if (tools)
63             tools.height = root.height
64         priv.transition = transition
65         root.tools = tools
66     }
67
68     LayoutMirroring.enabled: false
69     LayoutMirroring.childrenInherit: true
70
71     implicitWidth: Math.max(50, screen.width)
72     implicitHeight: (screen.width < screen.height)
73         ? privateStyle.toolBarHeightPortrait
74         : privateStyle.toolBarHeightLandscape
75
76     BorderImage {
77         id: background
78         anchors.fill: parent
79         source: privateStyle.imagePath("qtg_fr_toolbar", root.platformInverted)
80         border { left: 20; top: 20; right: 20; bottom: 20 }
81     }
82
83     //Prevents mouse events from propagating to elements below the ToolBar
84     MouseArea {
85         anchors.fill: parent
86     }
87
88     Item {
89         id: container
90         anchors.fill: parent
91     }
92
93     onToolsChanged: {
94         priv.performTransition(priv.transition || transition)
95         priv.transition = undefined
96     }
97
98     Component {
99         id: containerComponent
100
101         Item {
102             id: item
103
104             anchors.fill: parent
105
106             // The states correspond to the different possible positions of the item.
107             state: "Hidden"
108
109             // The tools held by this item.
110             property Item tools: null
111             // The owner of the tools.
112             property Item owner: null
113
114             states: [
115
116                 // Active state
117                 State {
118                     name: ""
119                     PropertyChanges { target: item; visible: true; opacity: 1 }
120                 },
121
122                 // Start state for pop entry, end state for push exit.
123                 State {
124                     name: "Prev"
125                     PropertyChanges { target: item; opacity: 0.0 }
126                 },
127                 // Start state for push entry, end state for pop exit.
128                 State {
129                     name: "Next"
130                     PropertyChanges { target: item; opacity: 0.0 }
131                 },
132                 // Start state for replace entry.
133                 State {
134                     name: "Front"
135                     PropertyChanges { target: item; opacity: 0.0 }
136                 },
137                 // End state for replace exit.
138                 State {
139                     name: "Back"
140                     PropertyChanges { target: item; opacity: 0.0 }
141                 },
142                 // Inactive state.
143                 State {
144                     name: "Hidden"
145                     PropertyChanges { target: item; visible: false }
146                     StateChangeScript {
147                         script: {
148                             if (item.tools) {
149                                 // re-parent back to original owner
150                                 tools.visible = false
151                                 tools.parent = owner
152
153                                 // reset item
154                                 item.tools = item.owner = null
155                             }
156                         }
157                     }
158                 }
159             ]
160
161             transitions: [
162                 // Pop entry and push exit transition.
163                 Transition {
164                     from: "";  to: "Prev";  reversible: true
165                     SequentialAnimation {
166                         PropertyAnimation { properties: "opacity";  easing.type: Easing.InCubic;  duration: priv.transitionDuration / 2 }
167                         PauseAnimation { duration: priv.transitionDuration / 2 }
168                         ScriptAction { script: if (state == "Prev") state = "Hidden" }
169                     }
170                 },
171                 // Push entry and pop exit transition.
172                 Transition {
173                     from: "";  to: "Next";  reversible: true
174                     SequentialAnimation {
175                         PropertyAnimation { properties: "opacity";  easing.type: Easing.InCubic;  duration: priv.transitionDuration / 2 }
176                         PauseAnimation { duration: priv.transitionDuration / 2 }
177                         ScriptAction { script: if (state == "Next") state = "Hidden" }
178                     }
179                 },
180                 Transition {
181                     // Replace entry transition.
182                     from: "Front";  to: ""
183                     SequentialAnimation {
184                         PropertyAnimation { properties: "opacity";  easing.type: Easing.InOutExpo;  duration: priv.transitionDuration }
185                     }
186                 },
187                 Transition {
188                     // Replace exit transition.
189                     from: "";  to: "Back"
190                     SequentialAnimation {
191                         PropertyAnimation { properties: "opacity";  easing.type: Easing.InOutExpo;  duration: priv.transitionDuration }
192                         ScriptAction { script: if (state == "Back") state = "Hidden"  }
193                     }
194                 }
195             ]
196         }
197     }
198
199     QtObject {
200         id: priv
201
202         property Item currentComponent: null
203
204         // Alternating components used for transitions.
205         property Item compA: null
206         property Item compB: null
207
208         // The transition to perform next.
209         property variant transition
210
211         // Duration of transition animation (in ms)
212         property int transitionDuration: 500
213
214         // Performs a transition between tools in the toolbar.
215         function performTransition(transition) {
216             // lazily create components if they have not been created
217             if (!priv.currentComponent) {
218                 priv.compA = containerComponent.createObject(container)
219                 priv.compB = containerComponent.createObject(container)
220                 priv.currentComponent = priv.compB
221             }
222
223             // no transition if the tools are unchanged
224             if (priv.currentComponent.tools == tools)
225                 return
226
227             // select component states based on the transition animation
228             var transitions = {
229                 "set":      { "new": "",        "old": "Hidden" },
230                 "push":     { "new": "Next",    "old": "Prev" },
231                 "pop":      { "new": "Prev",    "old": "Next" },
232                 "replace":  { "new": "Front",   "old": "Back" }
233             }
234
235             var animation = transitions[transition]
236             // initialize the free component
237             var component = priv.currentComponent == priv.compA ? priv.compB : priv.compA
238             component.state = "Hidden"
239             if (tools) {
240                 component.tools = tools
241                 component.owner = tools.parent
242                 tools.parent = component
243                 tools.visible = true
244                 if (tools.layoutChildren != undefined && typeof tools.layoutChildren == 'function' )
245                     tools.layoutChildren()
246             }
247
248             // perform transition
249             priv.currentComponent.state = animation["old"]
250             if (tools) {
251                 component.state = animation["new"]
252                 component.state = ""
253             }
254             priv.currentComponent = component
255         }
256     }
257
258     StateGroup {
259         id: stateGroup
260         states: [
261             // Inactive state.
262             State {
263                 name: "Hidden"
264                 PropertyChanges { target: root;  height: 0;  opacity: 0.0 }
265             }
266         ]
267
268         transitions: [
269             // Transition between active and inactive states.
270             Transition {
271                 from: "";  to: "Hidden";  reversible: true
272                 SequentialAnimation {
273                     PropertyAnimation { properties: "opacity";  easing.type: Easing.InOutExpo;  duration: priv.transitionDuration }
274                     PropertyAnimation { properties: "height";  duration: 10 }
275                 }
276             }
277         ]
278     }
279 }