~nik90/ubuntu-clock-app/prep-new-design-part1

« back to all changes in this revision

Viewing changes to alarm/AddAlarmPage.qml

  • Committer: Nekhelesh Ramananthan
  • Date: 2014-02-10 13:55:59 UTC
  • mfrom: (330.1.2 trunk)
  • Revision ID: krnekhelesh@gmail.com-20140210135559-lbh7qctjpz1iwqz8
merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
 
53
53
    // Property to retrieve and store the time format setting of the user
54
54
    property string timeFormat: appSetting.contents.timeFormat
55
 
    property bool isMilitaryFormat: timeFormat === "24-hour" ? true : false
 
55
    property bool isMilitaryFormat: timeFormat === "24-hour"
56
56
 
57
57
    visible: false
58
58
    flickable: null
62
62
        if(setFocus)
63
63
            nameAlarm.forceActiveFocus()
64
64
 
65
 
        if (isNewAlarm)
 
65
        if (isNewAlarm){
66
66
            addAlarmPage.title = i18n.tr("Add Alarm")
 
67
            showDefaultTime()
 
68
            addAlarmFace.hours = Number(hourLabel.text) // Set hour hand
 
69
        }
67
70
        else {
68
71
            addAlarmPage.title = i18n.tr("Edit Alarm")
69
72
            readAlarm()
96
99
            return true
97
100
    }
98
101
 
 
102
    // Function to show default time when creating new alarm ("00:00" or "12:00")
 
103
    function showDefaultTime() {
 
104
        hourLabel.text = isMilitaryFormat ? "00" : "12"
 
105
        minuteLabel.text = "00"
 
106
    }
 
107
 
99
108
    // Function to set the correct alarm time depending on AM/PM setting
100
109
    function setAlarmTime(alarmTime) {
101
 
        if(Math.round(addAlarmFace.minutes) === 60)
102
 
            addAlarmFace.minutes = 0
 
110
        var hour = Number(hourLabel.text)
 
111
        var minute = Number(minuteLabel.text)
103
112
 
104
 
        if(isMilitaryFormat)
105
 
            alarmTime.setHours(Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat),Math.round(addAlarmFace.minutes), 0)
 
113
        if(isPm && !isMilitaryFormat && hourLabel.text != "12"){
 
114
            alarmTime.setHours(hour + 12, minute, 0)
 
115
        }
 
116
        else if(!isPm && !isMilitaryFormat && hourLabel.text == "12"){
 
117
            alarmTime.setHours(0, minute, 0)
 
118
        }
106
119
        else {
107
 
            if(!isPm) {
108
 
                if(Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat) === 0)
109
 
                    alarmTime.setHours(12,Math.round(addAlarmFace.minutes), 0)
110
 
                else
111
 
                    alarmTime.setHours(Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat),Math.round(addAlarmFace.minutes), 0)
112
 
            }
113
 
            else {
114
 
                if((Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat)+12) === 24 || Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat)+12 === 12)
115
 
                    alarmTime.setHours(0,Math.round(addAlarmFace.minutes), 0)
116
 
                else
117
 
                    alarmTime.setHours(Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat)+12,Math.round(addAlarmFace.minutes), 0)
118
 
            }
 
120
            alarmTime.setHours(hour, minute, 0)
119
121
        }
120
122
    }
121
123
 
142
144
        alarm.date = temp_alarm.date
143
145
 
144
146
        // Determining if the alarm read is set to trigger in the morning or evening
145
 
        isPm = Qt.formatTime(alarm.date, "hh AP").split(" ")[1] === "AM" ? false : true
146
 
        addAlarmFace.hours = isMilitaryFormat ? Utils.circlePositiontoHours(Qt.formatTime(alarm.date, "hh"), timeFormat) : Utils.circlePositiontoHours(Qt.formatTime(alarm.date, "hh AP").split(" ")[0], timeFormat)
147
 
        addAlarmFace.minutes = Qt.formatTime(alarm.date, "mm")
 
147
        isPm = Qt.formatTime(alarm.date, "AP") === "PM"
 
148
        hourLabel.text = isMilitaryFormat ? Qt.formatTime(alarm.date, "hh") : Qt.formatTime(alarm.date, "h AP").split(" ")[0]
 
149
        minuteLabel.text = Qt.formatTime(alarm.date, "mm")
 
150
        addAlarmFace.hours = Number(hourLabel.text) // Set hour hand
148
151
    }
149
152
 
150
153
    // Function to update an existing alarm
164
167
            pagestack.pop()
165
168
    }
166
169
 
 
170
    // Function to print hour label
 
171
    function setHoursLabel(hours, maxValue) {
 
172
        var z
 
173
        if (maxValue === 24){
 
174
            z = 2
 
175
            if (addAlarmFace.hours > (maxValue - 1)) {
 
176
                hours = 0
 
177
            }
 
178
        }
 
179
        else {
 
180
            z = 1
 
181
            if (addAlarmFace.hours < 1){
 
182
                hours = 12
 
183
            }
 
184
        }
 
185
        addAlarmFace.hours = Math.round(hours)
 
186
 
 
187
        return Utils.zeroleft(hours, z);
 
188
    }
 
189
 
 
190
    // Function to print minute label
 
191
    function setMinutesLabel(minutes) {
 
192
        if (addAlarmFace.minutes > 59) {
 
193
            minutes = 0
 
194
        }
 
195
        addAlarmFace.minutes = Math.round(minutes)
 
196
 
 
197
        return Utils.zeroleft(minutes, 2);
 
198
    }
 
199
 
 
200
    // Function to update hour on minute hand rotation
 
201
    function updateHoursOnMinuteRotation(){
 
202
        var hour = Number(hourLabel.text)
 
203
        var minute = Number(minuteLabel.text)
 
204
        var maxHourValue = isMilitaryFormat ? 23 : 12
 
205
        var baseHourValue = isMilitaryFormat ? 0 : 12
 
206
 
 
207
        if (minute === 0 && addAlarmFace.valueWas >= 58){
 
208
            if(hour === baseHourValue - 1 && !isMilitaryFormat){
 
209
                isPm = !isPm
 
210
            }
 
211
            if (hour > maxHourValue - 1){
 
212
                hourLabel.text = isMilitaryFormat ? "00" : "12"
 
213
 
 
214
                if (hour === baseHourValue && !isMilitaryFormat){
 
215
                    hourLabel.text = "1"
 
216
                }
 
217
            }
 
218
            else {
 
219
                hourLabel.text = isMilitaryFormat ? ("0" + (hour + 1)).slice(-2) :  hour + 1
 
220
            }
 
221
        }
 
222
        else if (minute >= 58 && addAlarmFace.valueWas === 0){
 
223
            if (hour === baseHourValue && !isMilitaryFormat){
 
224
                isPm = !isPm
 
225
            }
 
226
            if (hour <= 1){
 
227
                hourLabel.text = isMilitaryFormat ? "00" : "12"
 
228
                if (hour === baseHourValue && isMilitaryFormat){
 
229
                    hourLabel.text = "23"
 
230
                }
 
231
            }
 
232
            else {
 
233
                hourLabel.text = isMilitaryFormat ? ("0" + (hour - 1)).slice(-2) : hour - 1
 
234
            }
 
235
        }
 
236
        addAlarmFace.valueWas = minute
 
237
    }
 
238
 
 
239
    // Function to update AM/PM indicator on the hour hands rotation
 
240
    function updateAmPmOnRotation(){
 
241
        var hour = Number(hourLabel.text)
 
242
        if (((hour === 11 && addAlarmFace.valueWas === 12) || (hour === 12 && addAlarmFace.valueWas === 11)) && !isMilitaryFormat ){
 
243
            isPm = !isPm
 
244
        }
 
245
        addAlarmFace.valueWas = hour
 
246
    }
 
247
 
167
248
    // Timer to show the error message for one second and then automatically hiding it
168
249
    Timer {
169
250
        id: errorTimer
188
269
                id: addAlarmFace
189
270
 
190
271
                draggable: true
191
 
                increments: true
192
272
                enableMouseArea: false
193
273
                showSecondHand: false
194
274
                showMinuteHand: false
195
275
                innerLabel.visible: false
196
276
                anchors.horizontalCenter: parent.horizontalCenter
 
277
                maximumValue: isMilitaryFormat ? 24 : 12
 
278
 
 
279
                onMinutesChanged: minuteLabel.text = setMinutesLabel(minutes)
 
280
                onHoursChanged: {hourLabel.text = setHoursLabel(hours, maximumValue); updateAmPmOnRotation() }
197
281
 
198
282
                states: [
199
283
                    State {
200
284
                        name: "minuteMode"
201
 
                        PropertyChanges { target: hourLabel; font.pixelSize: units.dp(41) }
202
 
                        PropertyChanges { target: hourLabel; color: "White" }
203
 
                        PropertyChanges { target: addAlarmFace; showHourHand: false }
204
 
                        PropertyChanges { target: minuteLabel; font.pixelSize: units.dp(55) }
205
 
                        PropertyChanges { target: minuteLabel; color: "LightGreen" }
206
 
                        PropertyChanges { target: addAlarmFace; showMinuteHand: true }
 
285
                        PropertyChanges { target: addAlarmFace; maximumValue: 60; showHourHand: false; showMinuteHand: true }
 
286
                        PropertyChanges { target: hourLabel; font.pixelSize: units.dp(41); color: "White" }
 
287
                        PropertyChanges { target: minuteLabel; font.pixelSize: units.dp(55); color: "LightGreen" }
207
288
                    }
208
289
                ]
209
290
 
232
313
                                color: "LightGreen"
233
314
                                font.pixelSize: units.dp(55)
234
315
                                anchors.verticalCenter: parent.verticalCenter
235
 
                                text: {
236
 
                                    if(isMilitaryFormat) {
237
 
                                        // In the 24 hour mode, if the hour is 24, show it as 00 since that's what it essentially is.
238
 
                                        if(Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat) === 24)
239
 
                                            return "00"
240
 
                                        else
241
 
                                            return Utils.zeroleft(Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat), 2)
242
 
                                    }
243
 
                                    else {
244
 
                                        // In the 12 hour mode, hours cannot be 0, hence show it as 12
245
 
                                        if(Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat) === 0)
246
 
                                            return "12"
247
 
                                        else
248
 
                                            return Utils.zeroleft(Utils.hourstoCirclePosition(addAlarmFace.hours, timeFormat), 2)
249
 
                                    }
250
 
                                }
 
316
 
251
317
                                MouseArea {
252
318
                                    anchors.fill: parent
253
319
                                    preventStealing: true
254
 
                                    onClicked: addAlarmFace.state = ""
 
320
                                    onClicked: {
 
321
                                        addAlarmFace.state = "";
 
322
                                        addAlarmFace.hours = Number(hourLabel.text)
 
323
                                    }
255
324
                                }
 
325
 
256
326
                                Behavior on font.pixelSize {
257
327
                                    UbuntuNumberAnimation { duration: UbuntuAnimation.BriskDuration }
258
328
                                }
268
338
                                id: minuteLabel
269
339
                                font.pixelSize: units.dp(41)
270
340
                                anchors.verticalCenter: parent.verticalCenter
271
 
                                text: Utils.zeroleft(addAlarmFace.minutes, 2) === "60" ? "00" : Utils.zeroleft(addAlarmFace.minutes, 2)
 
341
                                onTextChanged: if(addAlarmFace.state == "minuteMode") updateHoursOnMinuteRotation()
 
342
 
272
343
                                MouseArea {
273
344
                                    id: minuteMouseArea
274
345
                                    anchors.fill: parent
275
 
                                    onClicked: addAlarmFace.state = "minuteMode"
 
346
                                    onClicked: {
 
347
                                        addAlarmFace.state = "minuteMode";
 
348
                                        addAlarmFace.minutes = Number(minuteLabel.text)
 
349
                                    }
276
350
                                }
 
351
 
277
352
                                Behavior on font.pixelSize {
278
353
                                    UbuntuNumberAnimation { duration: UbuntuAnimation.BriskDuration }
279
354
                                }
289
364
                            Label {
290
365
                                id: timeFormatLabel
291
366
                                font.pixelSize: units.dp(31)
292
 
                                text: !isPm ? "AM" : "PM"
 
367
                                text: isPm ? "PM" : "AM"
293
368
                            }
294
369
                            MouseArea {
295
370
                                anchors.fill: parent