summaryrefslogtreecommitdiffstats
path: root/qt-models
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-12-11 10:37:50 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-03-10 09:25:57 -0700
commit7e1ac2167bb531b54974c182f131af8b14c401b1 (patch)
treef6ce375ebb7cd1d6f8c559fbfa609c5d45867630 /qt-models
parent8e9e536ffdac3c1d9a35331cc3a9073eac1de320 (diff)
downloadsubsurface-7e1ac2167bb531b54974c182f131af8b14c401b1.tar.gz
mobile/divelist: create memory management class for models
Since we want to add a second model, but not have to manage two models everywhere, create a class MobileModels that contains both of the models. When calling reset() on that class, it will reset both of the models, etc. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-models')
-rw-r--r--qt-models/mobilelistmodel.cpp48
-rw-r--r--qt-models/mobilelistmodel.h16
2 files changed, 40 insertions, 24 deletions
diff --git a/qt-models/mobilelistmodel.cpp b/qt-models/mobilelistmodel.cpp
index 87248ce3f..e679c72c6 100644
--- a/qt-models/mobilelistmodel.cpp
+++ b/qt-models/mobilelistmodel.cpp
@@ -2,21 +2,10 @@
#include "mobilelistmodel.h"
#include "core/divelist.h" // for shown_dives
-MobileListModel::MobileListModel(DiveTripModelBase *sourceIn) : source(sourceIn),
+MobileListModel::MobileListModel(DiveTripModelBase *sourceIn) :
+ source(sourceIn),
expandedRow(-1)
{
- connectSignals();
-}
-
-MobileListModel *MobileListModel::instance()
-{
- static DiveTripModelTree source;
- static MobileListModel self(&source);
- return &self;
-}
-
-void MobileListModel::connectSignals()
-{
connect(source, &DiveTripModelBase::modelAboutToBeReset, this, &MobileListModel::beginResetModel);
connect(source, &DiveTripModelBase::modelReset, this, &MobileListModel::endResetModel);
connect(source, &DiveTripModelBase::rowsAboutToBeRemoved, this, &MobileListModel::prepareRemove);
@@ -246,14 +235,6 @@ QVariant MobileListModel::data(const QModelIndex &index, int role) const
return source->data(mapToSource(index), role);
}
-void MobileListModel::resetModel()
-{
- beginResetModel();
- source->reset();
- connectSignals();
- endResetModel();
-}
-
// Trivial helper to return and erase the last element of a stack
template<typename T>
static T pop(std::vector<T> &v)
@@ -544,3 +525,28 @@ void MobileListModel::toggle(int row)
else
expand(row);
}
+MobileModels *MobileModels::instance()
+{
+ static MobileModels self;
+ return &self;
+}
+
+MobileModels::MobileModels() :
+ lm(&source)
+{
+}
+
+MobileListModel *MobileModels::listModel()
+{
+ return &lm;
+}
+
+void MobileModels::clear()
+{
+ source.clear();
+}
+
+void MobileModels::reset()
+{
+ source.reset();
+}
diff --git a/qt-models/mobilelistmodel.h b/qt-models/mobilelistmodel.h
index ab0026f32..f5242a3c9 100644
--- a/qt-models/mobilelistmodel.h
+++ b/qt-models/mobilelistmodel.h
@@ -45,8 +45,6 @@ public:
SelectedRole,
};
MobileListModel(DiveTripModelBase *source);
- static MobileListModel *instance();
- void resetModel();
void expand(int row);
void unexpand();
Q_INVOKABLE void toggle(int row);
@@ -58,7 +56,6 @@ private:
bool visible;
int first, last;
};
- void connectSignals();
std::vector<IndexRange> rangeStack;
QModelIndex sourceIndex(int row, int col, int parentRow = -1) const;
int numSubItems() const;
@@ -93,6 +90,19 @@ private slots:
void changed(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
};
+// This convenience class provides access to the two mobile models.
+// Moreover, it provides an interface to the source trip-model.
+class MobileModels {
+public:
+ static MobileModels *instance();
+ MobileListModel *listModel();
+ void clear(); // Clear all dive data
+ void reset(); // Reset model after having reloaded the core data
+private:
+ MobileModels();
+ DiveTripModelTree source;
+ MobileListModel lm;
+};
// Helper functions - these are actually defined in DiveObjectHelper.cpp. Why declare them here?
QString formatSac(const dive *d);