From 8dcc33d8ab4081310d2e18b2c3d6c95141999698 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 10 Mar 2019 16:03:39 +0100 Subject: 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 --- desktop-widgets/command_divelist.cpp | 14 +++++++++++++- desktop-widgets/command_divelist.h | 8 +++++++- 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'desktop-widgets') diff --git a/desktop-widgets/command_divelist.cpp b/desktop-widgets/command_divelist.cpp index 089fe3ec5..c7dad70e4 100644 --- a/desktop-widgets/command_divelist.cpp +++ b/desktop-widgets/command_divelist.cpp @@ -69,6 +69,8 @@ DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector &t // remove dive from trip and site - if this is the last dive in the trip // remove the whole trip. res.trip = unregister_dive_from_trip(d); + if (d->dive_site) + diveSiteCountChanged(d->dive_site); res.site = unregister_dive_from_dive_site(d); if (res.trip && res.trip->dives.nr == 0) { unregister_trip(res.trip, &trip_table); // Remove trip from backend @@ -80,6 +82,12 @@ DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector &t 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. // 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!) @@ -87,8 +95,10 @@ dive *DiveListBase::addDive(DiveToAdd &d) { if (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); + diveSiteCountChanged(d.site); + } dive *res = d.dive.release(); // Give up ownership of dive // 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 emit diveListNotifier.selectionChanged(); + for (dive_site *ds: sitesCountChanged) + emit diveListNotifier.diveSiteDiveCountChanged(ds); } // Set the current dive either from a list of selected dives, diff --git a/desktop-widgets/command_divelist.h b/desktop-widgets/command_divelist.h index 9199a2e58..841799473 100644 --- a/desktop-widgets/command_divelist.h +++ b/desktop-widgets/command_divelist.h @@ -66,6 +66,9 @@ protected: DivesAndTripsToAdd removeDives(DivesAndSitesToRemove &divesAndSitesToDelete); 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. void restoreSelection(const std::vector &selection, dive *currentDive); @@ -74,9 +77,12 @@ protected: // If this flag is set on first execution, a selectionChanged signal will // be sent. bool selectionChanged; + private: + // Keep track of dive sites where the number of dives changed + std::vector sitesCountChanged; 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 redo() override; virtual void redoit() = 0; -- cgit v1.2.3-70-g09d2