Add journey model and list
authorDavid Llewellyn-Jones <david@flypig.co.uk>
Mon, 2 Jul 2018 17:52:32 +0000 (18:52 +0100)
committerDavid Llewellyn-Jones <david@flypig.co.uk>
Mon, 2 Jul 2018 17:52:32 +0000 (18:52 +0100)
qml/pages/AddJourney.qml
qml/pages/JourneyList.qml
src/harbour-pedalo.cpp
src/journey.cpp
src/journey.h
src/journeymodel.cpp
src/journeymodel.h
translations/harbour-pedalo-de.ts
translations/harbour-pedalo.ts

index b0d8170..2330b13 100644 (file)
@@ -27,21 +27,19 @@ Dialog {
 
             ValueButton {
                 id: startDate
-                function openDateDialog() {
-                    var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", {
-                                    date: value
-                                 })
-
+                property date date: new Date()
+                label: "Date"
+                value: Qt.formatDate(date, 'd MMM yyyy')
+                width: parent.width
+                onClicked: {
+                    var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: value })
                     dialog.accepted.connect(function() {
-                        value = dialog.dateText
-                        selectedDate = dialog.date
+                        date = dialog.date
                     })
                 }
-
-                label: "Date"
-                value: Qt.formatDate(new Date(), 'd MMM yyyy')
-                width: parent.width
-                onClicked: openDateDialog()
+                onDateChanged: {
+                    value = Qt.formatDate(date, 'd MMM yyyy')
+                }
             }
 
             ValueButton {
@@ -123,4 +121,12 @@ 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)
+    }
 }
index 9d84129..8c6998f 100644 (file)
@@ -10,7 +10,7 @@ Page {
 
     SilicaListView {
         id: listView
-        model: 20
+        model: journeymodel
         anchors.fill: parent
         header: PageHeader {
             title: qsTr("Journey list")
@@ -24,17 +24,17 @@ Page {
 
                 Label {
                     width: columnwidth / 3.0
-                    text: qsTr("Item") + " " + index
+                    text: Qt.formatDate(journeymodel.epochToDate(start), "d MMM yyyy")
                     color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor
                 }
                 Label {
                     width: columnwidth / 3.0
-                    text: qsTr("1 May 2018")
+                    text: Qt.formatTime(journeymodel.epochToTime(start), "hh:mm")
                     color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor
                 }
                 Label {
                     width: columnwidth / 3.0
-                    text: qsTr("20 mins")
+                    text: parseInt(duration / (60 * 60)) + ":" + (parseInt(duration / 60) % 60)
                     color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor
                 }
             }
index 4cec530..acdcc2e 100644 (file)
@@ -1,9 +1,13 @@
 #ifdef QT_QML_DEBUG
 #include <QtQuick>
+#include <QDebug>
 #endif
 
 #include <sailfishapp.h>
 
+#include "journey.h"
+#include "journeymodel.h"
+
 int main(int argc, char *argv[])
 {
     // SailfishApp::main() will display "qml/harbour-pedalo.qml", if you need more
@@ -16,5 +20,29 @@ int main(int argc, char *argv[])
     //
     // To display the view, call "show()" (will show fullscreen on device).
 
-    return SailfishApp::main(argc, argv);
+    QScopedPointer<QGuiApplication> app(SailfishApp::application(argc, argv));
+
+    JourneyModel journeys;
+
+    QFile file;
+    file.setFileName(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/journeys.csv");
+    journeys.importFromFile(file);
+
+    QScopedPointer<QQuickView> view(SailfishApp::createView());
+    view->setSource(SailfishApp::pathTo("qml/harbour-pedalo.qml"));
+
+    QQmlContext *ctxt = view->rootContext();
+    ctxt->setContextProperty("journeymodel", &journeys);
+
+    view->show();
+    int result = app->exec();
+
+    // Write out the journey data
+    QDir dir;
+    dir.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
+    file.setFileName(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/journeys.csv");
+    qDebug() << "File saved as: " << file.fileName();
+    journeys.exportToFile(file);
+
+    return result;
 }
index af13ec8..775ece7 100644 (file)
@@ -21,6 +21,29 @@ quint64 Journey::getStart () const {
     return start;
 }
 
+QDate Journey::getStartDate() const {
+    QDateTime date;
+
+    date.setMSecsSinceEpoch(start);
+    return date.date();
+}
+
+QTime Journey::getStartTime() const {
+    QDateTime time;
+
+    time.setMSecsSinceEpoch(start);
+    return time.time();
+}
+
+QTime Journey::getEndTime() const {
+    QDateTime time;
+
+    time.setMSecsSinceEpoch(start);
+    time.addSecs(duration);
+
+    return time.time();
+}
+
 qint32 Journey::getDuration () const {
     return duration;
 }
@@ -38,6 +61,35 @@ void Journey::setStart (const quint64 value) {
     start = value;
 }
 
+void Journey::setStartDate (const QDate &value) {
+    QDateTime time;
+
+    time.setMSecsSinceEpoch(start);
+    time.setDate(value);
+}
+
+void Journey::setStartTime (const QTime &value) {
+    QDateTime time;
+
+    time.setMSecsSinceEpoch(start);
+    time.setTime(value);
+}
+
+void Journey::setEndTime(const QTime &value) {
+    qint64 difference;
+    QDateTime starttime;
+    QDateTime endtime;
+
+    starttime.setMSecsSinceEpoch(start);
+    endtime = QDateTime(starttime);
+    endtime.setTime(value);
+    difference = starttime.secsTo(endtime);
+    if (difference < 0) {
+        difference %= 24 * 60 * 60;
+    }
+    duration = difference;
+}
+
 void Journey::setDuration (qint32 value) {
     duration = value;
 }
index 45393b3..560937e 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <QObject>
 #include <QTime>
+#include <QDate>
 
 class Journey
 {
@@ -11,11 +12,17 @@ public:
     Journey(quint64 start, quint32 duration, quint32 overtook, quint32 overtakenby);
 
     quint64 getStart () const;
+    QDate getStartDate() const;
+    QTime getStartTime() const;
+    QTime getEndTime() const;
     qint32 getDuration () const;
     qint32 getOvertook () const;
     qint32 getOvertakenBy () const;
 
     void setStart (const quint64 value);
+    void setStartDate (const QDate &value);
+    void setStartTime (const QTime &value);
+    void setEndTime(const QTime &value);
     void setDuration (qint32 value);
     void setOvertook (qint32 value);
     void setOvertakenBy (qint32 value);
@@ -27,5 +34,4 @@ private:
     quint32 overtakenby;
 };
 
-
 #endif // JOURNEY_H
index 18f83c2..7286c6d 100644 (file)
@@ -19,6 +19,10 @@ void JourneyModel::addJourney(const Journey &journey)
     endInsertRows();
 }
 
+void JourneyModel::addJourney(QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby) {
+    addJourney(Journey(start.currentMSecsSinceEpoch(), duration, overtook, overtakenby));
+}
+
 int JourneyModel::rowCount(const QModelIndex & parent) const {
     Q_UNUSED(parent)
     return journeys.count();
@@ -55,6 +59,9 @@ void JourneyModel::exportToFile(QFile & file) {
         }
         file.close();
     }
+    else {
+        qDebug() << "File failed to export";
+    }
 }
 
 void JourneyModel::importFromFile(QFile & file) {
@@ -82,3 +89,16 @@ void JourneyModel::importFromFile(QFile & file) {
 }
 
 
+QDate JourneyModel::epochToDate(quint64 epoch) {
+    QDateTime date;
+
+    date.setMSecsSinceEpoch(epoch);
+    return date.date();
+}
+
+QTime JourneyModel::epochToTime(quint64 epoch) {
+    QDateTime date;
+
+    date.setMSecsSinceEpoch(epoch);
+    return date.time();
+}
index 36d4a92..398862a 100644 (file)
@@ -23,6 +23,7 @@ public:
     JourneyModel(QObject *parent = 0);
 
     void addJourney(const Journey &journey);
+    Q_INVOKABLE void addJourney(QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby);
 
     int rowCount(const QModelIndex & parent = QModelIndex()) const;
 
@@ -33,6 +34,8 @@ public:
     void exportToFile(QFile & file);
     void importFromFile(QFile & file);
 
+    Q_INVOKABLE static QDate epochToDate(quint64 epoch);
+    Q_INVOKABLE static QTime epochToTime(quint64 epoch);
 signals:
     // General signals
     void journeysChanged();
index acb00bb..b4b309c 100644 (file)
 <context>
     <name>AddJourney</name>
     <message>
-        <source>Add journey</source>
-        <translation type="unfinished"></translation>
-    </message>
-    <message>
         <source>Start time</source>
         <translation type="unfinished"></translation>
     </message>
 <context>
     <name>JourneyList</name>
     <message>
-        <source>Item</source>
-        <translation type="unfinished">Element</translation>
-    </message>
-    <message>
         <source>Journey list</source>
         <translation type="unfinished"></translation>
     </message>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <source>Time spent cycling</source>
+        <source>Journeys:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <source>Average journey duration</source>
+        <source>Time spent cycling:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <source>Speed percentile</source>
+        <source>Average journey duration:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <source>Journeys:</source>
+        <source>Speed percentile:</source>
         <translation type="unfinished"></translation>
     </message>
 </context>
index 2b821ce..098c5b2 100644 (file)
 <context>
     <name>AddJourney</name>
     <message>
-        <source>Add journey</source>
-        <translation type="unfinished"></translation>
-    </message>
-    <message>
         <source>Start time</source>
         <translation type="unfinished"></translation>
     </message>
 <context>
     <name>JourneyList</name>
     <message>
-        <source>Item</source>
-        <translation type="unfinished"></translation>
-    </message>
-    <message>
         <source>Journey list</source>
         <translation type="unfinished"></translation>
     </message>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <source>Time spent cycling</source>
+        <source>Journeys:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <source>Average journey duration</source>
+        <source>Time spent cycling:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <source>Speed percentile</source>
+        <source>Average journey duration:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <source>Journeys:</source>
+        <source>Speed percentile:</source>
         <translation type="unfinished"></translation>
     </message>
 </context>