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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-09-14 19:58:30 +02:00 committed by Dirk Hohndel
parent ce751bd696
commit 9322c54b6a
3 changed files with 12 additions and 15 deletions

View file

@ -353,8 +353,7 @@ Kirigami.ScrollablePage {
} }
Controls.Label { Controls.Label {
text: { text: {
var trip = diveListView.model.tripIdToObject(section); diveListView.model.tripShortDate(section)
diveListView.model.tripShortDate(trip);
} }
color: subsurfaceTheme.primaryTextColor color: subsurfaceTheme.primaryTextColor
font.pointSize: subsurfaceTheme.smallPointSize font.pointSize: subsurfaceTheme.smallPointSize
@ -379,8 +378,7 @@ Kirigami.ScrollablePage {
Controls.Label { Controls.Label {
id: sectionText id: sectionText
text: { text: {
var trip = diveListView.model.tripIdToObject(section); diveListView.model.tripTitle(section)
diveListView.model.tripTitle(trip);
} }
wrapMode: Text.WrapAtWordBoundaryOrAnywhere wrapMode: Text.WrapAtWordBoundaryOrAnywhere
visible: text !== "" visible: text !== ""

View file

@ -79,26 +79,26 @@ void DiveListSortModel::reload()
// In QtQuick ListView, section headings can only be strings. To identify dives // 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. // 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. // 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()) if (s.isEmpty())
return QVariant(); return nullptr;
int id = s.toInt(); int id = s.toInt();
dive_trip **trip = std::find_if(&trip_table.trips[0], &trip_table.trips[trip_table.nr], 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; }); [id] (const dive_trip *t) { return t->id == id; });
if (trip == &trip_table.trips[trip_table.nr]) { if (trip == &trip_table.trips[trip_table.nr]) {
fprintf(stderr, "Warning: unknown trip id passed through QML: %d\n", id); 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) // the trip title is designed to be location (# dives)
// or, if there is no location name date range (# 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" // 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 &section)
{ {
dive_trip *dt = tripIn.value<dive_trip *>(); const dive_trip *dt = tripIdToObject(section);
if (!dt) if (!dt)
return QString(); return QString();
QString numDives = tr("(%n dive(s))", "", dt->dives.nr); 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); return QStringLiteral("%1 %2%3").arg(title, numDives, shownDives);
} }
QString DiveListSortModel::tripShortDate(const QVariant &tripIn) QString DiveListSortModel::tripShortDate(const QString &section)
{ {
dive_trip *dt = tripIn.value<dive_trip *>(); const dive_trip *dt = tripIdToObject(section);
if (!dt) if (!dt)
return QString(); return QString();
QDateTime firstTime = QDateTime::fromMSecsSinceEpoch(1000*trip_date(dt), Qt::UTC); QDateTime firstTime = QDateTime::fromMSecsSinceEpoch(1000*trip_date(dt), Qt::UTC);

View file

@ -14,9 +14,8 @@ public:
DiveListSortModel(QObject *parent = 0); DiveListSortModel(QObject *parent = 0);
void setSourceModel(QAbstractItemModel *sourceModel); void setSourceModel(QAbstractItemModel *sourceModel);
Q_INVOKABLE void reload(); Q_INVOKABLE void reload();
Q_INVOKABLE QVariant tripIdToObject(const QString &s); Q_INVOKABLE QString tripTitle(const QString &trip);
Q_INVOKABLE QString tripTitle(const QVariant &trip); Q_INVOKABLE QString tripShortDate(const QString &trip);
Q_INVOKABLE QString tripShortDate(const QVariant &trip);
public slots: public slots:
int getIdxForId(int id); int getIdxForId(int id);
void setFilter(QString f); void setFilter(QString f);