From 9ab84bd46861557afb190ba4cb2481b35a7401e5 Mon Sep 17 00:00:00 2001 From: Jan-Arve Saether Date: Tue, 3 Jan 2012 09:40:16 +0100 Subject: Make QAccessibleQuickView::childAt() work properly with overlapping items The previous code did not consider items that were overlapped due to having different z coordinates. The approach used is now the same as found in QQuickCanvas::mousePressEvent(). Strictly speaking, this is a violation of childAt (since it will disregard the implementation of childAt of all the descendants along the path down to the item actually returned.) However, I don't see any good reason for that the implementation for childAt() would be different than how mousePressEvent behaves. It should also perform better than any other solution I managed to think of. Change-Id: I2d3fa2282437c7b5533c6149c62fc456ccf2ccfa Reviewed-by: Frederik Gladhorn --- .../accessible/quick/qaccessiblequickitem.cpp | 49 +++++++++++++--------- .../accessible/quick/qaccessiblequickitem.h | 3 ++ .../accessible/quick/qaccessiblequickview.cpp | 48 ++++++++++++++++++--- .../accessible/shared/qdeclarativeaccessible.cpp | 5 +-- 4 files changed, 77 insertions(+), 28 deletions(-) (limited to 'src/plugins/accessible') diff --git a/src/plugins/accessible/quick/qaccessiblequickitem.cpp b/src/plugins/accessible/quick/qaccessiblequickitem.cpp index 5cedfe62d6..10442b275e 100644 --- a/src/plugins/accessible/quick/qaccessiblequickitem.cpp +++ b/src/plugins/accessible/quick/qaccessiblequickitem.cpp @@ -58,26 +58,7 @@ int QAccessibleQuickItem::childCount() const QRect QAccessibleQuickItem::rect() const { - // ### no canvas in some cases. - // ### Should we really check for 0 opacity? - if (!item()->canvas() ||!item()->isVisible() || qFuzzyIsNull(item()->opacity())) { - return QRect(); - } - - QSizeF size = QSizeF(item()->width(), item()->height()); - // ### If the bounding rect fails, we first try the implicit size, then we go for the - // parent size. WE MIGHT HAVE TO REVISIT THESE FALLBACKS. - if (size.isEmpty()) { - size = QSizeF(item()->implicitWidth(), item()->implicitHeight()); - if (size.isEmpty()) - // ### Seems that the above fallback is not enough, fallback to use the parent size... - size = QSizeF(item()->parentItem()->width(), item()->parentItem()->height()); - } - - QRectF sceneRect = item()->mapRectToScene(QRectF(QPointF(0, 0), size)); - QPoint screenPos = item()->canvas()->mapToGlobal(sceneRect.topLeft().toPoint()); - - QRect r = QRect(screenPos, sceneRect.size().toSize()); + const QRect r = itemScreenRect(item()); if (!r.isValid()) { qWarning() << item()->metaObject()->className() << item()->property("accessibleText") << r; @@ -297,5 +278,33 @@ QVariant QAccessibleQuickItemValueInterface::minimumValue() const return item()->property("minimumValue"); } +/*! + \internal + Shared between QAccessibleQuickItem and QAccessibleQuickView +*/ +QRect itemScreenRect(QQuickItem *item) +{ + // ### no canvas in some cases. + // ### Should we really check for 0 opacity? + if (!item->canvas() ||!item->isVisible() || qFuzzyIsNull(item->opacity())) { + return QRect(); + } + + QSize itemSize((int)item->width(), (int)item->height()); + // ### If the bounding rect fails, we first try the implicit size, then we go for the + // parent size. WE MIGHT HAVE TO REVISIT THESE FALLBACKS. + if (itemSize.isEmpty()) { + itemSize = QSize((int)item->implicitWidth(), (int)item->implicitHeight()); + if (itemSize.isEmpty()) + // ### Seems that the above fallback is not enough, fallback to use the parent size... + itemSize = QSize((int)item->parentItem()->width(), (int)item->parentItem()->height()); + } + + QPointF scenePoint = item->mapToScene(QPointF(0, 0)); + QPoint screenPos = item->canvas()->mapToGlobal(scenePoint.toPoint()); + return QRect(screenPos, itemSize); +} + + QT_END_NAMESPACE diff --git a/src/plugins/accessible/quick/qaccessiblequickitem.h b/src/plugins/accessible/quick/qaccessiblequickitem.h index 5ef3a19247..75e7081dc0 100644 --- a/src/plugins/accessible/quick/qaccessiblequickitem.h +++ b/src/plugins/accessible/quick/qaccessiblequickitem.h @@ -77,6 +77,9 @@ protected: QQuickItem *item() const { return static_cast(object()); } }; +QRect itemScreenRect(QQuickItem *item); + + class QAccessibleQuickItemValueInterface: public QAccessibleQuickItem, public QAccessibleValueInterface { public: diff --git a/src/plugins/accessible/quick/qaccessiblequickview.cpp b/src/plugins/accessible/quick/qaccessiblequickview.cpp index 6a896efd29..d15e01d96c 100644 --- a/src/plugins/accessible/quick/qaccessiblequickview.cpp +++ b/src/plugins/accessible/quick/qaccessiblequickview.cpp @@ -42,6 +42,7 @@ #include "qaccessiblequickview.h" #include +#include #include "qaccessiblequickitem.h" #include "qdeclarativeaccessible.h" @@ -69,8 +70,8 @@ QAccessibleInterface *QAccessibleQuickView::parent() const QAccessibleInterface *QAccessibleQuickView::child(int index) const { if (index == 0) { - QQuickItem *declarativeRoot = view()->rootObject(); - return new QAccessibleQuickItem(declarativeRoot); + if (QQuickItem *declarativeRoot = view()->rootObject()) + return new QAccessibleQuickItem(declarativeRoot); } return 0; } @@ -108,11 +109,48 @@ QString QAccessibleQuickView::text(QAccessible::Text text) const return view()->windowTitle(); } + +/*! + \internal + + Can also return \a item itself + */ +static QQuickItem *childAt_helper(QQuickItem *item, int x, int y) +{ + if (item->opacity() == 0.0 || !item->isVisible() || !item->isEnabled()) + return 0; + + if (item->flags() & QQuickItem::ItemClipsChildrenToShape) { + if (!itemScreenRect(item).contains(x, y)) + return 0; + } + + QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item); + + QList children = itemPrivate->paintOrderChildItems(); + for (int i = children.count() - 1; i >= 0; --i) { + QQuickItem *child = children.at(i); + if (QQuickItem *childChild = childAt_helper(child, x, y)) + return childChild; + } + + QRect screenRect = itemScreenRect(item); + + if (screenRect.contains(x, y)) + return item; + + return 0; +} + QAccessibleInterface *QAccessibleQuickView::childAt(int x, int y) const { - Q_UNUSED(x); - Q_UNUSED(y); - return child(0); // return the top-level QML item + Q_ASSERT(view()); + QQuickItem *root = view()->rootItem(); + if (root) { + if (QQuickItem *item = childAt_helper(root, x, y)) + return QAccessible::queryAccessibleInterface(item); + } + return 0; } int QAccessibleQuickView::indexOfChild(const QAccessibleInterface *iface) const diff --git a/src/plugins/accessible/shared/qdeclarativeaccessible.cpp b/src/plugins/accessible/shared/qdeclarativeaccessible.cpp index 7db5f07cbe..1b35594e74 100644 --- a/src/plugins/accessible/shared/qdeclarativeaccessible.cpp +++ b/src/plugins/accessible/shared/qdeclarativeaccessible.cpp @@ -70,9 +70,8 @@ QFlags QDeclarativeAccessible::relationTo(const QAcce QAccessibleInterface *QDeclarativeAccessible::childAt(int x, int y) const { - // Look for children first. - // Start with the last child first, because children are ordered in paint order - // (which is opposite of hit test order) + // Note that this function will disregard stacking order. + // (QAccessibleQuickView::childAt() does this correctly and more efficient) // If the item clips its children, we can return early if the coordinate is outside its rect if (clipsChildren()) { -- cgit v1.2.3