mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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>
This commit is contained in:
parent
aeee2a0802
commit
fb6210a99a
18 changed files with 70 additions and 49 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
// This is called when the settings changed. Instead of rebuilding the model, send a changed signal on all entries.
|
||||
void MobileModels::invalidate()
|
||||
{
|
||||
source.clear();
|
||||
}
|
||||
|
||||
void MobileModels::reset()
|
||||
{
|
||||
source.reset();
|
||||
sm.invalidate();
|
||||
sm.invalidate();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue