Dive list: be more careful on when updating the UI after selection

Fix two issues:

1) When narrowing the selection, we didn't get setSelection()
   calls. Only, when the user released the mouse button was
   the selection updated. Therefore, hook into the mouse-release-
   event and update the UI if the selection changed.

2) We updated the ui in setSelection(). However, this was called
   on mouse-move even if the actual selection didn't change.
   Therefore, compare selection before and after processing of
   the event and only refresh the UI if there are changes.

Clearly, this can only be a quick stopgap solution and we
should find out how to properly hook into the selection change
machinery. Though see commit 4928c4ae04
for the reason why we do things as we do them.

Fixes #2595

Reported-by: Willem Ferguson <willemferguson@zoology.up.ac.za>
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-02-08 22:05:37 +01:00 committed by Dirk Hohndel
parent 1f03a8be81
commit 2cea115ddb
2 changed files with 36 additions and 1 deletions

View file

@ -533,10 +533,44 @@ void DiveListView::currentChanged(const QModelIndex &current, const QModelIndex&
scrollTo(current);
}
void DiveListView::mouseReleaseEvent(QMouseEvent *event)
{
// Oy vey. We hook into QTreeView's setSelection() to update the UI
// after selection changes. However, there is a case when this doesn't
// work: when narrowing the selection. If multiple dives are selected,
// and the user clicks on one of them, the selection is unchanged.
// Only on mouse-release the selection is changed, but then
// setSelection() is not called.
// Notably, this happens when the user selects a trip and the clicks
// on a dive in the same trip.
// To solve this, we hook into the mouseReleseEvent here and detect
// selection changes changes by comparing the selection before and after
// processing of the event.
// We really should find another way to solve this.
QModelIndexList selectionBefore = selectionModel()->selectedRows();
QTreeView::mouseReleaseEvent(event);
QModelIndexList selectionAfter = selectionModel()->selectedRows();
if (selectionBefore != selectionAfter)
selectionChangeDone();
}
void DiveListView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags)
{
// We hook into QTreeView's setSelection() to update the UI
// after selection changes. However, we must be careful:
// when the user clicks on an already selected dive, this is called
// with the QItemSelectionModel::NoUpdate flags. In this case, just
// route through. Moreover, we get setSelection() calls on every
// mouseMove event. To avoid excessive reloading of the UI check
// if the selection changed by comparing before and after processing
// the selection.
if (flags == QItemSelectionModel::NoUpdate)
return QTreeView::setSelection(rect, flags);
QModelIndexList selectionBefore = selectionModel()->selectedRows();
QTreeView::setSelection(rect, flags);
selectionChangeDone();
QModelIndexList selectionAfter = selectionModel()->selectedRows();
if (selectionBefore != selectionAfter)
selectionChangeDone();
}
void DiveListView::selectAll()

View file

@ -65,6 +65,7 @@ slots:
void tripChanged(dive_trip *trip, TripField);
private:
void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void selectAll() override;
void selectionChangeDone();
DiveTripModelBase::Layout currentLayout;