Dive list: implement DiveTripModelBase::reset()

On desktop, resetting the model is realized by generating a new
model object. This is due to the fact that we have two different
models (tree and list) and for switching between those, we have
to create a new object.

On mobile, currently there are no plans to support the list-mode.
Therefore, there is no reason the recreate the object. Instead,
implement a reset() function that reloads the core data.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-12-11 09:50:38 +01:00 committed by Dirk Hohndel
parent 37b24857ed
commit 7e12ac262b
2 changed files with 25 additions and 1 deletions

View file

@ -379,6 +379,15 @@ void DiveTripModelBase::clear()
endResetModel(); endResetModel();
} }
void DiveTripModelBase::reset()
{
beginResetModel();
clearData();
populate();
endResetModel();
initSelection();
}
DiveTripModelBase::DiveTripModelBase(QObject *parent) : QAbstractItemModel(parent) DiveTripModelBase::DiveTripModelBase(QObject *parent) : QAbstractItemModel(parent)
{ {
} }
@ -561,7 +570,11 @@ DiveTripModelTree::DiveTripModelTree(QObject *parent) : DiveTripModelBase(parent
connect(&diveListNotifier, &DiveListNotifier::tripChanged, this, &DiveTripModelTree::tripChanged); connect(&diveListNotifier, &DiveListNotifier::tripChanged, this, &DiveTripModelTree::tripChanged);
connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelTree::filterReset); connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelTree::filterReset);
// Fill model populate();
}
void DiveTripModelTree::populate()
{
for (int i = 0; i < dive_table.nr ; ++i) { for (int i = 0; i < dive_table.nr ; ++i) {
dive *d = get_dive(i); dive *d = get_dive(i);
update_cylinder_related_info(d); update_cylinder_related_info(d);
@ -1286,6 +1299,11 @@ DiveTripModelList::DiveTripModelList(QObject *parent) : DiveTripModelBase(parent
connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelList::divesSelected); connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelList::divesSelected);
connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelList::filterReset); connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelList::filterReset);
populate();
}
void DiveTripModelList::populate()
{
// Fill model // Fill model
items.reserve(dive_table.nr); items.reserve(dive_table.nr);
for (int i = 0; i < dive_table.nr ; ++i) for (int i = 0; i < dive_table.nr ; ++i)

View file

@ -63,6 +63,9 @@ public:
// Clear all dives // Clear all dives
void clear(); void clear();
// Reload data
void reset();
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
@ -91,6 +94,7 @@ protected:
virtual dive *diveOrNull(const QModelIndex &index) const = 0; // Returns a dive if this index represents a dive, null otherwise virtual dive *diveOrNull(const QModelIndex &index) const = 0; // Returns a dive if this index represents a dive, null otherwise
virtual void clearData() = 0; virtual void clearData() = 0;
virtual void populate() = 0;
}; };
class DiveTripModelTree : public DiveTripModelBase class DiveTripModelTree : public DiveTripModelBase
@ -112,6 +116,7 @@ public:
private: private:
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
void clearData() override; void clearData() override;
void populate() override;
QModelIndex index(int row, int column, const QModelIndex &parent) const override; QModelIndex index(int row, int column, const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &index) const override; QModelIndex parent(const QModelIndex &index) const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
@ -179,6 +184,7 @@ public:
private: private:
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
void clearData() override; void clearData() override;
void populate() override;
QModelIndex index(int row, int column, const QModelIndex &parent) const override; QModelIndex index(int row, int column, const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &index) const override; QModelIndex parent(const QModelIndex &index) const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;