diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-07-30 09:20:25 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-10-11 16:22:27 -0700 |
commit | ec7d85835fb26ee9c0a9c780907441c312d7ac3f (patch) | |
tree | 1ecb5231abdc50929ba6ba308c25196eb408c381 /core | |
parent | 6ac4ddbeeda5286faacac9633b622dcf298eea7b (diff) | |
download | subsurface-ec7d85835fb26ee9c0a9c780907441c312d7ac3f.tar.gz |
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core')
-rw-r--r-- | core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | core/subsurface-qt/DiveListNotifier.cpp | 4 | ||||
-rw-r--r-- | core/subsurface-qt/DiveListNotifier.h | 39 |
3 files changed, 44 insertions, 0 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index dec719929..46cf43251 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -123,6 +123,7 @@ set(SUBSURFACE_CORE_LIB_SRCS #Subsurface Qt have the Subsurface structs QObjectified for easy access via QML. subsurface-qt/DiveObjectHelper.cpp subsurface-qt/CylinderObjectHelper.cpp + subsurface-qt/DiveListNotifier.cpp ${SERIAL_FTDI} ${PLATFORM_SRC} diff --git a/core/subsurface-qt/DiveListNotifier.cpp b/core/subsurface-qt/DiveListNotifier.cpp new file mode 100644 index 000000000..2009defcd --- /dev/null +++ b/core/subsurface-qt/DiveListNotifier.cpp @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "DiveListNotifier.h" + +DiveListNotifier diveListNotifier; diff --git a/core/subsurface-qt/DiveListNotifier.h b/core/subsurface-qt/DiveListNotifier.h new file mode 100644 index 000000000..94b0063ea --- /dev/null +++ b/core/subsurface-qt/DiveListNotifier.h @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 + +// The DiveListNotifier emits signals when the dive-list changes (dives/trips created/deleted/moved) +// Note that vectors are passed by reference, so this will only work for signals inside the UI thread! + +#ifndef DIVELISTNOTIFIER_H +#define DIVELISTNOTIFIER_H + +#include "core/dive.h" + +#include <QObject> + +class DiveListNotifier : public QObject { + Q_OBJECT +signals: + + // Note that there are no signals for trips being added / created / time-shifted, + // because these events never happen without a dive being added / created / time-shifted. + + // We send one divesAdded, divesDeleted, divesChanged and divesTimeChanged signal per trip + // (non-associated dives being considered part of the null trip). This is ideal for the + // tree-view, but might be not-so-perfect for the list view, if trips intermingle or + // the deletion spans multiple trips. But most of the time only dives of a single trip + // will be affected and trips don't overlap, so these considerations are moot. + // Notes: + // - The dives are always sorted by start-time. + // - The "trip" arguments are null for top-level-dives. + void divesAdded(dive_trip *trip, bool addTrip, const QVector<dive *> &dives); + void divesDeleted(dive_trip *trip, bool deleteTrip, const QVector<dive *> &dives); + void divesChanged(dive_trip *trip, const QVector<dive *> &dives); + void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives); + void divesTimeChanged(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives); +}; + +// The DiveListNotifier class has no state and no constructor. +// We can simply define it as a global object. +extern DiveListNotifier diveListNotifier; + +#endif |