components for android added
[mardrone] / mardrone / imports / com / nokia / extras / TimePickerDialog.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 com.nokia.meego 1.0
43 import com.nokia.extras 1.1
44 import "constants.js" as C
45 import "TumblerIndexHelper.js" as TH
46
47 /*
48    Class: TimePickerDialog
49    Dialog that shows a time picker.
50 */
51
52 Dialog {
53     id: root
54
55     /*
56      * Property: titleText
57      * [string] If not null, it will be used as the title text for the dialog.
58      *          If further customization is needed, use property title instead
59      */
60     property alias titleText: title.text
61
62     /*
63      * Property: hour
64      * [int] The displayed hour (in 24h format).
65      */
66     property int hour: 0
67
68     /*
69      * Property: minute
70      * [int] The displayed minute.
71      */
72     property int minute: 0
73
74     /*
75      * Property: second
76      * [int] The displayed second.
77      */
78     property int second: 0
79
80     /*
81      * Property: fields
82      * [int=DateTime.All] Sets if the time picker should show hours, minutes,
83      *                          and/or seconds.
84      *                          (DateTime.Hours, DateTime.Minutes,
85      *                          DateTime.Seconds, DateTime.All)
86      */
87     property int fields: DateTime.All
88
89     /*
90      * Property: hourMode
91      * [int=DateTime.TwentyFourHours] Sets if the time picker should show time in 24-hour clock
92      *                          or 12-hour clock format.
93      *                          (DateTime.TwentyFourHours, DateTime.TwelveHours)
94      */
95     property int hourMode: dateTime.hourMode() //DateTime.TwentyFourHours
96
97     /*
98      * Property: mode24Hour
99      * [bool=false] Sets if the time picker should show time in military time (24h).
100      */
101     property bool mode24Hour
102
103     /*
104      * Property: showSeconds
105      * [bool=true] Sets if the time picker should show seconds. Modifying this value
106      *             after initialization will uninitialize everything.
107      */
108     property bool showSeconds
109
110     /*
111      * Property: acceptButtonText
112      * [string] Optional, the button text for the accept button.
113      */
114     property alias acceptButtonText: acceptButton.text
115
116     /*
117      * Property: rejectButtonText
118      * [string] Optional, the button text for the reject button.
119      */
120     property alias rejectButtonText: rejectButton.text
121
122     // TODO do not dismiss the dialog when empty area is clicked
123     style: DialogStyle {
124         titleBarHeight: 48
125         leftMargin: screen.currentOrientation == Screen.Portrait || screen.currentOrientation == Screen.PortraitInverted ? 16 : 215
126         rightMargin: screen.currentOrientation == Screen.Portrait || screen.currentOrientation == Screen.PortraitInverted ? 16 : 215
127         centered: true
128     }
129     title: Text {
130         id: title
131         objectName: "title"
132         visible: text.length > 0
133         color: theme.selectionColor
134         font { pixelSize: 32; family: C.FONT_FAMILY_BOLD }
135         elide: Text.ElideRight
136     }
137     content: Item {
138         height: 300
139         width: parent.width
140
141         Tumbler {
142             id: tumbler
143
144             columns: [hourColumn, minuteColumn, secondColumn, meridiemColumn]
145             height: 300
146             privateDelayInit: true
147
148             TumblerColumn {
149                 id: hourColumn
150                 items: ListModel {
151                     id: hourList
152                 }
153                 label: "HR"
154                 selectedIndex: root.hour - ((root.hourMode == DateTime.TwelveHours && root.hour > 11) ? 12 : 0)
155                 visible: fields & DateTime.Hours
156             }
157
158             TumblerColumn {
159                 id: minuteColumn
160                 items: ListModel {
161                     id: minuteList
162                 }
163                 label: "MIN"
164                 selectedIndex: root.minute
165                 visible: fields & DateTime.Minutes
166             }
167
168             TumblerColumn {
169                 id: secondColumn
170                 items: ListModel {
171                     id: secondList
172                 }
173                 label: "SEC"
174                 selectedIndex: root.second
175                 visible: fields & DateTime.Seconds
176             }
177
178             TumblerColumn {
179                 id: meridiemColumn
180                 items: ListModel {
181                     id: meridiemList
182                 }
183                 selectedIndex: root.hour > 11 ? 1: 0
184                 visible: root.hourMode == DateTime.TwelveHours
185                 privateLoopAround: false
186             }
187         }
188     }
189     buttons: Row {
190         height: 56
191         anchors.horizontalCenter: parent.horizontalCenter
192         spacing: 6
193         Button {
194             id: acceptButton
195             onClicked: accept()
196             width: (root.width / 2) - 3
197             style: ButtonStyle { inverted: true }
198         }
199         Button {
200             id: rejectButton
201             onClicked: reject()
202             width: (root.width / 2) - 3
203             style: ButtonStyle { inverted: true }
204         }
205     }
206     onMode24HourChanged: {
207         console.log("The property 'mode24Hour' from TimePickerDialog is deprecated.  Please use 'hourMode' instead.")
208         root.hourMode = mode24Hour == true ? DateTime.TwentyFourHours : DateTime.TwelveHours
209     }
210     onShowSecondsChanged: {
211         console.log("The property 'showSeconds' from TimePickerDialog is deprecated.  Please use 'fields' instead.")
212         root.fields = showSeconds == true ? DateTime.All : DateTime.Hours | DateTime.Minutes
213     }
214     onStatusChanged: {
215         if (status == DialogStatus.Opening) {
216             TH.saveIndex(tumbler);
217             if (!internal.initialised)
218                 internal.initializeDataModels();
219         }
220     }
221     onAccepted: {
222         tumbler.privateForceUpdate();
223         if (root.hourMode == DateTime.TwelveHours)
224             root.hour = hourColumn.selectedIndex + (meridiemColumn.selectedIndex > 0 ? 12 : 0);
225         else
226             root.hour = hourColumn.selectedIndex;
227         root.minute = minuteColumn.selectedIndex;
228         root.second = secondColumn.selectedIndex;
229     }
230     onRejected: {
231         TH.restoreIndex(tumbler);
232     }
233     onHourModeChanged: {
234         hourList.clear();
235         var tmp = hourColumn.selectedIndex;
236         if (root.hourMode == DateTime.TwentyFourHours) {
237             tmp = (root.hour < 12 ? tmp : tmp + 12)
238             for (var i = 0; i < 24; ++i)
239                 hourList.append({"value" : (i < 10 ? "0" : "") + i});
240         } else {
241             tmp = (root.hour < 12 ? tmp : tmp - 12)
242             hourList.append({"value" : 12 + ""});
243             for (var i = 1; i < 12; ++i)
244                 hourList.append({"value" : i + ""});
245         }
246         hourColumn.selectedIndex = -1;
247         hourColumn.selectedIndex = tmp;
248     }
249     onHourChanged: {
250         internal.validateTime()
251         hourColumn.selectedIndex = root.hour - ((root.hourMode == DateTime.TwelveHours && root.hour > 11) ? 12 : 0)
252         meridiemColumn.selectedIndex = root.hour > 11 ? 1: 0
253     }
254     onMinuteChanged: {
255         internal.validateTime()
256         minuteColumn.selectedIndex = root.minute
257     }
258     onSecondChanged: {
259         internal.validateTime()
260         secondColumn.selectedIndex = root.second
261     }
262
263     QtObject {
264         id: internal
265
266         property variant initialised: false
267
268         function initializeDataModels() {
269             if (root.hourMode == DateTime.TwelveHours) {
270                 hourList.append({"value" : 12 + ""});
271                 for (var i = 1; i < 12; ++i)
272                     hourList.append({"value" : i + ""});
273             }
274             for (var i = 0; i < 60; ++i) {
275                 minuteList.append({"value" : (i < 10 ? "0" : "") + i });
276                 secondList.append({"value" : (i < 10 ? "0" : "") + i });
277             }
278             meridiemList.append({"value" : dateTime.amText()});
279             meridiemList.append({"value" : dateTime.pmText()});
280
281             tumbler.privateInitialize();
282             internal.initialised = true;
283         }
284         
285         function validateTime() {
286             root.hour = Math.max(0, Math.min(23, root.hour))
287             root.minute = Math.max(0, Math.min(59, root.minute))
288             root.second = Math.max(0, Math.min(59, root.second))
289         }
290     }
291 }