selection: create global single_selected_trip() function

The DiveListView had a singleSelectedTrip function that
returns the selected trip if exactly one trip is selected.
This could be very slow if numerous non-trip items were
selected, because all the selection indices were back-
translated by the proxy model.

This could make selection changes very slow, because the
MainTab used said function to determine whether it should
show trip or dive data.. Indeed, with a 3500 dive test log,
when selecting all dives in tree mode, the updating of the
TabWidgets is sped up from 130 ms to 5 ms this commit.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-05-02 18:14:44 +02:00 committed by Dirk Hohndel
parent 5147131701
commit 769915f3fe
5 changed files with 17 additions and 23 deletions

View file

@ -254,6 +254,18 @@ extern "C" void deselect_trip(struct dive_trip *trip)
}
}
extern "C" struct dive_trip *single_selected_trip()
{
if (amount_trips_selected != 1)
return NULL;
for (int i = 0; i < trip_table.nr; ++i) {
if (trip_table.trips[i]->selected)
return trip_table.trips[i];
}
fprintf(stderr, "warning: found no selected trip even though one should be selected\n");
return NULL; // shouldn't happen
}
extern "C" void clear_selection(void)
{
current_dive = nullptr;

View file

@ -23,6 +23,7 @@ extern void select_newest_visible_dive();
extern void select_single_dive(struct dive *d); // wrapper for setSelection() with a single dive. NULL clears the selection.
extern void select_trip(struct dive_trip *trip);
extern void deselect_trip(struct dive_trip *trip);
extern struct dive_trip *single_selected_trip(); // returns trip if exactly one trip is selected, NULL otherwise.
extern void clear_selection(void);
#if DEBUG_SELECTION_TRACKING

View file

@ -263,9 +263,8 @@ void DiveListView::rowsInserted(const QModelIndex &parent, int start, int end)
// Shouldn't the core-layer call us?
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.
if (singleSelectedTrip() == trip)
// First check if the trip is already selected (and only this trip, as only then is it displayed).
if (single_selected_trip() == trip)
return;
unselectDives();
@ -297,23 +296,6 @@ void DiveListView::unselectDives()
selectionModel()->clearSelection();
}
// 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()
{
dive_trip_t *res = nullptr;
for (const QModelIndex &index: selectionModel()->selectedRows()) {
if (index.parent().isValid())
continue;
if (dive_trip_t *trip = index.data(DiveTripModelBase::TRIP_ROLE).value<dive_trip *>()) {
if (res)
return nullptr; // More than one
res = trip;
}
}
return res;
}
bool DiveListView::eventFilter(QObject *, QEvent *event)
{
if (event->type() != QEvent::KeyPress)

View file

@ -25,7 +25,6 @@ public:
~DiveListView();
void setSortOrder(int i, Qt::SortOrder order); // Call to set sort order
void reload(); // Call to reload model data
dive_trip *singleSelectedTrip();
static QString lastUsedImageDir();
static void updateLastUsedImageDir(const QString &s);
void loadImages();

View file

@ -365,7 +365,8 @@ void MainTab::updateDiveInfo()
// 2) the filter is reset, potentially erasing the current trip under our feet.
// TODO: Don't hard code tab location!
bool onDiveSiteTab = ui.tabWidget->currentIndex() == 6;
if ((currentTrip = MainWindow::instance()->diveList->singleSelectedTrip()) != nullptr) {
currentTrip = single_selected_trip();
if (currentTrip) {
// Remember the tab selected for last dive but only if we're not on the dive site tab
if (lastSelectedDive && !onDiveSiteTab)
lastTabSelectedDive = ui.tabWidget->currentIndex();
@ -412,7 +413,6 @@ void MainTab::updateDiveInfo()
if (!lastSelectedDive && !onDiveSiteTab)
ui.tabWidget->setCurrentIndex(lastTabSelectedDive);
lastSelectedDive = true;
currentTrip = NULL;
// make all the fields visible writeable
ui.diveTripLocation->hide();
ui.location->show();