aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-09-14 19:58:30 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-09-21 16:12:23 -0700
commit9322c54b6a1e1bb85954b106be5410e413a05c14 (patch)
tree3d2c4c5ea65bfa6c4bc4f68baa6f37d773117dc7
parentce751bd6963d13c23f554da4e5fc66ee0d74eb1f (diff)
downloadsubsurface-9322c54b6a1e1bb85954b106be5410e413a05c14.tar.gz
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>
-rw-r--r--mobile-widgets/qml/DiveList.qml6
-rw-r--r--qt-models/divelistmodel.cpp16
-rw-r--r--qt-models/divelistmodel.h5
3 files changed, 12 insertions, 15 deletions
diff --git a/mobile-widgets/qml/DiveList.qml b/mobile-widgets/qml/DiveList.qml
index a2cb1e40c..b2c99a02b 100644
--- a/mobile-widgets/qml/DiveList.qml
+++ b/mobile-widgets/qml/DiveList.qml
@@ -353,8 +353,7 @@ Kirigami.ScrollablePage {
}
Controls.Label {
text: {
- var trip = diveListView.model.tripIdToObject(section);
- diveListView.model.tripShortDate(trip);
+ diveListView.model.tripShortDate(section)
}
color: subsurfaceTheme.primaryTextColor
font.pointSize: subsurfaceTheme.smallPointSize
@@ -379,8 +378,7 @@ Kirigami.ScrollablePage {
Controls.Label {
id: sectionText
text: {
- var trip = diveListView.model.tripIdToObject(section);
- diveListView.model.tripTitle(trip);
+ diveListView.model.tripTitle(section)
}
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
visible: text !== ""
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 &section)
{
- dive_trip *dt = tripIn.value<dive_trip *>();
+ 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 &section)
{
- dive_trip *dt = tripIn.value<dive_trip *>();
+ const dive_trip *dt = tripIdToObject(section);
if (!dt)
return QString();
QDateTime firstTime = QDateTime::fromMSecsSinceEpoch(1000*trip_date(dt), Qt::UTC);
diff --git a/qt-models/divelistmodel.h b/qt-models/divelistmodel.h
index c355aceaf..d6eb07463 100644
--- a/qt-models/divelistmodel.h
+++ b/qt-models/divelistmodel.h
@@ -14,9 +14,8 @@ public:
DiveListSortModel(QObject *parent = 0);
void setSourceModel(QAbstractItemModel *sourceModel);
Q_INVOKABLE void reload();
- Q_INVOKABLE QVariant tripIdToObject(const QString &s);
- Q_INVOKABLE QString tripTitle(const QVariant &trip);
- Q_INVOKABLE QString tripShortDate(const QVariant &trip);
+ Q_INVOKABLE QString tripTitle(const QString &trip);
+ Q_INVOKABLE QString tripShortDate(const QString &trip);
public slots:
int getIdxForId(int id);
void setFilter(QString f);