Corrently add, edit and list journey details
authorDavid Llewellyn-Jones <david@flypig.co.uk>
Fri, 13 Jul 2018 23:55:03 +0000 (00:55 +0100)
committerDavid Llewellyn-Jones <david@flypig.co.uk>
Fri, 13 Jul 2018 23:55:03 +0000 (00:55 +0100)
harbour-pedalo.pro
qml/pages/AddJourney.qml
qml/pages/JourneyList.qml
qml/pages/MainPage.qml
src/harbour-pedalo.cpp
src/journeymodel.cpp
src/journeymodel.h
src/status.cpp [new file with mode: 0644]
src/status.h [new file with mode: 0644]

index cd82343..d1a3f1b 100644 (file)
@@ -16,7 +16,8 @@ CONFIG += sailfishapp
 
 SOURCES += src/harbour-pedalo.cpp \
     src/journey.cpp \
-    src/journeymodel.cpp
+    src/journeymodel.cpp \
+    src/status.cpp
 
 DISTFILES += qml/harbour-pedalo.qml \
     qml/cover/CoverPage.qml \
@@ -47,4 +48,5 @@ TRANSLATIONS += translations/harbour-pedalo-de.ts
 
 HEADERS += \
     src/journey.h \
-    src/journeymodel.h
+    src/journeymodel.h \
+    src/status.h
index 2330b13..9d7a436 100644 (file)
@@ -5,6 +5,25 @@ Dialog {
     id: addJourneyDialog
     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
@@ -27,39 +46,28 @@ Dialog {
 
             ValueButton {
                 id: startDate
-                property date date: new Date()
                 label: "Date"
-                value: Qt.formatDate(date, 'd MMM yyyy')
+                value: Qt.formatDate(start, 'd MMM yyyy')
                 width: parent.width
                 onClicked: {
-                    var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: value })
+                    var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: start })
                     dialog.accepted.connect(function() {
-                        date = dialog.date
+                        start = new Date(dialog.year, dialog.month - 1, dialog.day, start.getHours(), start.getMinutes())
                     })
                 }
-                onDateChanged: {
-                    value = Qt.formatDate(date, 'd MMM yyyy')
-                }
             }
 
             ValueButton {
                 id: startTime
-                property date time: new Date()
                 label: qsTr("Start time")
-                value: Qt.formatTime(time, 'hh:mm')
+                value: Qt.formatTime(start, 'hh:mm')
                 width: parent.width
                 onClicked: {
-                    console.log("Hours: " + time.getHours())
-                    console.log("Mins: " + time.getMinutes())
-                    var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: time.getHours(), minute: time.getMinutes()})
+                    var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: start.getHours(), minute: start.getMinutes()})
                     dialog.accepted.connect(function() {
-                        time = new Date(0, 0, 0, dialog.hour, dialog.minute)
+                        start = new Date(start.getFullYear(), start.getMonth(), start.getDate(), dialog.hour, dialog.minute)
                     })
                 }
-                onTimeChanged: {
-                    value = Qt.formatTime(time, 'hh:mm')
-                    endTime.time = new Date(0, 0, 0, startTime.time.getHours() + durationTime.duration.getHours(), startTime.time.getMinutes() + durationTime.duration.getMinutes())
-                }
             }
 
             ValueButton {
@@ -72,30 +80,25 @@ Dialog {
                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: time.getHours(), minute: time.getMinutes()})
                     dialog.accepted.connect(function() {
                         time = new Date(0, 0, 0, dialog.hour, dialog.minute)
+                        duration = ((endTime.time.getHours() - start.getHours()) * 60) + (endTime.time.getMinutes() - start.getMinutes()) * 60
                     })
                 }
                 onTimeChanged: {
                     value = Qt.formatTime(time, 'hh:mm')
-                    durationTime.duration = new Date(0, 0, 0, endTime.time.getHours() - startTime.time.getHours(), endTime.time.getMinutes() - startTime.time.getMinutes())
                 }
             }
 
             ValueButton {
                 id: durationTime
-                property date duration: new Date(0, 0, 0, 0, 0)
                 label: qsTr("Duration")
-                value: Qt.formatTime(duration, 'hh:mm')
+                value: Qt.formatTime(new Date(0, 0, 0, 0, parseInt(duration / 60)), 'hh:mm')
                 width: parent.width
                 onClicked: {
-                    var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: duration.getHours(), minute: duration.getMinutes()})
+                    var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: parseInt(duration / (60 * 60)), minute: parseInt(duration / 60) % 60})
                     dialog.accepted.connect(function() {
-                        duration = new Date(0, 0, 0, dialog.hour, dialog.minute)
+                        duration = ((dialog.hour * 60) + dialog.minute) * 60
                     })
                 }
-                onDurationChanged: {
-                    value = Qt.formatTime(duration, 'hh:mm')
-                    endTime.time = new Date(0, 0, 0, startTime.time.getHours() + durationTime.duration.getHours(), startTime.time.getMinutes() + durationTime.duration.getMinutes())
-                }
             }
 
             TextField {
@@ -104,6 +107,7 @@ Dialog {
                 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
@@ -115,6 +119,7 @@ Dialog {
                 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: addJourneyDialog.accept()
@@ -123,10 +128,13 @@ Dialog {
     }
 
     onAccepted: {
-        var start = new Date(startDate.date.getFullYear(), startDate.date.getMonth(), startDate.date.getDate(), startTime.time.getHours(), startTime.time.getMinutes())
-        var duration = (durationTime.duration.getHours() * 24 * 60) + (durationTime.duration.getMinutes() * 60) + (durationTime.duration.getSeconds())
         var overtook = parseInt(faster.text)
         var overtakenby = parseInt(slower.text)
-        journeymodel.addJourney(start, duration, overtook, overtakenby)
+        if (index < 0) {
+            journeymodel.addJourney(start, duration, overtook, overtakenby)
+        }
+        else {
+            journeymodel.editJourney(index, start, duration, overtook, overtakenby)
+        }
     }
 }
index 8c6998f..0468df0 100644 (file)
@@ -34,11 +34,11 @@ Page {
                 }
                 Label {
                     width: columnwidth / 3.0
-                    text: parseInt(duration / (60 * 60)) + ":" + (parseInt(duration / 60) % 60)
+                    text: Qt.formatTime(new Date(0, 0, 0, 0, 0, duration), 'hh:mm')
                     color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor
                 }
             }
-            onClicked: pageStack.push(Qt.resolvedUrl("AddJourney.qml"), {title: "Edit journey"})
+            onClicked: pageStack.push(Qt.resolvedUrl("AddJourney.qml"), {title: "Edit journey", index: index, start: journeymodel.epochToDateTime(start), duration: duration, overtook: overtook, overtakenby: overtakenby})
         }
         VerticalScrollDecorator {}
     }
index 31b1447..03d86a2 100644 (file)
@@ -3,7 +3,6 @@ import Sailfish.Silica 1.0
 
 Page {
     id: page
-    property bool cycling: false
 
     // The effective value will be restricted by ApplicationWindow.allowedOrientations
     allowedOrientations: Orientation.All
@@ -40,17 +39,17 @@ Page {
 
             Button {
                 anchors.horizontalCenter: parent.horizontalCenter
-                text: cycling ? qsTr("Finish") : qsTr("Start a journey")
+                text: currentStatus.cycling ? qsTr("Finish") : qsTr("Start a journey")
                 onClicked: {
-                    if (cycling) {
-                        var dialog = pageStack.push(Qt.resolvedUrl("AddJourney.qml"))
+                    if (currentStatus.cycling) {
+                        var dialog = pageStack.push(Qt.resolvedUrl("AddJourney.qml"), {title: "Finish journey", start: journeymodel.epochToDateTime(currentStatus.startTime), duration: currentStatus.getDuration()})
 
                         dialog.accepted.connect(function() {
-                            cycling = false
+                            currentStatus.cycling = false
                         })
                     }
                     else {
-                        cycling = true
+                        currentStatus.startJourney()
                     }
                 }
             }
index acdcc2e..144ddd3 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "journey.h"
 #include "journeymodel.h"
+#include "status.h"
 
 int main(int argc, char *argv[])
 {
@@ -23,6 +24,7 @@ int main(int argc, char *argv[])
     QScopedPointer<QGuiApplication> app(SailfishApp::application(argc, argv));
 
     JourneyModel journeys;
+    Status currentStatus;
 
     QFile file;
     file.setFileName(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/journeys.csv");
@@ -33,6 +35,7 @@ int main(int argc, char *argv[])
 
     QQmlContext *ctxt = view->rootContext();
     ctxt->setContextProperty("journeymodel", &journeys);
+    ctxt->setContextProperty("currentStatus", &currentStatus);
 
     view->show();
     int result = app->exec();
index 7286c6d..80a5eb5 100644 (file)
@@ -20,7 +20,14 @@ void JourneyModel::addJourney(const Journey &journey)
 }
 
 void JourneyModel::addJourney(QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby) {
-    addJourney(Journey(start.currentMSecsSinceEpoch(), duration, overtook, overtakenby));
+    quint64 startepoch = start.toMSecsSinceEpoch();
+    addJourney(Journey(startepoch, duration, overtook, overtakenby));
+}
+
+void JourneyModel::editJourney(quint32 index, QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby) {
+    quint64 startepoch = start.toMSecsSinceEpoch();
+    journeys.replace(index, Journey(startepoch, duration, overtook, overtakenby));
+    emit dataChanged(createIndex(index, 0), createIndex(index, 0));
 }
 
 int JourneyModel::rowCount(const QModelIndex & parent) const {
@@ -92,7 +99,9 @@ void JourneyModel::importFromFile(QFile & file) {
 QDate JourneyModel::epochToDate(quint64 epoch) {
     QDateTime date;
 
+    qDebug() << "Epoch: " << epoch;
     date.setMSecsSinceEpoch(epoch);
+    qDebug() << "Date: " << date.date();
     return date.date();
 }
 
@@ -102,3 +111,10 @@ QTime JourneyModel::epochToTime(quint64 epoch) {
     date.setMSecsSinceEpoch(epoch);
     return date.time();
 }
+
+QDateTime JourneyModel::epochToDateTime(quint64 epoch) {
+    QDateTime date;
+
+    date.setMSecsSinceEpoch(epoch);
+    return date;
+}
index 398862a..a068b14 100644 (file)
@@ -24,6 +24,7 @@ public:
 
     void addJourney(const Journey &journey);
     Q_INVOKABLE void addJourney(QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby);
+    Q_INVOKABLE void editJourney(quint32 index, QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby);
 
     int rowCount(const QModelIndex & parent = QModelIndex()) const;
 
@@ -36,6 +37,7 @@ public:
 
     Q_INVOKABLE static QDate epochToDate(quint64 epoch);
     Q_INVOKABLE static QTime epochToTime(quint64 epoch);
+    Q_INVOKABLE static QDateTime epochToDateTime(quint64 epoch);
 signals:
     // General signals
     void journeysChanged();
diff --git a/src/status.cpp b/src/status.cpp
new file mode 100644 (file)
index 0000000..2aa9f7a
--- /dev/null
@@ -0,0 +1,37 @@
+#include <QDateTime>
+
+#include "status.h"
+
+Status::Status(QObject *parent) : QObject(parent),
+  cycling(false),
+  startTime(0u)
+{
+
+}
+
+bool Status::getCycling() const {
+    return cycling;
+}
+
+quint64 Status::getStartTime() const {
+    return startTime;
+}
+
+quint64 Status::getDuration() const {
+    return (QDateTime::currentMSecsSinceEpoch() - startTime) / 1000;
+}
+
+void Status::setCycling(bool value) {
+    cycling = value;
+    emit cyclingChanged(cycling);
+}
+
+void Status::setStartTime(quint64 value) {
+    startTime = value;
+    emit startTimeChanged(startTime);
+}
+
+void Status::startJourney() {
+    setCycling(true);
+    setStartTime(QDateTime::currentMSecsSinceEpoch());
+}
diff --git a/src/status.h b/src/status.h
new file mode 100644 (file)
index 0000000..f0256e0
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef STATUS_H
+#define STATUS_H
+
+#include <QObject>
+
+class Status : public QObject
+{
+    Q_OBJECT
+public:
+    explicit Status(QObject *parent = nullptr);
+    Q_PROPERTY(bool cycling READ getCycling WRITE setCycling NOTIFY cyclingChanged)
+    Q_PROPERTY(quint64 startTime READ getStartTime WRITE setStartTime NOTIFY startTimeChanged)
+
+    bool getCycling() const;
+    quint64 getStartTime() const;
+    Q_INVOKABLE void startJourney();
+    Q_INVOKABLE quint64 getDuration() const;
+
+signals:
+    void cyclingChanged(bool cycling);
+    void startTimeChanged(quint64 startTime);
+
+public slots:
+    void setCycling(bool value);
+    void setStartTime(quint64 value);
+
+private:
+    bool cycling;
+    quint64 startTime;
+};
+
+#endif // STATUS_H