Add duration entry dialog
[harbour-pedalo.git] / qml / pages / AddJourney.qml
1 import QtQuick 2.0
2 import Sailfish.Silica 1.0
3
4 Dialog {
5     id: addJourneyDialog
6     canAccept: true
7     property string title: "Add journey"
8     property var start: new Date()
9     property int duration: 0
10     property int overtook: -1
11     property int overtakenby: -1
12     property int index: -1
13
14     onDurationChanged: {
15         var structured = new Date(0, 0, 0, 0, parseInt(duration / 60))
16         durationTime.value = Qt.formatTime(structured, 'hh:mm')
17         endTime.time = new Date(0, 0, 0, start.getHours() + structured.getHours(), start.getMinutes() + structured.getMinutes())
18     }
19
20     onStartChanged: {
21         startDate.value = Qt.formatDate(start, 'd MMM yyyy')
22         startTime.value = Qt.formatTime(start, 'hh:mm')
23
24         var structured = new Date(0, 0, 0, 0, parseInt(duration / 60))
25         endTime.time = new Date(0, 0, 0, start.getHours() + structured.getHours(), start.getMinutes() + structured.getMinutes())
26     }
27
28     // The effective value will be restricted by ApplicationWindow.allowedOrientations
29     allowedOrientations: Orientation.All
30
31     SilicaFlickable {
32         id: addJourneyView
33         anchors.fill: parent
34         contentHeight: addJourneyColumn.implicitHeight
35
36         VerticalScrollDecorator {}
37
38         Column {
39             id: addJourneyColumn
40             spacing: Theme.paddingMedium
41             width: parent.width
42
43             DialogHeader {
44                 title: addJourneyDialog.title
45             }
46
47             ValueButton {
48                 id: startDate
49                 label: "Date"
50                 value: Qt.formatDate(start, 'd MMM yyyy')
51                 width: parent.width
52                 onClicked: {
53                     var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: start })
54                     dialog.accepted.connect(function() {
55                         start = new Date(dialog.year, dialog.month - 1, dialog.day, start.getHours(), start.getMinutes())
56                     })
57                 }
58             }
59
60             ValueButton {
61                 id: startTime
62                 label: qsTr("Start time")
63                 value: Qt.formatTime(start, 'hh:mm')
64                 width: parent.width
65                 onClicked: {
66                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: start.getHours(), minute: start.getMinutes()})
67                     dialog.accepted.connect(function() {
68                         start = new Date(start.getFullYear(), start.getMonth(), start.getDate(), dialog.hour, dialog.minute)
69                     })
70                 }
71             }
72
73             ValueButton {
74                 id: endTime
75                 property date time: new Date()
76                 label: qsTr("End time")
77                 value: Qt.formatTime(time, 'hh:mm')
78                 width: parent.width
79                 onClicked: {
80                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: time.getHours(), minute: time.getMinutes()})
81                     dialog.accepted.connect(function() {
82                         // We have to hack around the fact that % in Javascript is 'remainder' and not 'mod' (hence will happpily return negative numbers)
83                         duration = ((((((dialog.hour - start.getHours()) * 60) + (dialog.minute - start.getMinutes())) * 60) % 86400) + 86400) % 86400
84                         // Updating the duration will automatically trigger the end time to be udpated
85                     })
86                 }
87                 onTimeChanged: {
88                     value = Qt.formatTime(time, 'hh:mm')
89                 }
90             }
91
92             ValueButton {
93                 id: durationTime
94                 label: qsTr("Duration")
95                 value: Qt.formatTime(new Date(0, 0, 0, 0, parseInt(duration / 60)), 'hh:mm')
96                 width: parent.width
97                 onClicked: {
98                     var dialog = pageStack.push("DurationEditDialog.qml", { hour: parseInt(duration / (60 * 60)), minute: parseInt(duration / 60) % 60})
99                     dialog.accepted.connect(function() {
100                         duration = ((dialog.hour * 60) + dialog.minute) * 60
101                     })
102                 }
103             }
104
105             TextField {
106                 id: faster
107                 width: parent.width
108                 inputMethodHints: Qt.ImhDigitsOnly
109                 label: qsTr("Cycles which you overtook")
110                 placeholderText: label
111                 text: overtook >= 0 ? "" + overtook : ""
112                 horizontalAlignment: TextInput.AlignLeft
113                 EnterKey.iconSource: "image://theme/icon-m-enter-next"
114                 EnterKey.onClicked: slower.focus = true
115             }
116
117             TextField {
118                 id: slower
119                 width: parent.width
120                 inputMethodHints: Qt.ImhDigitsOnly
121                 label: qsTr("Cycles which overtook you")
122                 placeholderText: label
123                 text: overtakenby >= 0 ? "" + overtakenby : ""
124                 horizontalAlignment: TextInput.AlignLeft
125                 EnterKey.iconSource: "image://theme/icon-m-enter-next"
126                 EnterKey.onClicked: addJourneyDialog.accept()
127             }
128         }
129     }
130
131     onAccepted: {
132         var overtook = parseInt(faster.text)
133         var overtakenby = parseInt(slower.text)
134         if (index < 0) {
135             journeymodel.addJourney(start, duration, overtook, overtakenby)
136         }
137         else {
138             journeymodel.editJourney(index, start, duration, overtook, overtakenby)
139         }
140     }
141 }