Display overall statistics
[harbour-pedalo.git] / qml / pages / JourneyEdit.qml
1 import QtQuick 2.0
2 import Sailfish.Silica 1.0
3
4 Dialog {
5     id: journeyEditDialog
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: journeyEditView
33         anchors.fill: parent
34         contentHeight: headerItem.height + Theme.paddingLarge + (isPortrait ?
35                            (journeyEditColumnFirst.implicitHeight + journeyEditColumnSecond.implicitHeight) :
36                            Math.max(journeyEditColumnFirst.implicitHeight, journeyEditColumnSecond.implicitHeight))
37
38         VerticalScrollDecorator {}
39
40         DialogHeader {
41             id: headerItem
42             title: journeyEditDialog.title
43         }
44
45         Column {
46             id: journeyEditColumnFirst
47             spacing: Theme.paddingMedium
48             width: isPortrait ? parent.width : (parent.width * 0.5)
49             x: 0
50             y: headerItem.height
51
52             ValueButton {
53                 id: startDate
54                 label: "Date"
55                 value: Qt.formatDate(start, 'd MMM yyyy')
56                 width: parent.width
57                 onClicked: {
58                     var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: start })
59                     dialog.accepted.connect(function() {
60                         start = new Date(dialog.year, dialog.month - 1, dialog.day, start.getHours(), start.getMinutes())
61                     })
62                 }
63             }
64
65             ValueButton {
66                 id: startTime
67                 label: qsTr("Start time")
68                 value: Qt.formatTime(start, 'hh:mm')
69                 width: parent.width
70                 onClicked: {
71                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: start.getHours(), minute: start.getMinutes()})
72                     dialog.accepted.connect(function() {
73                         start = new Date(start.getFullYear(), start.getMonth(), start.getDate(), dialog.hour, dialog.minute)
74                     })
75                 }
76             }
77
78             ValueButton {
79                 id: endTime
80                 property date time: new Date()
81                 label: qsTr("End time")
82                 value: Qt.formatTime(time, 'hh:mm')
83                 width: parent.width
84                 onClicked: {
85                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: time.getHours(), minute: time.getMinutes()})
86                     dialog.accepted.connect(function() {
87                         // We have to hack around the fact that % in Javascript is 'remainder' and not 'mod' (hence will happpily return negative numbers)
88                         duration = ((((((dialog.hour - start.getHours()) * 60) + (dialog.minute - start.getMinutes())) * 60) % 86400) + 86400) % 86400
89                         // Updating the duration will automatically trigger the end time to be udpated
90                     })
91                 }
92                 onTimeChanged: {
93                     value = Qt.formatTime(time, 'hh:mm')
94                 }
95             }
96
97             ValueButton {
98                 id: durationTime
99                 label: qsTr("Duration")
100                 value: Qt.formatTime(new Date(0, 0, 0, 0, parseInt(duration / 60)), 'hh:mm')
101                 width: parent.width
102                 onClicked: {
103                     var dialog = pageStack.push("DurationEditDialog.qml", { hour: parseInt(duration / (60 * 60)), minute: parseInt(duration / 60) % 60})
104                     dialog.accepted.connect(function() {
105                         duration = ((dialog.hour * 60) + dialog.minute) * 60
106                     })
107                 }
108             }
109         }
110
111         Column {
112             id: journeyEditColumnSecond
113             spacing: Theme.paddingMedium
114             width: isPortrait ? parent.width : (parent.width * 0.5)
115             x: isPortrait ? 0 : (parent.width * 0.5)
116             y: isPortrait ? journeyEditColumnFirst.y + journeyEditColumnFirst.height + Theme.paddingLarge : journeyEditColumnFirst.y
117
118             TextField {
119                 id: faster
120                 width: parent.width
121                 inputMethodHints: Qt.ImhDigitsOnly
122                 label: qsTr("Cycles which you overtook")
123                 placeholderText: label
124                 text: overtook >= 0 ? "" + overtook : ""
125                 horizontalAlignment: TextInput.AlignLeft
126                 EnterKey.iconSource: "image://theme/icon-m-enter-next"
127                 EnterKey.onClicked: slower.focus = true
128             }
129
130             TextField {
131                 id: slower
132                 width: parent.width
133                 inputMethodHints: Qt.ImhDigitsOnly
134                 label: qsTr("Cycles which overtook you")
135                 placeholderText: label
136                 text: overtakenby >= 0 ? "" + overtakenby : ""
137                 horizontalAlignment: TextInput.AlignLeft
138                 EnterKey.iconSource: "image://theme/icon-m-enter-accept"
139                 EnterKey.onClicked: journeyEditDialog.accept()
140             }
141         }
142
143     }
144
145     onAccepted: {
146         var overtook = parseInt(faster.text)
147         var overtakenby = parseInt(slower.text)
148         if (index < 0) {
149             journeymodel.addJourney(start, duration, overtook, overtakenby)
150         }
151         else {
152             journeymodel.editJourney(index, start, duration, overtook, overtakenby)
153         }
154     }
155 }