From: David Llewellyn-Jones Date: Mon, 16 Jul 2018 02:18:07 +0000 (+0100) Subject: List journeys in descending order of time X-Git-Url: https://www.flypig.org.uk/git/?p=harbour-pedalo.git;a=commitdiff_plain;h=70180c4c4fd2807d16f90562d6876f3f07d388b1;hp=d8058e5edcec89d8103ead111f87dc9040323685 List journeys in descending order of time --- diff --git a/src/harbour-pedalo.cpp b/src/harbour-pedalo.cpp index 6c0094d..e8f123d 100644 --- a/src/harbour-pedalo.cpp +++ b/src/harbour-pedalo.cpp @@ -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 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", ¤tStatus); diff --git a/src/journey.h b/src/journey.h index 560937e..cd3df88 100644 --- a/src/journey.h +++ b/src/journey.h @@ -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 { +public: + bool operator() (Journey const &lhs, Journey const &rhs) const { + return lhs.start < rhs.start; + } +}; + +class JourneyTimeDescending : std::binary_function { +public: + bool operator() (Journey const &lhs, Journey const &rhs) const { + return lhs.start > rhs.start; + } +}; + #endif // JOURNEY_H diff --git a/src/journeymodel.cpp b/src/journeymodel.cpp index 1dc7d58..2919d83 100644 --- a/src/journeymodel.cpp +++ b/src/journeymodel.cpp @@ -14,8 +14,17 @@ QHash JourneyModel::roleNames() const { void JourneyModel::addJourney(const Journey &journey) { - beginInsertRows(QModelIndex(), rowCount(), rowCount()); - journeys << journey; + // Find position to add it + QList::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 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; + } +} + diff --git a/src/journeymodel.h b/src/journeymodel.h index ded0184..ccb4ffc 100644 --- a/src/journeymodel.h +++ b/src/journeymodel.h @@ -41,6 +41,8 @@ public: Q_INVOKABLE static QDateTime epochToDateTime(quint64 epoch); QList const & getData() const; + + void sort(int column, Qt::SortOrder order); signals: // General signals void journeysChanged();