diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-08-13 08:19:04 +0200 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2019-09-14 13:20:59 +0200 |
commit | f8c5c8bedf9c0af8f031da7fc74c516a40e9e1c6 (patch) | |
tree | 92c557ab1de0506e1e0a33bc2ab22738e17393e4 | |
parent | be763452adc110cfcc011322d989698d897dd6ed (diff) | |
download | subsurface-f8c5c8bedf9c0af8f031da7fc74c516a40e9e1c6.tar.gz |
Mobile: Generate DiveObjectHelpers on the fly
Instead of keeping track of a list of DiveObjectHelpers, generate
them on-the-fly in DiveListModel. Thus, there is less danger of
model and core getting out of sync. On the flip-side, now the
DiveListModel and the DiveListSortModel might get out of sync.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | core/subsurface-qt/DiveObjectHelper.cpp | 10 | ||||
-rw-r--r-- | core/subsurface-qt/DiveObjectHelper.h | 2 | ||||
-rw-r--r-- | qt-models/divelistmodel.cpp | 48 | ||||
-rw-r--r-- | qt-models/divelistmodel.h | 3 |
4 files changed, 31 insertions, 32 deletions
diff --git a/core/subsurface-qt/DiveObjectHelper.cpp b/core/subsurface-qt/DiveObjectHelper.cpp index c19c03eb4..cb6cf6dd1 100644 --- a/core/subsurface-qt/DiveObjectHelper.cpp +++ b/core/subsurface-qt/DiveObjectHelper.cpp @@ -67,6 +67,16 @@ DiveObjectHelper::DiveObjectHelper(struct dive *d) : { } +DiveObjectHelper::operator bool() const +{ + return !!m_dive; +} + +bool DiveObjectHelper::operator!() const +{ + return !m_dive; +} + int DiveObjectHelper::number() const { return m_dive->number; diff --git a/core/subsurface-qt/DiveObjectHelper.h b/core/subsurface-qt/DiveObjectHelper.h index 7071c1580..674386e56 100644 --- a/core/subsurface-qt/DiveObjectHelper.h +++ b/core/subsurface-qt/DiveObjectHelper.h @@ -50,6 +50,8 @@ class DiveObjectHelper { public: DiveObjectHelper(); // This is only to be used by Qt's metatype system! DiveObjectHelper(struct dive *dive); + operator bool() const; // Returns false if this is an invalid default-generated object + bool operator!() const; // Returns true if this is an invalid default-generated object int number() const; int id() const; struct dive *getDive() const; diff --git a/qt-models/divelistmodel.cpp b/qt-models/divelistmodel.cpp index 028bd3600..385107b6c 100644 --- a/qt-models/divelistmodel.cpp +++ b/qt-models/divelistmodel.cpp @@ -51,8 +51,8 @@ void DiveListSortModel::resetFilter() bool DiveListSortModel::filterAcceptsRow(int source_row, const QModelIndex &) const { DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel()); - DiveObjectHelper *d = mySourceModel->at(source_row); - return d && !d->getDive()->hidden_by_filter; + DiveObjectHelper d = mySourceModel->at(source_row); + return d && !d.getDive()->hidden_by_filter; } int DiveListSortModel::shown() @@ -148,9 +148,6 @@ void DiveListModel::addDive(const QList<dive *> &listOfDives) if (listOfDives.isEmpty()) return; beginInsertRows(QModelIndex(), rowCount(), rowCount() + listOfDives.count() - 1); - for (dive *d: listOfDives) { - m_dives.append(new DiveObjectHelper(d)); - } endInsertRows(); } @@ -165,25 +162,22 @@ void DiveListModel::addAllDives() } -void DiveListModel::insertDive(int i, DiveObjectHelper *newDive) +void DiveListModel::insertDive(int i, DiveObjectHelper *) { beginInsertRows(QModelIndex(), i, i); - m_dives.insert(i, newDive); endInsertRows(); } void DiveListModel::removeDive(int i) { beginRemoveRows(QModelIndex(), i, i); - delete m_dives.at(i); - m_dives.removeAt(i); endRemoveRows(); } void DiveListModel::removeDiveById(int id) { - for (int i = 0; i < rowCount(); i++) { - if (m_dives.at(i)->id() == id) { + for (int i = 0; i < dive_table.nr; i++) { + if (dive_table.dives[i]->id == id) { removeDive(i); return; } @@ -192,21 +186,16 @@ void DiveListModel::removeDiveById(int id) void DiveListModel::updateDive(int i, dive *d) { - DiveObjectHelper *newDive = new DiveObjectHelper(d); // we need to make sure that QML knows that this dive has changed - // the only reliable way I've found is to remove and re-insert it removeDive(i); - insertDive(i, newDive); + insertDive(i, nullptr); // TODO: DiveObjectHelper not needed anymore - remove second argument } void DiveListModel::clear() { - if (m_dives.count()) { - beginRemoveRows(QModelIndex(), 0, m_dives.count() - 1); - qDeleteAll(m_dives); - m_dives.clear(); - endRemoveRows(); - } + beginResetModel(); + endResetModel(); } void DiveListModel::resetInternalData() @@ -221,25 +210,20 @@ void DiveListModel::resetInternalData() int DiveListModel::rowCount(const QModelIndex &) const { - return m_dives.count(); + return dive_table.nr; } int DiveListModel::getDiveIdx(int id) const { - int i; - for (i = 0; i < m_dives.count(); i++) { - if (m_dives.at(i)->id() == id) - return i; - } - return -1; + return get_idx_by_uniq_id(id); } QVariant DiveListModel::data(const QModelIndex &index, int role) const { - if(index.row() < 0 || index.row() >= m_dives.count()) + if(index.row() < 0 || index.row() >= dive_table.nr) return QVariant(); - DiveObjectHelper &curr_dive = *m_dives[index.row()]; + DiveObjectHelper curr_dive(dive_table.dives[index.row()]); const dive *d = curr_dive.getDive(); if (!d) return QVariant(); @@ -305,7 +289,11 @@ DiveListModel *DiveListModel::instance() return m_instance; } -DiveObjectHelper *DiveListModel::at(int i) +DiveObjectHelper DiveListModel::at(int i) { - return m_dives.at(i); + if (i < 0 || i >= dive_table.nr) { + qWarning("DiveListModel::at(): accessing invalid dive with id %d", i); + return DiveObjectHelper(); // Returns an invalid DiveObjectHelper that will crash on access. + } + return DiveObjectHelper(dive_table.dives[i]); } diff --git a/qt-models/divelistmodel.h b/qt-models/divelistmodel.h index adda8b127..65c117212 100644 --- a/qt-models/divelistmodel.h +++ b/qt-models/divelistmodel.h @@ -62,9 +62,8 @@ public: QHash<int, QByteArray> roleNames() const; QString startAddDive(); void resetInternalData(); - Q_INVOKABLE DiveObjectHelper* at(int i); + Q_INVOKABLE DiveObjectHelper at(int i); private: - QList<DiveObjectHelper*> m_dives; static DiveListModel *m_instance; }; |