diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-04-26 22:05:28 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-04-26 13:54:59 -0700 |
commit | 2021035cfcee08ec4c6f9d16683db8ce400bef30 (patch) | |
tree | 26d585dad696ca190a7d28a880894726cc14e378 /desktop-widgets/divelistview.cpp | |
parent | 6e83135fba49680fe1e951d19d6bc6690328c5d8 (diff) | |
download | subsurface-2021035cfcee08ec4c6f9d16683db8ce400bef30.tar.gz |
selection: replace selectedTrips() by singleSelectedTrip() function
To check wether the tab widgets should show the trip view, they called
the selectedTrips() function. The trip view was shown if that contained
only one trip. However, the selectedTrips() function was very slow,
because it has to query to core models.
Change the function to singleSelectedTrip(), which returns a trip
if there is exactly one trip selected. The function returns early
if there is more than one trip selected. This makes the select-all
case much faster.
There are two cases which are still very slow:
- List mode, because here all top-level items are queried.
- Dive log with many only top-level items.
Ultimately, we will have to cache the trip selection because
querying the model is too slow.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/divelistview.cpp')
-rw-r--r-- | desktop-widgets/divelistview.cpp | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/desktop-widgets/divelistview.cpp b/desktop-widgets/divelistview.cpp index 3e26ca125..f95afee25 100644 --- a/desktop-widgets/divelistview.cpp +++ b/desktop-widgets/divelistview.cpp @@ -265,8 +265,7 @@ void DiveListView::tripChanged(dive_trip *trip, TripField) { // First check if the trip is already selected (and only // this trip, as only then is it displayed). Is so, then do nothing. - QList<dive_trip *> selected = selectedTrips(); - if (selected.size() == 1 && selected[0] == trip) + if (singleSelectedTrip() == trip) return; unselectDives(); @@ -309,16 +308,21 @@ void DiveListView::unselectDives() } } -QList<dive_trip_t *> DiveListView::selectedTrips() +// This function returns a trip if there is one selected trip or NULL. +// Returning all selected trips turned out to be too slow. +dive_trip_t *DiveListView::singleSelectedTrip() { - QList<dive_trip_t *> ret; - Q_FOREACH (const QModelIndex &index, selectionModel()->selectedRows()) { - dive_trip_t *trip = index.data(DiveTripModelBase::TRIP_ROLE).value<dive_trip *>(); - if (!trip) + dive_trip_t *res = nullptr; + for (const QModelIndex &index: selectionModel()->selectedRows()) { + if (index.parent().isValid()) continue; - ret.push_back(trip); + if (dive_trip_t *trip = index.data(DiveTripModelBase::TRIP_ROLE).value<dive_trip *>()) { + if (res) + return nullptr; // More than one + res = trip; + } } - return ret; + return res; } bool DiveListView::eventFilter(QObject *, QEvent *event) |