~charlesk/ubuntu-clock-app/lp-1275560

20.2.8 by Nekhelesh Ramananthan
Added copyright info to every qml file in the project
1
/*
316.1.1 by Nekhelesh Ramananthan
updated changelog of all files
2
 * Copyright (C) 2013, 2014 Canonical Ltd
20.2.8 by Nekhelesh Ramananthan
Added copyright info to every qml file in the project
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License version 3 as
6
 * published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 *
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
16
 * Authored by: Nekhelesh Ramananthan <krnekhelesh@gmail.com>
20.2.8 by Nekhelesh Ramananthan
Added copyright info to every qml file in the project
17
 */
18
10.2.3 by Nekhelesh Ramananthan
Organized timer, stopwatch and alarm in similar fashion
19
import QtQuick 2.0
20
import Ubuntu.Components 0.1
43.1.1 by Alessandro Pozzi
fix little bugs
21
import Ubuntu.Components.ListItems 0.1 as ListItem
10.2.3 by Nekhelesh Ramananthan
Organized timer, stopwatch and alarm in similar fashion
22
import "../common/ClockUtils.js" as Utils
43.1.1 by Alessandro Pozzi
fix little bugs
23
import "../common"
10.2.3 by Nekhelesh Ramananthan
Organized timer, stopwatch and alarm in similar fashion
24
25
Page {
43.1.1 by Alessandro Pozzi
fix little bugs
26
    id: alarmPage
240.1.1 by nskaggs
basic add remove test works
27
    objectName: "alarmPage"
43.1.1 by Alessandro Pozzi
fix little bugs
28
330.3.5 by Nekhelesh Ramananthan
Added current time to alarm page
29
    property string currentTime
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
30
    property alias modelCount: listSavedAlarm.count
330.2.16 by Nekhelesh Ramananthan
Fix alarm message when there are no active alarms
31
    property int enabledAlarmCount: 0
169.1.24 by Nekhelesh Ramananthan
revert previous commit
32
    property var nextAlarm
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
33
    property real tempPosition
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
34
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
35
    Component.onCompleted: {
36
        Utils.log("AlarmPage loaded")
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
37
        alarmModel.count > 0 ? get_next_active_alarm() : Utils.log("No Alarms Present")
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
38
        tempPosition = clockAnimationContainer.contentY
330.3.5 by Nekhelesh Ramananthan
Added current time to alarm page
39
        currentTime = Utils.convertTime(new Date().getHours(), new Date().getMinutes(), new Date().getUTCSeconds(), appSetting.contents.timeFormat)
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
40
    }
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
41
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
42
    onModelCountChanged: modelCount == 0 ? clockAnimationContainer.contentY = tempPosition : undefined
43
169.1.16 by Nekhelesh Ramananthan
moved actions as a separate component
44
    actions: [
45
        Action {
46
            id: addAlarmAction
47
            iconSource: Qt.resolvedUrl("../images/add_icon.png")
169.1.25 by Nekhelesh Ramananthan
Edited HUD action description and keywords
48
            text: i18n.tr("Add alarm")
49
            description: i18n.tr("Add a new alarm")
50
            keywords: i18n.tr("Add;Alarm;Alert;New;Tone;Bell")
169.1.26 by Nekhelesh Ramananthan
Fixed several bugs and added recurring alarms support. Also improved data passing to pagestack.
51
            onTriggered: pagestack.push(Qt.resolvedUrl("AddAlarmPage.qml"), {"isNewAlarm": true})
169.1.16 by Nekhelesh Ramananthan
moved actions as a separate component
52
        }
53
    ]
54
330.3.5 by Nekhelesh Ramananthan
Added current time to alarm page
55
    function onTimerUpdate(now) {
56
        currentTime = Utils.convertTime(new Date().getHours(), new Date().getMinutes(), new Date().getUTCSeconds(), appSetting.contents.timeFormat)
57
    }
58
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
59
    function get_next_active_alarm()
60
    {
330.2.16 by Nekhelesh Ramananthan
Fix alarm message when there are no active alarms
61
        enabledAlarmCount = 0
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
62
        var tempAlarm;
63
64
        for(var i=0; i<alarmModel.count; i++) {
65
            tempAlarm = alarmModel.get(i)
66
            if (tempAlarm.enabled) {
330.2.16 by Nekhelesh Ramananthan
Fix alarm message when there are no active alarms
67
                enabledAlarmCount += 1
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
68
                nextAlarm = alarmModel.get(i)
69
                break;
70
            }
71
        }
72
330.2.16 by Nekhelesh Ramananthan
Fix alarm message when there are no active alarms
73
        if (enabledAlarmCount !== 0) {
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
74
            for (var i=0; i<alarmModel.count; i++) {
75
                tempAlarm = alarmModel.get(i)
76
                if (tempAlarm.enabled)
169.1.26 by Nekhelesh Ramananthan
Fixed several bugs and added recurring alarms support. Also improved data passing to pagestack.
77
                    if (tempAlarm.date < nextAlarm.date)
78
                        nextAlarm = alarmModel.get(i)
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
79
            }
80
            nameAlarm.text = nextAlarm.message
330.3.2 by Nekhelesh Ramananthan
Implemented new alarm text design
81
            alarmOccurrence.text = format_day_string(nextAlarm.daysOfWeek, nextAlarm.type)
286.2.9 by Nekhelesh Ramananthan
added 24 hour mode to edit alarm page
82
            alarmFace.hours = Utils.circlePositiontoHours(Qt.formatTime(nextAlarm.date, "hh AP").split(' ')[0], "12-hour")
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
83
            alarmFace.minutes = Qt.formatTime(nextAlarm.date, "mm")
286.2.12 by Nekhelesh Ramananthan
Fixed alarm page clock hand to use 12-hour format
84
            alarmFace.innerLabel.text = Utils.convertTime(Qt.formatTime(nextAlarm.date, "hh"), Qt.formatTime(nextAlarm.date, "mm"), 0, alarmFace.timeFormat)
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
85
        }
86
        else {
87
            nameAlarm.text = "";
330.3.2 by Nekhelesh Ramananthan
Implemented new alarm text design
88
            alarmOccurrence.text = "";
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
89
            alarmFace.hours = 0;
90
            alarmFace.minutes = 0;
286.2.12 by Nekhelesh Ramananthan
Fixed alarm page clock hand to use 12-hour format
91
            alarmFace.innerLabel.text = "00:00"
169.1.22 by Nekhelesh Ramananthan
added support to detect next active alarm
92
        }
93
94
    }
95
169.1.26 by Nekhelesh Ramananthan
Fixed several bugs and added recurring alarms support. Also improved data passing to pagestack.
96
    function format_day_string(value, type) {
97
        var occurs = get_day(value, type);
98
99
        if (value != Alarm.Daily) {
100
            if (occurs.length > 1)
169.1.28 by Nekhelesh Ramananthan
Added localised day names
101
                return i18n.tr("Every ") + occurs
169.1.26 by Nekhelesh Ramananthan
Fixed several bugs and added recurring alarms support. Also improved data passing to pagestack.
102
            else
103
                return i18n.tr("Once on ")  + occurs
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
104
        }
169.1.26 by Nekhelesh Ramananthan
Fixed several bugs and added recurring alarms support. Also improved data passing to pagestack.
105
        else
106
            return i18n.tr("Daily")
107
    }
108
109
    function get_day(value, type) {
110
        var occurs = [];
205.1.1 by Nekhelesh Ramananthan
Added more translations strings
111
        if (value & Alarm.Monday) occurs.push(Qt.locale().standaloneDayName(1, Locale.ShortFormat));
112
        if (value & Alarm.Tuesday) occurs.push(Qt.locale().standaloneDayName(2, Locale.ShortFormat));
113
        if (value & Alarm.Wednesday) occurs.push(Qt.locale().standaloneDayName(3, Locale.ShortFormat));
114
        if (value & Alarm.Thursday) occurs.push(Qt.locale().standaloneDayName(4, Locale.ShortFormat));
115
        if (value & Alarm.Friday) occurs.push(Qt.locale().standaloneDayName(5, Locale.ShortFormat));
116
        if (value & Alarm.Saturday) occurs.push(Qt.locale().standaloneDayName(6, Locale.ShortFormat));
117
        if (value & Alarm.Sunday) occurs.push(Qt.locale().standaloneDayName(7, Locale.ShortFormat));
169.1.26 by Nekhelesh Ramananthan
Fixed several bugs and added recurring alarms support. Also improved data passing to pagestack.
118
        return occurs;
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
119
    }
120
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
121
    Flickable {
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
122
        id: clockAnimationContainer
123
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
124
        clip: true
125
        anchors.fill: parent
126
        contentWidth: parent.width
330.2.10 by Nekhelesh Ramananthan
Updated image assets. Removed rectangular glow from the timer. Moved the visual image hints to the face files
127
        interactive: modelCount == 0 ? false : true
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
128
        contentHeight: alarmFace.height + alarmFace.anchors.topMargin + nameAlarm.height + nameAlarm.anchors.topMargin + listAlarm.height + listAlarm.anchors.topMargin + units.gu(2)
330.2.2 by Nekhelesh Ramananthan
Added an image to indicate alarms and also reduced the next alarm font size
129
281.1.3 by Nekhelesh Ramananthan
Converted all tabs to use the new SDK dialer
130
        AlarmFace {
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
131
            id: alarmFace
169.1.8 by Nekhelesh Ramananthan
Added initial text to help setup alarm
132
330.2.16 by Nekhelesh Ramananthan
Fix alarm message when there are no active alarms
133
            states: [
134
                State {
135
                    name: "NOACTIVEALARMS"
136
                    when: enabledAlarmCount === 0 && modelCount !== 0
330.3.7 by Nekhelesh Ramananthan
merged prerequisite branch
137
                    PropertyChanges { target: alarmFace.nextAlarmText; text: i18n.tr("No Active Alarms"); fontSize: "large"; anchors.verticalCenterOffset: 0 }
330.2.16 by Nekhelesh Ramananthan
Fix alarm message when there are no active alarms
138
                    PropertyChanges { target: alarmFace.innerLabel; visible: false }
330.3.7 by Nekhelesh Ramananthan
merged prerequisite branch
139
                    PropertyChanges { target: alarmFace.currentTimeLabel; visible: false }
330.2.16 by Nekhelesh Ramananthan
Fix alarm message when there are no active alarms
140
                }
141
            ]
142
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
143
            anchors { top: parent.top; topMargin: units.gu(10); horizontalCenter: parent.horizontalCenter }
330.3.7 by Nekhelesh Ramananthan
merged prerequisite branch
144
            currentTimeLabel.text: i18n.tr("Now") + " " + currentTime
330.2.16 by Nekhelesh Ramananthan
Fix alarm message when there are no active alarms
145
            showSetupMessage: modelCount === 0
286.2.12 by Nekhelesh Ramananthan
Fixed alarm page clock hand to use 12-hour format
146
            onTimeFormatChanged: get_next_active_alarm()
281.1.3 by Nekhelesh Ramananthan
Converted all tabs to use the new SDK dialer
147
            onPressAndHold: pagestack.push(Qt.resolvedUrl("AddAlarmPage.qml"), {"isNewAlarm": true})
43.1.1 by Alessandro Pozzi
fix little bugs
148
        }
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
149
330.3.2 by Nekhelesh Ramananthan
Implemented new alarm text design
150
        // Label to show the name of the next active alarm
169.1.29 by Nekhelesh Ramananthan
Fixed editablelabel and 24 hour time
151
        Label {
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
152
            id: nameAlarm
330.3.2 by Nekhelesh Ramananthan
Implemented new alarm text design
153
154
            fontSize: "large"
155
            font.bold: true
156
157
            anchors {
158
                horizontalCenter: alarmFace.horizontalCenter
159
                top: alarmFace.bottom
160
                topMargin: units.gu(5)
161
            }
162
        }
163
164
        Label {
165
            id: alarmOccurrence
166
            anchors {
167
                horizontalCenter: alarmFace.horizontalCenter
168
                top: nameAlarm.bottom
169
            }
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
170
        }
171
169.1.5 by Nekhelesh Ramananthan
Implemented add alarm page ui
172
        // Column element to hold the saved alarms
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
173
        Column {
174
            id: listAlarm
175
169.1.44 by Nekhelesh Ramananthan
Tweaked anchor positioning of list view
176
            property int dynamicTopSpacing: alarmPage.height - alarmPage.header.height - alarmFace.height - savedAlarmHeader.height - alarmFace.anchors.topMargin - divider.height;
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
177
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
178
            height: childrenRect.height
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
179
            visible: listSavedAlarm.count != 0 ? true : false
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
180
            anchors { left: parent.left; right: parent.right; top: alarmFace.bottom; topMargin: listAlarm.dynamicTopSpacing }
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
181
169.1.44 by Nekhelesh Ramananthan
Tweaked anchor positioning of list view
182
            ListItem.ThinDivider { id: divider }
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
183
184
            ListItem.Header {
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
185
                id: savedAlarmHeader
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
186
                Label {
169.1.27 by Nekhelesh Ramananthan
Autonomous call of get_active_alarm when alarmmodel changes
187
                    text: i18n.tr("Alarms")
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
188
                    anchors { verticalCenter: parent.verticalCenter; left: parent.left; leftMargin: units.gu(2) }
189
                    color: Theme.palette.normal.baseText
190
                    fontSize: "medium"
191
                }
192
            }
193
194
            ListView {
195
                id: listSavedAlarm
240.1.1 by nskaggs
basic add remove test works
196
                objectName: "listSavedAlarm"
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
197
43.1.1 by Alessandro Pozzi
fix little bugs
198
                clip: true
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
199
                anchors { left: parent.left; right: parent.right }
169.1.38 by Nekhelesh Ramananthan
Made positioning of elements dynamic and replaced AnimationContainer with flickable
200
                height: 4 * units.gu(6) + units.gu(1)
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
201
                model: alarmModel
169.1.3 by Nekhelesh Ramananthan
Removed old alarm UI files completely
202
                currentIndex: -1
169.1.5 by Nekhelesh Ramananthan
Implemented add alarm page ui
203
204
                delegate: ListItem.Base {
326.1.13 by Nekhelesh Ramananthan
Added assert statement to verify a test and added a test to delete alarms
205
                    objectName: "alarm" + index
169.1.5 by Nekhelesh Ramananthan
Implemented add alarm page ui
206
                    Label {
207
                        id: alarmTime
326.1.14 by Nekhelesh Ramananthan
Added test to toggle alarms (skipped by default)
208
                        objectName: "listAlarmTime" + index
169.1.5 by Nekhelesh Ramananthan
Implemented add alarm page ui
209
                        fontSize: "large";
287.1.4 by Nekhelesh Ramananthan
alarms page now respects 12/24 hour format
210
                        text: Utils.convertTime(date.getHours(), date.getMinutes(), 0, appSetting.contents.timeFormat)
321.1.7 by Nekhelesh Ramananthan
Moved alarm times to the right to main consistency with other tabs
211
                        anchors { verticalCenter: parent.verticalCenter; right: alarmStatus.left; rightMargin: units.gu(2) }
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
212
                        color: Theme.palette.normal.baseText
213
                    }
214
215
                    Label {
216
                        id: alarmLabel
326.1.14 by Nekhelesh Ramananthan
Added test to toggle alarms (skipped by default)
217
                        objectName: "listAlarmLabel" + index
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
218
                        fontSize: "large";
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
219
                        text: message
321.1.5 by Nekhelesh Ramananthan
Fixed overflowing alarm label
220
                        elide: Text.ElideRight
221
                        anchors {
222
                            left: parent.left;
321.1.7 by Nekhelesh Ramananthan
Moved alarm times to the right to main consistency with other tabs
223
                            right: alarmTime.left
224
                            rightMargin: units.gu(2)
225
                            leftMargin: units.gu(1);
321.1.5 by Nekhelesh Ramananthan
Fixed overflowing alarm label
226
                            verticalCenter: alarmTime.top;
227
                            verticalCenterOffset: units.gu(0.4)
228
                        }
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
229
                        color: Theme.palette.normal.baseText
230
                    }
231
232
                    Label {
233
                        id: alarmSubtitle
326.1.14 by Nekhelesh Ramananthan
Added test to toggle alarms (skipped by default)
234
                        objectName: "listAlarmSubtitle" + index
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
235
                        fontSize: "small";
169.1.26 by Nekhelesh Ramananthan
Fixed several bugs and added recurring alarms support. Also improved data passing to pagestack.
236
                        text: format_day_string(daysOfWeek, type)
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
237
                        anchors { left: alarmLabel.left; top: alarmLabel.bottom }
238
                        color: Theme.palette.normal.backgroundText
239
                    }
240
241
                    Switch {
242
                        id: alarmStatus
326.1.14 by Nekhelesh Ramananthan
Added test to toggle alarms (skipped by default)
243
                        objectName: "listAlarmStatus" + index
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
244
                        checked: model.enabled
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
245
                        anchors { right: parent.right; verticalCenter: alarmTime.verticalCenter }
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
246
                        onClicked: {
247
                            var alarm = alarmModel.get(index);
248
                            alarm.enabled = checked
249
                            alarm.save();
250
                        }
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
251
                    }
252
253
                    selected: listSavedAlarm.currentIndex == index
254
                    removable: true
244.2.1 by Nekhelesh Ramananthan
switched to default swipe delete behavior provided by the SDK
255
                    confirmRemoval: true
169.1.7 by Nekhelesh Ramananthan
Added ability to view saved alarms and delete them
256
257
                    onItemRemoved: {
169.1.17 by Nekhelesh Ramananthan
replaced sqlite database with alarm model
258
                        var alarm = alarmModel.get(index)
259
                        alarm.cancel()
169.1.5 by Nekhelesh Ramananthan
Implemented add alarm page ui
260
                    }
169.1.11 by Nekhelesh Ramananthan
Changed to optionselector and add basic support for editing alarms
261
169.1.26 by Nekhelesh Ramananthan
Fixed several bugs and added recurring alarms support. Also improved data passing to pagestack.
262
                    onClicked: pagestack.push(Qt.resolvedUrl("AddAlarmPage.qml"), {"isNewAlarm": false, "alarmIndex": index})
169.1.5 by Nekhelesh Ramananthan
Implemented add alarm page ui
263
                }
264
            }
265
        }
266
    }
267
268
    tools: ToolbarItems {
269
        id: toolbarAlarm
270
287.1.4 by Nekhelesh Ramananthan
alarms page now respects 12/24 hour format
271
        ToolbarButton {
272
            id: settings
273
            action: appSettingsAction
274
        }
169.1.5 by Nekhelesh Ramananthan
Implemented add alarm page ui
275
276
        ToolbarButton {
277
            id: addPresetToolbarButton
278
            objectName: "addAlarmButton"
169.1.16 by Nekhelesh Ramananthan
moved actions as a separate component
279
            action: addAlarmAction
10.2.5 by Nekhelesh Ramananthan
Merge from master
280
        }
281
    }
10.2.3 by Nekhelesh Ramananthan
Organized timer, stopwatch and alarm in similar fashion
282
}