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:
Berthold Stoeger 2020-05-05 00:12:36 +02:00 committed by Dirk Hohndel
parent aeee2a0802
commit fb6210a99a
18 changed files with 70 additions and 49 deletions

View file

@ -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();
}