Filter: move calculation of shown dives to undo command

The filter-model was catching dives-added / dives-deleted signals
from the models to keep track of the number of shown dives.

To simplify the data flow, do this directly in the undo-command.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-11-17 17:41:23 +01:00 committed by Dirk Hohndel
parent 2d09819ddf
commit 6d6d10f03a
5 changed files with 23 additions and 23 deletions

View file

@ -40,6 +40,9 @@ DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector<OwningTripPtr> &t
if (idx < 0)
qWarning("Deletion of unknown dive!");
if (!d->hidden_by_filter)
--shown_dives;
res.dive.reset(unregister_dive(idx)); // Remove dive from backend
return res;
@ -67,6 +70,8 @@ dive *DiveListBase::addDive(DiveToAdd &d)
// Set the filter flag according to current filter settings
bool show = MultiFilterSortModel::instance()->showDive(res);
res->hidden_by_filter = !show;
if (show)
++shown_dives;
int idx = dive_table_get_insertion_index(&dive_table, res);
add_to_dive_table(&dive_table, idx, res); // Return ownership to backend
@ -118,6 +123,9 @@ DivesAndTripsToAdd DiveListBase::removeDives(DivesAndSitesToRemove &divesAndSite
divesToAdd.reserve(divesAndSitesToDelete.dives.size());
sitesToAdd.reserve(divesAndSitesToDelete.sites.size());
// Remember old number of shown dives
int oldShown = shown_dives;
// Make sure that the dive list is sorted. The added dives will be sent in a signal
// and the recipients assume that the dives are sorted the same way as they are
// in the core list.
@ -149,6 +157,10 @@ DivesAndTripsToAdd DiveListBase::removeDives(DivesAndSitesToRemove &divesAndSite
{ return ptr.get() == trip; }) != tripsToAdd.end();
emit diveListNotifier.divesDeleted(trip, deleteTrip, divesInTrip);
});
if (oldShown != shown_dives)
emit diveListNotifier.numShownChanged();
return { std::move(divesToAdd), std::move(tripsToAdd), std::move(sitesToAdd) };
}
@ -172,6 +184,9 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd)
[](const DiveToAdd &d, const DiveToAdd &d2)
{ return dive_less_than(d.dive.get(), d2.dive.get()); });
// Remember old number of shown dives
int oldShown = shown_dives;
// Now, add the dives
// Note: the idiomatic STL-way would be std::transform, but let's use a loop since
// that is closer to classical C-style.
@ -207,6 +222,10 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd)
// Finally, emit the signal
emit diveListNotifier.divesAdded(trip, createTrip, divesInTrip);
});
if (oldShown != shown_dives)
emit diveListNotifier.numShownChanged();
return { res, sites };
}

View file

@ -92,6 +92,9 @@ signals:
void diveSiteChanged(dive_site *ds, int field); // field according to LocationInformationModel
void diveSiteDivesChanged(dive_site *ds); // The dives associated with that site changed
// Filter-related signals
void numShownChanged();
// 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

View file

@ -214,6 +214,7 @@ MainWindow::MainWindow() : QMainWindow(),
connect(this, &MainWindow::showError, ui.mainErrorMessage, &NotificationWidget::showError, Qt::AutoConnection);
connect(&windowTitleUpdate, &WindowTitleUpdate::updateTitle, this, &MainWindow::setAutomaticTitle);
connect(&diveListNotifier, &DiveListNotifier::numShownChanged, this, &MainWindow::setAutomaticTitle);
#ifdef NO_PRINTING
plannerDetails->printPlan()->hide();
ui.menuFile->removeAction(ui.actionPrint);

View file

@ -111,9 +111,6 @@ MultiFilterSortModel::MultiFilterSortModel(QObject *parent) : QSortFilterProxyMo
{
setFilterKeyColumn(-1); // filter all columns
setFilterCaseSensitivity(Qt::CaseInsensitive);
connect(&diveListNotifier, &DiveListNotifier::divesAdded, this, &MultiFilterSortModel::divesAdded);
connect(&diveListNotifier, &DiveListNotifier::divesDeleted, this, &MultiFilterSortModel::divesDeleted);
}
void MultiFilterSortModel::resetModel(DiveTripModelBase::Layout layout)
@ -343,24 +340,6 @@ void MultiFilterSortModel::filterDataChanged(const FilterData &data)
myInvalidate();
}
void MultiFilterSortModel::divesAdded(dive_trip *, bool, const QVector<dive *> &dives)
{
for (dive *d: dives) {
if (!d->hidden_by_filter)
++shown_dives;
}
countsChanged();
}
void MultiFilterSortModel::divesDeleted(dive_trip *, bool, const QVector<dive *> &dives)
{
for (dive *d: dives) {
if (!d->hidden_by_filter)
--shown_dives;
}
countsChanged();
}
void MultiFilterSortModel::countsChanged()
{
updateWindowTitle();

View file

@ -72,8 +72,6 @@ slots:
void stopFilterDiveSites();
void resetModel(DiveTripModelBase::Layout layout);
void filterDataChanged(const FilterData &data);
void divesAdded(struct dive_trip *, bool, const QVector<dive *> &dives);
void divesDeleted(struct dive_trip *, bool, const QVector<dive *> &dives);
signals:
void filterFinished();