Mobile: provide direct access to dives in DiveListModel

Accesses were via DiveObjectHelpers. Provide a direct access to
struct dive *. Use this for the filter - there is no point in
mass generating DiveHelperObjects in the filter code.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-08-13 21:11:08 +02:00 committed by bstoeger
parent ca939300e2
commit 62f1a92068
2 changed files with 14 additions and 6 deletions

View file

@ -51,8 +51,8 @@ void DiveListSortModel::resetFilter()
bool DiveListSortModel::filterAcceptsRow(int source_row, const QModelIndex &) const bool DiveListSortModel::filterAcceptsRow(int source_row, const QModelIndex &) const
{ {
DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel()); DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel());
DiveObjectHelper d = mySourceModel->at(source_row); const dive *d = mySourceModel->getDive(source_row);
return d && !d.getDive()->hidden_by_filter; return d && !d->hidden_by_filter;
} }
int DiveListSortModel::shown() int DiveListSortModel::shown()
@ -262,11 +262,18 @@ DiveListModel *DiveListModel::instance()
return m_instance; return m_instance;
} }
DiveObjectHelper DiveListModel::at(int i) struct dive *DiveListModel::getDive(int i)
{ {
if (i < 0 || i >= dive_table.nr) { if (i < 0 || i >= dive_table.nr) {
qWarning("DiveListModel::at(): accessing invalid dive with id %d", i); qWarning("DiveListModel::getDive(): accessing invalid dive with id %d", i);
return DiveObjectHelper(); // Returns an invalid DiveObjectHelper that will crash on access. return nullptr;
} }
return DiveObjectHelper(dive_table.dives[i]); return dive_table.dives[i];
}
DiveObjectHelper DiveListModel::at(int i)
{
// For an invalid id, returns an invalid DiveObjectHelper that will crash on access.
dive *d = getDive(i);
return d ? DiveObjectHelper(d) : DiveObjectHelper();
} }

View file

@ -55,6 +55,7 @@ public:
void removeDiveById(int id); void removeDiveById(int id);
void updateDive(int i, dive *d); void updateDive(int i, dive *d);
void reload(); void reload();
struct dive *getDive(int i);
int rowCount(const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const;
int getDiveIdx(int id) const; int getDiveIdx(int id) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;