Change behaviour of selectRow, selectColumn, unselectRow, unselectColumn

According to the comments of selectRow and selectColumn, the expected
behaviour of this method was to select a row or a column and unselect
any cell that were previously selected. However the actual behavior
was to select only one cell and not deselect any cell.

Moreover, according to the specification there's no simple way of
selecting multiple rows or columns as when one of the methods is
called for selecting one row or column the others should be
unselected.

The specification was changed not to require the rest of the cells
to be deselected, although they might be deselected if the
selectionMode requires that in order for the new row/column to be
selected.

The implementation of these methods was changed in QAccessibleTable
and QAccessibleTree to select the whole row/column and take into
acount selectionMode and selectionBehavior.

tst_qaccessibility.cpp was modified to test the new behaviour of
the methods.

Change-Id: I29635d014792169302435e81704e02c16f951238
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
This commit is contained in:
José Millán Soto 2012-09-24 14:16:16 +02:00 committed by The Qt Project
parent 533105cadf
commit 4b7be05058
3 changed files with 152 additions and 12 deletions

View File

@ -185,9 +185,9 @@ public:
virtual bool isColumnSelected(int column) const = 0;
// Returns a boolean value indicating whether the specified row is completely selected.
virtual bool isRowSelected(int row) const = 0;
// Selects a row and unselects all previously selected rows.
// Selects a row and it might unselect all previously selected rows.
virtual bool selectRow(int row) = 0;
// Selects a column and unselects all previously selected columns.
// Selects a column it might unselect all previously selected columns.
virtual bool selectColumn(int column) = 0;
// Unselects one row, leaving other selected rows selected (if any).
virtual bool unselectRow(int row) = 0;

View File

@ -299,9 +299,28 @@ bool QAccessibleTable::selectRow(int row)
if (!view()->model() || !view()->selectionModel())
return false;
QModelIndex index = view()->model()->index(row, 0, view()->rootIndex());
if (!index.isValid() || view()->selectionMode() & QAbstractItemView::NoSelection)
if (!index.isValid() || view()->selectionBehavior() == QAbstractItemView::SelectColumns)
return false;
view()->selectionModel()->select(index, QItemSelectionModel::Select);
switch (view()->selectionMode()) {
case QAbstractItemView::NoSelection:
return false;
case QAbstractItemView::SingleSelection:
if (view()->selectionBehavior() != QAbstractItemView::SelectRows && columnCount() > 1 )
return false;
view()->clearSelection();
break;
case QAbstractItemView::ContiguousSelection:
if ((!row || !view()->selectionModel()->isRowSelected(row - 1, view()->rootIndex()))
&& !view()->selectionModel()->isRowSelected(row + 1, view()->rootIndex()))
view()->clearSelection();
break;
default:
break;
}
view()->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
return true;
}
@ -310,9 +329,26 @@ bool QAccessibleTable::selectColumn(int column)
if (!view()->model() || !view()->selectionModel())
return false;
QModelIndex index = view()->model()->index(0, column, view()->rootIndex());
if (!index.isValid() || view()->selectionMode() & QAbstractItemView::NoSelection)
if (!index.isValid() || view()->selectionBehavior() == QAbstractItemView::SelectRows)
return false;
view()->selectionModel()->select(index, QItemSelectionModel::Select);
switch (view()->selectionMode()) {
case QAbstractItemView::NoSelection:
return false;
case QAbstractItemView::SingleSelection:
if (view()->selectionBehavior() != QAbstractItemView::SelectColumns && rowCount() > 1)
return false;
case QAbstractItemView::ContiguousSelection:
if ((!column || !view()->selectionModel()->isColumnSelected(column - 1, view()->rootIndex()))
&& !view()->selectionModel()->isColumnSelected(column + 1, view()->rootIndex()))
view()->clearSelection();
break;
default:
break;
}
view()->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Columns);
return true;
}
@ -320,10 +356,35 @@ bool QAccessibleTable::unselectRow(int row)
{
if (!view()->model() || !view()->selectionModel())
return false;
QModelIndex index = view()->model()->index(row, 0, view()->rootIndex());
if (!index.isValid() || view()->selectionMode() & QAbstractItemView::NoSelection)
if (!index.isValid())
return false;
view()->selectionModel()->select(index, QItemSelectionModel::Deselect);
QItemSelection selection(index, index);
switch (view()->selectionMode()) {
case QAbstractItemView::SingleSelection:
//In SingleSelection and ContiguousSelection once an item
//is selected, there's no way for the user to unselect all items
if (selectedRowCount() == 1)
return false;
break;
case QAbstractItemView::ContiguousSelection:
if (selectedRowCount() == 1)
return false;
if ((!row || view()->selectionModel()->isRowSelected(row - 1, view()->rootIndex()))
&& view()->selectionModel()->isRowSelected(row + 1, view()->rootIndex())) {
//If there are rows selected both up the current row and down the current rown,
//the ones which are down the current row will be deselected
selection = QItemSelection(index, view()->model()->index(rowCount() - 1, 0, view()->rootIndex()));
}
default:
break;
}
view()->selectionModel()->select(selection, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
return true;
}
@ -331,10 +392,35 @@ bool QAccessibleTable::unselectColumn(int column)
{
if (!view()->model() || !view()->selectionModel())
return false;
QModelIndex index = view()->model()->index(0, column, view()->rootIndex());
if (!index.isValid() || view()->selectionMode() & QAbstractItemView::NoSelection)
if (!index.isValid())
return false;
view()->selectionModel()->select(index, QItemSelectionModel::Columns & QItemSelectionModel::Deselect);
QItemSelection selection(index, index);
switch (view()->selectionMode()) {
case QAbstractItemView::SingleSelection:
//In SingleSelection and ContiguousSelection once an item
//is selected, there's no way for the user to unselect all items
if (selectedColumnCount() == 1)
return false;
break;
case QAbstractItemView::ContiguousSelection:
if (selectedColumnCount() == 1)
return false;
if ((!column || view()->selectionModel()->isColumnSelected(column - 1, view()->rootIndex()))
&& view()->selectionModel()->isColumnSelected(column + 1, view()->rootIndex())) {
//If there are columns selected both at the left of the current row and at the right
//of the current rown, the ones which are at the right will be deselected
selection = QItemSelection(index, view()->model()->index(0, columnCount() - 1, view()->rootIndex()));
}
default:
break;
}
view()->selectionModel()->select(selection, QItemSelectionModel::Deselect | QItemSelectionModel::Columns);
return true;
}
@ -576,9 +662,28 @@ bool QAccessibleTree::selectRow(int row)
if (!view()->selectionModel())
return false;
QModelIndex index = indexFromLogical(row);
if (!index.isValid() || view()->selectionMode() & QAbstractItemView::NoSelection)
if (!index.isValid() || view()->selectionBehavior() == QAbstractItemView::SelectColumns)
return false;
view()->selectionModel()->select(index, QItemSelectionModel::Select);
switch (view()->selectionMode()) {
case QAbstractItemView::NoSelection:
return false;
case QAbstractItemView::SingleSelection:
if ((view()->selectionBehavior() != QAbstractItemView::SelectRows) && (columnCount() > 1))
return false;
view()->clearSelection();
break;
case QAbstractItemView::ContiguousSelection:
if ((!row || !view()->selectionModel()->isRowSelected(row - 1, view()->rootIndex()))
&& !view()->selectionModel()->isRowSelected(row + 1, view()->rootIndex()))
view()->clearSelection();
break;
default:
break;
}
view()->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
return true;
}

View File

@ -2651,6 +2651,41 @@ void tst_QAccessibility::tableTest()
QCOMPARE(table2->rowDescription(1), QString("v2"));
QCOMPARE(table2->rowDescription(2), QString("v3"));
tableView->clearSelection();
tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
QVERIFY(!table2->selectRow(0));
QVERIFY(!table2->isRowSelected(0));
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
QVERIFY(table2->selectRow(0));
QVERIFY(table2->selectRow(1));
QVERIFY(!table2->isRowSelected(0));
tableView->setSelectionMode(QAbstractItemView::MultiSelection);
QVERIFY(table2->selectRow(0));
QVERIFY(table2->isRowSelected(1));
QVERIFY(table2->unselectRow(0));
QVERIFY(!table2->isRowSelected(0));
tableView->setSelectionBehavior(QAbstractItemView::SelectColumns);
QVERIFY(!table2->selectRow(0));
QVERIFY(!table2->isRowSelected(0));
tableView->clearSelection();
QCOMPARE(table2->selectedColumnCount(), 0);
QCOMPARE(table2->selectedRowCount(), 0);
QVERIFY(table2->selectColumn(1));
QVERIFY(table2->isColumnSelected(1));
tableView->clearSelection();
tableView->setSelectionMode(QAbstractItemView::ContiguousSelection);
table2->selectColumn(0);
table2->selectColumn(2);
QVERIFY(!(table2->isColumnSelected(2) && table2->isColumnSelected(0)));
tableView->clearSelection();
tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
tableView->setSelectionMode(QAbstractItemView::MultiSelection);
table2->selectColumn(1);
table2->selectRow(1);
QVERIFY(table2->isColumnSelected(1));
QVERIFY(table2->isRowSelected(1));
delete tableView;
QTestAccessibility::clearEvents();