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>
2018-07-30 07:20:25 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
2019-06-08 11:38:31 +00:00
|
|
|
// The DiveListNotifier emits signals when the dive-list changes (dives/trips/divesites created/deleted/moved/edited)
|
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>
2018-07-30 07:20:25 +00:00
|
|
|
|
|
|
|
#ifndef DIVELISTNOTIFIER_H
|
|
|
|
#define DIVELISTNOTIFIER_H
|
|
|
|
|
|
|
|
#include "core/dive.h"
|
2020-04-17 21:18:58 +00:00
|
|
|
#include "core/pictureobj.h"
|
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>
2018-07-30 07:20:25 +00:00
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
2020-10-17 14:07:39 +00:00
|
|
|
struct device;
|
|
|
|
|
2019-10-13 10:44:39 +00:00
|
|
|
// Dive and trip fields that can be edited. Use bit fields so that we can pass multiple fields at once.
|
|
|
|
// Provides an inlined flag-based constructur because sadly C-style designated initializers are only supported since C++20.
|
|
|
|
struct DiveField {
|
|
|
|
// Note: using int instead of the more natural bool, because gcc produces significantly worse code with
|
|
|
|
// bool. clang, on the other hand, does fine.
|
|
|
|
unsigned int nr : 1;
|
|
|
|
unsigned int datetime : 1;
|
|
|
|
unsigned int depth : 1;
|
|
|
|
unsigned int duration : 1;
|
|
|
|
unsigned int air_temp : 1;
|
|
|
|
unsigned int water_temp : 1;
|
|
|
|
unsigned int atm_press : 1;
|
|
|
|
unsigned int divesite : 1;
|
|
|
|
unsigned int divemaster : 1;
|
|
|
|
unsigned int buddy : 1;
|
|
|
|
unsigned int rating : 1;
|
|
|
|
unsigned int visibility : 1;
|
2019-11-28 19:04:52 +00:00
|
|
|
unsigned int wavesize : 1;
|
|
|
|
unsigned int current : 1;
|
|
|
|
unsigned int surge : 1;
|
|
|
|
unsigned int chill : 1;
|
2019-10-13 10:44:39 +00:00
|
|
|
unsigned int suit : 1;
|
|
|
|
unsigned int tags : 1;
|
|
|
|
unsigned int mode : 1;
|
|
|
|
unsigned int notes : 1;
|
|
|
|
unsigned int salinity : 1;
|
2019-12-12 22:07:17 +00:00
|
|
|
unsigned int invalid : 1;
|
2019-10-13 10:44:39 +00:00
|
|
|
enum Flags {
|
|
|
|
NONE = 0,
|
|
|
|
NR = 1 << 0,
|
|
|
|
DATETIME = 1 << 1,
|
|
|
|
DEPTH = 1 << 2,
|
|
|
|
DURATION = 1 << 3,
|
|
|
|
AIR_TEMP = 1 << 4,
|
|
|
|
WATER_TEMP = 1 << 5,
|
|
|
|
ATM_PRESS = 1 << 6,
|
|
|
|
DIVESITE = 1 << 7,
|
|
|
|
DIVEMASTER = 1 << 8,
|
|
|
|
BUDDY = 1 << 9,
|
|
|
|
RATING = 1 << 10,
|
|
|
|
VISIBILITY = 1 << 11,
|
2019-11-28 19:04:52 +00:00
|
|
|
WAVESIZE = 1 << 12,
|
|
|
|
CURRENT = 1 << 13,
|
|
|
|
SURGE = 1 << 14,
|
|
|
|
CHILL = 1 << 15,
|
|
|
|
SUIT = 1 << 16,
|
|
|
|
TAGS = 1 << 17,
|
|
|
|
MODE = 1 << 18,
|
|
|
|
NOTES = 1 << 19,
|
2019-12-12 22:07:17 +00:00
|
|
|
SALINITY = 1 << 20,
|
|
|
|
INVALID = 1 << 21
|
2019-10-13 10:44:39 +00:00
|
|
|
};
|
|
|
|
DiveField(int flags);
|
2019-01-27 21:08:13 +00:00
|
|
|
};
|
2019-10-13 10:44:39 +00:00
|
|
|
struct TripField {
|
|
|
|
unsigned int location : 1;
|
|
|
|
unsigned int notes : 1;
|
|
|
|
enum Flags {
|
|
|
|
NONE = 0,
|
|
|
|
LOCATION = 1 << 0,
|
|
|
|
NOTES = 1 << 1
|
|
|
|
};
|
|
|
|
TripField(int flags);
|
2019-02-24 20:22:33 +00:00
|
|
|
};
|
2019-01-27 21:08:13 +00:00
|
|
|
|
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>
2018-07-30 07:20:25 +00:00
|
|
|
class DiveListNotifier : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
signals:
|
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>
2020-05-04 22:12:36 +00:00
|
|
|
// The core structures were completely reset. Repopulate all models.
|
|
|
|
void dataReset();
|
|
|
|
|
2019-06-23 07:22:26 +00:00
|
|
|
// Note that there are no signals for trips being added and created
|
|
|
|
// because these events never happen without a dive being added, removed or moved.
|
|
|
|
// The dives are always sorted according to the dives_less_than() function of the core.
|
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>
2018-07-30 07:20:25 +00:00
|
|
|
void divesAdded(dive_trip *trip, bool addTrip, const QVector<dive *> &dives);
|
|
|
|
void divesDeleted(dive_trip *trip, bool deleteTrip, const QVector<dive *> &dives);
|
|
|
|
void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives);
|
2019-06-23 07:22:26 +00:00
|
|
|
void divesChanged(const QVector<dive *> &dives, DiveField field);
|
|
|
|
void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives);
|
Dive list view: replace signal-magic by flag
In DiveListView, we have a very fundamental problem: When
On the one hand, we get informed of user-selection in the
DiveListView::selectionChanged() slot. This has to set the
correct flags in the C-backend.
On the other hand, sometimes we have to set the selection
programatically, e.g. when selecting a trip. This is done
by calling QItemSelectionModel::select().
But: this will *also* call into the above slot, in which
we can't tell whether it was a user interaction or an
internal call. This can lead to either infinite loops or
very inefficient behavior, because the current dive
is set numerous times.
The current code is aware of that and disconnects the
corresponding signal. This is scary, as these signals are
set internally by the model and view. Replace this
by a global "command executing" flag in DiveListNotifier.
The flag is set using a "marker" class, which resets the flag
once it goes out of scope (cf. RAII pattern).
In DiveListView, only process a selection if the flag is not
set. Otherwise simply call the QTreeView base class, to reflect
the new selection in the UI.
To have a common point for notifications of selection changes,
add such a signal to DiveListNotifier. This signal will be
used by the DiveListView as well as the Command-objects.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-01 17:23:43 +00:00
|
|
|
|
2019-06-23 07:22:26 +00:00
|
|
|
void cylindersReset(const QVector<dive *> &dives);
|
2020-02-23 10:43:50 +00:00
|
|
|
void cylinderAdded(dive *d, int pos);
|
|
|
|
void cylinderRemoved(dive *d, int pos);
|
|
|
|
void cylinderEdited(dive *d, int pos);
|
2019-06-23 07:22:26 +00:00
|
|
|
void weightsystemsReset(const QVector<dive *> &dives);
|
2019-11-02 20:19:29 +00:00
|
|
|
void weightAdded(dive *d, int pos);
|
|
|
|
void weightRemoved(dive *d, int pos);
|
2019-11-08 21:47:38 +00:00
|
|
|
void weightEdited(dive *d, int pos);
|
2019-02-23 21:09:34 +00:00
|
|
|
|
2019-02-24 20:22:33 +00:00
|
|
|
// Trip edited signal
|
|
|
|
void tripChanged(dive_trip *trip, TripField field);
|
|
|
|
|
2019-11-18 17:19:29 +00:00
|
|
|
// Selection changes
|
2020-03-11 21:21:18 +00:00
|
|
|
void divesSelected(const QVector<dive *> &dives);
|
2019-03-10 15:03:39 +00:00
|
|
|
|
2019-03-11 23:25:31 +00:00
|
|
|
// Dive site signals. Add and delete events are sent per dive site and
|
|
|
|
// provide an index into the global dive site table.
|
|
|
|
void diveSiteAdded(dive_site *ds, int idx);
|
|
|
|
void diveSiteDeleted(dive_site *ds, int idx);
|
2019-03-10 15:03:39 +00:00
|
|
|
void diveSiteDiveCountChanged(dive_site *ds);
|
2019-03-12 22:51:39 +00:00
|
|
|
void diveSiteChanged(dive_site *ds, int field); // field according to LocationInformationModel
|
2019-03-20 20:46:58 +00:00
|
|
|
void diveSiteDivesChanged(dive_site *ds); // The dives associated with that site changed
|
2019-05-24 19:38:56 +00:00
|
|
|
|
2019-11-17 16:41:23 +00:00
|
|
|
// Filter-related signals
|
|
|
|
void numShownChanged();
|
2019-11-17 18:53:18 +00:00
|
|
|
void filterReset();
|
2019-11-17 16:41:23 +00:00
|
|
|
|
2020-03-03 21:42:51 +00:00
|
|
|
// Event-related signals. Currently, we're very blunt: only one signal for any changes to the events.
|
|
|
|
void eventsChanged(dive *d);
|
|
|
|
|
2020-04-14 20:07:00 +00:00
|
|
|
// Picture (media) related signals
|
|
|
|
void pictureOffsetChanged(dive *d, QString filename, offset_t offset);
|
2020-04-17 21:18:58 +00:00
|
|
|
void picturesRemoved(dive *d, QVector<QString> filenames);
|
|
|
|
void picturesAdded(dive *d, QVector<PictureObj> pics);
|
2020-04-14 20:07:00 +00:00
|
|
|
|
2020-10-17 14:07:39 +00:00
|
|
|
// Devices related signals
|
|
|
|
void deviceAdded(int index);
|
|
|
|
void deviceDeleted(int index);
|
|
|
|
|
2020-05-27 07:31:26 +00:00
|
|
|
// Filter related signals
|
|
|
|
void filterPresetAdded(int index);
|
|
|
|
void filterPresetRemoved(int index);
|
|
|
|
void filterPresetChanged(int index);
|
|
|
|
|
2019-05-24 19:38:56 +00:00
|
|
|
// This signal is emited every time a command is executed.
|
|
|
|
// This is used to hide an old multi-dives-edited warning message.
|
|
|
|
// This is necessary, so that the user can't click on the "undo" button and undo
|
|
|
|
// an unrelated command.
|
|
|
|
void commandExecuted();
|
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>
2018-07-30 07:20:25 +00:00
|
|
|
};
|
|
|
|
|
Dive list view: replace signal-magic by flag
In DiveListView, we have a very fundamental problem: When
On the one hand, we get informed of user-selection in the
DiveListView::selectionChanged() slot. This has to set the
correct flags in the C-backend.
On the other hand, sometimes we have to set the selection
programatically, e.g. when selecting a trip. This is done
by calling QItemSelectionModel::select().
But: this will *also* call into the above slot, in which
we can't tell whether it was a user interaction or an
internal call. This can lead to either infinite loops or
very inefficient behavior, because the current dive
is set numerous times.
The current code is aware of that and disconnects the
corresponding signal. This is scary, as these signals are
set internally by the model and view. Replace this
by a global "command executing" flag in DiveListNotifier.
The flag is set using a "marker" class, which resets the flag
once it goes out of scope (cf. RAII pattern).
In DiveListView, only process a selection if the flag is not
set. Otherwise simply call the QTreeView base class, to reflect
the new selection in the UI.
To have a common point for notifications of selection changes,
add such a signal to DiveListNotifier. This signal will be
used by the DiveListView as well as the Command-objects.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-01 17:23:43 +00:00
|
|
|
// The DiveListNotifier class has only trivial state.
|
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>
2018-07-30 07:20:25 +00:00
|
|
|
// We can simply define it as a global object.
|
|
|
|
extern DiveListNotifier diveListNotifier;
|
|
|
|
|
2019-10-13 10:44:39 +00:00
|
|
|
inline DiveField::DiveField(int flags) :
|
|
|
|
nr((flags & NR) != 0),
|
|
|
|
datetime((flags & DATETIME) != 0),
|
|
|
|
depth((flags & DEPTH) != 0),
|
|
|
|
duration((flags & DURATION) != 0),
|
|
|
|
air_temp((flags & AIR_TEMP) != 0),
|
|
|
|
water_temp((flags & WATER_TEMP) != 0),
|
|
|
|
atm_press((flags & ATM_PRESS) != 0),
|
|
|
|
divesite((flags & DIVESITE) != 0),
|
|
|
|
divemaster((flags & DIVEMASTER) != 0),
|
|
|
|
buddy((flags & BUDDY) != 0),
|
|
|
|
rating((flags & RATING) != 0),
|
|
|
|
visibility((flags & VISIBILITY) != 0),
|
2019-11-28 19:04:52 +00:00
|
|
|
wavesize((flags & WAVESIZE) != 0),
|
|
|
|
current((flags & CURRENT) != 0),
|
|
|
|
surge((flags & SURGE) != 0),
|
|
|
|
chill((flags & CHILL) != 0),
|
2019-10-13 10:44:39 +00:00
|
|
|
suit((flags & SUIT) != 0),
|
|
|
|
tags((flags & TAGS) != 0),
|
|
|
|
mode((flags & MODE) != 0),
|
|
|
|
notes((flags & NOTES) != 0),
|
2020-03-21 20:40:53 +00:00
|
|
|
salinity((flags & SALINITY) != 0),
|
|
|
|
invalid((flags & INVALID) != 0)
|
2019-10-13 10:44:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline TripField::TripField(int flags) :
|
|
|
|
location((flags & LOCATION) != 0),
|
|
|
|
notes((flags & NOTES) != 0)
|
|
|
|
{
|
|
|
|
}
|
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>
2018-07-30 07:20:25 +00:00
|
|
|
#endif
|