mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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>
This commit is contained in:
parent
8e9e536ffd
commit
7e1ac2167b
4 changed files with 45 additions and 30 deletions
|
@ -300,7 +300,7 @@ void QMLManager::applicationStateChanged(Qt::ApplicationState state)
|
||||||
void QMLManager::openLocalThenRemote(QString url)
|
void QMLManager::openLocalThenRemote(QString url)
|
||||||
{
|
{
|
||||||
DiveListModel::instance()->clear();
|
DiveListModel::instance()->clear();
|
||||||
MobileListModel::instance()->resetModel();
|
MobileModels::instance()->clear();
|
||||||
setNotificationText(tr("Open local dive data file"));
|
setNotificationText(tr("Open local dive data file"));
|
||||||
QByteArray fileNamePrt = QFile::encodeName(url);
|
QByteArray fileNamePrt = QFile::encodeName(url);
|
||||||
/* if this is a cloud storage repo and we have no local cache (i.e., it's the first time
|
/* if this is a cloud storage repo and we have no local cache (i.e., it's the first time
|
||||||
|
@ -338,7 +338,7 @@ void QMLManager::openLocalThenRemote(QString url)
|
||||||
qPrefPartialPressureGas::set_po2(git_prefs.pp_graphs.po2);
|
qPrefPartialPressureGas::set_po2(git_prefs.pp_graphs.po2);
|
||||||
process_loaded_dives();
|
process_loaded_dives();
|
||||||
DiveListModel::instance()->reload();
|
DiveListModel::instance()->reload();
|
||||||
MobileListModel::instance()->resetModel();
|
MobileModels::instance()->reset();
|
||||||
appendTextToLog(QStringLiteral("%1 dives loaded from cache").arg(dive_table.nr));
|
appendTextToLog(QStringLiteral("%1 dives loaded from cache").arg(dive_table.nr));
|
||||||
setNotificationText(tr("%1 dives loaded from local dive data file").arg(dive_table.nr));
|
setNotificationText(tr("%1 dives loaded from local dive data file").arg(dive_table.nr));
|
||||||
}
|
}
|
||||||
|
@ -374,7 +374,7 @@ static struct dive *diveInRow(const QAbstractItemModel *model, int row)
|
||||||
|
|
||||||
void QMLManager::selectRow(int row)
|
void QMLManager::selectRow(int row)
|
||||||
{
|
{
|
||||||
dive *d = diveInRow(MobileListModel::instance(), row);
|
dive *d = diveInRow(MobileModels::instance()->listModel(), row);
|
||||||
select_single_dive(d);
|
select_single_dive(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -574,8 +574,8 @@ void QMLManager::saveCloudCredentials(const QString &newEmail, const QString &ne
|
||||||
getCloudURL(url);
|
getCloudURL(url);
|
||||||
manager()->clearAccessCache(); // remove any chached credentials
|
manager()->clearAccessCache(); // remove any chached credentials
|
||||||
clear_git_id(); // invalidate our remembered GIT SHA
|
clear_git_id(); // invalidate our remembered GIT SHA
|
||||||
|
MobileModels::instance()->clear();
|
||||||
DiveListModel::instance()->reload();
|
DiveListModel::instance()->reload();
|
||||||
MobileListModel::instance()->resetModel();
|
|
||||||
GpsListModel::instance()->clear();
|
GpsListModel::instance()->clear();
|
||||||
setStartPageText(tr("Attempting to open cloud storage with new credentials"));
|
setStartPageText(tr("Attempting to open cloud storage with new credentials"));
|
||||||
// we therefore know that no one else is already accessing THIS git repo;
|
// we therefore know that no one else is already accessing THIS git repo;
|
||||||
|
|
|
@ -2,20 +2,9 @@
|
||||||
#include "mobilelistmodel.h"
|
#include "mobilelistmodel.h"
|
||||||
#include "core/divelist.h" // for shown_dives
|
#include "core/divelist.h" // for shown_dives
|
||||||
|
|
||||||
MobileListModel::MobileListModel(DiveTripModelBase *sourceIn) : source(sourceIn),
|
MobileListModel::MobileListModel(DiveTripModelBase *sourceIn) :
|
||||||
|
source(sourceIn),
|
||||||
expandedRow(-1)
|
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::modelAboutToBeReset, this, &MobileListModel::beginResetModel);
|
||||||
connect(source, &DiveTripModelBase::modelReset, this, &MobileListModel::endResetModel);
|
connect(source, &DiveTripModelBase::modelReset, this, &MobileListModel::endResetModel);
|
||||||
|
@ -246,14 +235,6 @@ QVariant MobileListModel::data(const QModelIndex &index, int role) const
|
||||||
return source->data(mapToSource(index), role);
|
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
|
// Trivial helper to return and erase the last element of a stack
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static T pop(std::vector<T> &v)
|
static T pop(std::vector<T> &v)
|
||||||
|
@ -544,3 +525,28 @@ void MobileListModel::toggle(int row)
|
||||||
else
|
else
|
||||||
expand(row);
|
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();
|
||||||
|
}
|
||||||
|
|
|
@ -45,8 +45,6 @@ public:
|
||||||
SelectedRole,
|
SelectedRole,
|
||||||
};
|
};
|
||||||
MobileListModel(DiveTripModelBase *source);
|
MobileListModel(DiveTripModelBase *source);
|
||||||
static MobileListModel *instance();
|
|
||||||
void resetModel();
|
|
||||||
void expand(int row);
|
void expand(int row);
|
||||||
void unexpand();
|
void unexpand();
|
||||||
Q_INVOKABLE void toggle(int row);
|
Q_INVOKABLE void toggle(int row);
|
||||||
|
@ -58,7 +56,6 @@ private:
|
||||||
bool visible;
|
bool visible;
|
||||||
int first, last;
|
int first, last;
|
||||||
};
|
};
|
||||||
void connectSignals();
|
|
||||||
std::vector<IndexRange> rangeStack;
|
std::vector<IndexRange> rangeStack;
|
||||||
QModelIndex sourceIndex(int row, int col, int parentRow = -1) const;
|
QModelIndex sourceIndex(int row, int col, int parentRow = -1) const;
|
||||||
int numSubItems() const;
|
int numSubItems() const;
|
||||||
|
@ -93,6 +90,19 @@ private slots:
|
||||||
void changed(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
|
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?
|
// Helper functions - these are actually defined in DiveObjectHelper.cpp. Why declare them here?
|
||||||
QString formatSac(const dive *d);
|
QString formatSac(const dive *d);
|
||||||
|
|
|
@ -103,10 +103,9 @@ void run_ui()
|
||||||
gpsSortModel->setSortRole(GpsListModel::GpsWhenRole);
|
gpsSortModel->setSortRole(GpsListModel::GpsWhenRole);
|
||||||
gpsSortModel->sort(0, Qt::DescendingOrder);
|
gpsSortModel->sort(0, Qt::DescendingOrder);
|
||||||
QQmlContext *ctxt = engine.rootContext();
|
QQmlContext *ctxt = engine.rootContext();
|
||||||
MobileListModel *mlm(MobileListModel::instance());
|
|
||||||
ctxt->setContextProperty("diveModel", mlm);
|
|
||||||
ctxt->setContextProperty("gpsModel", gpsSortModel);
|
ctxt->setContextProperty("gpsModel", gpsSortModel);
|
||||||
ctxt->setContextProperty("vendorList", vendorList);
|
ctxt->setContextProperty("vendorList", vendorList);
|
||||||
|
ctxt->setContextProperty("diveModel", MobileModels::instance()->listModel());
|
||||||
set_non_bt_addresses();
|
set_non_bt_addresses();
|
||||||
|
|
||||||
ctxt->setContextProperty("connectionListModel", &connectionListModel);
|
ctxt->setContextProperty("connectionListModel", &connectionListModel);
|
||||||
|
|
Loading…
Add table
Reference in a new issue