selection: send selection signal from selectionChanged()

In DiveListView user actions (select-all, key-press,
mouse-release) were intercepted to send the selection-changed
signal if the selection changed.

However, with the recent cleanups, this can be done
directly from selectionChanged(), since in all cases (at
least the ones I tested), the part of the function that
is responsible for manual selection changes is called
only once.

This avoids quite some complex code flow.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-08-28 10:32:55 +02:00 committed by bstoeger
parent 018be753c3
commit 616dbd9671
2 changed files with 2 additions and 74 deletions

View file

@ -415,76 +415,6 @@ 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 then 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::keyPressEvent(QKeyEvent *event)
{
// Hook into key events that change the selection (i.e. cursor-up, cursor-down,
// page-up, page-down, home and end) and update selection if necessary.
// See comment in mouseReleaseEvent().
switch (event->key()) {
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_PageUp:
case Qt::Key_PageDown:
case Qt::Key_Home:
case Qt::Key_End:
break;
default:
return QTreeView::keyPressEvent(event);
}
QModelIndexList selectionBefore = selectionModel()->selectedRows();
QTreeView::keyPressEvent(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);
QModelIndexList selectionAfter = selectionModel()->selectedRows();
if (selectionBefore != selectionAfter)
selectionChangeDone();
}
void DiveListView::selectAll()
{
QTreeView::selectAll();
selectionChangeDone();
}
void DiveListView::selectionChangeDone()
{
#ifdef MAP_SUPPORT
@ -594,6 +524,8 @@ void DiveListView::selectionChanged(const QItemSelection &selected, const QItemS
// Display the new, processed, selection
QTreeView::selectionChanged(selectionModel()->selection(), newDeselected);
selectionChangeDone();
programmaticalSelectionChange = false;
}

View file

@ -56,10 +56,6 @@ slots:
private:
void rowsInserted(const QModelIndex &parent, int start, int end) override;
void reset() override;
void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void selectAll() override;
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override;
void selectionChangeDone();
void selectTripItems(QModelIndex index);