7#if QT_CONFIG(accessibility)
9#include <QtQml/qqmlinfo.h>
11#include "private/qquickitem_p.h"
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
62
63
64
65
66
67
68
69
72
73
74
75
76
77
78
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
125
126
127
128
129
130
131
134
135
136
137
138
139
140
142
143
144
145
146
147
149
150
151
152
153
155
156
157
158
159
161
162
163
164
165
166
168
169
170
171
172
173
175
176
177
178
180
181
182
183
184
186
187
188
189
190
191
192
193
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
222
223
224
225
227
228
229
230
231
233
234
235
236
237
239
240
241
242
243
245
246
247
248
250
251
252
253
254
256
257
258
259
261
262
263
264
266
267
268
269
271
272
273
274
275
276
277
278
280
281
282
283
284
285
286
287
290
291
292
293
295
296
297
298
300
301
302
303
305
306
307
308
310
311
312
313
314
316
317
318
319
321
322
323
324
326
327
328
329
331
332
333
334
336
337
338
339
341
342
343
344
346
347
348
349
350
351
353QMetaMethod QQuickAccessibleAttached::sigPress;
354QMetaMethod QQuickAccessibleAttached::sigToggle;
355QMetaMethod QQuickAccessibleAttached::sigIncrease;
356QMetaMethod QQuickAccessibleAttached::sigDecrease;
357QMetaMethod QQuickAccessibleAttached::sigShowOnScreen;
358QMetaMethod QQuickAccessibleAttached::sigScrollUp;
359QMetaMethod QQuickAccessibleAttached::sigScrollDown;
360QMetaMethod QQuickAccessibleAttached::sigScrollLeft;
361QMetaMethod QQuickAccessibleAttached::sigScrollRight;
362QMetaMethod QQuickAccessibleAttached::sigPreviousPage;
363QMetaMethod QQuickAccessibleAttached::sigNextPage;
364QMetaMethod QQuickAccessibleAttached::sigShowMenu;
370static void connectPropertyChangeSignals(QQuickAccessibleAttached *attached,
371 QAccessibleInterface *accessibleInterface)
373 QObject *parent = attached->parent();
374 const QMetaObject *parentMetaObject = parent->metaObject();
375 if (!accessibleInterface || !parentMetaObject)
378 auto connectPropertyChangeSignal = [parent, parentMetaObject, attached](
379 const char *propertyName,
const char *signalName,
int slotIndex)
388 int propertyIndex = parentMetaObject->indexOfProperty(propertyName);
389 if (propertyIndex != -1) {
390 const QMetaProperty property = parentMetaObject->property(propertyIndex);
391 const QMetaMethod signal = property.notifySignal();
392 if (signal.name() == signalName) {
393 QMetaObject::connect(parent, signal.methodIndex(), attached, slotIndex,
394 Qt::UniqueConnection);
398 const QMetaObject &attachedMetaObject = QQuickAccessibleAttached::staticMetaObject;
400 if (accessibleInterface->valueInterface()) {
401 static const int valueChangedIndex = attachedMetaObject.indexOfSlot(
"valueChanged()");
402 connectPropertyChangeSignal(
"value",
"valueChanged", valueChangedIndex);
405 if (accessibleInterface->textInterface()) {
406 static const int cursorPositionChangedIndex =
407 attachedMetaObject.indexOfSlot(
"cursorPositionChanged()");
408 connectPropertyChangeSignal(
"cursorPosition",
"cursorPositionChanged",
409 cursorPositionChangedIndex);
413QQuickAccessibleAttached::QQuickAccessibleAttached(QObject *parent)
414 : QObject(parent), m_role(QAccessible::NoRole)
419 auto item = qobject_cast<QQuickItem *>(parent);
421 item->d_func()->setAccessible();
423 const QLatin1StringView className(QQmlData::ensurePropertyCache(parent)->firstCppMetaObject()->className());
424 if (className != QLatin1StringView(
"QQuickAction")) {
425 qmlWarning(parent) <<
"Accessible attached property must be attached to an object deriving from Item or Action";
429 QAccessibleEvent ev(parent, QAccessible::ObjectCreated);
430 QAccessible::updateAccessibility(&ev);
432 connectPropertyChangeSignals(
this, ev.accessibleInterface());
434 if (!sigPress.isValid()) {
435 sigPress = QMetaMethod::fromSignal(&QQuickAccessibleAttached::pressAction);
436 sigToggle = QMetaMethod::fromSignal(&QQuickAccessibleAttached::toggleAction);
437 sigIncrease = QMetaMethod::fromSignal(&QQuickAccessibleAttached::increaseAction);
438 sigDecrease = QMetaMethod::fromSignal(&QQuickAccessibleAttached::decreaseAction);
439 sigShowOnScreen = QMetaMethod::fromSignal(&QQuickAccessibleAttached::showOnScreenAction);
440 sigScrollUp = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollUpAction);
441 sigScrollDown = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollDownAction);
442 sigScrollLeft = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollLeftAction);
443 sigScrollRight = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollRightAction);
444 sigPreviousPage = QMetaMethod::fromSignal(&QQuickAccessibleAttached::previousPageAction);
445 sigNextPage= QMetaMethod::fromSignal(&QQuickAccessibleAttached::nextPageAction);
446 sigShowMenu = QMetaMethod::fromSignal(&QQuickAccessibleAttached::showMenuAction);
450QQuickAccessibleAttached::~QQuickAccessibleAttached()
454void QQuickAccessibleAttached::setRole(QAccessible::Role role)
456 if (role != m_role) {
458 Q_EMIT roleChanged();
463 case QAccessible::CheckBox:
464 case QAccessible::RadioButton:
465 case QAccessible::Switch:
466 if (!m_stateExplicitlySet.focusable)
467 m_state.focusable =
true;
468 if (!m_stateExplicitlySet.checkable)
469 m_state.checkable =
true;
471 case QAccessible::Button:
472 case QAccessible::MenuItem:
473 case QAccessible::PageTab:
474 case QAccessible::SpinBox:
475 case QAccessible::ComboBox:
476 case QAccessible::Terminal:
477 case QAccessible::ScrollBar:
478 if (!m_stateExplicitlySet.focusable)
479 m_state.focusable =
true;
481 case QAccessible::EditableText:
482 if (!m_stateExplicitlySet.editable)
483 m_state.editable =
true;
484 if (!m_stateExplicitlySet.focusable)
485 m_state.focusable =
true;
487 case QAccessible::StaticText:
488 if (!m_stateExplicitlySet.readOnly)
489 m_state.readOnly =
true;
490 if (!m_stateExplicitlySet.focusable)
491 m_state.focusable =
true;
497 connectPropertyChangeSignals(
this, QAccessible::queryAccessibleInterface(parent()));
501bool QQuickAccessibleAttached::wasNameExplicitlySet()
const
503 return m_nameExplicitlySet;
508void QQuickAccessibleAttached::setNameImplicitly(
const QString &name)
511 m_nameExplicitlySet =
false;
514void QQuickAccessibleAttached::setDescriptionImplicitly(
const QString &desc)
516 if (m_descriptionExplicitlySet)
518 setDescription(desc);
519 m_descriptionExplicitlySet =
false;
522QQuickAccessibleAttached *QQuickAccessibleAttached::qmlAttachedProperties(QObject *obj)
524 return new QQuickAccessibleAttached(obj);
527bool QQuickAccessibleAttached::ignored()
const
529 auto item = qobject_cast<QQuickItem *>(parent());
530 return item ? !item->d_func()->isAccessible :
false;
533void QQuickAccessibleAttached::setIgnored(
bool ignored)
535 auto item = qobject_cast<QQuickItem *>(parent());
536 if (item &&
this->ignored() != ignored) {
537 item->d_func()->isAccessible = !ignored;
538 QAccessibleEvent event(item,
539 ignored ? QAccessible::ObjectDestroyed : QAccessible::ObjectCreated);
540 QAccessible::updateAccessibility(&event);
541 emit ignoredChanged();
545bool QQuickAccessibleAttached::doAction(
const QString &actionName)
547 QMetaMethod *sig =
nullptr;
548 if (actionName == QAccessibleActionInterface::pressAction())
550 else if (actionName == QAccessibleActionInterface::toggleAction())
552 else if (actionName == QAccessibleActionInterface::increaseAction())
554 else if (actionName == QAccessibleActionInterface::decreaseAction())
556 else if (actionName == QAccessibleActionInterface::showOnScreenAction())
557 sig = &sigShowOnScreen;
558 else if (actionName == QAccessibleActionInterface::scrollUpAction())
560 else if (actionName == QAccessibleActionInterface::scrollDownAction())
561 sig = &sigScrollDown;
562 else if (actionName == QAccessibleActionInterface::scrollLeftAction())
563 sig = &sigScrollLeft;
564 else if (actionName == QAccessibleActionInterface::scrollRightAction())
565 sig = &sigScrollRight;
566 else if (actionName == QAccessibleActionInterface::previousPageAction())
567 sig = &sigPreviousPage;
568 else if (actionName == QAccessibleActionInterface::nextPageAction())
570 else if (actionName == QAccessibleActionInterface::showMenuAction())
572 if (sig && isSignalConnected(*sig)) {
575 ret = sig->invoke(m_proxying);
577 ret = sig->invoke(
this);
583void QQuickAccessibleAttached::availableActions(QStringList *actions)
const
585 if (isSignalConnected(sigPress))
586 actions->append(QAccessibleActionInterface::pressAction());
587 if (isSignalConnected(sigToggle))
588 actions->append(QAccessibleActionInterface::toggleAction());
589 if (isSignalConnected(sigIncrease))
590 actions->append(QAccessibleActionInterface::increaseAction());
591 if (isSignalConnected(sigDecrease))
592 actions->append(QAccessibleActionInterface::decreaseAction());
593 if (isSignalConnected(sigShowOnScreen))
594 actions->append(QAccessibleActionInterface::showOnScreenAction());
595 if (isSignalConnected(sigScrollUp))
596 actions->append(QAccessibleActionInterface::scrollUpAction());
597 if (isSignalConnected(sigScrollDown))
598 actions->append(QAccessibleActionInterface::scrollDownAction());
599 if (isSignalConnected(sigScrollLeft))
600 actions->append(QAccessibleActionInterface::scrollLeftAction());
601 if (isSignalConnected(sigScrollRight))
602 actions->append(QAccessibleActionInterface::scrollRightAction());
603 if (isSignalConnected(sigPreviousPage))
604 actions->append(QAccessibleActionInterface::previousPageAction());
605 if (isSignalConnected(sigNextPage))
606 actions->append(QAccessibleActionInterface::nextPageAction());
607 if (isSignalConnected(sigShowMenu))
608 actions->append(QAccessibleActionInterface::showMenuAction());
611QString QQuickAccessibleAttached::stripHtml(
const QString &html)
613#ifndef QT_NO_TEXTHTMLPARSER
616 return doc.toPlainText();
622void QQuickAccessibleAttached::setProxying(QQuickAccessibleAttached *proxying)
624 if (proxying == m_proxying)
627 const QMetaObject &mo = staticMetaObject;
630 auto mo = m_proxying->metaObject();
631 auto propertyCache = QQmlData::ensurePropertyCache(m_proxying);
632 for (
int signalIndex = propertyCache->signalOffset();
633 signalIndex < propertyCache->signalCount(); ++signalIndex) {
634 const QMetaMethod m = mo->method(propertyCache->signal(signalIndex)->coreIndex());
635 Q_ASSERT(m.methodType() == QMetaMethod::Signal);
636 if (m.methodType() != QMetaMethod::Signal)
639 disconnect(m_proxying, m,
this, m);
643 m_proxying = proxying;
647 auto propertyCache = QQmlData::ensurePropertyCache(m_proxying);
648 auto mo = m_proxying->metaObject();
649 for (
int signalIndex = propertyCache->signalOffset();
650 signalIndex < propertyCache->signalCount(); ++signalIndex) {
651 const QMetaMethod m = mo->method(propertyCache->signal(signalIndex)->coreIndex());
652 Q_ASSERT(m.methodType() == QMetaMethod::Signal);
653 connect(proxying, m,
this, m);
658 for (
int prop = mo.propertyOffset(); prop < mo.propertyCount(); ++prop) {
659 const QMetaProperty p = mo.property(prop);
660 if (!p.hasNotifySignal()) {
664 const QMetaMethod signal = p.notifySignal();
665 if (signal.parameterCount() == 0)
668 signal.invoke(
this, Q_ARG(
bool, p.read(
this).toBool()));
673
674
675
676
677
678
679
680void QQuickAccessibleAttached::announce(
const QString &message, QAccessible::AnnouncementPoliteness politeness)
682 QAccessibleAnnouncementEvent event(parent(), message);
683 event.setPoliteness(politeness);
684 QAccessible::updateAccessibility(&event);
687QQuickItem *QQuickAccessibleAttached::findRelation(QAccessible::Relation relation)
const
689 const auto it = std::find_if(m_relations.cbegin(), m_relations.cend(),
690 [relation](
const auto &rel) {
691 return rel.second == relation;
694 return it != m_relations.cend() ? it->first :
nullptr;
699#include "moc_qquickaccessibleattached_p.cpp"