List journeys in descending order of time
[harbour-pedalo.git] / src / journeymodel.cpp
index 1dc7d58..2919d83 100644 (file)
@@ -14,8 +14,17 @@ QHash<int, QByteArray> JourneyModel::roleNames() const {
 
 void JourneyModel::addJourney(const Journey &journey)
 {
 
 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();
 }
 
     endInsertRows();
 }
 
@@ -126,3 +135,25 @@ QDateTime JourneyModel::epochToDateTime(quint64 epoch) {
 QList<Journey> const & JourneyModel::getData() const {
     return journeys;
 }
 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;
+    }
+}
+