libandroidplugin added
[mardrone] / mardrone / imports / com / nokia / android.1.1 / Dialog.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 alias title: titleItem.children
48     property alias content: contentItem.children
49     property alias buttons: buttonItem.children
50     property alias visualParent: dialog.visualParent
51     property alias status: dialog.status
52
53     // Symbian specific API
54     property bool platformInverted: false
55     // read-only
56     property int platformContentMaximumWidth: dialog.width
57     // read-only
58     property int platformContentMaximumHeight:
59         dialog.maxHeight() - titleBar.height - buttonItem.height
60
61     property alias privateTitleHeight: titleBar.height
62     property alias privateButtonsHeight: buttonItem.height
63
64     signal accepted
65     signal rejected
66     signal clickedOutside
67
68     function open() {
69         dialog.open()
70         dialog.focus = true
71     }
72
73     function accept() {
74         if (status == DialogStatus.Open) {
75             dialog.close()
76             accepted()
77         }
78     }
79
80     function reject() {
81         if (status == DialogStatus.Open) {
82             dialog.close()
83             rejected()
84         }
85     }
86
87     function close() {
88         dialog.close()
89     }
90
91     visible: false
92
93     Popup {
94         id: dialog
95
96         function defaultHeight() {
97             if (root.height > 0)
98                 return root.height
99             else
100                 return titleBar.height + contentItem.childrenRect.height + buttonItem.height
101         }
102
103         function defaultWidth() {
104             return root.width > 0 ? root.width : maxWidth()
105         }
106
107         function maxWidth() {
108             return Math.min(screen.width - 2 * platformStyle.paddingMedium, privateStyle.dialogMaxSize)
109         }
110
111         function maxHeight() {
112             return inputContext.visible
113                     ? Math.min(privateStyle.dialogMaxSize, screen.height-inputContext.height)
114                     : Math.min(privateStyle.dialogMaxSize, screen.height-2*platformStyle.paddingMedium);
115         }
116
117         function minWidth() {
118             return Math.min(screen.displayWidth, screen.displayHeight) - 2 * platformStyle.paddingMedium
119         }
120
121         function minHeight() {
122             return inputContext.visible
123                     ? Math.min(privateStyle.dialogMinSize, screen.height-inputContext.height)
124                     : Math.min(privateStyle.dialogMinSize, screen.height-2*platformStyle.paddingMedium);
125         }
126
127         function updateSize() {
128             width = Math.max(Math.min(defaultWidth(), maxWidth()), minWidth())
129             height = Math.max(Math.min(defaultHeight(), maxHeight()), minHeight())
130         }
131
132         Connections { target: inputContext; onVisibleChanged: updateTimer.start() }
133         Connections { target: screen; onHeightChanged: updateTimer.start(); onWidthChanged: updateTimer.start() }
134         Connections { target: root; onHeightChanged: updateTimer.start(); onWidthChanged: updateTimer.start() }
135
136         Component.onCompleted: updateTimer.start()
137
138         Timer {
139             id: updateTimer
140             interval: 1; repeat: false
141             onTriggered: dialog.updateSize()
142         }
143
144         onFaderClicked: root.clickedOutside()
145
146         width: 0 // Defined in updateSize()
147         height: 0 // Defined in updateSize()
148
149         state: "Hidden"
150         visible: true
151         anchors.centerIn: parent
152         animationDuration: 250
153         platformInverted: root.platformInverted
154
155         // Consume all key events that are not processed by children
156         Keys.onPressed: event.accepted = true
157         Keys.onReleased: event.accepted = true
158
159         BorderImage {
160             source: privateStyle.imagePath("qtg_fr_popup", root.platformInverted)
161             border { left: 20; top: 20; right: 20; bottom: 20 }
162             anchors.fill: parent
163         }
164
165         Item {
166             id: titleBar
167
168             height: platformStyle.graphicSizeSmall + 2 * platformStyle.paddingLarge
169             anchors {
170                 top: parent.top
171                 left: parent.left
172                 right: parent.right
173             }
174
175             BorderImage {
176                 source: privateStyle.imagePath("qtg_fr_popup_heading", root.platformInverted)
177                 border { left: 40; top: 0; right: 40; bottom: 0 }
178                 anchors.fill: parent
179             }
180
181             Item {
182                 id: titleItem
183
184                 clip: true
185                 anchors {
186                     fill: parent
187                 }
188             }
189         }
190
191         Item {
192             id: contentItem
193
194             clip: true
195             anchors {
196                 top: titleBar.bottom
197                 left: parent.left
198                 right: parent.right
199                 bottom: buttonItem.top
200             }
201         }
202
203         Item {
204             id: buttonItem
205
206             height: childrenRect.height
207             clip: true
208             anchors {
209                 left: parent.left
210                 right: parent.right
211                 bottom: parent.bottom
212             }
213         }
214
215         states: [
216             State {
217                 name: "Visible"
218                 when: status == DialogStatus.Opening || status == DialogStatus.Open
219                 PropertyChanges { target: dialog; opacity: 1.0; scale: 1; }
220             },
221             State {
222                 name: "Hidden"
223                 when: status == DialogStatus.Closing || status == DialogStatus.Closed
224                 PropertyChanges { target: dialog; opacity: 0.0; scale: 0.9; }
225             }
226         ]
227
228         transitions: [
229             Transition {
230                 from: "Visible"; to: "Hidden"
231                 SequentialAnimation {
232                     ScriptAction {script: status = DialogStatus.Closing }
233                     ParallelAnimation {
234                         NumberAnimation {
235                             properties: "opacity"
236                             duration: dialog.animationDuration
237                             easing.type: Easing.Linear
238                         }
239                         NumberAnimation {
240                             property: "scale"
241                             duration: dialog.animationDuration
242                             easing.type: Easing.InQuad
243                         }
244                     }
245                     ScriptAction {script: status = DialogStatus.Closed }
246                 }
247             },
248             Transition {
249                 from: "Hidden"; to: "Visible"
250                 SequentialAnimation {
251                     ScriptAction { script: status = DialogStatus.Opening }
252                     PropertyAction { target: dialog; property: "visible"; value: true}
253                     ParallelAnimation {
254                         NumberAnimation {
255                             properties: "opacity"
256                             duration: dialog.animationDuration
257                             easing.type: Easing.Linear
258                         }
259                         NumberAnimation {
260                             property: "scale"
261                             duration: dialog.animationDuration
262                             easing.type: Easing.OutQuad
263                         }
264                     }
265                     ScriptAction { script: status = DialogStatus.Open }
266                 }
267             }
268         ]
269     }
270 }