From 9322c54b6a1e1bb85954b106be5410e413a05c14 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 14 Sep 2019 19:58:30 +0200 Subject: Mobile: pass section directly to tripTitle() and tripShortDate() Instead of converting the section-heading string to a trip-pointer in QML and pass that to the tripTitle() and tripShortDate() functions, pass the string and convert in C++ code. Hopefully, this makes the code more robust. Signed-off-by: Berthold Stoeger --- qt-models/divelistmodel.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'qt-models/divelistmodel.cpp') diff --git a/qt-models/divelistmodel.cpp b/qt-models/divelistmodel.cpp index 4b0058b43..779e695ab 100644 --- a/qt-models/divelistmodel.cpp +++ b/qt-models/divelistmodel.cpp @@ -79,26 +79,26 @@ void DiveListSortModel::reload() // In QtQuick ListView, section headings can only be strings. To identify dives // that belong to the same trip, a string containing the trip-id is passed in. // To format the trip heading, the string is then converted back with this function. -QVariant DiveListSortModel::tripIdToObject(const QString &s) +static dive_trip *tripIdToObject(const QString &s) { if (s.isEmpty()) - return QVariant(); + return nullptr; int id = s.toInt(); dive_trip **trip = std::find_if(&trip_table.trips[0], &trip_table.trips[trip_table.nr], [id] (const dive_trip *t) { return t->id == id; }); if (trip == &trip_table.trips[trip_table.nr]) { fprintf(stderr, "Warning: unknown trip id passed through QML: %d\n", id); - return QVariant(); + return nullptr; } - return QVariant::fromValue(*trip); + return *trip; } // the trip title is designed to be location (# dives) // or, if there is no location name date range (# dives) // where the date range is given as "month year" or "month-month year" or "month year - month year" -QString DiveListSortModel::tripTitle(const QVariant &tripIn) +QString DiveListSortModel::tripTitle(const QString §ion) { - dive_trip *dt = tripIn.value(); + const dive_trip *dt = tripIdToObject(section); if (!dt) return QString(); QString numDives = tr("(%n dive(s))", "", dt->dives.nr); @@ -124,9 +124,9 @@ QString DiveListSortModel::tripTitle(const QVariant &tripIn) return QStringLiteral("%1 %2%3").arg(title, numDives, shownDives); } -QString DiveListSortModel::tripShortDate(const QVariant &tripIn) +QString DiveListSortModel::tripShortDate(const QString §ion) { - dive_trip *dt = tripIn.value(); + const dive_trip *dt = tripIdToObject(section); if (!dt) return QString(); QDateTime firstTime = QDateTime::fromMSecsSinceEpoch(1000*trip_date(dt), Qt::UTC); -- cgit v1.2.3-70-g09d2