1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qaccessibledeclarativeitem.h"
44 #include <QtQuick1/qdeclarativeitem.h>
45 #include <QtQuick1/private/qdeclarativeaccessibleattached_p.h>
49 QAccessibleDeclarativeItem::QAccessibleDeclarativeItem(QGraphicsObject *item, QGraphicsView *view)
50 :QDeclarativeAccessible(item)
57 int QAccessibleDeclarativeItem::childCount() const
59 QList<QGraphicsItem *> children = m_item->childItems();
60 return children.count();
63 QRect QAccessibleDeclarativeItem::rect() const
65 QRectF sceneRect = m_item->sceneTransform().mapRect(m_item->boundingRect());
66 QPoint pos = m_view->mapFromScene(m_view->mapToGlobal(sceneRect.topLeft().toPoint()));
67 QSize size = sceneRect.size().toSize();
68 return QRect(pos, size);
71 QRect QAccessibleDeclarativeItem::viewRect() const
73 QPoint screenPos = m_view->mapToGlobal(m_view->pos());
74 return QRect(screenPos, m_view->size());
77 bool QAccessibleDeclarativeItem::clipsChildren() const
79 return static_cast<QDeclarativeItem *>(m_item)->clip();
82 static inline bool isAncestor(const QObject *ancestorCandidate, const QObject *child)
85 if (child == ancestorCandidate)
87 child = child->parent();
93 QAccessibleInterface *QAccessibleDeclarativeItem::parent() const
95 QGraphicsItem *parent = m_item->parentItem();
96 QGraphicsObject *parentObj = parent ? parent->toGraphicsObject() : 0;
97 if (parent && !parentObj)
98 qWarning("Can not make QGraphicsItems accessible");
99 QAccessibleInterface *ancestor = (parentObj
100 ? new QAccessibleDeclarativeItem(parentObj, m_view)
101 : QAccessible::queryAccessibleInterface(m_view));
105 QAccessibleInterface *QAccessibleDeclarativeItem::child(int index) const
107 QList<QGraphicsItem *> children = m_item->childItems();
109 if (index >= children.count())
112 QGraphicsItem *child = children.at(index);
113 QGraphicsObject *childObject = qobject_cast<QGraphicsObject *>(child);
117 return new QAccessibleDeclarativeItem(childObject, m_view);
120 int QAccessibleDeclarativeItem::navigate(QAccessible::RelationFlag rel, int entry, QAccessibleInterface **target) const
122 //qDebug() << "QAccessibleDeclarativeItem navigate" << rel << entry;
123 Q_ASSERT(entry >= 0);
127 *target = new QAccessibleDeclarativeItem(m_item->toGraphicsObject(), m_view);
132 case QAccessible::FocusChild: {
133 QGraphicsObject *focusObject = 0;
134 if (m_item->hasFocus()) {
135 focusObject = m_item->toGraphicsObject();
137 if (QGraphicsItem *focusItem = m_view->scene()->focusItem()) {
138 if (m_item->isAncestorOf(focusItem)) {
139 focusObject = focusItem->toGraphicsObject();
143 //qDebug() << "QAccessibleDeclarativeItem navigate QAccessible::FocusChild" << rel << entry;
145 *target = new QAccessibleDeclarativeItem(focusObject, m_view);
155 int QAccessibleDeclarativeItem::indexOfChild(const QAccessibleInterface *iface) const
157 // ### No QAccessibleInterfaces are created with a QGraphicsItem.
158 // However, we want to support QML, not QGraphicsView in general.
159 // And since the UI is written in QML, this means we can assume that *all*
160 // QGraphicsItems are actually QGraphicsObjects
162 const QGraphicsObject *childObj = static_cast<QGraphicsObject*>(iface->object());
163 if (m_item == childObj)
166 QList<QGraphicsItem*> kids = m_item->childItems();
167 int index = kids.indexOf(const_cast<QGraphicsItem*>(static_cast<const QGraphicsItem*>(childObj)));
174 QAccessible::State QAccessibleDeclarativeItem::state() const
176 QAccessible::State state;
177 state.focused = m_item->hasFocus();
181 QAccessible::Role QAccessibleDeclarativeItem::role() const
183 // ### Workaround for setAccessibleRole() not working.
184 // Text items are special since they are defined
185 // entirely from C++ (setting the role from QML works.)
186 // if (qobject_cast<QDeclarative1Text*>(m_item))
187 // return QAccessible::StaticText;
189 QVariant v = QDeclarativeAccessibleAttached::property(m_item, "role");
191 QAccessible::Role role = (QAccessible::Role)v.toInt(&ok);
192 if (!ok) // Not sure if this check is needed.
193 role = QAccessible::Pane;
197 bool QAccessibleDeclarativeItem::isAccessible() const
202 QString QAccessibleDeclarativeItem::text(QAccessible::Text textType) const
204 // handles generic behaviour not specific to an item
206 case QAccessible::Name: {
207 QVariant accessibleName = QDeclarativeAccessibleAttached::property(object(), "name");
208 if (!accessibleName.isNull())
209 return accessibleName.toString();
211 case QAccessible::Description: {
212 QVariant accessibleDecription = QDeclarativeAccessibleAttached::property(object(), "description");
213 if (!accessibleDecription.isNull())
214 return accessibleDecription.toString();
216 case QAccessible::Value:
217 case QAccessible::Help:
218 case QAccessible::Accelerator:
223 // the following blocks handles item-specific behaviour
224 if (role() == QAccessible::EditableText) {
225 if (textType == QAccessible::Value) {
226 QVariant text = object()->property("text");
227 return text.toString();
228 } else if (textType == QAccessible::Name) {
229 return object()->objectName();
232 if (textType == QAccessible::Name) {
233 QVariant text = object()->property("text");
234 return text.toString();