Qt 4.8
qaccessibleobject.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 "qapplication.h"
47 #include "qwidget.h"
48 #include "qpointer.h"
49 #include "qmetaobject.h"
50 #include "qvarlengtharray.h"
51 
53 
55 {
56 public:
58 
60 };
61 
63 {
65 
66  if (!object)
67  return actionList;
68 
69  const QMetaObject *mo = object->metaObject();
70  Q_ASSERT(mo);
71 
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.signature() == defaultAction)
82  actionList.prepend(defaultAction);
83  else
84  actionList << member.signature();
85  }
86  }
87 
88  return actionList;
89 }
90 
112 {
114  d->object = object;
115 }
116 
124 {
125  delete d;
126 }
127 
132 {
133 #ifndef QT_NO_DEBUG
134  if (!d->object)
135  qWarning("QAccessibleInterface is invalid. Crash pending...");
136 #endif
137  return d->object;
138 }
139 
144 {
145  return !d->object.isNull();
146 }
147 
150 {
151  return QRect();
152 }
153 
156 {
157 }
158 
161 {
162  return 0;
163 }
164 
167 {
168  return false;
169 }
170 
171 static const char * const action_text[][5] =
172 {
173  // Name, Description, Value, Help, Accelerator
174  { "Press", "", "", "", "Space" },
175  { "SetFocus", "Passes focus to this widget", "", "", "" },
176  { "Increase", "", "", "", "" },
177  { "Decrease", "", "", "", "" },
178  { "Accept", "", "", "", "" },
179  { "Cancel", "", "", "", "" },
180  { "Select", "", "", "", "" },
181  { "ClearSelection", "", "", "", "" },
182  { "RemoveSelection", "", "", "", "" },
183  { "ExtendSelection", "", "", "", "" },
184  { "AddToSelection", "", "", "", "" }
185 };
186 
188 QString QAccessibleObject::actionText(int action, Text t, int child) const
189 {
190  if (child || action > FirstStandardAction || action < LastStandardAction || t > Accelerator)
191  return QString();
192 
193  return QString::fromLatin1(action_text[-(action - FirstStandardAction)][t]);
194 }
195 
196 
214 {
215 }
216 
217 // all toplevel widgets except popups and the desktop
219 {
220  QWidgetList list;
222  for (int i = 0; i < tlw.count(); ++i) {
223  QWidget *w = tlw.at(i);
224  if (!(w->windowType() == Qt::Popup) && !(w->windowType() == Qt::Desktop))
225  list.append(w);
226  }
227 
228  return list;
229 }
230 
233 {
234  return topLevelWidgets().count();
235 }
236 
239 {
240  if (!child->object()->isWidgetType())
241  return -1;
242 
243  const QWidgetList tlw(topLevelWidgets());
244  int index = tlw.indexOf(static_cast<QWidget*>(child->object()));
245  if (index != -1)
246  ++index;
247  return index;
248 }
249 
251 int QAccessibleApplication::childAt(int x, int y) const
252 {
253  const QWidgetList tlw(topLevelWidgets());
254  for (int i = 0; i < tlw.count(); ++i) {
255  QWidget *w = tlw.at(i);
256  if (w->frameGeometry().contains(x,y))
257  return i+1;
258  }
259  return -1;
260 }
261 
263 QAccessible::Relation QAccessibleApplication::relationTo(int child, const
264  QAccessibleInterface *other, int otherChild) const
265 {
266  QObject *o = other ? other->object() : 0;
267  if (!o)
268  return Unrelated;
269 
270  if(o == object()) {
271  if (child && !otherChild)
272  return Child;
273  if (!child && otherChild)
274  return Ancestor;
275  if (!child && !otherChild)
276  return Self;
277  }
278 
280  if (tlw.contains(qobject_cast<QWidget*>(o)))
281  return Ancestor;
282 
283  for (int i = 0; i < tlw.count(); ++i) {
284  QWidget *w = tlw.at(i);
285  QObjectList cl = w->findChildren<QObject *>(QString());
286  if (cl.contains(o))
287  return Ancestor;
288  }
289 
290  return Unrelated;
291 }
292 
295  QAccessibleInterface **target) const
296 {
297  if (!target)
298  return -1;
299 
300  *target = 0;
301  QObject *targetObject = 0;
302 
303  switch (relation) {
304  case Self:
305  targetObject = object();
306  break;
307  case Child:
308  if (entry > 0 && entry <= childCount()) {
309  const QWidgetList tlw(topLevelWidgets());
310  if (tlw.count() >= entry)
311  targetObject = tlw.at(entry-1);
312  } else {
313  return -1;
314  }
315  break;
316  case FocusChild:
317  targetObject = QApplication::activeWindow();
318  break;
319  default:
320  break;
321  }
322  *target = QAccessible::queryAccessibleInterface(targetObject);
323  return *target ? 0 : -1;
324 }
325 
328 {
329  switch (t) {
330  case Name:
332  case Description:
334  default:
335  break;
336  }
337  return QString();
338 }
339 
342 {
343  return Application;
344 }
345 
348 {
350 }
351 
354 {
355  return 1;
356 }
357 
359 bool QAccessibleApplication::doAction(int action, int child, const QVariantList &param)
360 {
361  if (action == 0 || action == 1) {
362  QWidget *w = 0;
364  if (!w)
365  w = topLevelWidgets().at(0);
366  if (!w)
367  return false;
368  w->activateWindow();
369  return true;
370  }
371  return QAccessibleObject::doAction(action, child, param);
372 }
373 
376 {
377  QString str;
378  if ((action == 0 || action == 1) && !child) switch (text) {
379  case Name:
380  return QApplication::tr("Activate");
381  case Description:
382  return QApplication::tr("Activates the program's main window");
383  default:
384  break;
385  }
386  return QAccessibleObject::actionText(action, text, child);
387 }
388 
389 // ### Qt 5: remove me - binary compatibility hack
391 {
393  d->object = object;
394 }
396 { return reinterpret_cast<const QAccessibleObject *>(this)->QAccessibleObject::isValid(); }
398 { return reinterpret_cast<const QAccessibleObject *>(this)->QAccessibleObject::object(); }
400 { return reinterpret_cast<const QAccessibleObject *>(this)->QAccessibleObject::rect(child); }
401 void QAccessibleObjectEx::setText(Text t, int child, const QString &text)
402 { reinterpret_cast<QAccessibleObject *>(this)->QAccessibleObject::setText(t, child, text); }
404 { return reinterpret_cast<const QAccessibleObject *>(this)->QAccessibleObject::userActionCount(child); }
405 bool QAccessibleObjectEx::doAction(int action, int child, const QVariantList &params)
406 { return reinterpret_cast<QAccessibleObject *>(this)->QAccessibleObject::doAction(action, child, params); }
407 QString QAccessibleObjectEx::actionText(int action, Text t, int child) const
408 { return reinterpret_cast<const QAccessibleObject *>(this)->QAccessibleObject::actionText(action, t, child); }
410 { delete d; }
411 
413 
414 #endif //QT_NO_ACCESSIBILITY
double d
Definition: qnumeric_p.h:62
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
static QWidgetList topLevelWidgets()
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QByteArray normalizedSignature(const char *method)
Normalizes the signature of the given method.
int childCount() const
Reimplemented Function
Relation relationTo(int, const QAccessibleInterface *, int) const
Reimplemented Function
static QString applicationFilePath()
Returns the file path of the application executable.
QString text(Text t, int child) const
Reimplemented Function
QObject * object() const
Returns a pointer to the QObject this interface implementation provides information for...
void setText(Text t, int child, const QString &text)
Reimplemented Function
QMetaClassInfo classInfo(int index) const
Returns the meta-data for the item of class information with the given index.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Role
This enum defines the role of an accessible object.
Definition: qaccessible.h:188
static QWidget * activeWindow()
Returns the application top-level window that has the keyboard input focus, or 0 if no application wi...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QRect frameGeometry
geometry of the widget relative to its parent including any window frame
Definition: qwidget.h:159
int userActionCount(int child) const
Returns the number of custom actions of the object, or of the object&#39;s child if child is not 0...
QAccessibleApplication()
Creates a QAccessibleApplication for the QApplication object referenced by qApp.
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
QList< QByteArray > actionList() const
virtual QObject * object() const =0
Returns a pointer to the QObject this interface implementation provides information for...
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
static const char *const action_text[][5]
bool doAction(int action, int child, const QVariantList &params)
Reimplemented Function
QString actionText(int action, Text t, int child) const
Reimplemented Function
void setText(Text t, int child, const QString &text)
Sets the text property t of the object, or of the object&#39;s child if child is not 0, to text.
Role role(int child) const
Reimplemented Function
int navigate(RelationFlag, int, QAccessibleInterface **) const
Reimplemented Function
QAccessibleObject(QObject *object)
Creates a QAccessibleObject for object.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int childAt(int x, int y) const
Reimplemented Function
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
int indexOfChild(const QAccessibleInterface *) const
Reimplemented Function
#define qApp
RelationFlag
This enum type defines bit flags that can be combined to indicate the relationship between two access...
Definition: qaccessible.h:268
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
Q_CORE_EXPORT void qWarning(const char *,...)
bool doAction(int action, int child, const QVariantList &params)
Reimplemented Function
int userActionCount(int child) const
Reimplemented Function
static QWidgetList topLevelWidgets()
Returns a list of the top-level widgets (windows) in the application.
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
QPointer< QObject > object
bool doAction(int action, int child, const QVariantList &params)
Asks the object, or the object&#39;s child if child is not 0, to execute action using the parameters...
static QAccessibleInterface * queryAccessibleInterface(QObject *)
If a QAccessibleInterface implementation exists for the given object, this function returns a pointer...
QString actionText(int action, Text t, int child) const
Reimplemented Function
MethodType methodType() const
Returns the type of this method (signal, slot, or method).
QAccessibleObjectPrivate * d
bool isWidgetType() const
Returns true if the object is a widget; otherwise returns false.
Definition: qobject.h:146
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the list, searching forward from index...
Definition: qlist.h:847
QRect rect(int child) const
Returns the geometry of the object, or of the object&#39;s child if child is not 0.
State
Definition: qaudio.h:59
The QAccessibleInterface class defines an interface that exposes information about accessible objects...
Definition: qaccessible.h:370
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
const char * tag() const
Returns the tag associated with this method.
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
QAccessibleObjectEx(QObject *object)
The QAccessibleObject class implements parts of the QAccessibleInterface for QObjects.
int userActionCount(int child) const
Reimplemented Function
QObject * object() const
Reimplemented Function
void activateWindow()
Sets the top-level widget containing this widget to be the active window.
quint16 index
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
Qt::WindowType windowType() const
Returns the window type of this widget.
Definition: qwidget.h:937
int qstrcmp(const QByteArray &str1, const char *str2)
Definition: qbytearray.cpp:336
bool isValid() const
Returns true if all the data necessary to use this interface implementation is valid (e...
const char * signature() const
Returns the signature of this method (e.g., setValue(double)).
Access access() const
Returns the access specification of this method (private, protected, or public).
static QString applicationName()
virtual ~QAccessibleObject()
Destroys the QAccessibleObject.
bool isValid() const
Reimplemented Function
State state(int child) const
Reimplemented Function
int indexOfClassInfo(const char *name) const
Finds class information item name and returns its index; otherwise returns -1.
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
QRect rect(int child) const
Reimplemented Function
QList< T > findChildren(const QString &aName=QString()) const
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
Definition: qobject.h:162
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
QString actionText(int action, Text t, int child) const
Returns the text property t of the action action supported by the object, or of the object&#39;s child if...
The Text item allows you to add formatted text to a scene.