improved desktop UI
[mardrone] / mardrone / imports / com / nokia / meego / Sheet.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
47     width: parent ? parent.width : 0
48     height: parent ? parent.height : 0
49
50     property alias title: titleBar.children 
51     property alias content: contentField.children
52     property alias buttons: buttonRow.children
53     property Item visualParent
54     property int status: DialogStatus.Closed
55
56     property alias acceptButtonText: acceptButton.text
57     property alias rejectButtonText: rejectButton.text
58
59     property alias acceptButton: acceptButton
60     property alias rejectButton: rejectButton
61
62     signal accepted
63     signal rejected
64
65     property QtObject platformStyle: SheetStyle {}
66
67     //Deprecated, TODO Remove this on w13
68     property alias style: root.platformStyle
69
70     function reject() {
71         close();
72         rejected();
73     }
74
75     function accept() {
76         close();
77         accepted();
78     }
79
80     visible: status != DialogStatus.Closed;
81     
82     function open() {
83         parent = visualParent || __findParent();
84         sheet.state = "";
85     }
86
87     function close() {
88         sheet.state = "closed";
89     }
90
91     function __findParent() {
92         var next = parent;
93         while (next && next.parent
94                && next.objectName != "appWindowContent"
95                && next.objectName != "windowContent") {
96             next = next.parent;
97         }
98         return next;
99     }
100
101     function getButton(name) {
102         for (var i=0; i<buttons.length; ++i) {
103             if (buttons[i].objectName == name)
104                 return buttons[i];
105         }
106         return undefined;
107     }
108
109     MouseArea {
110         id: blockMouseInput
111         anchors.fill: parent
112     }
113     
114     Item {
115         id: sheet
116
117         //when the sheet is part of a page do nothing
118         //when the sheet is a direct child of a PageStackWindow, consider the status bar
119         property int statusBarOffset: (typeof __isPage != "undefined") ? 0
120                                      : (typeof __statusBarHeight == "undefined") ? 0
121                                      :  __statusBarHeight
122         
123         width: parent.width
124         height: parent.height - statusBarOffset
125
126         y: statusBarOffset
127
128         clip: true
129         
130         property int transitionDurationIn: 300
131         property int transitionDurationOut: 450
132         
133         state: "closed"
134         
135         function transitionStarted() {
136             status = (state == "closed") ? DialogStatus.Closing : DialogStatus.Opening;
137         }
138         
139         function transitionEnded() {
140             status = (state == "closed") ? DialogStatus.Closed : DialogStatus.Open;
141         }
142         
143         states: [
144             // Closed state.
145             State {
146                 name: "closed"
147                 PropertyChanges { target: sheet; y: height; }
148             }
149         ]
150
151         transitions: [
152             // Transition between open and closed states.
153             Transition {
154                 from: ""; to: "closed"; reversible: false
155                 SequentialAnimation {
156                     ScriptAction { script: if (sheet.state == "closed") { sheet.transitionStarted(); } else { sheet.transitionEnded(); } }
157                     PropertyAnimation { properties: "y"; easing.type: Easing.InOutQuint; duration: sheet.transitionDurationOut }
158                     ScriptAction { script: if (sheet.state == "closed") { sheet.transitionEnded(); } else { sheet.transitionStarted(); } }
159                 }                
160             },
161             Transition {
162                 from: "closed"; to: ""; reversible: false
163                 SequentialAnimation {
164                     ScriptAction { script: if (sheet.state == "") { sheet.transitionStarted(); } else { sheet.transitionEnded(); } }
165                     PropertyAnimation { properties: "y"; easing.type: Easing.OutQuint; duration: sheet.transitionDurationIn }
166                     ScriptAction { script: if (sheet.state == "") { sheet.transitionEnded(); } else { sheet.transitionStarted(); } }
167                 }
168             }
169         ]
170         
171         BorderImage {
172             source: platformStyle.background
173             width: parent.width
174             anchors.top: header.bottom
175             anchors.bottom: parent.bottom
176             Item {
177                 id: contentField
178                 anchors.fill: parent
179             }
180         }
181
182         Item {
183             id: header
184             width: parent.width
185             height: headerBackground.height
186             BorderImage {
187                 id: headerBackground
188                 border {
189                     left: platformStyle.headerBackgroundMarginLeft
190                     right: platformStyle.headerBackgroundMarginRight
191                     top: platformStyle.headerBackgroundMarginTop
192                     bottom: platformStyle.headerBackgroundMarginBottom
193                 }
194                 source: platformStyle.headerBackground
195                 width: header.width
196             }
197             Item {
198                 id: buttonRow
199                 anchors.fill: parent
200                 SheetButton {
201                     id: rejectButton
202                     objectName: "rejectButton"
203                     anchors.left: parent.left
204                     anchors.leftMargin: root.platformStyle.rejectButtonLeftMargin
205                     anchors.verticalCenter: parent.verticalCenter
206                     visible: text != ""
207                     onClicked: close()
208                 }
209                 SheetButton {
210                     id: acceptButton
211                     objectName: "acceptButton"
212                     anchors.right: parent.right
213                     anchors.rightMargin: root.platformStyle.acceptButtonRightMargin
214                     anchors.verticalCenter: parent.verticalCenter
215                     platformStyle: SheetButtonAccentStyle { }
216                     visible: text != ""     
217                     onClicked: close()
218                 }
219                 Component.onCompleted: {
220                     acceptButton.clicked.connect(accepted)
221                     rejectButton.clicked.connect(rejected)
222                 }
223             }
224             Item {
225                 id: titleBar
226                 anchors.fill: parent
227             }
228         }
229     }
230 }