List journeys in descending order of time
authorDavid Llewellyn-Jones <david@flypig.co.uk>
Mon, 16 Jul 2018 02:18:07 +0000 (03:18 +0100)
committerDavid Llewellyn-Jones <david@flypig.co.uk>
Mon, 16 Jul 2018 02:18:07 +0000 (03:18 +0100)
src/harbour-pedalo.cpp
src/journey.h
src/journeymodel.cpp
src/journeymodel.h

index 6c0094d..e8f123d 100644 (file)
@@ -43,6 +43,7 @@ int main(int argc, char *argv[])
     QFile file;
     file.setFileName(Settings::getConfigDir() + "/journeys.csv");
     journeys.importFromFile(file);
+    journeys.sort(JourneyModel::StartRole, Qt::DescendingOrder);
 
     QScopedPointer<QQuickView> view(SailfishApp::createView());
     view->engine()->addImageProvider(QLatin1String("pedalo"), new ImageProvider(Settings::getInstance()));
@@ -54,6 +55,7 @@ int main(int argc, char *argv[])
     qDebug() << "VERSION_MAJOR: " << VERSION_MAJOR;
     qDebug() << "VERSION_MINOR: " << VERSION_MINOR;
     qDebug() << "VERSION_BUILD: " << VERSION_BUILD;
+
     ctxt->setContextProperty("journeymodel", &journeys);
     ctxt->setContextProperty("currentStatus", &currentStatus);
 
index 560937e..cd3df88 100644 (file)
@@ -8,6 +8,9 @@
 class Journey
 {
 public:
+    friend class JourneyTimeDescending;
+    friend class JourneyTimeAscending;
+
     Journey();
     Journey(quint64 start, quint32 duration, quint32 overtook, quint32 overtakenby);
 
@@ -34,4 +37,18 @@ private:
     quint32 overtakenby;
 };
 
+class JourneyTimeAscending : std::binary_function<Journey,Journey, bool> {
+public:
+    bool operator() (Journey const &lhs, Journey const &rhs) const {
+        return lhs.start < rhs.start;
+    }
+};
+
+class JourneyTimeDescending : std::binary_function<Journey,Journey, bool> {
+public:
+    bool operator() (Journey const &lhs, Journey const &rhs) const {
+        return lhs.start > rhs.start;
+    }
+};
+
 #endif // JOURNEY_H
index 1dc7d58..2919d83 100644 (file)
@@ -14,8 +14,17 @@ QHash<int, QByteArray> JourneyModel::roleNames() const {
 
 void JourneyModel::addJourney(const Journey &journey)
 {
-    beginInsertRows(QModelIndex(), rowCount(), rowCount());
-    journeys << journey;
+    // Find position to add it
+    QList<Journey>::const_iterator it = journeys.constBegin();
+
+    unsigned int position = 0;
+    while ((it != journeys.constEnd()) && (journey.getStart() < (*it).getStart())) {
+        position++;
+        it++;
+    }
+
+    beginInsertRows(QModelIndex(), position, position);
+    journeys.insert(position, journey);
     endInsertRows();
 }
 
@@ -126,3 +135,25 @@ QDateTime JourneyModel::epochToDateTime(quint64 epoch) {
 QList<Journey> const & JourneyModel::getData() const {
     return journeys;
 }
+
+void JourneyModel::sort(int column, Qt::SortOrder order) {
+    switch (column) {
+    case JourneyRoles::StartRole:
+        switch (order) {
+        case Qt::SortOrder::AscendingOrder:
+            std::sort(journeys.begin(), journeys.end(), JourneyTimeAscending());
+            break;
+        case Qt::SortOrder::DescendingOrder:
+            std::sort(journeys.begin(), journeys.end(), JourneyTimeDescending());
+            break;
+        default:
+            // Do nothing
+            break;
+        }
+        break;
+    default:
+        // Do nothing
+        break;
+    }
+}
+
index ded0184..ccb4ffc 100644 (file)
@@ -41,6 +41,8 @@ public:
     Q_INVOKABLE static QDateTime epochToDateTime(quint64 epoch);
 
     QList<Journey> const & getData() const;
+
+    void sort(int column, Qt::SortOrder order);
 signals:
     // General signals
     void journeysChanged();