Corrently add, edit and list journey details
[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                         time = new Date(0, 0, 0, dialog.hour, dialog.minute)
83                         duration = ((endTime.time.getHours() - start.getHours()) * 60) + (endTime.time.getMinutes() - start.getMinutes()) * 60
84                     })
85                 }
86                 onTimeChanged: {
87                     value = Qt.formatTime(time, 'hh:mm')
88                 }
89             }
90
91             ValueButton {
92                 id: durationTime
93                 label: qsTr("Duration")
94                 value: Qt.formatTime(new Date(0, 0, 0, 0, parseInt(duration / 60)), 'hh:mm')
95                 width: parent.width
96                 onClicked: {
97                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: parseInt(duration / (60 * 60)), minute: parseInt(duration / 60) % 60})
98                     dialog.accepted.connect(function() {
99                         duration = ((dialog.hour * 60) + dialog.minute) * 60
100                     })
101                 }
102             }
103
104             TextField {
105                 id: faster
106                 width: parent.width
107                 inputMethodHints: Qt.ImhDigitsOnly
108                 label: qsTr("Cycles which you overtook")
109                 placeholderText: label
110                 text: overtook >= 0 ? "" + overtook : ""
111                 horizontalAlignment: TextInput.AlignLeft
112                 EnterKey.iconSource: "image://theme/icon-m-enter-next"
113                 EnterKey.onClicked: slower.focus = true
114             }
115
116             TextField {
117                 id: slower
118                 width: parent.width
119                 inputMethodHints: Qt.ImhDigitsOnly
120                 label: qsTr("Cycles which overtook you")
121                 placeholderText: label
122                 text: overtakenby >= 0 ? "" + overtakenby : ""
123                 horizontalAlignment: TextInput.AlignLeft
124                 EnterKey.iconSource: "image://theme/icon-m-enter-next"
125                 EnterKey.onClicked: addJourneyDialog.accept()
126             }
127         }
128     }
129
130     onAccepted: {
131         var overtook = parseInt(faster.text)
132         var overtakenby = parseInt(slower.text)
133         if (index < 0) {
134             journeymodel.addJourney(start, duration, overtook, overtakenby)
135         }
136         else {
137             journeymodel.editJourney(index, start, duration, overtook, overtakenby)
138         }
139     }
140 }