Add journey model and list
[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
9     // The effective value will be restricted by ApplicationWindow.allowedOrientations
10     allowedOrientations: Orientation.All
11
12     SilicaFlickable {
13         id: addJourneyView
14         anchors.fill: parent
15         contentHeight: addJourneyColumn.implicitHeight
16
17         VerticalScrollDecorator {}
18
19         Column {
20             id: addJourneyColumn
21             spacing: Theme.paddingMedium
22             width: parent.width
23
24             DialogHeader {
25                 title: addJourneyDialog.title
26             }
27
28             ValueButton {
29                 id: startDate
30                 property date date: new Date()
31                 label: "Date"
32                 value: Qt.formatDate(date, 'd MMM yyyy')
33                 width: parent.width
34                 onClicked: {
35                     var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: value })
36                     dialog.accepted.connect(function() {
37                         date = dialog.date
38                     })
39                 }
40                 onDateChanged: {
41                     value = Qt.formatDate(date, 'd MMM yyyy')
42                 }
43             }
44
45             ValueButton {
46                 id: startTime
47                 property date time: new Date()
48                 label: qsTr("Start time")
49                 value: Qt.formatTime(time, 'hh:mm')
50                 width: parent.width
51                 onClicked: {
52                     console.log("Hours: " + time.getHours())
53                     console.log("Mins: " + time.getMinutes())
54                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: time.getHours(), minute: time.getMinutes()})
55                     dialog.accepted.connect(function() {
56                         time = new Date(0, 0, 0, dialog.hour, dialog.minute)
57                     })
58                 }
59                 onTimeChanged: {
60                     value = Qt.formatTime(time, 'hh:mm')
61                     endTime.time = new Date(0, 0, 0, startTime.time.getHours() + durationTime.duration.getHours(), startTime.time.getMinutes() + durationTime.duration.getMinutes())
62                 }
63             }
64
65             ValueButton {
66                 id: endTime
67                 property date time: new Date()
68                 label: qsTr("End time")
69                 value: Qt.formatTime(time, 'hh:mm')
70                 width: parent.width
71                 onClicked: {
72                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: time.getHours(), minute: time.getMinutes()})
73                     dialog.accepted.connect(function() {
74                         time = new Date(0, 0, 0, dialog.hour, dialog.minute)
75                     })
76                 }
77                 onTimeChanged: {
78                     value = Qt.formatTime(time, 'hh:mm')
79                     durationTime.duration = new Date(0, 0, 0, endTime.time.getHours() - startTime.time.getHours(), endTime.time.getMinutes() - startTime.time.getMinutes())
80                 }
81             }
82
83             ValueButton {
84                 id: durationTime
85                 property date duration: new Date(0, 0, 0, 0, 0)
86                 label: qsTr("Duration")
87                 value: Qt.formatTime(duration, 'hh:mm')
88                 width: parent.width
89                 onClicked: {
90                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: duration.getHours(), minute: duration.getMinutes()})
91                     dialog.accepted.connect(function() {
92                         duration = new Date(0, 0, 0, dialog.hour, dialog.minute)
93                     })
94                 }
95                 onDurationChanged: {
96                     value = Qt.formatTime(duration, 'hh:mm')
97                     endTime.time = new Date(0, 0, 0, startTime.time.getHours() + durationTime.duration.getHours(), startTime.time.getMinutes() + durationTime.duration.getMinutes())
98                 }
99             }
100
101             TextField {
102                 id: faster
103                 width: parent.width
104                 inputMethodHints: Qt.ImhDigitsOnly
105                 label: qsTr("Cycles which you overtook")
106                 placeholderText: label
107                 horizontalAlignment: TextInput.AlignLeft
108                 EnterKey.iconSource: "image://theme/icon-m-enter-next"
109                 EnterKey.onClicked: slower.focus = true
110             }
111
112             TextField {
113                 id: slower
114                 width: parent.width
115                 inputMethodHints: Qt.ImhDigitsOnly
116                 label: qsTr("Cycles which overtook you")
117                 placeholderText: label
118                 horizontalAlignment: TextInput.AlignLeft
119                 EnterKey.iconSource: "image://theme/icon-m-enter-next"
120                 EnterKey.onClicked: addJourneyDialog.accept()
121             }
122         }
123     }
124
125     onAccepted: {
126         var start = new Date(startDate.date.getFullYear(), startDate.date.getMonth(), startDate.date.getDate(), startTime.time.getHours(), startTime.time.getMinutes())
127         var duration = (durationTime.duration.getHours() * 24 * 60) + (durationTime.duration.getMinutes() * 60) + (durationTime.duration.getSeconds())
128         var overtook = parseInt(faster.text)
129         var overtakenby = parseInt(slower.text)
130         journeymodel.addJourney(start, duration, overtook, overtakenby)
131     }
132 }