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
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
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 Button, \c MenuItem, \c PageTab, \c EditableText, \c SpinBox, \c ComboBox,
138 \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 if (!m_stateExplicitlySet.focusable)
413 m_state.focusable = true;
414 if (!m_stateExplicitlySet.checkable)
415 m_state.checkable = true;
416 break;
417 case QAccessible::Button:
418 case QAccessible::MenuItem:
419 case QAccessible::PageTab:
420 case QAccessible::SpinBox:
421 case QAccessible::ComboBox:
422 case QAccessible::Terminal:
423 case QAccessible::ScrollBar:
424 if (!m_stateExplicitlySet.focusable)
425 m_state.focusable = true;
426 break;
427 case QAccessible::EditableText:
428 if (!m_stateExplicitlySet.editable)
429 m_state.editable = true;
430 if (!m_stateExplicitlySet.focusable)
431 m_state.focusable = true;
432 break;
433 case QAccessible::StaticText:
434 if (!m_stateExplicitlySet.readOnly)
435 m_state.readOnly = true;
436 if (!m_stateExplicitlySet.focusable)
437 m_state.focusable = true;
438 break;
439 default:
440 break;
441 }
442 }
443}
444
445bool QQuickAccessibleAttached::wasNameExplicitlySet() const
446{
447 return m_nameExplicitlySet;
448}
449
450// Allows types to attach an accessible name to an item as a convenience,
451// so long as the user hasn't done so themselves.
452void QQuickAccessibleAttached::setNameImplicitly(const QString &name)
453{
454 setName(name);
455 m_nameExplicitlySet = false;
456}
457
458void QQuickAccessibleAttached::setDescriptionImplicitly(const QString &desc)
459{
460 if (m_descriptionExplicitlySet)
461 return;
462 setDescription(desc);
463 m_descriptionExplicitlySet = false;
464}
465
466QQuickAccessibleAttached *QQuickAccessibleAttached::qmlAttachedProperties(QObject *obj)
467{
468 return new QQuickAccessibleAttached(obj);
469}
470
471bool QQuickAccessibleAttached::ignored() const
472{
473 auto item = qobject_cast<QQuickItem *>(parent());
474 return item ? !item->d_func()->isAccessible : false;
475}
476
477void QQuickAccessibleAttached::setIgnored(bool ignored)
478{
479 auto item = qobject_cast<QQuickItem *>(parent());
480 if (item && this->ignored() != ignored) {
481 item->d_func()->isAccessible = !ignored;
482 QAccessibleEvent event(item,
483 ignored ? QAccessible::ObjectDestroyed : QAccessible::ObjectCreated);
484 QAccessible::updateAccessibility(&event);
485 emit ignoredChanged();
486 }
487}
488
489bool QQuickAccessibleAttached::doAction(const QString &actionName)
490{
491 QMetaMethod *sig = nullptr;
492 if (actionName == QAccessibleActionInterface::pressAction())
493 sig = &sigPress;
494 else if (actionName == QAccessibleActionInterface::toggleAction())
495 sig = &sigToggle;
496 else if (actionName == QAccessibleActionInterface::increaseAction())
497 sig = &sigIncrease;
498 else if (actionName == QAccessibleActionInterface::decreaseAction())
499 sig = &sigDecrease;
500 else if (actionName == QAccessibleActionInterface::scrollUpAction())
501 sig = &sigScrollUp;
502 else if (actionName == QAccessibleActionInterface::scrollDownAction())
503 sig = &sigScrollDown;
504 else if (actionName == QAccessibleActionInterface::scrollLeftAction())
505 sig = &sigScrollLeft;
506 else if (actionName == QAccessibleActionInterface::scrollRightAction())
507 sig = &sigScrollRight;
508 else if (actionName == QAccessibleActionInterface::previousPageAction())
509 sig = &sigPreviousPage;
510 else if (actionName == QAccessibleActionInterface::nextPageAction())
511 sig = &sigNextPage;
512 if (sig && isSignalConnected(*sig)) {
513 bool ret = false;
514 if (m_proxying)
515 ret = sig->invoke(m_proxying);
516 if (!ret)
517 ret = sig->invoke(this);
518 return ret;
519 }
520 return false;
521}
522
523void QQuickAccessibleAttached::availableActions(QStringList *actions) const
524{
525 if (isSignalConnected(sigPress))
526 actions->append(QAccessibleActionInterface::pressAction());
527 if (isSignalConnected(sigToggle))
528 actions->append(QAccessibleActionInterface::toggleAction());
529 if (isSignalConnected(sigIncrease))
530 actions->append(QAccessibleActionInterface::increaseAction());
531 if (isSignalConnected(sigDecrease))
532 actions->append(QAccessibleActionInterface::decreaseAction());
533 if (isSignalConnected(sigScrollUp))
534 actions->append(QAccessibleActionInterface::scrollUpAction());
535 if (isSignalConnected(sigScrollDown))
536 actions->append(QAccessibleActionInterface::scrollDownAction());
537 if (isSignalConnected(sigScrollLeft))
538 actions->append(QAccessibleActionInterface::scrollLeftAction());
539 if (isSignalConnected(sigScrollRight))
540 actions->append(QAccessibleActionInterface::scrollRightAction());
541 if (isSignalConnected(sigPreviousPage))
542 actions->append(QAccessibleActionInterface::previousPageAction());
543 if (isSignalConnected(sigNextPage))
544 actions->append(QAccessibleActionInterface::nextPageAction());
545}
546
547QString QQuickAccessibleAttached::stripHtml(const QString &html)
548{
549#ifndef QT_NO_TEXTHTMLPARSER
550 QTextDocument doc;
551 doc.setHtml(html);
552 return doc.toPlainText();
553#else
554 return html;
555#endif
556}
557
558void QQuickAccessibleAttached::setProxying(QQuickAccessibleAttached *proxying)
559{
560 if (proxying == m_proxying)
561 return;
562
563 const QMetaObject &mo = staticMetaObject;
564 if (m_proxying) {
565 // We disconnect all signals from the proxy into this object
566 auto mo = m_proxying->metaObject();
567 auto propertyCache = QQmlData::ensurePropertyCache(m_proxying);
568 for (int signalIndex = propertyCache->signalOffset();
569 signalIndex < propertyCache->signalCount(); ++signalIndex) {
570 const QMetaMethod m = mo->method(propertyCache->signal(signalIndex)->coreIndex());
571 Q_ASSERT(m.methodType() == QMetaMethod::Signal);
572 if (m.methodType() != QMetaMethod::Signal)
573 continue;
574
575 disconnect(m_proxying, m, this, m);
576 }
577 }
578
579 m_proxying = proxying;
580
581 if (m_proxying) {
582 // We connect all signals from the proxy into this object
583 auto propertyCache = QQmlData::ensurePropertyCache(m_proxying);
584 auto mo = m_proxying->metaObject();
585 for (int signalIndex = propertyCache->signalOffset();
586 signalIndex < propertyCache->signalCount(); ++signalIndex) {
587 const QMetaMethod m = mo->method(propertyCache->signal(signalIndex)->coreIndex());
588 Q_ASSERT(m.methodType() == QMetaMethod::Signal);
589 connect(proxying, m, this, m);
590 }
591 }
592
593 // We check all properties
594 for (int prop = mo.propertyOffset(); prop < mo.propertyCount(); ++prop) {
595 const QMetaProperty p = mo.property(prop);
596 if (!p.hasNotifySignal()) {
597 continue;
598 }
599
600 const QMetaMethod signal = p.notifySignal();
601 if (signal.parameterCount() == 0)
602 signal.invoke(this);
603 else
604 signal.invoke(this, Q_ARG(bool, p.read(this).toBool()));
605 }
606}
607
608/*!
609 * \qmlmethod void QtQuick::Accessible::announce(string message, AnnouncementPoliteness politeness)
610 *
611 * \since 6.8
612 * Issues an announcement event with a \a message with politeness \a politeness.
613 *
614 * \sa QAccessibleAnnouncementEvent
615 */
616void QQuickAccessibleAttached::announce(const QString &message, QAccessible::AnnouncementPoliteness politeness)
617{
618 QAccessibleAnnouncementEvent event(parent(), message);
619 event.setPoliteness(politeness);
620 QAccessible::updateAccessibility(&event);
621}
622
623QQuickItem *QQuickAccessibleAttached::findRelation(QAccessible::Relation relation) const
624{
625 const auto it = std::find_if(m_relations.cbegin(), m_relations.cend(),
626 [relation](const auto &rel) {
627 return rel.second == relation;
628 });
629
630 return it != m_relations.cend() ? it->first : nullptr;
631}
632
633QT_END_NAMESPACE
634
635#include "moc_qquickaccessibleattached_p.cpp"
636
637#endif