1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the Qt Components project.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
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
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
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."
39 ****************************************************************************/
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
48 Class: TimePickerDialog
49 Dialog that shows a time picker.
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
60 property alias titleText: title.text
64 * [int] The displayed hour (in 24h format).
70 * [int] The displayed minute.
72 property int minute: 0
76 * [int] The displayed second.
78 property int second: 0
82 * [int=DateTime.All] Sets if the time picker should show hours, minutes,
84 * (DateTime.Hours, DateTime.Minutes,
85 * DateTime.Seconds, DateTime.All)
87 property int fields: DateTime.All
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)
95 property int hourMode: dateTime.hourMode() //DateTime.TwentyFourHours
98 * Property: mode24Hour
99 * [bool=false] Sets if the time picker should show time in military time (24h).
101 property bool mode24Hour
104 * Property: showSeconds
105 * [bool=true] Sets if the time picker should show seconds. Modifying this value
106 * after initialization will uninitialize everything.
108 property bool showSeconds
111 * Property: acceptButtonText
112 * [string] Optional, the button text for the accept button.
114 property alias acceptButtonText: acceptButton.text
117 * Property: rejectButtonText
118 * [string] Optional, the button text for the reject button.
120 property alias rejectButtonText: rejectButton.text
122 // TODO do not dismiss the dialog when empty area is clicked
125 leftMargin: screen.currentOrientation == Screen.Portrait || screen.currentOrientation == Screen.PortraitInverted ? 16 : 215
126 rightMargin: screen.currentOrientation == Screen.Portrait || screen.currentOrientation == Screen.PortraitInverted ? 16 : 215
132 visible: text.length > 0
133 color: theme.selectionColor
134 font { pixelSize: 32; family: C.FONT_FAMILY_BOLD }
135 elide: Text.ElideRight
144 columns: [hourColumn, minuteColumn, secondColumn, meridiemColumn]
146 privateDelayInit: true
154 selectedIndex: root.hour - ((root.hourMode == DateTime.TwelveHours && root.hour > 11) ? 12 : 0)
155 visible: fields & DateTime.Hours
164 selectedIndex: root.minute
165 visible: fields & DateTime.Minutes
174 selectedIndex: root.second
175 visible: fields & DateTime.Seconds
183 selectedIndex: root.hour > 11 ? 1: 0
184 visible: root.hourMode == DateTime.TwelveHours
185 privateLoopAround: false
191 anchors.horizontalCenter: parent.horizontalCenter
196 width: (root.width / 2) - 3
197 style: ButtonStyle { inverted: true }
202 width: (root.width / 2) - 3
203 style: ButtonStyle { inverted: true }
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
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
215 if (status == DialogStatus.Opening) {
216 TH.saveIndex(tumbler);
217 if (!internal.initialised)
218 internal.initializeDataModels();
222 tumbler.privateForceUpdate();
223 if (root.hourMode == DateTime.TwelveHours)
224 root.hour = hourColumn.selectedIndex + (meridiemColumn.selectedIndex > 0 ? 12 : 0);
226 root.hour = hourColumn.selectedIndex;
227 root.minute = minuteColumn.selectedIndex;
228 root.second = secondColumn.selectedIndex;
231 TH.restoreIndex(tumbler);
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});
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 + ""});
246 hourColumn.selectedIndex = -1;
247 hourColumn.selectedIndex = tmp;
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
255 internal.validateTime()
256 minuteColumn.selectedIndex = root.minute
259 internal.validateTime()
260 secondColumn.selectedIndex = root.second
266 property variant initialised: false
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 + ""});
274 for (var i = 0; i < 60; ++i) {
275 minuteList.append({"value" : (i < 10 ? "0" : "") + i });
276 secondList.append({"value" : (i < 10 ? "0" : "") + i });
278 meridiemList.append({"value" : dateTime.amText()});
279 meridiemList.append({"value" : dateTime.pmText()});
281 tumbler.privateInitialize();
282 internal.initialised = true;
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))