5#ifndef QQUICKACCESSIBLEATTACHED_H
6#define QQUICKACCESSIBLEATTACHED_H
19#include <QtQuick/qquickitem.h>
21#include <QtCore/qobject.h>
22#include <QtCore/qstring.h>
24#if QT_CONFIG(accessibility)
26#include <QtGui/qaccessible.h>
27#include <private/qtquickglobal_p.h>
28#include <QtQml/qqml.h>
29#include <QtQml/qqmlinfo.h>
34#define STATE_PROPERTY(P)
35 Q_PROPERTY(bool P READ P WRITE set_ ## P NOTIFY P ## Changed FINAL)
36 bool P() const { return m_proxying && !m_stateExplicitlySet.P ? m_proxying->P() : m_state.P ; }
37 void set_ ## P(bool arg)
40 m_proxying->set_##P(arg);
41 m_stateExplicitlySet.P = true;
45 Q_EMIT P ## Changed(arg);
46 QAccessible::State changedState;
47 changedState.P = true;
48 QAccessibleStateChangeEvent ev(parent(), changedState);
49 QAccessible::updateAccessibility(&ev);
51 Q_SIGNAL void P ## Changed(bool arg);
53class Q_QUICK_EXPORT QQuickAccessibleAttached :
public QObject
56 Q_PROPERTY(QAccessible::Role role READ role WRITE setRole NOTIFY roleChanged FINAL)
57 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL)
58 Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged FINAL)
59 Q_PROPERTY(QString id READ id WRITE setId NOTIFY idChanged FINAL)
60 Q_PROPERTY(
bool ignored READ ignored WRITE setIgnored NOTIFY ignoredChanged FINAL)
61 Q_PROPERTY(
int levelAttribute READ levelAttribute WRITE setLevelAttribute NOTIFY levelAttributeChanged FINAL)
62 Q_PROPERTY(QQuickItem *labelledBy READ labelledBy WRITE setLabelledBy NOTIFY labelledByChanged FINAL)
63 Q_PROPERTY(QQuickItem *labelFor READ labelFor WRITE setLabelFor NOTIFY labelForChanged FINAL)
65 QML_NAMED_ELEMENT(Accessible)
66 QML_ADDED_IN_VERSION(2, 0)
67 QML_UNCREATABLE(
"Accessible is only available via attached properties.")
68 QML_ATTACHED(QQuickAccessibleAttached)
69 QML_EXTENDED_NAMESPACE(QAccessible)
72 STATE_PROPERTY(checkable)
73 STATE_PROPERTY(checked)
74 STATE_PROPERTY(editable)
75 STATE_PROPERTY(expandable)
76 STATE_PROPERTY(expanded)
77 STATE_PROPERTY(focusable)
78 STATE_PROPERTY(focused)
79 STATE_PROPERTY(multiLine)
80 STATE_PROPERTY(readOnly)
81 STATE_PROPERTY(selected)
82 STATE_PROPERTY(selectable)
83 STATE_PROPERTY(pressed)
84 STATE_PROPERTY(checkStateMixed)
85 STATE_PROPERTY(defaultButton)
86 STATE_PROPERTY(passwordEdit)
87 STATE_PROPERTY(selectableText)
88 STATE_PROPERTY(searchEdit)
90 QQuickAccessibleAttached(QObject *parent);
91 ~QQuickAccessibleAttached();
93 QAccessible::Role role()
const {
return m_role; }
94 void setRole(QAccessible::Role role);
95 QString name()
const {
96 if (m_state.passwordEdit)
98 if (!m_nameExplicitlySet && m_proxying && m_proxying->wasNameExplicitlySet())
99 return m_proxying->name();
103 bool wasNameExplicitlySet()
const;
104 void setName(
const QString &name) {
105 m_nameExplicitlySet =
true;
106 if (name != m_name) {
108 Q_EMIT nameChanged();
109 QAccessibleEvent ev(parent(), QAccessible::NameChanged);
110 QAccessible::updateAccessibility(&ev);
113 void setNameImplicitly(
const QString &name);
115 QString description()
const {
116 return !m_descriptionExplicitlySet && m_proxying ? m_proxying->description() : m_description;
118 void setDescription(
const QString &description)
120 if (!m_descriptionExplicitlySet && m_proxying) {
121 disconnect(m_proxying, &QQuickAccessibleAttached::descriptionChanged,
this, &QQuickAccessibleAttached::descriptionChanged);
123 m_descriptionExplicitlySet =
true;
124 if (m_description != description) {
125 m_description = description;
126 Q_EMIT descriptionChanged();
127 QAccessibleEvent ev(parent(), QAccessible::DescriptionChanged);
128 QAccessible::updateAccessibility(&ev);
131 void setDescriptionImplicitly(
const QString &desc);
133 QString id()
const {
return m_id; }
134 void setId(
const QString &id)
139 QAccessibleEvent ev(parent(), QAccessible::IdentifierChanged);
140 QAccessible::updateAccessibility(&ev);
144 QQuickItem *labelledBy()
const
146 return findRelation(QAccessible::Labelled);
149 void setLabelledBy(QQuickItem *labelledBy)
151 QQuickItem *p = qobject_cast<QQuickItem *>(parent());
153 qmlWarning(p) <<
"labelledBy cannot be null";
156 setLabelledByInternal(labelledBy);
158 QQuickAccessibleAttached *label = qobject_cast<QQuickAccessibleAttached *>(
159 qmlAttachedPropertiesObject<QQuickAccessibleAttached>(labelledBy));
161 label->setLabelForInternal(p);
163 void setLabelledByInternal(QQuickItem *labelledBy)
165 m_relations.append({ labelledBy, QAccessible::Labelled });
166 Q_EMIT labelledByChanged();
169 QQuickItem *labelFor()
const
171 return findRelation(QAccessible::Label);
174 void setLabelFor(QQuickItem *labelFor)
176 QQuickItem *p = qobject_cast<QQuickItem *>(parent());
178 qmlWarning(p) <<
"labelFor cannot be null";
181 setLabelForInternal(labelFor);
183 QQuickAccessibleAttached *labelled = qobject_cast<QQuickAccessibleAttached *>(
184 qmlAttachedPropertiesObject<QQuickAccessibleAttached>(labelFor));
185 labelled->setLabelledBy(p);
188 void setLabelForInternal(QQuickItem *labelFor)
190 m_relations.append({ labelFor, QAccessible::Label });
191 Q_EMIT labelForChanged();
194 int levelAttribute()
const {
return m_levelAttribute; }
195 void setLevelAttribute(
int levelAttribute)
197 if (m_levelAttribute != levelAttribute) {
198 m_levelAttribute = levelAttribute;
199 Q_EMIT levelAttributeChanged();
200 QAccessibleEvent ev(parent(), QAccessible::AttributeChanged);
201 QAccessible::updateAccessibility(&ev);
206 static QQuickAccessibleAttached *qmlAttachedProperties(QObject *obj);
208 static QQuickAccessibleAttached *attachedProperties(
const QObject *obj)
210 return qobject_cast<QQuickAccessibleAttached*>(qmlAttachedPropertiesObject<QQuickAccessibleAttached>(obj,
false));
214 static QVariant property(
const QObject *object,
const char *propertyName)
216 if (QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(object))
217 return attachedObject->property(propertyName);
221 static bool setProperty(QObject *object,
const char *propertyName,
const QVariant &value)
223 QObject *obj = qmlAttachedPropertiesObject<QQuickAccessibleAttached>(object,
true);
225 qWarning(
"cannot set property Accessible.%s of QObject %s", propertyName, object->metaObject()->className());
228 return obj->setProperty(propertyName, value);
231 static QObject *findAccessible(QObject *object, QAccessible::Role role = QAccessible::NoRole)
234 QQuickAccessibleAttached *att = QQuickAccessibleAttached::attachedProperties(object);
235 if (att && (role == QAccessible::NoRole || att->role() == role)) {
238 if (
auto action = object->property(
"action").value<QObject *>(); action) {
239 QQuickAccessibleAttached *att = QQuickAccessibleAttached::attachedProperties(action);
240 if (att && (role == QAccessible::NoRole || att->role() == role)) {
245 object = object->parent();
250 QAccessible::State state()
const {
return m_state; }
251 bool ignored()
const;
252 bool doAction(
const QString &actionName);
253 void availableActions(QStringList *actions)
const;
255 Q_REVISION(6, 2) Q_INVOKABLE
static QString stripHtml(
const QString &html);
256 void setProxying(QQuickAccessibleAttached *proxying);
258 Q_REVISION(6, 8) Q_INVOKABLE
void announce(
const QString &message, QAccessible::AnnouncementPoliteness politeness = QAccessible::AnnouncementPoliteness::Polite);
261 void valueChanged() {
262 QAccessibleValueChangeEvent ev(parent(), parent()->property(
"value"));
263 QAccessible::updateAccessibility(&ev);
265 void cursorPositionChanged() {
266 QAccessibleTextCursorEvent ev(parent(), parent()->property(
"cursorPosition").toInt());
267 QAccessible::updateAccessibility(&ev);
270 void setIgnored(
bool ignored);
275 void descriptionChanged();
277 void ignoredChanged();
278 void levelAttributeChanged();
279 void labelledByChanged();
280 void labelForChanged();
283 void increaseAction();
284 void decreaseAction();
285 void showOnScreenAction();
286 void scrollUpAction();
287 void scrollDownAction();
288 void scrollLeftAction();
289 void scrollRightAction();
290 void previousPageAction();
291 void nextPageAction();
294 QQuickItem *findRelation(QAccessible::Relation relation)
const;
296 QAccessible::Role m_role;
297 QAccessible::State m_state;
298 QAccessible::State m_stateExplicitlySet;
300 bool m_nameExplicitlySet =
false;
301 QString m_description;
302 bool m_descriptionExplicitlySet =
false;
303 QQuickAccessibleAttached* m_proxying =
nullptr;
305 QList<std::pair<QQuickItem *, QAccessible::Relation>> m_relations;
306 int m_levelAttribute = 0;
308 static QMetaMethod sigPress;
309 static QMetaMethod sigToggle;
310 static QMetaMethod sigIncrease;
311 static QMetaMethod sigDecrease;
312 static QMetaMethod sigShowOnScreen;
313 static QMetaMethod sigScrollUp;
314 static QMetaMethod sigScrollDown;
315 static QMetaMethod sigScrollLeft;
316 static QMetaMethod sigScrollRight;
317 static QMetaMethod sigPreviousPage;
318 static QMetaMethod sigNextPage;
321 using QObject::property;