| 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 | #ifndef QQUICKACCESSIBLEATTACHED_H |
| 5 | #define QQUICKACCESSIBLEATTACHED_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtQuick/qquickitem.h> |
| 19 | |
| 20 | #include <QtCore/qobject.h> |
| 21 | #include <QtCore/qstring.h> |
| 22 | |
| 23 | #if QT_CONFIG(accessibility) |
| 24 | |
| 25 | #include <QtGui/qaccessible.h> |
| 26 | #include <private/qtquickglobal_p.h> |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | |
| 31 | #define STATE_PROPERTY(P) \ |
| 32 | Q_PROPERTY(bool P READ P WRITE set_ ## P NOTIFY P ## Changed FINAL) \ |
| 33 | bool P() const { return m_proxying && !m_stateExplicitlySet.P ? m_proxying->P() : m_state.P ; } \ |
| 34 | void set_ ## P(bool arg) \ |
| 35 | { \ |
| 36 | if (m_proxying) \ |
| 37 | m_proxying->set_##P(arg);\ |
| 38 | m_stateExplicitlySet.P = true; \ |
| 39 | if (m_state.P == arg) \ |
| 40 | return; \ |
| 41 | m_state.P = arg; \ |
| 42 | Q_EMIT P ## Changed(arg); \ |
| 43 | QAccessible::State changedState; \ |
| 44 | changedState.P = true; \ |
| 45 | QAccessibleStateChangeEvent ev(parent(), changedState); \ |
| 46 | QAccessible::updateAccessibility(&ev); \ |
| 47 | } \ |
| 48 | Q_SIGNAL void P ## Changed(bool arg); |
| 49 | |
| 50 | class Q_QUICK_EXPORT QQuickAccessibleAttached : public QObject |
| 51 | { |
| 52 | Q_OBJECT |
| 53 | Q_PROPERTY(QAccessible::Role role READ role WRITE setRole NOTIFY roleChanged FINAL) |
| 54 | Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) |
| 55 | Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged FINAL) |
| 56 | Q_PROPERTY(QString id READ id WRITE setId NOTIFY idChanged FINAL) |
| 57 | Q_PROPERTY(bool ignored READ ignored WRITE setIgnored NOTIFY ignoredChanged FINAL) |
| 58 | |
| 59 | QML_NAMED_ELEMENT(Accessible) |
| 60 | QML_ADDED_IN_VERSION(2, 0) |
| 61 | QML_UNCREATABLE("Accessible is only available via attached properties." ) |
| 62 | QML_ATTACHED(QQuickAccessibleAttached) |
| 63 | QML_EXTENDED_NAMESPACE(QAccessible) |
| 64 | |
| 65 | public: |
| 66 | STATE_PROPERTY(checkable) |
| 67 | STATE_PROPERTY(checked) |
| 68 | STATE_PROPERTY(editable) |
| 69 | STATE_PROPERTY(focusable) |
| 70 | STATE_PROPERTY(focused) |
| 71 | STATE_PROPERTY(multiLine) |
| 72 | STATE_PROPERTY(readOnly) |
| 73 | STATE_PROPERTY(selected) |
| 74 | STATE_PROPERTY(selectable) |
| 75 | STATE_PROPERTY(pressed) |
| 76 | STATE_PROPERTY(checkStateMixed) |
| 77 | STATE_PROPERTY(defaultButton) |
| 78 | STATE_PROPERTY(passwordEdit) |
| 79 | STATE_PROPERTY(selectableText) |
| 80 | STATE_PROPERTY(searchEdit) |
| 81 | |
| 82 | QQuickAccessibleAttached(QObject *parent); |
| 83 | ~QQuickAccessibleAttached(); |
| 84 | |
| 85 | QAccessible::Role role() const { return m_role; } |
| 86 | void setRole(QAccessible::Role role); |
| 87 | QString name() const { |
| 88 | if (m_state.passwordEdit) |
| 89 | return QString(); |
| 90 | if (!m_nameExplicitlySet && m_proxying && m_proxying->wasNameExplicitlySet()) |
| 91 | return m_proxying->name(); |
| 92 | return m_name; |
| 93 | } |
| 94 | |
| 95 | bool wasNameExplicitlySet() const; |
| 96 | void setName(const QString &name) { |
| 97 | m_nameExplicitlySet = true; |
| 98 | if (name != m_name) { |
| 99 | m_name = name; |
| 100 | Q_EMIT nameChanged(); |
| 101 | QAccessibleEvent ev(parent(), QAccessible::NameChanged); |
| 102 | QAccessible::updateAccessibility(event: &ev); |
| 103 | } |
| 104 | } |
| 105 | void setNameImplicitly(const QString &name); |
| 106 | |
| 107 | QString description() const { |
| 108 | return !m_descriptionExplicitlySet && m_proxying ? m_proxying->description() : m_description; |
| 109 | } |
| 110 | void setDescription(const QString &description) |
| 111 | { |
| 112 | if (!m_descriptionExplicitlySet && m_proxying) { |
| 113 | disconnect(sender: m_proxying, signal: &QQuickAccessibleAttached::descriptionChanged, receiver: this, slot: &QQuickAccessibleAttached::descriptionChanged); |
| 114 | } |
| 115 | m_descriptionExplicitlySet = true; |
| 116 | if (m_description != description) { |
| 117 | m_description = description; |
| 118 | Q_EMIT descriptionChanged(); |
| 119 | QAccessibleEvent ev(parent(), QAccessible::DescriptionChanged); |
| 120 | QAccessible::updateAccessibility(event: &ev); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | QString id() const { return m_id; } |
| 125 | void setId(const QString &id) |
| 126 | { |
| 127 | if (m_id != id) { |
| 128 | m_id = id; |
| 129 | Q_EMIT idChanged(); |
| 130 | QAccessibleEvent ev(parent(), QAccessible::IdentifierChanged); |
| 131 | QAccessible::updateAccessibility(event: &ev); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Factory function |
| 136 | static QQuickAccessibleAttached *qmlAttachedProperties(QObject *obj); |
| 137 | |
| 138 | static QQuickAccessibleAttached *attachedProperties(const QObject *obj) |
| 139 | { |
| 140 | return qobject_cast<QQuickAccessibleAttached*>(object: qmlAttachedPropertiesObject<QQuickAccessibleAttached>(obj, create: false)); |
| 141 | } |
| 142 | |
| 143 | // Property getter |
| 144 | static QVariant property(const QObject *object, const char *propertyName) |
| 145 | { |
| 146 | if (QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(obj: object)) |
| 147 | return attachedObject->property(name: propertyName); |
| 148 | return QVariant(); |
| 149 | } |
| 150 | |
| 151 | static bool setProperty(QObject *object, const char *propertyName, const QVariant &value) |
| 152 | { |
| 153 | QObject *obj = qmlAttachedPropertiesObject<QQuickAccessibleAttached>(obj: object, create: true); |
| 154 | if (!obj) { |
| 155 | qWarning(msg: "cannot set property Accessible.%s of QObject %s" , propertyName, object->metaObject()->className()); |
| 156 | return false; |
| 157 | } |
| 158 | return obj->setProperty(name: propertyName, value); |
| 159 | } |
| 160 | |
| 161 | static QObject *findAccessible(QObject *object, QAccessible::Role role = QAccessible::NoRole) |
| 162 | { |
| 163 | while (object) { |
| 164 | QQuickAccessibleAttached *att = QQuickAccessibleAttached::attachedProperties(obj: object); |
| 165 | if (att && (role == QAccessible::NoRole || att->role() == role)) { |
| 166 | break; |
| 167 | } |
| 168 | if (auto action = object->property(name: "action" ).value<QObject *>(); action) { |
| 169 | QQuickAccessibleAttached *att = QQuickAccessibleAttached::attachedProperties(obj: action); |
| 170 | if (att && (role == QAccessible::NoRole || att->role() == role)) { |
| 171 | object = action; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | object = object->parent(); |
| 176 | } |
| 177 | return object; |
| 178 | } |
| 179 | |
| 180 | QAccessible::State state() const { return m_state; } |
| 181 | bool ignored() const; |
| 182 | bool doAction(const QString &actionName); |
| 183 | void availableActions(QStringList *actions) const; |
| 184 | |
| 185 | Q_REVISION(6, 2) Q_INVOKABLE static QString stripHtml(const QString &html); |
| 186 | void setProxying(QQuickAccessibleAttached *proxying); |
| 187 | |
| 188 | Q_REVISION(6, 8) Q_INVOKABLE void announce(const QString &message, QAccessible::AnnouncementPoliteness politeness = QAccessible::AnnouncementPoliteness::Polite); |
| 189 | |
| 190 | public Q_SLOTS: |
| 191 | void valueChanged() { |
| 192 | QAccessibleValueChangeEvent ev(parent(), parent()->property(name: "value" )); |
| 193 | QAccessible::updateAccessibility(event: &ev); |
| 194 | } |
| 195 | void cursorPositionChanged() { |
| 196 | QAccessibleTextCursorEvent ev(parent(), parent()->property(name: "cursorPosition" ).toInt()); |
| 197 | QAccessible::updateAccessibility(event: &ev); |
| 198 | } |
| 199 | |
| 200 | void setIgnored(bool ignored); |
| 201 | |
| 202 | Q_SIGNALS: |
| 203 | void roleChanged(); |
| 204 | void nameChanged(); |
| 205 | void descriptionChanged(); |
| 206 | void idChanged(); |
| 207 | void ignoredChanged(); |
| 208 | void pressAction(); |
| 209 | void toggleAction(); |
| 210 | void increaseAction(); |
| 211 | void decreaseAction(); |
| 212 | void scrollUpAction(); |
| 213 | void scrollDownAction(); |
| 214 | void scrollLeftAction(); |
| 215 | void scrollRightAction(); |
| 216 | void previousPageAction(); |
| 217 | void nextPageAction(); |
| 218 | |
| 219 | private: |
| 220 | QAccessible::Role m_role; |
| 221 | QAccessible::State m_state; |
| 222 | QAccessible::State m_stateExplicitlySet; |
| 223 | QString m_name; |
| 224 | bool m_nameExplicitlySet = false; |
| 225 | QString m_description; |
| 226 | bool m_descriptionExplicitlySet = false; |
| 227 | QQuickAccessibleAttached* m_proxying = nullptr; |
| 228 | QString m_id; |
| 229 | |
| 230 | static QMetaMethod sigPress; |
| 231 | static QMetaMethod sigToggle; |
| 232 | static QMetaMethod sigIncrease; |
| 233 | static QMetaMethod sigDecrease; |
| 234 | static QMetaMethod sigScrollUp; |
| 235 | static QMetaMethod sigScrollDown; |
| 236 | static QMetaMethod sigScrollLeft; |
| 237 | static QMetaMethod sigScrollRight; |
| 238 | static QMetaMethod sigPreviousPage; |
| 239 | static QMetaMethod sigNextPage; |
| 240 | |
| 241 | public: |
| 242 | using QObject::property; |
| 243 | }; |
| 244 | |
| 245 | |
| 246 | QT_END_NAMESPACE |
| 247 | |
| 248 | #endif // accessibility |
| 249 | |
| 250 | #endif |
| 251 | |