Undo: keep frontend informed of changes to dive site count

Add a new signal to DiveListNotifier. Send signal if dives are
added or removed and therefore the dive count of a dive site
changes. The dive sites are collected and the signal is sent
at the end of the command.

Add code to update the table view.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-03-10 16:03:39 +01:00 committed by Dirk Hohndel
parent dd12bdaf94
commit 8dcc33d8ab
6 changed files with 37 additions and 2 deletions

View file

@ -23,6 +23,7 @@ int get_divesite_idx(const struct dive_site *ds, struct dive_site_table *ds_tabl
return -1; return -1;
} }
// TODO: keep table sorted by UUID and do a binary search?
struct dive_site *get_dive_site_by_uuid(uint32_t uuid, struct dive_site_table *ds_table) struct dive_site *get_dive_site_by_uuid(uint32_t uuid, struct dive_site_table *ds_table)
{ {
int i; int i;

View file

@ -42,6 +42,9 @@ signals:
void divesDeselected(dive_trip *trip, const QVector<dive *> &dives); void divesDeselected(dive_trip *trip, const QVector<dive *> &dives);
void currentDiveChanged(); void currentDiveChanged();
void selectionChanged(); void selectionChanged();
// Dive site signals
void diveSiteDiveCountChanged(dive_site *ds);
public: public:
// Desktop uses the QTreeView class to present the list of dives. The layout // Desktop uses the QTreeView class to present the list of dives. The layout
// of this class gives us a very fundamental problem, as we can not easily // of this class gives us a very fundamental problem, as we can not easily

View file

@ -69,6 +69,8 @@ DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector<OwningTripPtr> &t
// remove dive from trip and site - if this is the last dive in the trip // remove dive from trip and site - if this is the last dive in the trip
// remove the whole trip. // remove the whole trip.
res.trip = unregister_dive_from_trip(d); res.trip = unregister_dive_from_trip(d);
if (d->dive_site)
diveSiteCountChanged(d->dive_site);
res.site = unregister_dive_from_dive_site(d); res.site = unregister_dive_from_dive_site(d);
if (res.trip && res.trip->dives.nr == 0) { if (res.trip && res.trip->dives.nr == 0) {
unregister_trip(res.trip, &trip_table); // Remove trip from backend unregister_trip(res.trip, &trip_table); // Remove trip from backend
@ -80,6 +82,12 @@ DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector<OwningTripPtr> &t
return res; return res;
} }
void DiveListBase::diveSiteCountChanged(struct dive_site *ds)
{
if (std::find(sitesCountChanged.begin(), sitesCountChanged.end(), ds) == sitesCountChanged.end())
sitesCountChanged.push_back(ds);
}
// This helper function adds a dive and returns ownership to the backend. It may also add a dive trip. // This helper function adds a dive and returns ownership to the backend. It may also add a dive trip.
// It is crucial that dives are added in reverse order of deletion (see comment above)! // It is crucial that dives are added in reverse order of deletion (see comment above)!
// Returns pointer to added dive (which is owned by the backend!) // Returns pointer to added dive (which is owned by the backend!)
@ -87,8 +95,10 @@ dive *DiveListBase::addDive(DiveToAdd &d)
{ {
if (d.trip) if (d.trip)
add_dive_to_trip(d.dive.get(), d.trip); add_dive_to_trip(d.dive.get(), d.trip);
if (d.site) if (d.site) {
add_dive_to_dive_site(d.dive.get(), d.site); add_dive_to_dive_site(d.dive.get(), d.site);
diveSiteCountChanged(d.site);
}
dive *res = d.dive.release(); // Give up ownership of dive dive *res = d.dive.release(); // Give up ownership of dive
// Set the filter flag according to current filter settings // Set the filter flag according to current filter settings
@ -370,6 +380,8 @@ void DiveListBase::finishWork()
{ {
if (selectionChanged) // If the selection changed -> tell the frontend if (selectionChanged) // If the selection changed -> tell the frontend
emit diveListNotifier.selectionChanged(); emit diveListNotifier.selectionChanged();
for (dive_site *ds: sitesCountChanged)
emit diveListNotifier.diveSiteDiveCountChanged(ds);
} }
// Set the current dive either from a list of selected dives, // Set the current dive either from a list of selected dives,

View file

@ -66,6 +66,9 @@ protected:
DivesAndTripsToAdd removeDives(DivesAndSitesToRemove &divesAndSitesToDelete); DivesAndTripsToAdd removeDives(DivesAndSitesToRemove &divesAndSitesToDelete);
DivesAndSitesToRemove addDives(DivesAndTripsToAdd &toAdd); DivesAndSitesToRemove addDives(DivesAndTripsToAdd &toAdd);
// Register dive sites where counts changed so that we can signal the frontend later.
void diveSiteCountChanged(struct dive_site *ds);
// Set the selection to a given state. Set the selectionChanged flag if anything changed. // Set the selection to a given state. Set the selectionChanged flag if anything changed.
void restoreSelection(const std::vector<dive *> &selection, dive *currentDive); void restoreSelection(const std::vector<dive *> &selection, dive *currentDive);
@ -74,9 +77,12 @@ protected:
// If this flag is set on first execution, a selectionChanged signal will // If this flag is set on first execution, a selectionChanged signal will
// be sent. // be sent.
bool selectionChanged; bool selectionChanged;
private: private:
// Keep track of dive sites where the number of dives changed
std::vector<dive_site *> sitesCountChanged;
void initWork(); // reset selectionChanged flag void initWork(); // reset selectionChanged flag
void finishWork(); // emit signal if selection changed void finishWork(); // emit signals if selection or dive site counts changed
void undo() override; void undo() override;
void redo() override; void redo() override;
virtual void redoit() = 0; virtual void redoit() = 0;

View file

@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "core/units.h" #include "core/units.h"
#include "qt-models/divelocationmodel.h" #include "qt-models/divelocationmodel.h"
#include "core/subsurface-qt/DiveListNotifier.h"
#include "core/qthelper.h" #include "core/qthelper.h"
#include "core/divesite.h" #include "core/divesite.h"
#include "core/metrics.h" #include "core/metrics.h"
@ -23,6 +24,7 @@ LocationInformationModel *LocationInformationModel::instance()
LocationInformationModel::LocationInformationModel(QObject *obj) : QAbstractTableModel(obj) LocationInformationModel::LocationInformationModel(QObject *obj) : QAbstractTableModel(obj)
{ {
connect(&diveListNotifier, &DiveListNotifier::diveSiteDiveCountChanged, this, &LocationInformationModel::diveSiteDiveCountChanged);
} }
int LocationInformationModel::columnCount(const QModelIndex &) const int LocationInformationModel::columnCount(const QModelIndex &) const
@ -154,6 +156,13 @@ bool LocationInformationModel::removeRows(int row, int, const QModelIndex&)
return true; return true;
} }
void LocationInformationModel::diveSiteDiveCountChanged(dive_site *ds)
{
int idx = get_divesite_idx(ds, &dive_site_table);
if (idx >= 0)
dataChanged(createIndex(idx, NUM_DIVES), createIndex(idx, NUM_DIVES));
}
GeoReferencingOptionsModel *GeoReferencingOptionsModel::instance() GeoReferencingOptionsModel *GeoReferencingOptionsModel::instance()
{ {
static GeoReferencingOptionsModel *self = new GeoReferencingOptionsModel(); static GeoReferencingOptionsModel *self = new GeoReferencingOptionsModel();

View file

@ -9,6 +9,9 @@
#define RECENTLY_ADDED_DIVESITE ((struct dive_site *)~0) #define RECENTLY_ADDED_DIVESITE ((struct dive_site *)~0)
struct dive;
struct dive_trip;
class LocationInformationModel : public QAbstractTableModel { class LocationInformationModel : public QAbstractTableModel {
Q_OBJECT Q_OBJECT
public: public:
@ -30,6 +33,7 @@ public:
public slots: public slots:
void update(); void update();
QStringList allSiteNames() const; QStringList allSiteNames() const;
void diveSiteDiveCountChanged(struct dive_site *ds);
private: private:
QStringList locationNames; QStringList locationNames;
}; };