summaryrefslogtreecommitdiffstats
path: root/qt-models
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-05-05 00:12:36 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-05-07 08:43:27 -0700
commitfb6210a99a1eda0f7f7197ed57f3c61a82132dff (patch)
tree7562feb3291d06173b971437d1bd3395b2722eff /qt-models
parentaeee2a08027b177495635262114374f085351ce7 (diff)
downloadsubsurface-fb6210a99a1eda0f7f7197ed57f3c61a82132dff.tar.gz
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs were calling into the dive-list models, which then reset the core data structures, themselves and the unrelated locationinformation model. The UI code then reset various other things, such as the TankInformation model or the map. . This was unsatisfying from a control-flow perspective, as the models should display the core data, not act on it. Moreover, this meant lots of intricate intermodule-dependencies. Thus, straighten up the control flow: give the C core the possibility to send a "all data reset" event. And do that in those functions that reset the core data structures. Let each module react to this event by itself. This removes inter-module dependencies. For example, the MainWindow now doesn't have to reset the TankInfoModel or the MapWidget. Then, to reset the core data structures, let the UI code simply directly call the respective core functions. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models')
-rw-r--r--qt-models/divelocationmodel.cpp1
-rw-r--r--qt-models/divelocationmodel.h1
-rw-r--r--qt-models/divetripmodel.cpp18
-rw-r--r--qt-models/divetripmodel.h8
-rw-r--r--qt-models/filtermodels.cpp5
-rw-r--r--qt-models/filtermodels.h1
-rw-r--r--qt-models/gpslistmodel.cpp2
-rw-r--r--qt-models/mobilelistmodel.cpp36
-rw-r--r--qt-models/mobilelistmodel.h5
-rw-r--r--qt-models/tankinfomodel.cpp2
-rw-r--r--qt-models/weightsysteminfomodel.cpp2
11 files changed, 44 insertions, 37 deletions
diff --git a/qt-models/divelocationmodel.cpp b/qt-models/divelocationmodel.cpp
index 26cf3353e..f8bad01a7 100644
--- a/qt-models/divelocationmodel.cpp
+++ b/qt-models/divelocationmodel.cpp
@@ -26,6 +26,7 @@ LocationInformationModel::LocationInformationModel(QObject *obj) : QAbstractTabl
connect(&diveListNotifier, &DiveListNotifier::diveSiteDeleted, this, &LocationInformationModel::diveSiteDeleted);
connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &LocationInformationModel::diveSiteChanged);
connect(&diveListNotifier, &DiveListNotifier::diveSiteDivesChanged, this, &LocationInformationModel::diveSiteDivesChanged);
+ connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &LocationInformationModel::update);
}
int LocationInformationModel::columnCount(const QModelIndex &) const
diff --git a/qt-models/divelocationmodel.h b/qt-models/divelocationmodel.h
index 661debc53..623296de2 100644
--- a/qt-models/divelocationmodel.h
+++ b/qt-models/divelocationmodel.h
@@ -31,6 +31,7 @@ public:
public slots:
void update();
+private slots:
void diveSiteDiveCountChanged(struct dive_site *ds);
void diveSiteAdded(struct dive_site *ds, int idx);
void diveSiteDeleted(struct dive_site *ds, int idx);
diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp
index ed5542fcc..db086ca59 100644
--- a/qt-models/divetripmodel.cpp
+++ b/qt-models/divetripmodel.cpp
@@ -476,27 +476,13 @@ void DiveTripModelBase::initSelection()
select_newest_visible_dive();
}
-void DiveTripModelBase::clear()
-{
- Command::clear(); // If we clear the dive list, all undo-information becomes stalte.
- beginResetModel();
- clear_dive_file_data();
- clearData();
- LocationInformationModel::instance()->update();
- oldCurrent = nullptr;
- emit diveListNotifier.divesSelected({}); // Inform profile, etc of changed selection
- endResetModel();
- emit diveListNotifier.numShownChanged();
-}
-
// Currently only used by the mobile models
void DiveTripModelBase::reset()
{
beginResetModel();
+ oldCurrent = nullptr;
clearData();
populate();
- uiNotification(tr("setting up dive sites"));
- LocationInformationModel::instance()->update();
uiNotification(tr("finish populating data store"));
endResetModel();
uiNotification(tr("setting up internal data structures"));
@@ -721,6 +707,7 @@ DiveTripModelTree::DiveTripModelTree(QObject *parent) : DiveTripModelBase(parent
connect(&diveListNotifier, &DiveListNotifier::pictureOffsetChanged, this, &DiveTripModelTree::diveChanged);
connect(&diveListNotifier, &DiveListNotifier::picturesRemoved, this, &DiveTripModelTree::diveChanged);
connect(&diveListNotifier, &DiveListNotifier::picturesAdded, this, &DiveTripModelTree::diveChanged);
+ connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &DiveTripModelTree::reset);
populate();
}
@@ -1485,6 +1472,7 @@ DiveTripModelList::DiveTripModelList(QObject *parent) : DiveTripModelBase(parent
connect(&diveListNotifier, &DiveListNotifier::pictureOffsetChanged, this, &DiveTripModelList::diveChanged);
connect(&diveListNotifier, &DiveListNotifier::picturesRemoved, this, &DiveTripModelList::diveChanged);
connect(&diveListNotifier, &DiveListNotifier::picturesAdded, this, &DiveTripModelList::diveChanged);
+ connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &DiveTripModelList::reset);
populate();
}
diff --git a/qt-models/divetripmodel.h b/qt-models/divetripmodel.h
index 3e7955b5b..4b2376bd9 100644
--- a/qt-models/divetripmodel.h
+++ b/qt-models/divetripmodel.h
@@ -64,12 +64,6 @@ public:
// Call after having set the model to be informed of the current selection.
void initSelection();
- // Clear all dives
- void clear();
-
- // Reload data
- void reset();
-
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
DiveTripModelBase(QObject *parent = 0);
@@ -78,6 +72,8 @@ public:
// Used for sorting. This is a bit of a layering violation, as sorting should be performed
// by the higher-up QSortFilterProxyModel, but it makes things so much easier!
virtual bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const = 0;
+protected slots:
+ void reset();
signals:
// The propagation of selection changes is complex.
// The control flow of dive-selection goes:
diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp
index 9b2ab6a7b..a5fae4520 100644
--- a/qt-models/filtermodels.cpp
+++ b/qt-models/filtermodels.cpp
@@ -33,11 +33,6 @@ void MultiFilterSortModel::resetModel(DiveTripModelBase::Layout layout)
LocationInformationModel::instance()->update();
}
-void MultiFilterSortModel::clear()
-{
- model->clear();
-}
-
// Translate selection into local indices and re-emit signal
void MultiFilterSortModel::selectionChangedSlot(const QVector<QModelIndex> &indices)
{
diff --git a/qt-models/filtermodels.h b/qt-models/filtermodels.h
index 588bfe2b5..8895f5b2c 100644
--- a/qt-models/filtermodels.h
+++ b/qt-models/filtermodels.h
@@ -17,7 +17,6 @@ public:
bool lessThan(const QModelIndex &, const QModelIndex &) const override;
void resetModel(DiveTripModelBase::Layout layout);
- void clear();
signals:
void selectionChanged(const QVector<QModelIndex> &indices);
void currentDiveChanged(QModelIndex index);
diff --git a/qt-models/gpslistmodel.cpp b/qt-models/gpslistmodel.cpp
index 33eb8a5ea..c463c3ed2 100644
--- a/qt-models/gpslistmodel.cpp
+++ b/qt-models/gpslistmodel.cpp
@@ -1,10 +1,12 @@
// SPDX-License-Identifier: GPL-2.0
#include "qt-models/gpslistmodel.h"
+#include "core/subsurface-qt/divelistnotifier.h"
#include "core/qthelper.h"
#include <QVector>
GpsListModel::GpsListModel()
{
+ connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &GpsListModel::update);
}
void GpsListModel::update()
diff --git a/qt-models/mobilelistmodel.cpp b/qt-models/mobilelistmodel.cpp
index 6dd74a57c..17ec252d7 100644
--- a/qt-models/mobilelistmodel.cpp
+++ b/qt-models/mobilelistmodel.cpp
@@ -507,6 +507,18 @@ void MobileListModel::changed(const QModelIndex &topLeft, const QModelIndex &bot
}
}
+void MobileListModel::invalidate()
+{
+ // Qt's model/view API can't handle empty ranges and we have to subtract one from the last item,
+ // because ranges are given as [first,last] (i.e. last inclusive).
+ int rows = rowCount(QModelIndex());
+ if (rows <= 0)
+ return;
+ QModelIndex fromIdx = createIndex(0, 0);
+ QModelIndex toIdx = createIndex(rows - 1, 0);
+ dataChanged(fromIdx, toIdx);
+}
+
void MobileListModel::unexpand()
{
if (expandedRow < 0)
@@ -903,6 +915,18 @@ void MobileSwipeModel::changed(const QModelIndex &topLeft, const QModelIndex &bo
emit currentDiveChanged(fromIdx);
}
+void MobileSwipeModel::invalidate()
+{
+ // Qt's model/view API can't handle empty ranges and we have to subtract one from the last item,
+ // because ranges are given as [first,last] (i.e. last inclusive).
+ int rows = rowCount(QModelIndex());
+ if (rows <= 0)
+ return;
+ QModelIndex fromIdx = createIndex(0, 0);
+ QModelIndex toIdx = createIndex(rows - 1, 0);
+ dataChanged(fromIdx, toIdx);
+}
+
QVariant MobileSwipeModel::data(const QModelIndex &index, int role) const
{
return source->data(mapToSource(index), role);
@@ -925,7 +949,6 @@ MobileModels::MobileModels() :
lm(&source),
sm(&source)
{
- reset();
}
MobileListModel *MobileModels::listModel()
@@ -938,12 +961,9 @@ MobileSwipeModel *MobileModels::swipeModel()
return &sm;
}
-void MobileModels::clear()
-{
- source.clear();
-}
-
-void MobileModels::reset()
+// This is called when the settings changed. Instead of rebuilding the model, send a changed signal on all entries.
+void MobileModels::invalidate()
{
- source.reset();
+ sm.invalidate();
+ sm.invalidate();
}
diff --git a/qt-models/mobilelistmodel.h b/qt-models/mobilelistmodel.h
index c898ed118..e315e9ebd 100644
--- a/qt-models/mobilelistmodel.h
+++ b/qt-models/mobilelistmodel.h
@@ -77,6 +77,7 @@ public:
MobileListModel(DiveTripModelBase *source);
void expand(int row);
void unexpand();
+ void invalidate();
Q_INVOKABLE void toggle(int row);
Q_PROPERTY(int shown READ shown NOTIFY shownChanged);
signals:
@@ -121,6 +122,7 @@ public:
MobileSwipeModel(DiveTripModelBase *source);
static MobileSwipeModel *instance();
void resetModel(DiveTripModelBase::Layout layout); // Switch between tree and list view
+ void invalidate();
private:
struct IndexRange {
int first, last;
@@ -175,8 +177,7 @@ public:
static MobileModels *instance();
MobileListModel *listModel();
MobileSwipeModel *swipeModel();
- void clear(); // Clear all dive data
- void reset(); // Reset model after having reloaded the core data
+ void invalidate(); // Invalidate all entries to force a re-render.
private:
MobileModels();
DiveTripModelTree source;
diff --git a/qt-models/tankinfomodel.cpp b/qt-models/tankinfomodel.cpp
index df1841fa3..8d78cf3d5 100644
--- a/qt-models/tankinfomodel.cpp
+++ b/qt-models/tankinfomodel.cpp
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include "qt-models/tankinfomodel.h"
#include "core/dive.h"
+#include "core/subsurface-qt/divelistnotifier.h"
#include "core/gettextfromc.h"
#include "core/metrics.h"
@@ -85,6 +86,7 @@ int TankInfoModel::rowCount(const QModelIndex&) const
TankInfoModel::TankInfoModel()
{
setHeaderDataStrings(QStringList() << tr("Description") << tr("ml") << tr("bar"));
+ connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &TankInfoModel::update);
update();
}
diff --git a/qt-models/weightsysteminfomodel.cpp b/qt-models/weightsysteminfomodel.cpp
index 21d6f538f..9c28d5f64 100644
--- a/qt-models/weightsysteminfomodel.cpp
+++ b/qt-models/weightsysteminfomodel.cpp
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "qt-models/weightsysteminfomodel.h"
+#include "core/subsurface-qt/divelistnotifier.h"
#include "core/dive.h"
#include "core/metrics.h"
#include "core/gettextfromc.h"
@@ -74,6 +75,7 @@ int WSInfoModel::rowCount(const QModelIndex&) const
WSInfoModel::WSInfoModel()
{
setHeaderDataStrings(QStringList() << tr("Description") << tr("kg"));
+ connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &WSInfoModel::update);
update();
}