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(QQuickItem *labelledBy READ labelledBy WRITE setLabelledBy NOTIFY labelledByChanged FINAL)
62 Q_PROPERTY(QQuickItem *labelFor READ labelFor WRITE setLabelFor NOTIFY labelForChanged FINAL)
64 QML_NAMED_ELEMENT(Accessible)
65 QML_ADDED_IN_VERSION(2, 0)
66 QML_UNCREATABLE(
"Accessible is only available via attached properties.")
67 QML_ATTACHED(QQuickAccessibleAttached)
68 QML_EXTENDED_NAMESPACE(QAccessible)
71 STATE_PROPERTY(checkable)
72 STATE_PROPERTY(checked)
73 STATE_PROPERTY(editable)
74 STATE_PROPERTY(expandable)
75 STATE_PROPERTY(expanded)
76 STATE_PROPERTY(focusable)
77 STATE_PROPERTY(focused)
78 STATE_PROPERTY(multiLine)
79 STATE_PROPERTY(readOnly)
80 STATE_PROPERTY(selected)
81 STATE_PROPERTY(selectable)
82 STATE_PROPERTY(pressed)
83 STATE_PROPERTY(checkStateMixed)
84 STATE_PROPERTY(defaultButton)
85 STATE_PROPERTY(passwordEdit)
86 STATE_PROPERTY(selectableText)
87 STATE_PROPERTY(searchEdit)
89 QQuickAccessibleAttached(QObject *parent);
90 ~QQuickAccessibleAttached();
92 QAccessible::Role role()
const {
return m_role; }
93 void setRole(QAccessible::Role role);
94 QString name()
const {
95 if (m_state.passwordEdit)
97 if (!m_nameExplicitlySet && m_proxying && m_proxying->wasNameExplicitlySet())
98 return m_proxying->name();
102 bool wasNameExplicitlySet()
const;
103 void setName(
const QString &name) {
104 m_nameExplicitlySet =
true;
105 if (name != m_name) {
107 Q_EMIT nameChanged();
108 QAccessibleEvent ev(parent(), QAccessible::NameChanged);
109 QAccessible::updateAccessibility(&ev);
112 void setNameImplicitly(
const QString &name);
114 QString description()
const {
115 return !m_descriptionExplicitlySet && m_proxying ? m_proxying->description() : m_description;
117 void setDescription(
const QString &description)
119 if (!m_descriptionExplicitlySet && m_proxying) {
120 disconnect(m_proxying, &QQuickAccessibleAttached::descriptionChanged,
this, &QQuickAccessibleAttached::descriptionChanged);
122 m_descriptionExplicitlySet =
true;
123 if (m_description != description) {
124 m_description = description;
125 Q_EMIT descriptionChanged();
126 QAccessibleEvent ev(parent(), QAccessible::DescriptionChanged);
127 QAccessible::updateAccessibility(&ev);
130 void setDescriptionImplicitly(
const QString &desc);
132 QString id()
const {
return m_id; }
133 void setId(
const QString &id)
138 QAccessibleEvent ev(parent(), QAccessible::IdentifierChanged);
139 QAccessible::updateAccessibility(&ev);
143 QQuickItem *labelledBy()
const
145 return findRelation(QAccessible::Labelled);
148 void setLabelledBy(QQuickItem *labelledBy)
150 QQuickItem *p = qobject_cast<QQuickItem *>(parent());
152 qmlWarning(p) <<
"labelledBy cannot be null";
155 setLabelledByInternal(labelledBy);
157 QQuickAccessibleAttached *label = qobject_cast<QQuickAccessibleAttached *>(
158 qmlAttachedPropertiesObject<QQuickAccessibleAttached>(labelledBy));
160 label->setLabelForInternal(p);
162 void setLabelledByInternal(QQuickItem *labelledBy)
164 m_relations.append({ labelledBy, QAccessible::Labelled });
165 Q_EMIT labelledByChanged();
168 QQuickItem *labelFor()
const
170 return findRelation(QAccessible::Label);
173 void setLabelFor(QQuickItem *labelFor)
175 QQuickItem *p = qobject_cast<QQuickItem *>(parent());
177 qmlWarning(p) <<
"labelFor cannot be null";
180 setLabelForInternal(labelFor);
182 QQuickAccessibleAttached *labelled = qobject_cast<QQuickAccessibleAttached *>(
183 qmlAttachedPropertiesObject<QQuickAccessibleAttached>(labelFor));
184 labelled->setLabelledBy(p);
187 void setLabelForInternal(QQuickItem *labelFor)
189 m_relations.append({ labelFor, QAccessible::Label });
190 Q_EMIT labelForChanged();
194 static QQuickAccessibleAttached *qmlAttachedProperties(QObject *obj);
196 static QQuickAccessibleAttached *attachedProperties(
const QObject *obj)
198 return qobject_cast<QQuickAccessibleAttached*>(qmlAttachedPropertiesObject<QQuickAccessibleAttached>(obj,
false));
202 static QVariant property(
const QObject *object,
const char *propertyName)
204 if (QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(object))
205 return attachedObject->property(propertyName);
209 static bool setProperty(QObject *object,
const char *propertyName,
const QVariant &value)
211 QObject *obj = qmlAttachedPropertiesObject<QQuickAccessibleAttached>(object,
true);
213 qWarning(
"cannot set property Accessible.%s of QObject %s", propertyName, object->metaObject()->className());
216 return obj->setProperty(propertyName, value);
219 static QObject *findAccessible(QObject *object, QAccessible::Role role = QAccessible::NoRole)
222 QQuickAccessibleAttached *att = QQuickAccessibleAttached::attachedProperties(object);
223 if (att && (role == QAccessible::NoRole || att->role() == role)) {
226 if (
auto action = object->property(
"action").value<QObject *>(); action) {
227 QQuickAccessibleAttached *att = QQuickAccessibleAttached::attachedProperties(action);
228 if (att && (role == QAccessible::NoRole || att->role() == role)) {
233 object = object->parent();
238 QAccessible::State state()
const {
return m_state; }
239 bool ignored()
const;
240 bool doAction(
const QString &actionName);
241 void availableActions(QStringList *actions)
const;
243 Q_REVISION(6, 2) Q_INVOKABLE
static QString stripHtml(
const QString &html);
244 void setProxying(QQuickAccessibleAttached *proxying);
246 Q_REVISION(6, 8) Q_INVOKABLE
void announce(
const QString &message, QAccessible::AnnouncementPoliteness politeness = QAccessible::AnnouncementPoliteness::Polite);
249 void valueChanged() {
250 QAccessibleValueChangeEvent ev(parent(), parent()->property(
"value"));
251 QAccessible::updateAccessibility(&ev);
253 void cursorPositionChanged() {
254 QAccessibleTextCursorEvent ev(parent(), parent()->property(
"cursorPosition").toInt());
255 QAccessible::updateAccessibility(&ev);
258 void setIgnored(
bool ignored);
263 void descriptionChanged();
265 void ignoredChanged();
266 void labelledByChanged();
267 void labelForChanged();
270 void increaseAction();
271 void decreaseAction();
272 void showOnScreenAction();
273 void scrollUpAction();
274 void scrollDownAction();
275 void scrollLeftAction();
276 void scrollRightAction();
277 void previousPageAction();
278 void nextPageAction();
281 QQuickItem *findRelation(QAccessible::Relation relation)
const;
283 QAccessible::Role m_role;
284 QAccessible::State m_state;
285 QAccessible::State m_stateExplicitlySet;
287 bool m_nameExplicitlySet =
false;
288 QString m_description;
289 bool m_descriptionExplicitlySet =
false;
290 QQuickAccessibleAttached* m_proxying =
nullptr;
292 QList<std::pair<QQuickItem *, QAccessible::Relation>> m_relations;
294 static QMetaMethod sigPress;
295 static QMetaMethod sigToggle;
296 static QMetaMethod sigIncrease;
297 static QMetaMethod sigDecrease;
298 static QMetaMethod sigShowOnScreen;
299 static QMetaMethod sigScrollUp;
300 static QMetaMethod sigScrollDown;
301 static QMetaMethod sigScrollLeft;
302 static QMetaMethod sigScrollRight;
303 static QMetaMethod sigPreviousPage;
304 static QMetaMethod sigNextPage;
307 using QObject::property;