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
28QT_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_state.P ; } \
34 void set_ ## P(bool arg) \
35 { \
36 m_stateExplicitlySet.P = true; \
37 if (m_state.P == arg) \
38 return; \
39 m_state.P = arg; \
40 Q_EMIT P ## Changed(arg); \
41 QAccessible::State changedState; \
42 changedState.P = true; \
43 QAccessibleStateChangeEvent ev(parent(), changedState); \
44 QAccessible::updateAccessibility(&ev); \
45 } \
46 Q_SIGNAL void P ## Changed(bool arg);
47
48class Q_QUICK_PRIVATE_EXPORT QQuickAccessibleAttached : public QObject
49{
50 Q_OBJECT
51 Q_PROPERTY(QAccessible::Role role READ role WRITE setRole NOTIFY roleChanged FINAL)
52 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL)
53 Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged FINAL)
54 Q_PROPERTY(bool ignored READ ignored WRITE setIgnored NOTIFY ignoredChanged FINAL)
55
56 QML_NAMED_ELEMENT(Accessible)
57 QML_ADDED_IN_VERSION(2, 0)
58 QML_UNCREATABLE("Accessible is only available via attached properties.")
59 QML_ATTACHED(QQuickAccessibleAttached)
60 QML_EXTENDED_NAMESPACE(QAccessible)
61
62public:
63 STATE_PROPERTY(checkable)
64 STATE_PROPERTY(checked)
65 STATE_PROPERTY(editable)
66 STATE_PROPERTY(focusable)
67 STATE_PROPERTY(focused)
68 STATE_PROPERTY(multiLine)
69 STATE_PROPERTY(readOnly)
70 STATE_PROPERTY(selected)
71 STATE_PROPERTY(selectable)
72 STATE_PROPERTY(pressed)
73 STATE_PROPERTY(checkStateMixed)
74 STATE_PROPERTY(defaultButton)
75 STATE_PROPERTY(passwordEdit)
76 STATE_PROPERTY(selectableText)
77 STATE_PROPERTY(searchEdit)
78
79 QQuickAccessibleAttached(QObject *parent);
80 ~QQuickAccessibleAttached();
81
82 QAccessible::Role role() const { return m_role; }
83 void setRole(QAccessible::Role role);
84 QString name() const {
85 if (m_state.passwordEdit)
86 return QString();
87 return m_name;
88 }
89
90 bool wasNameExplicitlySet() const;
91 void setName(const QString &name) {
92 m_nameExplicitlySet = true;
93 if (name != m_name) {
94 m_name = name;
95 Q_EMIT nameChanged();
96 QAccessibleEvent ev(parent(), QAccessible::NameChanged);
97 QAccessible::updateAccessibility(event: &ev);
98 }
99 }
100 void setNameImplicitly(const QString &name);
101
102 QString description() const { return m_description; }
103 void setDescription(const QString &description)
104 {
105 if (m_description != description) {
106 m_description = description;
107 Q_EMIT descriptionChanged();
108 QAccessibleEvent ev(parent(), QAccessible::DescriptionChanged);
109 QAccessible::updateAccessibility(event: &ev);
110 }
111 }
112
113 // Factory function
114 static QQuickAccessibleAttached *qmlAttachedProperties(QObject *obj);
115
116 static QQuickAccessibleAttached *attachedProperties(const QObject *obj)
117 {
118 return qobject_cast<QQuickAccessibleAttached*>(object: qmlAttachedPropertiesObject<QQuickAccessibleAttached>(obj, create: false));
119 }
120
121 // Property getter
122 static QVariant property(const QObject *object, const char *propertyName)
123 {
124 if (QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(obj: object))
125 return attachedObject->property(name: propertyName);
126 return QVariant();
127 }
128
129 static bool setProperty(QObject *object, const char *propertyName, const QVariant &value)
130 {
131 QObject *obj = qmlAttachedPropertiesObject<QQuickAccessibleAttached>(obj: object, create: true);
132 if (!obj) {
133 qWarning(msg: "cannot set property Accessible.%s of QObject %s", propertyName, object->metaObject()->className());
134 return false;
135 }
136 return obj->setProperty(name: propertyName, value);
137 }
138
139 static QObject *findAccessible(QObject *object, QAccessible::Role role = QAccessible::NoRole)
140 {
141 while (object) {
142 QQuickAccessibleAttached *att = QQuickAccessibleAttached::attachedProperties(obj: object);
143 if (att && (role == QAccessible::NoRole || att->role() == role)) {
144 break;
145 }
146 object = object->parent();
147 }
148 return object;
149 }
150
151 QAccessible::State state() const { return m_state; }
152 bool ignored() const;
153 bool doAction(const QString &actionName);
154 void availableActions(QStringList *actions) const;
155
156 Q_REVISION(6, 2) Q_INVOKABLE static QString stripHtml(const QString &html);
157
158public Q_SLOTS:
159 void valueChanged() {
160 QAccessibleValueChangeEvent ev(parent(), parent()->property(name: "value"));
161 QAccessible::updateAccessibility(event: &ev);
162 }
163 void cursorPositionChanged() {
164 QAccessibleTextCursorEvent ev(parent(), parent()->property(name: "cursorPosition").toInt());
165 QAccessible::updateAccessibility(event: &ev);
166 }
167
168 void setIgnored(bool ignored);
169
170Q_SIGNALS:
171 void roleChanged();
172 void nameChanged();
173 void descriptionChanged();
174 void ignoredChanged();
175 void pressAction();
176 void toggleAction();
177 void increaseAction();
178 void decreaseAction();
179 void scrollUpAction();
180 void scrollDownAction();
181 void scrollLeftAction();
182 void scrollRightAction();
183 void previousPageAction();
184 void nextPageAction();
185
186private:
187 QQuickItem *item() const { return qobject_cast<QQuickItem*>(o: parent()); }
188
189 QAccessible::Role m_role;
190 QAccessible::State m_state;
191 QAccessible::State m_stateExplicitlySet;
192 QString m_name;
193 bool m_nameExplicitlySet = false;
194 QString m_description;
195
196 static QMetaMethod sigPress;
197 static QMetaMethod sigToggle;
198 static QMetaMethod sigIncrease;
199 static QMetaMethod sigDecrease;
200 static QMetaMethod sigScrollUp;
201 static QMetaMethod sigScrollDown;
202 static QMetaMethod sigScrollLeft;
203 static QMetaMethod sigScrollRight;
204 static QMetaMethod sigPreviousPage;
205 static QMetaMethod sigNextPage;
206
207public:
208 using QObject::property;
209};
210
211
212QT_END_NAMESPACE
213
214QML_DECLARE_TYPE(QQuickAccessibleAttached)
215
216#endif // accessibility
217
218#endif
219

source code of qtdeclarative/src/quick/items/qquickaccessibleattached_p.h