Clean up index handling in itemviews accessibility.

Change-Id: Icc017c7df9cb0dc4bf17e5168c1e3acda6af7523
Reviewed-by: Jan Arve Sæther <jan-arve.saether@digia.com>
This commit is contained in:
Frederik Gladhorn 2012-10-15 14:22:25 +02:00 committed by The Qt Project
parent 0ceb6541d7
commit 596f23554a
2 changed files with 61 additions and 29 deletions

View File

@ -79,8 +79,7 @@ int QAccessibleTable::logicalIndex(const QModelIndex &index) const
return -1;
int vHeader = verticalHeader() ? 1 : 0;
int hHeader = horizontalHeader() ? 1 : 0;
// row * number columns + column + 1 for one based counting
return (index.row() + hHeader)*(index.model()->columnCount() + vHeader) + (index.column() + vHeader) + 1;
return (index.row() + hHeader)*(index.model()->columnCount() + vHeader) + (index.column() + vHeader);
}
QAccessibleInterface *QAccessibleTable::childFromLogical(int logicalIndex) const
@ -88,7 +87,6 @@ QAccessibleInterface *QAccessibleTable::childFromLogical(int logicalIndex) const
if (!view()->model())
return 0;
logicalIndex--; // one based counting ftw
int vHeader = verticalHeader() ? 1 : 0;
int hHeader = horizontalHeader() ? 1 : 0;
@ -376,16 +374,20 @@ int QAccessibleTable::indexOfChild(const QAccessibleInterface *iface) const
{
if (!view()->model())
return -1;
QSharedPointer<QAccessibleInterface> parent(iface->parent());
if (parent->object() != view())
return -1;
Q_ASSERT(iface->role() != QAccessible::TreeItem); // should be handled by tree class
if (iface->role() == QAccessible::Cell || iface->role() == QAccessible::ListItem) {
const QAccessibleTableCell* cell = static_cast<const QAccessibleTableCell*>(iface);
return logicalIndex(cell->m_index) - 1;
return logicalIndex(cell->m_index);
} else if (iface->role() == QAccessible::ColumnHeader){
const QAccessibleTableHeaderCell* cell = static_cast<const QAccessibleTableHeaderCell*>(iface);
return cell->index + (verticalHeader() ? 1 : 0);
} else if (iface->role() == QAccessible::RowHeader){
const QAccessibleTableHeaderCell* cell = static_cast<const QAccessibleTableHeaderCell*>(iface);
return (cell->index+1) * (view()->model()->rowCount()+1);
return (cell->index + 1) * (view()->model()->rowCount() + 1);
} else if (iface->role() == QAccessible::Pane) {
return 0; // corner button
} else {
@ -424,8 +426,7 @@ QAccessibleInterface *QAccessibleTable::parent() const
QAccessibleInterface *QAccessibleTable::child(int index) const
{
// Fixme: get rid of the +1 madness
return childFromLogical(index + 1);
return childFromLogical(index);
}
void *QAccessibleTable::interface_cast(QAccessible::InterfaceType t)
@ -470,9 +471,8 @@ QAccessibleInterface *QAccessibleTree::childAt(int x, int y) const
int row = treeView->d_func()->viewIndex(index) + (horizontalHeader() ? 1 : 0);
int column = index.column();
int i = row * view()->model()->columnCount() + column + 1;
Q_ASSERT(i > view()->model()->columnCount());
return child(i - 1);
int i = row * view()->model()->columnCount() + column;
return child(i);
}
int QAccessibleTree::childCount() const
@ -521,7 +521,11 @@ int QAccessibleTree::indexOfChild(const QAccessibleInterface *iface) const
{
if (!view()->model())
return -1;
if (iface->role() == QAccessible::TreeItem) {
QSharedPointer<QAccessibleInterface> parent(iface->parent());
if (parent->object() != view())
return -1;
if (iface->role() == QAccessible::TreeItem) {
const QAccessibleTableCell* cell = static_cast<const QAccessibleTableCell*>(iface);
const QTreeView *treeView = qobject_cast<const QTreeView*>(view());
Q_ASSERT(treeView);
@ -534,7 +538,7 @@ int QAccessibleTree::indexOfChild(const QAccessibleInterface *iface) const
return index;
} else if (iface->role() == QAccessible::ColumnHeader){
const QAccessibleTableHeaderCell* cell = static_cast<const QAccessibleTableHeaderCell*>(iface);
//qDebug() << "QAccessibleTree::indexOfChild header " << cell->index << "is: " << cell->index + 1;
//qDebug() << "QAccessibleTree::indexOfChild header " << cell->index;
return cell->index;
} else {
qWarning() << "WARNING QAccessibleTable::indexOfChild invalid child"
@ -846,7 +850,7 @@ void QAccessibleTableHeaderCell::setText(QAccessible::Text, const QString &)
bool QAccessibleTableHeaderCell::isValid() const
{
return view && view->model() && (index > 0)
return view && view->model() && (index >= 0)
&& ((orientation == Qt::Horizontal) ? (index < view->model()->columnCount()) : (index < view->model()->rowCount()));
}

View File

@ -2543,13 +2543,27 @@ void tst_QAccessibility::treeTest()
QVERIFY(!(cell2->state().expandable));
QCOMPARE(iface->indexOfChild(cell2), 10);
QPoint pos = treeView->mapToGlobal(QPoint(0,0));
QModelIndex index = treeView->model()->index(0, 0, treeView->model()->index(1, 0));
pos += treeView->visualRect(index).center();
pos += QPoint(0, treeView->header()->height());
QAIPtr childAt2(iface->childAt(pos.x(), pos.y()));
QVERIFY(childAt2);
QCOMPARE(childAt2->text(QAccessible::Name), QString("Klimt"));
QCOMPARE(table2->columnDescription(0), QString("Artist"));
QCOMPARE(table2->columnDescription(1), QString("Work"));
delete iface;
delete treeView;
QTestAccessibility::clearEvents();
}
// The table used below is this:
// Button (0) | h1 (1) | h2 (2) | h3 (3)
// v1 (4) | 0.0 (5) | 1.0 (6) | 2.0 (7)
// v2 (8) | 0.1 (9) | 1.1 (10) | 2.1 (11)
// v3 (12) | 0.2 (13) | 1.2 (14) | 2.2 (15)
void tst_QAccessibility::tableTest()
{
QTableWidget *tableView = new QTableWidget(3, 3);
@ -2584,24 +2598,38 @@ void tst_QAccessibility::tableTest()
QCOMPARE(iface->indexOfChild(cornerButton.data()), 0);
QCOMPARE(cornerButton->role(), QAccessible::Pane);
QAIPtr child1(iface->child(2));
QVERIFY(child1);
QCOMPARE(iface->indexOfChild(child1.data()), 2);
QCOMPARE(child1->text(QAccessible::Name), QString("h2"));
QCOMPARE(child1->role(), QAccessible::ColumnHeader);
QVERIFY(!(child1->state().expanded));
QAIPtr h2(iface->child(2));
QVERIFY(h2);
QCOMPARE(iface->indexOfChild(h2.data()), 2);
QCOMPARE(h2->text(QAccessible::Name), QString("h2"));
QCOMPARE(h2->role(), QAccessible::ColumnHeader);
QVERIFY(!(h2->state().expanded));
QAIPtr child2(iface->child(10));
QVERIFY(child2);
QCOMPARE(iface->indexOfChild(child2.data()), 10);
QCOMPARE(child2->text(QAccessible::Name), QString("1.1"));
QAccessibleTableCellInterface *cell2Iface = child2->tableCellInterface();
QCOMPARE(cell2Iface->rowIndex(), 1);
QCOMPARE(cell2Iface->columnIndex(), 1);
QAIPtr v3(iface->child(12));
QVERIFY(v3);
QCOMPARE(iface->indexOfChild(v3.data()), 12);
QCOMPARE(v3->text(QAccessible::Name), QString("v3"));
QCOMPARE(v3->role(), QAccessible::RowHeader);
QVERIFY(!(v3->state().expanded));
QAIPtr child10(iface->child(10));
QVERIFY(child10);
QCOMPARE(iface->indexOfChild(child10.data()), 10);
QCOMPARE(child10->text(QAccessible::Name), QString("1.1"));
QAccessibleTableCellInterface *cell10Iface = child10->tableCellInterface();
QCOMPARE(cell10Iface->rowIndex(), 1);
QCOMPARE(cell10Iface->columnIndex(), 1);
QPoint pos = tableView->mapToGlobal(QPoint(0,0));
pos += tableView->visualRect(tableView->model()->index(1, 1)).center();
pos += QPoint(tableView->verticalHeader()->width(), tableView->horizontalHeader()->height());
QAIPtr childAt10(iface->childAt(pos.x(), pos.y()));
QCOMPARE(childAt10->text(QAccessible::Name), QString("1.1"));
QAIPtr child11(iface->child(11));
QCOMPARE(iface->indexOfChild(child11.data()), 11);
QCOMPARE(child11->text(QAccessible::Name), QString("1.2"));
QAIPtr child3(iface->child(11));
QCOMPARE(iface->indexOfChild(child3.data()), 11);
QCOMPARE(child3->text(QAccessible::Name), QString("1.2"));
QTestAccessibility::clearEvents();