Rename AddJourney to JourneyEdit
[harbour-pedalo.git] / qml / pages / JourneyEdit.qml
diff --git a/qml/pages/JourneyEdit.qml b/qml/pages/JourneyEdit.qml
new file mode 100644 (file)
index 0000000..1f61b39
--- /dev/null
@@ -0,0 +1,141 @@
+import QtQuick 2.0
+import Sailfish.Silica 1.0
+
+Dialog {
+    id: journeyEditDialog
+    canAccept: true
+    property string title: "Add journey"
+    property var start: new Date()
+    property int duration: 0
+    property int overtook: -1
+    property int overtakenby: -1
+    property int index: -1
+
+    onDurationChanged: {
+        var structured = new Date(0, 0, 0, 0, parseInt(duration / 60))
+        durationTime.value = Qt.formatTime(structured, 'hh:mm')
+        endTime.time = new Date(0, 0, 0, start.getHours() + structured.getHours(), start.getMinutes() + structured.getMinutes())
+    }
+
+    onStartChanged: {
+        startDate.value = Qt.formatDate(start, 'd MMM yyyy')
+        startTime.value = Qt.formatTime(start, 'hh:mm')
+
+        var structured = new Date(0, 0, 0, 0, parseInt(duration / 60))
+        endTime.time = new Date(0, 0, 0, start.getHours() + structured.getHours(), start.getMinutes() + structured.getMinutes())
+    }
+
+    // The effective value will be restricted by ApplicationWindow.allowedOrientations
+    allowedOrientations: Orientation.All
+
+    SilicaFlickable {
+        id: journeyEditView
+        anchors.fill: parent
+        contentHeight: journeyEditColumn.implicitHeight
+
+        VerticalScrollDecorator {}
+
+        Column {
+            id: journeyEditColumn
+            spacing: Theme.paddingMedium
+            width: parent.width
+
+            DialogHeader {
+                title: journeyEditDialog.title
+            }
+
+            ValueButton {
+                id: startDate
+                label: "Date"
+                value: Qt.formatDate(start, 'd MMM yyyy')
+                width: parent.width
+                onClicked: {
+                    var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: start })
+                    dialog.accepted.connect(function() {
+                        start = new Date(dialog.year, dialog.month - 1, dialog.day, start.getHours(), start.getMinutes())
+                    })
+                }
+            }
+
+            ValueButton {
+                id: startTime
+                label: qsTr("Start time")
+                value: Qt.formatTime(start, 'hh:mm')
+                width: parent.width
+                onClicked: {
+                    var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: start.getHours(), minute: start.getMinutes()})
+                    dialog.accepted.connect(function() {
+                        start = new Date(start.getFullYear(), start.getMonth(), start.getDate(), dialog.hour, dialog.minute)
+                    })
+                }
+            }
+
+            ValueButton {
+                id: endTime
+                property date time: new Date()
+                label: qsTr("End time")
+                value: Qt.formatTime(time, 'hh:mm')
+                width: parent.width
+                onClicked: {
+                    var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: time.getHours(), minute: time.getMinutes()})
+                    dialog.accepted.connect(function() {
+                        // We have to hack around the fact that % in Javascript is 'remainder' and not 'mod' (hence will happpily return negative numbers)
+                        duration = ((((((dialog.hour - start.getHours()) * 60) + (dialog.minute - start.getMinutes())) * 60) % 86400) + 86400) % 86400
+                        // Updating the duration will automatically trigger the end time to be udpated
+                    })
+                }
+                onTimeChanged: {
+                    value = Qt.formatTime(time, 'hh:mm')
+                }
+            }
+
+            ValueButton {
+                id: durationTime
+                label: qsTr("Duration")
+                value: Qt.formatTime(new Date(0, 0, 0, 0, parseInt(duration / 60)), 'hh:mm')
+                width: parent.width
+                onClicked: {
+                    var dialog = pageStack.push("DurationEditDialog.qml", { hour: parseInt(duration / (60 * 60)), minute: parseInt(duration / 60) % 60})
+                    dialog.accepted.connect(function() {
+                        duration = ((dialog.hour * 60) + dialog.minute) * 60
+                    })
+                }
+            }
+
+            TextField {
+                id: faster
+                width: parent.width
+                inputMethodHints: Qt.ImhDigitsOnly
+                label: qsTr("Cycles which you overtook")
+                placeholderText: label
+                text: overtook >= 0 ? "" + overtook : ""
+                horizontalAlignment: TextInput.AlignLeft
+                EnterKey.iconSource: "image://theme/icon-m-enter-next"
+                EnterKey.onClicked: slower.focus = true
+            }
+
+            TextField {
+                id: slower
+                width: parent.width
+                inputMethodHints: Qt.ImhDigitsOnly
+                label: qsTr("Cycles which overtook you")
+                placeholderText: label
+                text: overtakenby >= 0 ? "" + overtakenby : ""
+                horizontalAlignment: TextInput.AlignLeft
+                EnterKey.iconSource: "image://theme/icon-m-enter-next"
+                EnterKey.onClicked: journeyEditDialog.accept()
+            }
+        }
+    }
+
+    onAccepted: {
+        var overtook = parseInt(faster.text)
+        var overtakenby = parseInt(slower.text)
+        if (index < 0) {
+            journeymodel.addJourney(start, duration, overtook, overtakenby)
+        }
+        else {
+            journeymodel.editJourney(index, start, duration, overtook, overtakenby)
+        }
+    }
+}