Simplify QAccessibleAbstractScrollArea and Calendar.

Change-Id: Iea1df37fd9fd486295458ec7627f5b6908053cdf
Reviewed-on: http://codereview.qt-project.org/5727
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Jan-Arve Sæther <jan-arve.saether@nokia.com>
This commit is contained in:
Frederik Gladhorn 2011-09-28 15:36:52 +02:00 committed by Qt by Nokia
parent 9ac51af083
commit 5ea2eab00d
5 changed files with 82 additions and 172 deletions

View File

@ -1847,59 +1847,16 @@ QAccessibleAbstractScrollArea::QAccessibleAbstractScrollArea(QWidget *widget)
Q_ASSERT(qobject_cast<QAbstractScrollArea *>(widget)); Q_ASSERT(qobject_cast<QAbstractScrollArea *>(widget));
} }
QString QAccessibleAbstractScrollArea::text(Text textType, int child) const
{
if (child == Self)
return QAccessibleWidget::text(textType, 0);
QWidgetList children = accessibleChildren();
if (child < 1 || child > children.count())
return QString();
QAccessibleInterface *childInterface = queryAccessibleInterface(children.at(child - 1));
if (!childInterface)
return QString();
QString string = childInterface->text(textType, 0);
delete childInterface;
return string;
}
void QAccessibleAbstractScrollArea::setText(Text textType, int child, const QString &text)
{
if (text.isEmpty())
return;
if (child == 0) {
QAccessibleWidget::setText(textType, 0, text);
return;
}
QWidgetList children = accessibleChildren();
if (child < 1 || child > children.count())
return;
QAccessibleInterface *childInterface = queryAccessibleInterface(children.at(child - 1));
if (!childInterface)
return;
childInterface->setText(textType, 0, text);
delete childInterface;
}
QAccessible::State QAccessibleAbstractScrollArea::state(int child) const
{
if (child == Self)
return QAccessibleWidget::state(child);
QWidgetList children = accessibleChildren();
if (child < 1 || child > children.count())
return QAccessibleWidget::state(Self);
QAccessibleInterface *childInterface = queryAccessibleInterface(children.at(child - 1));
if (!childInterface)
return QAccessibleWidget::state(Self);
QAccessible::State returnState = childInterface->state(0);
delete childInterface;
return returnState;
}
QVariant QAccessibleAbstractScrollArea::invokeMethod(QAccessible::Method, int, const QVariantList &) QVariant QAccessibleAbstractScrollArea::invokeMethod(QAccessible::Method, int, const QVariantList &)
{ {
return QVariant(); return QVariant();
} }
QAccessibleInterface *QAccessibleAbstractScrollArea::child(int index) const
{
return QAccessible::queryAccessibleInterface(accessibleChildren().at(index));
}
int QAccessibleAbstractScrollArea::childCount() const int QAccessibleAbstractScrollArea::childCount() const
{ {
return accessibleChildren().count(); return accessibleChildren().count();
@ -1948,9 +1905,10 @@ int QAccessibleAbstractScrollArea::navigate(RelationFlag relation, int entry, QA
// to the reader. :-) // to the reader. :-)
switch (relation) { switch (relation) {
case Child: case Child:
if (entry > 0) if (entry > 0) {
targetWidget = children.at(entry - 1); *target = child(entry - 1);
break; return *target ? 0 : -1;
}
case Left: case Left:
if (entry < 1) if (entry < 1)
break; break;
@ -2039,46 +1997,31 @@ int QAccessibleAbstractScrollArea::navigate(RelationFlag relation, int entry, QA
return *target ? 0: -1; return *target ? 0: -1;
} }
QRect QAccessibleAbstractScrollArea::rect(int child) const //int QAccessibleAbstractScrollArea::childAt(int x, int y) const
{ //{
if (!abstractScrollArea()->isVisible()) // if (!abstractScrollArea()->isVisible())
return QRect(); // return -1;
if (child == Self) //#if 0
return QAccessibleWidget::rect(child); // const QRect globalSelfGeometry = rect(Self);
QWidgetList children = accessibleChildren(); // if (!globalSelfGeometry.isValid() || !globalSelfGeometry.contains(QPoint(x, y)))
if (child < 1 || child > children.count()) // return -1;
return QRect(); // const QWidgetList children = accessibleChildren();
const QWidget *childWidget = children.at(child - 1); // for (int i = 0; i < children.count(); ++i) {
if (!childWidget->isVisible()) // const QWidget *child = children.at(i);
return QRect(); // const QRect globalChildGeometry = QRect(child->mapToGlobal(QPoint(0, 0)), child->size());
return QRect(childWidget->mapToGlobal(QPoint(0, 0)), childWidget->size()); // if (globalChildGeometry.contains(QPoint(x, y))) {
} // return ++i;
// }
int QAccessibleAbstractScrollArea::childAt(int x, int y) const // }
{ // return 0;
if (!abstractScrollArea()->isVisible()) //#else
return -1; // for (int i = childCount(); i >= 0; --i) {
#if 0 // if (rect().contains(x, y))
const QRect globalSelfGeometry = rect(Self); // return i;
if (!globalSelfGeometry.isValid() || !globalSelfGeometry.contains(QPoint(x, y))) // }
return -1; // return -1;
const QWidgetList children = accessibleChildren(); //#endif
for (int i = 0; i < children.count(); ++i) { //}
const QWidget *child = children.at(i);
const QRect globalChildGeometry = QRect(child->mapToGlobal(QPoint(0, 0)), child->size());
if (globalChildGeometry.contains(QPoint(x, y))) {
return ++i;
}
}
return 0;
#else
for (int i = childCount(); i >= 0; --i) {
if (rect(i).contains(x, y))
return i;
}
return -1;
#endif
}
QAbstractScrollArea *QAccessibleAbstractScrollArea::abstractScrollArea() const QAbstractScrollArea *QAccessibleAbstractScrollArea::abstractScrollArea() const
{ {

View File

@ -74,16 +74,13 @@ public:
Undefined Undefined
}; };
QString text(Text textType, int child) const; QAccessibleInterface *child(int index) const;
void setText(Text textType, int child, const QString &text); QVariant invokeMethod(QAccessible::Method method, int, const QVariantList &params);
State state(int child) const;
QVariant invokeMethod(QAccessible::Method method, int child, const QVariantList &params);
int childCount() const; int childCount() const;
int indexOfChild(const QAccessibleInterface *child) const; int indexOfChild(const QAccessibleInterface *child) const;
bool isValid() const; bool isValid() const;
int navigate(RelationFlag relation, int entry, QAccessibleInterface **target) const; int navigate(RelationFlag relation, int entry, QAccessibleInterface **target) const;
QRect rect(int child) const; // int childAt(int x, int y) const;
int childAt(int x, int y) const;
//protected: //protected:
QAbstractScrollArea *abstractScrollArea() const; QAbstractScrollArea *abstractScrollArea() const;

View File

@ -1200,6 +1200,17 @@ int QAccessibleCalendarWidget::indexOfChild(const QAccessibleInterface *child) c
return 1; return 1;
} }
QAccessibleInterface *QAccessibleCalendarWidget::child(int index) const
{
if (index < 0 || index >= childCount())
return 0;
if (childCount() > 1 && index == 0)
return queryAccessibleInterface(navigationBar());
return queryAccessibleInterface(calendarView());
}
int QAccessibleCalendarWidget::navigate(RelationFlag relation, int entry, QAccessibleInterface **target) const int QAccessibleCalendarWidget::navigate(RelationFlag relation, int entry, QAccessibleInterface **target) const
{ {
*target = 0; *target = 0;
@ -1208,15 +1219,8 @@ int QAccessibleCalendarWidget::navigate(RelationFlag relation, int entry, QAcces
QWidget *targetWidget = 0; QWidget *targetWidget = 0;
switch (relation) { switch (relation) {
case Child: case Child:
if (childCount() == 1) { *target = child(entry - 1);
targetWidget = calendarView(); return *target ? 0 : -1;
} else {
if (entry == 1)
targetWidget = navigationBar();
else
targetWidget = calendarView();
}
break;
case Up: case Up:
if (entry == 2) if (entry == 2)
targetWidget = navigationBar(); targetWidget = navigationBar();
@ -1229,33 +1233,7 @@ int QAccessibleCalendarWidget::navigate(RelationFlag relation, int entry, QAcces
return QAccessibleWidget::navigate(relation, entry, target); return QAccessibleWidget::navigate(relation, entry, target);
} }
*target = queryAccessibleInterface(targetWidget); *target = queryAccessibleInterface(targetWidget);
return *target ? 0: -1; return *target ? 0 : -1;
}
QRect QAccessibleCalendarWidget::rect(int child) const
{
if (!calendarWidget()->isVisible() || child > childCount())
return QRect();
if (child == 0)
return QAccessibleWidget::rect(child);
QWidget *childWidget = 0;
if (childCount() == 2)
childWidget = child == 1 ? navigationBar() : calendarView();
else
childWidget = calendarView();
return QRect(childWidget->mapToGlobal(QPoint(0, 0)), childWidget->size());
}
int QAccessibleCalendarWidget::childAt(int x, int y) const
{
const QPoint globalTargetPos = QPoint(x, y);
if (!rect(0).contains(globalTargetPos))
return -1;
if (rect(1).contains(globalTargetPos))
return 1;
if (rect(2).contains(globalTargetPos))
return 2;
return 0;
} }
QCalendarWidget *QAccessibleCalendarWidget::calendarWidget() const QCalendarWidget *QAccessibleCalendarWidget::calendarWidget() const

View File

@ -228,13 +228,14 @@ class QAccessibleCalendarWidget : public QAccessibleWidget
public: public:
explicit QAccessibleCalendarWidget(QWidget *widget); explicit QAccessibleCalendarWidget(QWidget *widget);
QVariant invokeMethod(QAccessible::Method method, int child, const QVariantList &params);
int childCount() const; int childCount() const;
int indexOfChild(const QAccessibleInterface *child) const; int indexOfChild(const QAccessibleInterface *child) const;
int navigate(RelationFlag relation, int entry, QAccessibleInterface **target) const; int navigate(RelationFlag relation, int entry, QAccessibleInterface **target) const;
QRect rect(int child) const;
int childAt(int x, int y) const;
QAccessibleInterface *child(int index) const;
QVariant invokeMethod(QAccessible::Method method, int child, const QVariantList &params);
protected: protected:
QCalendarWidget *calendarWidget() const; QCalendarWidget *calendarWidget() const;

View File

@ -81,6 +81,32 @@ static inline bool verifyChild(QWidget *child, QAccessibleInterface *interface,
return false; return false;
} }
// Verify that we get a valid QAccessibleInterface for the child.
QAccessibleInterface *childInterface = QAccessible::queryAccessibleInterface(child);
if (!childInterface) {
qWarning("tst_QAccessibility::verifyChild: Failed to retrieve interface for child.");
return false;
}
// QAccessibleInterface::indexOfChild():
// Verify that indexOfChild() returns an index equal to the index passed in
int indexFromIndexOfChild = interface->indexOfChild(childInterface);
delete childInterface;
if (indexFromIndexOfChild != index) {
qWarning("tst_QAccessibility::verifyChild (indexOfChild()):");
qWarning() << "Expected:" << index;
qWarning() << "Actual: " << indexFromIndexOfChild;
return false;
}
// Navigate to child, compare its object and role with the interface from queryAccessibleInterface(child).
QAccessibleInterface *navigatedChildInterface = interface->child(index - 1);
if (navigatedChildInterface == 0)
return false;
const QRect rectFromInterface = navigatedChildInterface->rect();
delete navigatedChildInterface;
// QAccessibleInterface::childAt(): // QAccessibleInterface::childAt():
// Calculate global child position and check that the interface // Calculate global child position and check that the interface
// returns the correct index for that position. // returns the correct index for that position.
@ -97,7 +123,6 @@ static inline bool verifyChild(QWidget *child, QAccessibleInterface *interface,
// Calculate global child geometry and check that the interface // Calculate global child geometry and check that the interface
// returns a QRect which is equal to the calculated QRect. // returns a QRect which is equal to the calculated QRect.
const QRect expectedGlobalRect = QRect(globalChildPos, child->size()); const QRect expectedGlobalRect = QRect(globalChildPos, child->size());
const QRect rectFromInterface = interface->rect(index);
if (expectedGlobalRect != rectFromInterface) { if (expectedGlobalRect != rectFromInterface) {
qWarning("tst_QAccessibility::verifyChild (rect()):"); qWarning("tst_QAccessibility::verifyChild (rect()):");
qWarning() << "Expected:" << expectedGlobalRect; qWarning() << "Expected:" << expectedGlobalRect;
@ -111,39 +136,6 @@ static inline bool verifyChild(QWidget *child, QAccessibleInterface *interface,
return false; return false;
} }
// Verify that we get a valid QAccessibleInterface for the child.
QAccessibleInterface *childInterface = QAccessible::queryAccessibleInterface(child);
if (!childInterface) {
qWarning("tst_QAccessibility::verifyChild: Failed to retrieve interface for child.");
return false;
}
// QAccessibleInterface::indexOfChild():
// Verify that indexOfChild() returns an index equal to the index passed by,
// or -1 if child is "Self" (index == 0).
int indexFromIndexOfChild = interface->indexOfChild(childInterface);
delete childInterface;
int expectedIndex = index == 0 ? -1 : index;
if (indexFromIndexOfChild != expectedIndex) {
qWarning("tst_QAccessibility::verifyChild (indexOfChild()):");
qWarning() << "Expected:" << expectedIndex;
qWarning() << "Actual: " << indexFromIndexOfChild;
return false;
}
// Navigate to child, compare its object and role with the interface from queryAccessibleInterface(child).
{
QAccessibleInterface *navigatedChildInterface = 0;
const int status = interface->navigate(QAccessible::Child, index, &navigatedChildInterface);
// We are navigating to a separate widget/interface, so status should be 0.
if (status != 0)
return false;
if (navigatedChildInterface == 0)
return false;
delete navigatedChildInterface;
}
return true; return true;
} }
@ -2413,7 +2405,6 @@ void tst_QAccessibility::abstractScrollAreaTest()
QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&abstractScrollArea); QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&abstractScrollArea);
QVERIFY(interface); QVERIFY(interface);
QVERIFY(!interface->rect(0).isValid()); QVERIFY(!interface->rect(0).isValid());
QVERIFY(!interface->rect(1).isValid());
QCOMPARE(interface->childAt(200, 200), -1); QCOMPARE(interface->childAt(200, 200), -1);
abstractScrollArea.resize(400, 400); abstractScrollArea.resize(400, 400);
@ -3138,7 +3129,7 @@ void tst_QAccessibility::calendarWidgetTest()
const QRect globalGeometry = QRect(calendarWidget.mapToGlobal(QPoint(0, 0)), const QRect globalGeometry = QRect(calendarWidget.mapToGlobal(QPoint(0, 0)),
calendarWidget.size()); calendarWidget.size());
QCOMPARE(interface->rect(0), globalGeometry); QCOMPARE(interface->rect(), globalGeometry);
QWidget *navigationBar = 0; QWidget *navigationBar = 0;
foreach (QObject *child, calendarWidget.children()) { foreach (QObject *child, calendarWidget.children()) {