From e917baa52e7157c7c41424527c3881c22ff65588 Mon Sep 17 00:00:00 2001 From: David Llewellyn-Jones Date: Mon, 2 Jul 2018 18:52:32 +0100 Subject: [PATCH] Add journey model and list --- qml/pages/AddJourney.qml | 30 +++++++++++------- qml/pages/JourneyList.qml | 8 ++--- src/harbour-pedalo.cpp | 30 +++++++++++++++++- src/journey.cpp | 52 +++++++++++++++++++++++++++++++ src/journey.h | 8 ++++- src/journeymodel.cpp | 20 ++++++++++++ src/journeymodel.h | 3 ++ translations/harbour-pedalo-de.ts | 16 +++------- translations/harbour-pedalo.ts | 16 +++------- 9 files changed, 141 insertions(+), 42 deletions(-) diff --git a/qml/pages/AddJourney.qml b/qml/pages/AddJourney.qml index b0d8170..2330b13 100644 --- a/qml/pages/AddJourney.qml +++ b/qml/pages/AddJourney.qml @@ -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) + } } diff --git a/qml/pages/JourneyList.qml b/qml/pages/JourneyList.qml index 9d84129..8c6998f 100644 --- a/qml/pages/JourneyList.qml +++ b/qml/pages/JourneyList.qml @@ -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 } } diff --git a/src/harbour-pedalo.cpp b/src/harbour-pedalo.cpp index 4cec530..acdcc2e 100644 --- a/src/harbour-pedalo.cpp +++ b/src/harbour-pedalo.cpp @@ -1,9 +1,13 @@ #ifdef QT_QML_DEBUG #include +#include #endif #include +#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 app(SailfishApp::application(argc, argv)); + + JourneyModel journeys; + + QFile file; + file.setFileName(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/journeys.csv"); + journeys.importFromFile(file); + + QScopedPointer 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; } diff --git a/src/journey.cpp b/src/journey.cpp index af13ec8..775ece7 100644 --- a/src/journey.cpp +++ b/src/journey.cpp @@ -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; } diff --git a/src/journey.h b/src/journey.h index 45393b3..560937e 100644 --- a/src/journey.h +++ b/src/journey.h @@ -3,6 +3,7 @@ #include #include +#include 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 diff --git a/src/journeymodel.cpp b/src/journeymodel.cpp index 18f83c2..7286c6d 100644 --- a/src/journeymodel.cpp +++ b/src/journeymodel.cpp @@ -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(); +} diff --git a/src/journeymodel.h b/src/journeymodel.h index 36d4a92..398862a 100644 --- a/src/journeymodel.h +++ b/src/journeymodel.h @@ -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(); diff --git a/translations/harbour-pedalo-de.ts b/translations/harbour-pedalo-de.ts index acb00bb..b4b309c 100644 --- a/translations/harbour-pedalo-de.ts +++ b/translations/harbour-pedalo-de.ts @@ -14,10 +14,6 @@ AddJourney - - Add journey - - Start time @@ -48,10 +44,6 @@ JourneyList - - Item - Element - Journey list @@ -111,19 +103,19 @@ - Time spent cycling + Journeys: - Average journey duration + Time spent cycling: - Speed percentile + Average journey duration: - Journeys: + Speed percentile: diff --git a/translations/harbour-pedalo.ts b/translations/harbour-pedalo.ts index 2b821ce..098c5b2 100644 --- a/translations/harbour-pedalo.ts +++ b/translations/harbour-pedalo.ts @@ -14,10 +14,6 @@ AddJourney - - Add journey - - Start time @@ -48,10 +44,6 @@ JourneyList - - Item - - Journey list @@ -111,19 +103,19 @@ - Time spent cycling + Journeys: - Average journey duration + Time spent cycling: - Speed percentile + Average journey duration: - Journeys: + Speed percentile: -- 2.25.1