X-Git-Url: https://www.flypig.org.uk/git/?p=harbour-pedalo.git;a=blobdiff_plain;f=src%2Fjourneymodel.cpp;h=2919d83388c47fe3cd520936c4e8d12f8d47c12d;hp=80a5eb593ec1b48c7de07df2f9cfceb42e35b32a;hb=70180c4c4fd2807d16f90562d6876f3f07d388b1;hpb=371dcf3335b355f8d421352a394161dc6d9b9f24 diff --git a/src/journeymodel.cpp b/src/journeymodel.cpp index 80a5eb5..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(); } @@ -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 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; + } +} +