1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qaccessibleobject.h"
41
42#ifndef QT_NO_ACCESSIBILITY
43
44#include <QtGui/QGuiApplication>
45#include <QtGui/QWindow>
46
47#include "qpointer.h"
48#include "qmetaobject.h"
49
50QT_BEGIN_NAMESPACE
51
52class QAccessibleObjectPrivate
53{
54public:
55 QPointer<QObject> object;
56};
57
58/*!
59 \class QAccessibleObject
60 \brief The QAccessibleObject class implements parts of the
61 QAccessibleInterface for QObjects.
62
63 \ingroup accessibility
64 \inmodule QtGui
65
66 This class is part of \l {Accessibility for QWidget Applications}.
67
68 This class is mainly provided for convenience. All subclasses of
69 the QAccessibleInterface that provide implementations of non-widget objects
70 should use this class as their base class.
71
72 \sa QAccessible, QAccessibleWidget
73*/
74
75/*!
76 Creates a QAccessibleObject for \a object.
77*/
78QAccessibleObject::QAccessibleObject(QObject *object)
79{
80 d = new QAccessibleObjectPrivate;
81 d->object = object;
82}
83
84/*!
85 Destroys the QAccessibleObject.
86
87 This only happens when a call to release() decrements the internal
88 reference counter to zero.
89*/
90QAccessibleObject::~QAccessibleObject()
91{
92 delete d;
93}
94
95/*!
96 \reimp
97*/
98QObject *QAccessibleObject::object() const
99{
100 return d->object;
101}
102
103/*!
104 \reimp
105*/
106bool QAccessibleObject::isValid() const
107{
108 return !d->object.isNull();
109}
110
111/*! \reimp */
112QRect QAccessibleObject::rect() const
113{
114 return QRect();
115}
116
117/*! \reimp */
118void QAccessibleObject::setText(QAccessible::Text, const QString &)
119{
120}
121
122/*! \reimp */
123QAccessibleInterface *QAccessibleObject::childAt(int x, int y) const
124{
125 for (int i = 0; i < childCount(); ++i) {
126 QAccessibleInterface *childIface = child(index: i);
127 Q_ASSERT(childIface);
128 if (childIface->isValid() && childIface->rect().contains(ax: x,ay: y))
129 return childIface;
130 }
131 return nullptr;
132}
133
134/*!
135 \class QAccessibleApplication
136 \brief The QAccessibleApplication class implements the QAccessibleInterface for QApplication.
137
138 \internal
139
140 \ingroup accessibility
141*/
142
143/*!
144 Creates a QAccessibleApplication for the QApplication object referenced by qApp.
145*/
146QAccessibleApplication::QAccessibleApplication()
147: QAccessibleObject(qApp)
148{
149}
150
151QWindow *QAccessibleApplication::window() const
152{
153 // an application can have several windows, and AFAIK we don't need
154 // to notify about changes on the application.
155 return nullptr;
156}
157
158// all toplevel windows except popups and the desktop
159static QObjectList topLevelObjects()
160{
161 QObjectList list;
162 const QWindowList tlw(QGuiApplication::topLevelWindows());
163 for (int i = 0; i < tlw.count(); ++i) {
164 QWindow *w = tlw.at(i);
165 if (w->type() != Qt::Popup && w->type() != Qt::Desktop) {
166 if (QAccessibleInterface *root = w->accessibleRoot()) {
167 if (root->object())
168 list.append(t: root->object());
169 }
170 }
171 }
172
173 return list;
174}
175
176/*! \reimp */
177int QAccessibleApplication::childCount() const
178{
179 return topLevelObjects().count();
180}
181
182/*! \reimp */
183int QAccessibleApplication::indexOfChild(const QAccessibleInterface *child) const
184{
185 if (!child)
186 return -1;
187 const QObjectList tlw(topLevelObjects());
188 return tlw.indexOf(t: child->object());
189}
190
191QAccessibleInterface *QAccessibleApplication::parent() const
192{
193 return nullptr;
194}
195
196QAccessibleInterface *QAccessibleApplication::child(int index) const
197{
198 const QObjectList tlo(topLevelObjects());
199 if (index >= 0 && index < tlo.count())
200 return QAccessible::queryAccessibleInterface(tlo.at(i: index));
201 return nullptr;
202}
203
204
205/*! \reimp */
206QAccessibleInterface *QAccessibleApplication::focusChild() const
207{
208 if (QWindow *window = QGuiApplication::focusWindow())
209 return window->accessibleRoot();
210 return nullptr;
211}
212
213/*! \reimp */
214QString QAccessibleApplication::text(QAccessible::Text t) const
215{
216 switch (t) {
217 case QAccessible::Name:
218 return QGuiApplication::applicationName();
219 case QAccessible::Description:
220 return QGuiApplication::applicationFilePath();
221 default:
222 break;
223 }
224 return QString();
225}
226
227/*! \reimp */
228QAccessible::Role QAccessibleApplication::role() const
229{
230 return QAccessible::Application;
231}
232
233/*! \reimp */
234QAccessible::State QAccessibleApplication::state() const
235{
236 return QAccessible::State();
237}
238
239
240QT_END_NAMESPACE
241
242#endif //QT_NO_ACCESSIBILITY
243

source code of qtbase/src/gui/accessible/qaccessibleobject.cpp