Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickaccessibleattached.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6
7#if QT_CONFIG(accessibility)
8
9#include <QtQml/qqmlinfo.h>
10
11#include "private/qquickitem_p.h"
12
13QT_BEGIN_NAMESPACE
14
15/*!
16 \qmltype Accessible
17 \nativetype QQuickAccessibleAttached
18 \brief Enables accessibility of QML items.
19
20 \inqmlmodule QtQuick
21 \ingroup qtquick-visual-utility
22 \ingroup accessibility
23
24 This class is part of the \l {Accessibility for Qt Quick Applications}.
25
26 Items the user interacts with or that give information to the user
27 need to expose their information to the accessibility framework.
28 Then assistive tools can make use of that information to enable
29 users to interact with the application in various ways.
30 This enables Qt Quick applications to be used with screen-readers for example.
31
32 The most important properties are \l name, \l description and \l role.
33
34 Example implementation of a simple button:
35 \qml
36 Rectangle {
37 id: myButton
38 Text {
39 id: label
40 text: "next"
41 }
42 Accessible.role: Accessible.Button
43 Accessible.name: label.text
44 Accessible.description: "shows the next page"
45 Accessible.onPressAction: {
46 // do a button click
47 }
48 }
49 \endqml
50 The \l role is set to \c Button to indicate the type of control.
51 \l {Accessible::}{name} is the most important information and bound to the text on the button.
52 The name is a short and consise description of the control and should reflect the visual label.
53 In this case it is not clear what the button does with the name only, so \l description contains
54 an explanation.
55 There is also a signal handler \l {Accessible::pressAction}{Accessible.pressAction} which can be invoked by assistive tools to trigger
56 the button. This signal handler needs to have the same effect as tapping or clicking the button would have.
57
58 \sa Accessibility
59*/
60
61/*!
62 \qmlproperty string QtQuick::Accessible::name
63
64 This property sets an accessible name.
65 For a button for example, this should have a binding to its text.
66 In general this property should be set to a simple and concise
67 but human readable name. Do not include the type of control
68 you want to represent but just the name.
69*/
70
71/*!
72 \qmlproperty string QtQuick::Accessible::description
73
74 This property sets an accessible description.
75 Similar to the name it describes the item. The description
76 can be a little more verbose and tell what the item does,
77 for example the functionality of the button it describes.
78*/
79
80/*!
81 \qmlproperty enumeration QtQuick::Accessible::role
82
83 This flags sets the semantic type of the widget.
84 A button for example would have "Button" as type.
85 The value must be one of \l QAccessible::Role.
86
87 Some roles have special semantics.
88 In order to implement check boxes for example a "checked" property is expected.
89
90 \table
91 \header
92 \li \b {Role}
93 \li \b {Properties and signals}
94 \li \b {Explanation}
95 \row
96 \li All interactive elements
97 \li \l focusable and \l focused
98 \li All elements that the user can interact with should have focusable set to \c true and
99 set \c focus to \c true when they have the focus. This is important even for applications
100 that run on touch-only devices since screen readers often implement a virtual focus that
101 can be moved from item to item.
102 \row
103 \li Button, CheckBox, RadioButton, Switch
104 \li \l {Accessible::pressAction}{Accessible.pressAction}
105 \li A button should have a signal handler with the name \c onPressAction.
106 This signal may be emitted by an assistive tool such as a screen-reader.
107 The implementation needs to behave the same as a mouse click or tap on the button.
108 \row
109 \li CheckBox, RadioButton, Switch
110 \li \l checkable, \l checked, \l {Accessible::toggleAction}{Accessible.toggleAction}
111
112 \li The check state of the check box. Updated on Press, Check and Uncheck actions.
113 \row
114 \li Slider, SpinBox, Dial, ScrollBar
115 \li \c value, \c minimumValue, \c maximumValue, \c stepSize
116 \li These properties reflect the state and possible values for the elements.
117 \row
118 \li Slider, SpinBox, Dial, ScrollBar
119 \li \l {Accessible::increaseAction}{Accessible.increaseAction}, \l {Accessible::decreaseAction}{Accessible.decreaseAction}
120 \li Actions to increase and decrease the value of the element.
121 \endtable
122*/
123
124/*!
125 \qmlproperty string QtQuick::Accessible::id
126
127 This property sets an identifier for the object.
128 It can be used to provide stable identifiers to UI tests.
129 By default, the identifier is set to the ID of the QML object.
130 If the ID is not set the default of \l QAccessible::Identifier is used.
131*/
132
133/*! \qmlproperty bool QtQuick::Accessible::focusable
134 \brief This property holds whether this item is focusable.
135
136 By default, this property is \c false except for items where the role is one of
137 \c CheckBox, \c RadioButton, \c Switch, \c Button, \c MenuItem, \c PageTab,
138 \c EditableText, \c SpinBox, \c ComboBox, \c Terminal or \c ScrollBar.
139 \sa focused
140*/
141/*! \qmlproperty bool QtQuick::Accessible::focused
142 \brief This property holds whether this item currently has the active focus.
143
144 By default, this property is \c false, but it will return \c true for items that
145 have \l QQuickItem::hasActiveFocus() returning \c true.
146 \sa focusable
147*/
148/*! \qmlproperty bool QtQuick::Accessible::checkable
149 \brief This property holds whether this item is checkable (like a check box or some buttons).
150
151 By default this property is \c false.
152 \sa checked
153*/
154/*! \qmlproperty bool QtQuick::Accessible::checked
155 \brief This property holds whether this item is currently checked.
156
157 By default this property is \c false.
158 \sa checkable
159*/
160/*! \qmlproperty bool QtQuick::Accessible::editable
161 \brief This property holds whether this item has editable text.
162
163 By default this property is \c false.
164*/
165/*! \qmlproperty bool QtQuick::Accessible::searchEdit
166 \brief This property holds whether this item is input for a search query.
167 This property will only affect editable text.
168
169 By default this property is \c false.
170*/
171/*! \qmlproperty bool QtQuick::Accessible::ignored
172 \brief This property holds whether this item should be ignored by the accessibility framework.
173
174 Sometimes an item is part of a group of items that should be treated as one. For example two labels might be
175 visually placed next to each other, but separate items. For accessibility purposes they should be treated as one
176 and thus they are represented by a third invisible item with the right geometry.
177
178 For example a speed display adds "m/s" as a smaller label:
179 \qml
180 Row {
181 Label {
182 id: speedLabel
183 text: "Speed: 5"
184 Accessible.ignored: true
185 }
186 Label {
187 text: qsTr("m/s")
188 Accessible.ignored: true
189 }
190 Accessible.role: Accessible.StaticText
191 Accessible.name: speedLabel.text + " meters per second"
192 }
193 \endqml
194
195 \since 5.4
196 By default this property is \c false.
197*/
198/*! \qmlproperty bool QtQuick::Accessible::multiLine
199 \brief This property holds whether this item has multiple text lines.
200
201 By default this property is \c false.
202*/
203/*! \qmlproperty bool QtQuick::Accessible::readOnly
204 \brief This property indicates that a text field is read only.
205
206 It is relevant when the role is \l QAccessible::EditableText and set to be read-only.
207 By default this property is \c false.
208*/
209/*! \qmlproperty bool QtQuick::Accessible::selected
210 \brief This property holds whether this item is selected.
211
212 By default this property is \c false.
213 \sa selectable
214*/
215/*! \qmlproperty bool QtQuick::Accessible::selectable
216 \brief This property holds whether this item can be selected.
217
218 By default this property is \c false.
219 \sa selected
220*/
221/*! \qmlproperty bool QtQuick::Accessible::pressed
222 \brief This property holds whether this item is pressed (for example a button during a mouse click).
223
224 By default this property is \c false.
225*/
226/*! \qmlproperty bool QtQuick::Accessible::checkStateMixed
227 \brief This property holds whether this item is in the partially checked state.
228
229 By default this property is \c false.
230 \sa checked, checkable
231*/
232/*! \qmlproperty bool QtQuick::Accessible::defaultButton
233 \brief This property holds whether this item is the default button of a dialog.
234
235 By default this property is \c false.
236*/
237/*! \qmlproperty bool QtQuick::Accessible::passwordEdit
238 \brief This property holds whether this item is a password text edit.
239
240 By default this property is \c false.
241*/
242/*! \qmlproperty bool QtQuick::Accessible::selectableText
243 \brief This property holds whether this item contains selectable text.
244
245 By default this property is \c false.
246*/
247/*! \qmlproperty Item QtQuick::Accessible::labelledBy
248 \brief This property holds the item that is used as a label for this item.
249
250 Setting this property automatically sets up the labelFor relation of the other object.
251
252 By default this property is \c undefined.
253
254 \since 6.10
255 */
256/*! \qmlproperty Item QtQuick::Accessible::labelFor
257 \brief This property holds the item that this item is a label for.
258
259 Setting this property automatically sets up the labelledBy relation of the other object.
260
261 By default this property is \c undefined.
262
263 \since 6.10
264 */
265
266/*!
267 \qmlsignal QtQuick::Accessible::pressAction()
268
269 This signal is emitted when a press action is received from an assistive tool such as a screen-reader.
270*/
271/*!
272 \qmlsignal QtQuick::Accessible::toggleAction()
273
274 This signal is emitted when a toggle action is received from an assistive tool such as a screen-reader.
275*/
276/*!
277 \qmlsignal QtQuick::Accessible::increaseAction()
278
279 This signal is emitted when a increase action is received from an assistive tool such as a screen-reader.
280*/
281/*!
282 \qmlsignal QtQuick::Accessible::decreaseAction()
283
284 This signal is emitted when a decrease action is received from an assistive tool such as a screen-reader.
285*/
286/*!
287 \qmlsignal QtQuick::Accessible::scrollUpAction()
288
289 This signal is emitted when a scroll up action is received from an assistive tool such as a screen-reader.
290*/
291/*!
292 \qmlsignal QtQuick::Accessible::scrollDownAction()
293
294 This signal is emitted when a scroll down action is received from an assistive tool such as a screen-reader.
295*/
296/*!
297 \qmlsignal QtQuick::Accessible::scrollLeftAction()
298
299 This signal is emitted when a scroll left action is received from an assistive tool such as a screen-reader.
300*/
301/*!
302 \qmlsignal QtQuick::Accessible::scrollRightAction()
303
304 This signal is emitted when a scroll right action is received from an assistive tool such as a screen-reader.
305*/
306/*!
307 \qmlsignal QtQuick::Accessible::previousPageAction()
308
309 This signal is emitted when a previous page action is received from an assistive tool such as a screen-reader.
310*/
311/*!
312 \qmlsignal QtQuick::Accessible::nextPageAction()
313
314 This signal is emitted when a next page action is received from an assistive tool such as a screen-reader.
315*/
316
317QMetaMethod QQuickAccessibleAttached::sigPress;
318QMetaMethod QQuickAccessibleAttached::sigToggle;
319QMetaMethod QQuickAccessibleAttached::sigIncrease;
320QMetaMethod QQuickAccessibleAttached::sigDecrease;
321QMetaMethod QQuickAccessibleAttached::sigScrollUp;
322QMetaMethod QQuickAccessibleAttached::sigScrollDown;
323QMetaMethod QQuickAccessibleAttached::sigScrollLeft;
324QMetaMethod QQuickAccessibleAttached::sigScrollRight;
325QMetaMethod QQuickAccessibleAttached::sigPreviousPage;
326QMetaMethod QQuickAccessibleAttached::sigNextPage;
327
328QQuickAccessibleAttached::QQuickAccessibleAttached(QObject *parent)
329 : QObject(parent), m_role(QAccessible::NoRole)
330{
331 Q_ASSERT(parent);
332 // Enable accessibility for items with accessible content. This also
333 // enables accessibility for the ancestors of such items.
334 auto item = qobject_cast<QQuickItem *>(parent);
335 if (item) {
336 item->d_func()->setAccessible();
337 } else {
338 const QLatin1StringView className(QQmlData::ensurePropertyCache(parent)->firstCppMetaObject()->className());
339 if (className != QLatin1StringView("QQuickAction")) {
340 qmlWarning(parent) << "Accessible attached property must be attached to an object deriving from Item or Action";
341 return;
342 }
343 }
344 QAccessibleEvent ev(parent, QAccessible::ObjectCreated);
345 QAccessible::updateAccessibility(&ev);
346
347 if (const QMetaObject *pmo = parent->metaObject()) {
348 auto connectPropertyChangeSignal = [parent, pmo, this](
349 const char *propertyName, const char *signalName, int slotIndex)
350 {
351 // basically does this:
352 // if the parent has the property \a propertyName with the associated \a signalName:
353 // connect(parent, signalName, this, slotIndex)
354
355 // Note that we explicitly want to only connect to standard property/signal naming
356 // convention: "value" & "valueChanged"
357 // (e.g. avoid a compound property with e.g. a signal notifier named "updated()")
358 int idxProperty = pmo->indexOfProperty(propertyName);
359 if (idxProperty != -1) {
360 const QMetaProperty property = pmo->property(idxProperty);
361 const QMetaMethod signal = property.notifySignal();
362 if (signal.name() == signalName)
363 QMetaObject::connect(parent, signal.methodIndex(), this, slotIndex);
364 }
365 return;
366 };
367 const QMetaObject &smo = staticMetaObject;
368
369 QAccessibleInterface *iface = ev.accessibleInterface();
370 if (iface && iface->valueInterface()) {
371 static const int valueChangedIndex = smo.indexOfSlot("valueChanged()");
372 connectPropertyChangeSignal("value", "valueChanged", valueChangedIndex);
373 }
374
375 if (iface && iface->textInterface()) {
376 static const int cursorPositionChangedIndex =
377 smo.indexOfSlot("cursorPositionChanged()");
378 connectPropertyChangeSignal("cursorPosition", "cursorPositionChanged",
379 cursorPositionChangedIndex);
380 }
381 }
382
383 if (!sigPress.isValid()) {
384 sigPress = QMetaMethod::fromSignal(&QQuickAccessibleAttached::pressAction);
385 sigToggle = QMetaMethod::fromSignal(&QQuickAccessibleAttached::toggleAction);
386 sigIncrease = QMetaMethod::fromSignal(&QQuickAccessibleAttached::increaseAction);
387 sigDecrease = QMetaMethod::fromSignal(&QQuickAccessibleAttached::decreaseAction);
388 sigScrollUp = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollUpAction);
389 sigScrollDown = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollDownAction);
390 sigScrollLeft = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollLeftAction);
391 sigScrollRight = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollRightAction);
392 sigPreviousPage = QMetaMethod::fromSignal(&QQuickAccessibleAttached::previousPageAction);
393 sigNextPage= QMetaMethod::fromSignal(&QQuickAccessibleAttached::nextPageAction);
394 }
395}
396
397QQuickAccessibleAttached::~QQuickAccessibleAttached()
398{
399}
400
401void QQuickAccessibleAttached::setRole(QAccessible::Role role)
402{
403 if (role != m_role) {
404 m_role = role;
405 Q_EMIT roleChanged();
406 // There is no way to signify role changes at the moment.
407 // QAccessible::updateAccessibility(parent(), 0, QAccessible::);
408
409 switch (role) {
410 case QAccessible::CheckBox:
411 case QAccessible::RadioButton:
412 case QAccessible::Switch:
413 if (!m_stateExplicitlySet.focusable)
414 m_state.focusable = true;
415 if (!m_stateExplicitlySet.checkable)
416 m_state.checkable = true;
417 break;
418 case QAccessible::Button:
419 case QAccessible::MenuItem:
420 case QAccessible::PageTab:
421 case QAccessible::SpinBox:
422 case QAccessible::ComboBox:
423 case QAccessible::Terminal:
424 case QAccessible::ScrollBar:
425 if (!m_stateExplicitlySet.focusable)
426 m_state.focusable = true;
427 break;
428 case QAccessible::EditableText:
429 if (!m_stateExplicitlySet.editable)
430 m_state.editable = true;
431 if (!m_stateExplicitlySet.focusable)
432 m_state.focusable = true;
433 break;
434 case QAccessible::StaticText:
435 if (!m_stateExplicitlySet.readOnly)
436 m_state.readOnly = true;
437 if (!m_stateExplicitlySet.focusable)
438 m_state.focusable = true;
439 break;
440 default:
441 break;
442 }
443 }
444}
445
446bool QQuickAccessibleAttached::wasNameExplicitlySet() const
447{
448 return m_nameExplicitlySet;
449}
450
451// Allows types to attach an accessible name to an item as a convenience,
452// so long as the user hasn't done so themselves.
453void QQuickAccessibleAttached::setNameImplicitly(const QString &name)
454{
455 setName(name);
456 m_nameExplicitlySet = false;
457}
458
459void QQuickAccessibleAttached::setDescriptionImplicitly(const QString &desc)
460{
461 if (m_descriptionExplicitlySet)
462 return;
463 setDescription(desc);
464 m_descriptionExplicitlySet = false;
465}
466
467QQuickAccessibleAttached *QQuickAccessibleAttached::qmlAttachedProperties(QObject *obj)
468{
469 return new QQuickAccessibleAttached(obj);
470}
471
472bool QQuickAccessibleAttached::ignored() const
473{
474 auto item = qobject_cast<QQuickItem *>(parent());
475 return item ? !item->d_func()->isAccessible : false;
476}
477
478void QQuickAccessibleAttached::setIgnored(bool ignored)
479{
480 auto item = qobject_cast<QQuickItem *>(parent());
481 if (item && this->ignored() != ignored) {
482 item->d_func()->isAccessible = !ignored;
483 QAccessibleEvent event(item,
484 ignored ? QAccessible::ObjectDestroyed : QAccessible::ObjectCreated);
485 QAccessible::updateAccessibility(&event);
486 emit ignoredChanged();
487 }
488}
489
490bool QQuickAccessibleAttached::doAction(const QString &actionName)
491{
492 QMetaMethod *sig = nullptr;
493 if (actionName == QAccessibleActionInterface::pressAction())
494 sig = &sigPress;
495 else if (actionName == QAccessibleActionInterface::toggleAction())
496 sig = &sigToggle;
497 else if (actionName == QAccessibleActionInterface::increaseAction())
498 sig = &sigIncrease;
499 else if (actionName == QAccessibleActionInterface::decreaseAction())
500 sig = &sigDecrease;
501 else if (actionName == QAccessibleActionInterface::scrollUpAction())
502 sig = &sigScrollUp;
503 else if (actionName == QAccessibleActionInterface::scrollDownAction())
504 sig = &sigScrollDown;
505 else if (actionName == QAccessibleActionInterface::scrollLeftAction())
506 sig = &sigScrollLeft;
507 else if (actionName == QAccessibleActionInterface::scrollRightAction())
508 sig = &sigScrollRight;
509 else if (actionName == QAccessibleActionInterface::previousPageAction())
510 sig = &sigPreviousPage;
511 else if (actionName == QAccessibleActionInterface::nextPageAction())
512 sig = &sigNextPage;
513 if (sig && isSignalConnected(*sig)) {
514 bool ret = false;
515 if (m_proxying)
516 ret = sig->invoke(m_proxying);
517 if (!ret)
518 ret = sig->invoke(this);
519 return ret;
520 }
521 return false;
522}
523
524void QQuickAccessibleAttached::availableActions(QStringList *actions) const
525{
526 if (isSignalConnected(sigPress))
527 actions->append(QAccessibleActionInterface::pressAction());
528 if (isSignalConnected(sigToggle))
529 actions->append(QAccessibleActionInterface::toggleAction());
530 if (isSignalConnected(sigIncrease))
531 actions->append(QAccessibleActionInterface::increaseAction());
532 if (isSignalConnected(sigDecrease))
533 actions->append(QAccessibleActionInterface::decreaseAction());
534 if (isSignalConnected(sigScrollUp))
535 actions->append(QAccessibleActionInterface::scrollUpAction());
536 if (isSignalConnected(sigScrollDown))
537 actions->append(QAccessibleActionInterface::scrollDownAction());
538 if (isSignalConnected(sigScrollLeft))
539 actions->append(QAccessibleActionInterface::scrollLeftAction());
540 if (isSignalConnected(sigScrollRight))
541 actions->append(QAccessibleActionInterface::scrollRightAction());
542 if (isSignalConnected(sigPreviousPage))
543 actions->append(QAccessibleActionInterface::previousPageAction());
544 if (isSignalConnected(sigNextPage))
545 actions->append(QAccessibleActionInterface::nextPageAction());
546}
547
548QString QQuickAccessibleAttached::stripHtml(const QString &html)
549{
550#ifndef QT_NO_TEXTHTMLPARSER
551 QTextDocument doc;
552 doc.setHtml(html);
553 return doc.toPlainText();
554#else
555 return html;
556#endif
557}
558
559void QQuickAccessibleAttached::setProxying(QQuickAccessibleAttached *proxying)
560{
561 if (proxying == m_proxying)
562 return;
563
564 const QMetaObject &mo = staticMetaObject;
565 if (m_proxying) {
566 // We disconnect all signals from the proxy into this object
567 auto mo = m_proxying->metaObject();
568 auto propertyCache = QQmlData::ensurePropertyCache(m_proxying);
569 for (int signalIndex = propertyCache->signalOffset();
570 signalIndex < propertyCache->signalCount(); ++signalIndex) {
571 const QMetaMethod m = mo->method(propertyCache->signal(signalIndex)->coreIndex());
572 Q_ASSERT(m.methodType() == QMetaMethod::Signal);
573 if (m.methodType() != QMetaMethod::Signal)
574 continue;
575
576 disconnect(m_proxying, m, this, m);
577 }
578 }
579
580 m_proxying = proxying;
581
582 if (m_proxying) {
583 // We connect all signals from the proxy into this object
584 auto propertyCache = QQmlData::ensurePropertyCache(m_proxying);
585 auto mo = m_proxying->metaObject();
586 for (int signalIndex = propertyCache->signalOffset();
587 signalIndex < propertyCache->signalCount(); ++signalIndex) {
588 const QMetaMethod m = mo->method(propertyCache->signal(signalIndex)->coreIndex());
589 Q_ASSERT(m.methodType() == QMetaMethod::Signal);
590 connect(proxying, m, this, m);
591 }
592 }
593
594 // We check all properties
595 for (int prop = mo.propertyOffset(); prop < mo.propertyCount(); ++prop) {
596 const QMetaProperty p = mo.property(prop);
597 if (!p.hasNotifySignal()) {
598 continue;
599 }
600
601 const QMetaMethod signal = p.notifySignal();
602 if (signal.parameterCount() == 0)
603 signal.invoke(this);
604 else
605 signal.invoke(this, Q_ARG(bool, p.read(this).toBool()));
606 }
607}
608
609/*!
610 * \qmlmethod void QtQuick::Accessible::announce(string message, AnnouncementPoliteness politeness)
611 *
612 * \since 6.8
613 * Issues an announcement event with a \a message with politeness \a politeness.
614 *
615 * \sa QAccessibleAnnouncementEvent
616 */
617void QQuickAccessibleAttached::announce(const QString &message, QAccessible::AnnouncementPoliteness politeness)
618{
619 QAccessibleAnnouncementEvent event(parent(), message);
620 event.setPoliteness(politeness);
621 QAccessible::updateAccessibility(&event);
622}
623
624QQuickItem *QQuickAccessibleAttached::findRelation(QAccessible::Relation relation) const
625{
626 const auto it = std::find_if(m_relations.cbegin(), m_relations.cend(),
627 [relation](const auto &rel) {
628 return rel.second == relation;
629 });
630
631 return it != m_relations.cend() ? it->first : nullptr;
632}
633
634QT_END_NAMESPACE
635
636#include "moc_qquickaccessibleattached_p.cpp"
637
638#endif