List journeys in descending order of time
[harbour-pedalo.git] / src / journeymodel.cpp
index 80a5eb5..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();
 }
 
@@ -30,6 +39,12 @@ void JourneyModel::editJourney(quint32 index, QDateTime start, quint32 duration,
     emit dataChanged(createIndex(index, 0), createIndex(index, 0));
 }
 
+void JourneyModel::deleteJourney(quint32 index) {
+    beginRemoveRows(QModelIndex(), index, index);
+    journeys.removeAt(index);
+    endRemoveRows();
+}
+
 int JourneyModel::rowCount(const QModelIndex & parent) const {
     Q_UNUSED(parent)
     return journeys.count();
@@ -99,9 +114,7 @@ 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();
 }
 
@@ -118,3 +131,29 @@ QDateTime JourneyModel::epochToDateTime(quint64 epoch) {
     date.setMSecsSinceEpoch(epoch);
     return date;
 }
+
+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;
+    }
+}
+