5d958f01a9319331e2d66c139970b441d9519a3b
[profile/ivi/qtbase.git] / src / gui / accessible / qaccessibleobject.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qaccessibleobject.h"
43
44 #ifndef QT_NO_ACCESSIBILITY
45
46 #include <QtGui/QGuiApplication>
47 #include <QtGui/QWindow>
48
49 #include "qpointer.h"
50 #include "qmetaobject.h"
51
52 QT_BEGIN_NAMESPACE
53
54 class QAccessibleObjectPrivate
55 {
56 public:
57     QPointer<QObject> object;
58
59     QList<QByteArray> actionList() const;
60 };
61
62 QList<QByteArray> QAccessibleObjectPrivate::actionList() const
63 {
64     QList<QByteArray> actionList;
65
66     if (!object)
67         return actionList;
68
69     const QMetaObject *mo = object->metaObject();
70     Q_ASSERT(mo);
71
72     QByteArray defaultAction = QMetaObject::normalizedSignature(
73         mo->classInfo(mo->indexOfClassInfo("DefaultSlot")).value());
74
75     for (int i = 0; i < mo->methodCount(); ++i) {
76         const QMetaMethod member = mo->method(i);
77         if (member.methodType() != QMetaMethod::Slot && member.access() != QMetaMethod::Public)
78             continue;
79
80         if (!qstrcmp(member.tag(), "QACCESSIBLE_SLOT")) {
81             if (member.methodSignature() == defaultAction)
82                 actionList.prepend(defaultAction);
83             else
84                 actionList << member.methodSignature();
85         }
86     }
87
88     return actionList;
89 }
90
91 /*!
92     \class QAccessibleObject
93     \brief The QAccessibleObject class implements parts of the
94     QAccessibleInterface for QObjects.
95     \internal
96
97     \ingroup accessibility
98     \inmodule QtWidgets
99
100     This class is part of \l {Accessibility for QWidget Applications}.
101
102     This class is mainly provided for convenience. All subclasses of
103     the QAccessibleInterface that provide implementations of non-widget objects
104     should use this class as their base class.
105
106     \sa QAccessible, QAccessibleWidget
107 */
108
109 /*!
110     Creates a QAccessibleObject for \a object.
111 */
112 QAccessibleObject::QAccessibleObject(QObject *object)
113 {
114     d = new QAccessibleObjectPrivate;
115     d->object = object;
116 }
117
118 /*!
119     Destroys the QAccessibleObject.
120
121     This only happens when a call to release() decrements the internal
122     reference counter to zero.
123 */
124 QAccessibleObject::~QAccessibleObject()
125 {
126     delete d;
127 }
128
129 /*!
130     \reimp
131 */
132 QObject *QAccessibleObject::object() const
133 {
134 #ifndef QT_NO_DEBUG
135     if (!d->object)
136         qWarning("QAccessibleInterface is invalid. Crash pending...");
137 #endif
138     return d->object;
139 }
140
141 /*!
142     \reimp
143 */
144 bool QAccessibleObject::isValid() const
145 {
146     return !d->object.isNull();
147 }
148
149 /*! \reimp */
150 QRect QAccessibleObject::rect() const
151 {
152     return QRect();
153 }
154
155 /*! \reimp */
156 void QAccessibleObject::setText(QAccessible::Text, const QString &)
157 {
158 }
159
160 /*! \reimp */
161 QAccessibleInterface *QAccessibleObject::childAt(int x, int y) const
162 {
163     for (int i = 0; i < childCount(); ++i) {
164         QAccessibleInterface *childIface = child(i);
165         Q_ASSERT(childIface);
166         if (childIface->rect().contains(x,y)) {
167             return childIface;
168         }
169     }
170     return 0;
171 }
172
173 /*!
174     \class QAccessibleApplication
175     \brief The QAccessibleApplication class implements the QAccessibleInterface for QApplication.
176
177     \internal
178
179     \ingroup accessibility
180 */
181
182 /*!
183     Creates a QAccessibleApplication for the QApplication object referenced by qApp.
184 */
185 QAccessibleApplication::QAccessibleApplication()
186 : QAccessibleObject(qApp)
187 {
188 }
189
190 QWindow *QAccessibleApplication::window() const
191 {
192     // an application can have several windows, and AFAIK we don't need
193     // to notify about changes on the application.
194     return 0;
195 }
196
197 // all toplevel windows except popups and the desktop
198 static QObjectList topLevelObjects()
199 {
200     QObjectList list;
201     const QWindowList tlw(QGuiApplication::topLevelWindows());
202     for (int i = 0; i < tlw.count(); ++i) {
203         QWindow *w = tlw.at(i);
204         if (w->windowType() != Qt::Popup && w->windowType() != Qt::Desktop) {
205             if (QAccessibleInterface *root = w->accessibleRoot()) {
206                 if (root->object())
207                     list.append(root->object());
208                 delete root;
209             }
210         }
211     }
212
213     return list;
214 }
215
216 /*! \reimp */
217 int QAccessibleApplication::childCount() const
218 {
219     return topLevelObjects().count();
220 }
221
222 /*! \reimp */
223 int QAccessibleApplication::indexOfChild(const QAccessibleInterface *child) const
224 {
225     const QObjectList tlw(topLevelObjects());
226     return tlw.indexOf(child->object());
227 }
228
229 QAccessibleInterface *QAccessibleApplication::parent() const
230 {
231     return 0;
232 }
233
234 QAccessibleInterface *QAccessibleApplication::child(int index) const
235 {
236     const QObjectList tlo(topLevelObjects());
237     if (index >= 0 && index < tlo.count())
238         return QAccessible::queryAccessibleInterface(tlo.at(index));
239     return 0;
240 }
241
242
243 /*! \reimp */
244 QAccessibleInterface *QAccessibleApplication::focusChild() const
245 {
246     if (QWindow *window = QGuiApplication::focusWindow())
247         return window->accessibleRoot();
248     return 0;
249 }
250
251 /*! \reimp */
252 QString QAccessibleApplication::text(QAccessible::Text t) const
253 {
254     switch (t) {
255     case QAccessible::Name:
256         return QGuiApplication::applicationName();
257     case QAccessible::Description:
258         return QGuiApplication::applicationFilePath();
259     default:
260         break;
261     }
262     return QString();
263 }
264
265 /*! \reimp */
266 QAccessible::Role QAccessibleApplication::role() const
267 {
268     return QAccessible::Application;
269 }
270
271 /*! \reimp */
272 QAccessible::State QAccessibleApplication::state() const
273 {
274     return QAccessible::State();
275 }
276
277
278 QT_END_NAMESPACE
279
280 #endif //QT_NO_ACCESSIBILITY