From edf6debbabe997dee9e636a831ccff73d184b6c1 Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Fri, 3 Nov 2017 13:24:18 +0100 Subject: QitemSelectionModel: Fix a bug in isColumnSelected Previously the code for isColumnSelected and isRowSelected differed slightly, in how unselectable indexes would be treated. This made isColumnSelected return false for a column, which mixed unselectable indexes and selected indexes. Thus in some situations, the user could not deselect a column via a QTableView header. By copying the isRowSelected code to isColumnSelected, rows and columns behave identical. Task-number: QTBUG-18001 Change-Id: I6ca85ac64b31a481fafeaa3bec958b18283eed8d Reviewed-by: Richard Moe Gustavsen --- src/corelib/itemmodels/qitemselectionmodel.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/corelib/itemmodels') diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp index 9af4fd9133..ceec0e4c94 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.cpp +++ b/src/corelib/itemmodels/qitemselectionmodel.cpp @@ -1577,8 +1577,12 @@ bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent for (int row = 0; row < rowCount; ++row) { for (it = joined.constBegin(); it != joined.constEnd(); ++it) { if ((*it).contains(row, column, parent)) { - Qt::ItemFlags flags = d->model->index(row, column, parent).flags(); - if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) { + bool selectable = false; + for (int i = row; !selectable && i <= (*it).bottom(); ++i) { + Qt::ItemFlags flags = d->model->index(i, column, parent).flags(); + selectable = flags & Qt::ItemIsSelectable; + } + if (selectable){ row = qMax(row, (*it).bottom()); break; } -- cgit v1.2.3 From 259648f876b2092f7d28925ba4569ac8a5612ca8 Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Mon, 6 Nov 2017 14:44:42 +0100 Subject: QItemSelectionModel: More fixes for is(Column/Row)Selected Replace the code for isRowSelected and isColumnSelected with a much simpler algorithm for deciding if a row/column is selected. In a model with a cross-hatch of unselectable indexes, the return values of is(Column/Row)Selected would depend on the order in which the selections were done. Task-number: QTBUG-18001 Change-Id: I6aa4b1df7c07fae469a686041927fa8c42bc9b16 Reviewed-by: Oswald Buddenhagen Reviewed-by: Richard Moe Gustavsen --- src/corelib/itemmodels/qitemselectionmodel.cpp | 73 ++++++++++++++++---------- 1 file changed, 46 insertions(+), 27 deletions(-) (limited to 'src/corelib/itemmodels') diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp index ceec0e4c94..edb9bb9098 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.cpp +++ b/src/corelib/itemmodels/qitemselectionmodel.cpp @@ -1502,30 +1502,40 @@ bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) cons && d->currentSelection.at(i).intersected(d->ranges.at(j)).isValid()) return false; } + + auto isSelectable = [&](int row, int column) { + Qt::ItemFlags flags = d->model->index(row, column, parent).flags(); + return (flags & Qt::ItemIsSelectable); + }; + + const int colCount = d->model->columnCount(parent); + int unselectable = 0; // add ranges and currentSelection and check through them all QList::const_iterator it; QList joined = d->ranges; if (d->currentSelection.count()) joined += d->currentSelection; - int colCount = d->model->columnCount(parent); for (int column = 0; column < colCount; ++column) { + if (!isSelectable(row, column)) { + ++unselectable; + continue; + } + for (it = joined.constBegin(); it != joined.constEnd(); ++it) { if ((*it).contains(row, column, parent)) { - bool selectable = false; - for (int i = column; !selectable && i <= (*it).right(); ++i) { - Qt::ItemFlags flags = d->model->index(row, i, parent).flags(); - selectable = flags & Qt::ItemIsSelectable; - } - if (selectable){ - column = qMax(column, (*it).right()); - break; + for (int i = column; i <= (*it).right(); ++i) { + if (!isSelectable(row, i)) + ++unselectable; } + + column = qMax(column, (*it).right()); + break; } } if (it == joined.constEnd()) return false; } - return colCount > 0; // no columns means no selected items + return unselectable < colCount; } /*! @@ -1568,30 +1578,39 @@ bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent } } } + + auto isSelectable = [&](int row, int column) { + Qt::ItemFlags flags = d->model->index(row, column, parent).flags(); + return (flags & Qt::ItemIsSelectable); + }; + const int rowCount = d->model->rowCount(parent); + int unselectable = 0; + // add ranges and currentSelection and check through them all QList::const_iterator it; QList joined = d->ranges; if (d->currentSelection.count()) joined += d->currentSelection; - int rowCount = d->model->rowCount(parent); for (int row = 0; row < rowCount; ++row) { - for (it = joined.constBegin(); it != joined.constEnd(); ++it) { - if ((*it).contains(row, column, parent)) { - bool selectable = false; - for (int i = row; !selectable && i <= (*it).bottom(); ++i) { - Qt::ItemFlags flags = d->model->index(i, column, parent).flags(); - selectable = flags & Qt::ItemIsSelectable; - } - if (selectable){ - row = qMax(row, (*it).bottom()); - break; - } - } - } - if (it == joined.constEnd()) - return false; + if (!isSelectable(row, column)) { + ++unselectable; + continue; + } + for (it = joined.constBegin(); it != joined.constEnd(); ++it) { + if ((*it).contains(row, column, parent)) { + for (int i = row; i <= (*it).bottom(); ++i) { + if (!isSelectable(i, column)) { + ++unselectable; + } + } + row = qMax(row, (*it).bottom()); + break; + } + } + if (it == joined.constEnd()) + return false; } - return rowCount > 0; // no rows means no selected items + return unselectable < rowCount; } /*! -- cgit v1.2.3