diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2021-10-11 16:18:35 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2021-10-12 09:07:54 +0200 |
commit | 826765f654e43034929ff6fbc975ab0ab5d6922a (patch) | |
tree | 3c5bec62d9e46fba9fa4b8fa45c67a281b59170e /src/widgets/itemviews/qtableview.cpp | |
parent | 9a772306854bf0ca2f0c16699744136530656f43 (diff) |
Select a single range of cells in QTableView, away from merged cells
- when there is no intersection between the current selection and the
spans collection, get ranges for all cells, just as if no span exists
- when there is an intersection between the current selection and the
spans collection, get separate ranges for each cell (as before)
This fixes the regular case of selecting multiple non-spanned cells
after some cells are merged (get a single range for all cells instead of
separate range for each cell). However, when selecting together a group
of spanned and non-spanned cells, you still get a separate range for
each cell. But this is normal behavior in similar applications; for
example in LibreOffice, you cannot select and merge spanned and
non-spanned cells: an error dialog tells you that it's not allowed.
Done-with: Christos Kokkinidis
Pick-to: 6.2
Fixes: QTBUG-255
Change-Id: Ic38f9a064a1f499825e7f750668013fc2dc564ba
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/widgets/itemviews/qtableview.cpp')
-rw-r--r-- | src/widgets/itemviews/qtableview.cpp | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp index d1793a3049..c3138860fa 100644 --- a/src/widgets/itemviews/qtableview.cpp +++ b/src/widgets/itemviews/qtableview.cpp @@ -2004,6 +2004,9 @@ void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionF if (d->hasSpans()) { bool expanded; + // when the current selection does not intersect with any spans of merged cells, + // the range of selected cells must be the same as if there were no merged cells + bool intersectsSpan = false; int top = qMin(d->visualRow(tl.row()), d->visualRow(br.row())); int left = qMin(d->visualColumn(tl.column()), d->visualColumn(br.column())); int bottom = qMax(d->visualRow(tl.row()), d->visualRow(br.row())); @@ -2018,6 +2021,7 @@ void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionF int r = d->visualColumn(d->columnSpanEndLogical(span.left(), span.width())); if ((t > bottom) || (l > right) || (top > b) || (left > r)) continue; // no intersect + intersectsSpan = true; if (t < top) { top = t; expanded = true; @@ -2038,14 +2042,20 @@ void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionF break; } } while (expanded); - selection.reserve((right - left + 1) * (bottom - top + 1)); - for (int horizontal = left; horizontal <= right; ++horizontal) { - int column = d->logicalColumn(horizontal); - for (int vertical = top; vertical <= bottom; ++vertical) { - int row = d->logicalRow(vertical); - QModelIndex index = d->model->index(row, column, d->root); - selection.append(QItemSelectionRange(index)); + if (intersectsSpan) { + selection.reserve((right - left + 1) * (bottom - top + 1)); + for (int horizontal = left; horizontal <= right; ++horizontal) { + int column = d->logicalColumn(horizontal); + for (int vertical = top; vertical <= bottom; ++vertical) { + int row = d->logicalRow(vertical); + QModelIndex index = d->model->index(row, column, d->root); + selection.append(QItemSelectionRange(index)); + } } + } else { + QItemSelectionRange range(tl, br); + if (!range.isEmpty()) + selection.append(range); } } else if (verticalMoved && horizontalMoved) { int top = d->visualRow(tl.row()); |