| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qaccessibleobject.h" |
| 5 | |
| 6 | #if QT_CONFIG(accessibility) |
| 7 | |
| 8 | #include <QtGui/QGuiApplication> |
| 9 | #include <QtGui/QWindow> |
| 10 | |
| 11 | #include "qpointer.h" |
| 12 | #include "qmetaobject.h" |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | |
| 16 | class QAccessibleObjectPrivate |
| 17 | { |
| 18 | public: |
| 19 | QPointer<QObject> object; |
| 20 | }; |
| 21 | |
| 22 | /*! |
| 23 | \class QAccessibleObject |
| 24 | \brief The QAccessibleObject class implements parts of the |
| 25 | QAccessibleInterface for QObjects. |
| 26 | |
| 27 | \ingroup accessibility |
| 28 | \inmodule QtGui |
| 29 | |
| 30 | This class is part of \l {Accessibility for QWidget Applications}. |
| 31 | |
| 32 | This class is mainly provided for convenience. All subclasses of |
| 33 | the QAccessibleInterface that provide implementations of non-widget objects |
| 34 | should use this class as their base class. |
| 35 | |
| 36 | \sa QAccessible, QAccessibleWidget |
| 37 | */ |
| 38 | |
| 39 | /*! |
| 40 | Creates a QAccessibleObject for \a object. |
| 41 | */ |
| 42 | QAccessibleObject::QAccessibleObject(QObject *object) |
| 43 | { |
| 44 | d = new QAccessibleObjectPrivate; |
| 45 | d->object = object; |
| 46 | } |
| 47 | |
| 48 | /*! |
| 49 | Destroys the QAccessibleObject. |
| 50 | |
| 51 | This only happens when a call to release() decrements the internal |
| 52 | reference counter to zero. |
| 53 | */ |
| 54 | QAccessibleObject::~QAccessibleObject() |
| 55 | { |
| 56 | delete d; |
| 57 | } |
| 58 | |
| 59 | /*! |
| 60 | \reimp |
| 61 | */ |
| 62 | QObject *QAccessibleObject::object() const |
| 63 | { |
| 64 | return d->object; |
| 65 | } |
| 66 | |
| 67 | /*! |
| 68 | \reimp |
| 69 | */ |
| 70 | bool QAccessibleObject::isValid() const |
| 71 | { |
| 72 | return !d->object.isNull(); |
| 73 | } |
| 74 | |
| 75 | /*! \reimp */ |
| 76 | QRect QAccessibleObject::rect() const |
| 77 | { |
| 78 | return QRect(); |
| 79 | } |
| 80 | |
| 81 | /*! \reimp */ |
| 82 | void QAccessibleObject::setText(QAccessible::Text, const QString &) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | /*! \reimp */ |
| 87 | QAccessibleInterface *QAccessibleObject::childAt(int x, int y) const |
| 88 | { |
| 89 | for (int i = 0; i < childCount(); ++i) { |
| 90 | QAccessibleInterface *childIface = child(index: i); |
| 91 | Q_ASSERT(childIface); |
| 92 | if (childIface->isValid() && childIface->rect().contains(ax: x,ay: y)) |
| 93 | return childIface; |
| 94 | } |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | /*! |
| 99 | \class QAccessibleApplication |
| 100 | \brief The QAccessibleApplication class implements the QAccessibleInterface for QApplication. |
| 101 | |
| 102 | \internal |
| 103 | |
| 104 | \ingroup accessibility |
| 105 | */ |
| 106 | |
| 107 | /*! |
| 108 | Creates a QAccessibleApplication for the QApplication object referenced by qApp. |
| 109 | */ |
| 110 | QAccessibleApplication::QAccessibleApplication() |
| 111 | : QAccessibleObject(qApp) |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | QWindow *QAccessibleApplication::window() const |
| 116 | { |
| 117 | // an application can have several windows, and AFAIK we don't need |
| 118 | // to notify about changes on the application. |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
| 122 | // all toplevel windows except popups and the desktop |
| 123 | static QObjectList topLevelObjects() |
| 124 | { |
| 125 | QObjectList list; |
| 126 | const QWindowList tlw(QGuiApplication::topLevelWindows()); |
| 127 | for (int i = 0; i < tlw.size(); ++i) { |
| 128 | QWindow *w = tlw.at(i); |
| 129 | if (w->type() != Qt::Popup && w->type() != Qt::Desktop) { |
| 130 | if (QAccessibleInterface *root = w->accessibleRoot()) { |
| 131 | if (root->object()) |
| 132 | list.append(t: root->object()); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return list; |
| 138 | } |
| 139 | |
| 140 | /*! \reimp */ |
| 141 | int QAccessibleApplication::childCount() const |
| 142 | { |
| 143 | return topLevelObjects().size(); |
| 144 | } |
| 145 | |
| 146 | /*! \reimp */ |
| 147 | int QAccessibleApplication::indexOfChild(const QAccessibleInterface *child) const |
| 148 | { |
| 149 | if (!child) |
| 150 | return -1; |
| 151 | const QObjectList tlw(topLevelObjects()); |
| 152 | return tlw.indexOf(t: child->object()); |
| 153 | } |
| 154 | |
| 155 | QAccessibleInterface *QAccessibleApplication::parent() const |
| 156 | { |
| 157 | return nullptr; |
| 158 | } |
| 159 | |
| 160 | QAccessibleInterface *QAccessibleApplication::child(int index) const |
| 161 | { |
| 162 | const QObjectList tlo(topLevelObjects()); |
| 163 | if (index >= 0 && index < tlo.size()) |
| 164 | return QAccessible::queryAccessibleInterface(tlo.at(i: index)); |
| 165 | return nullptr; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /*! \reimp */ |
| 170 | QAccessibleInterface *QAccessibleApplication::focusChild() const |
| 171 | { |
| 172 | if (QWindow *window = QGuiApplication::focusWindow()) |
| 173 | return window->accessibleRoot(); |
| 174 | return nullptr; |
| 175 | } |
| 176 | |
| 177 | /*! \reimp */ |
| 178 | QString QAccessibleApplication::text(QAccessible::Text t) const |
| 179 | { |
| 180 | switch (t) { |
| 181 | case QAccessible::Name: |
| 182 | return QGuiApplication::applicationName(); |
| 183 | case QAccessible::Description: |
| 184 | return QGuiApplication::applicationFilePath(); |
| 185 | case QAccessible::Identifier: |
| 186 | return QGuiApplication::desktopFileName(); |
| 187 | default: |
| 188 | break; |
| 189 | } |
| 190 | return QString(); |
| 191 | } |
| 192 | |
| 193 | /*! \reimp */ |
| 194 | QAccessible::Role QAccessibleApplication::role() const |
| 195 | { |
| 196 | return QAccessible::Application; |
| 197 | } |
| 198 | |
| 199 | /*! \reimp */ |
| 200 | QAccessible::State QAccessibleApplication::state() const |
| 201 | { |
| 202 | return QAccessible::State(); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | QT_END_NAMESPACE |
| 207 | |
| 208 | #endif // QT_CONFIG(accessibility) |
| 209 |
