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
qaccessiblequickitem.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#include <QtGui/private/qaccessiblehelper_p.h>
8#include <QtGui/qguiapplication.h>
9#include <QtGui/qtextdocument.h>
10
11#include "QtQuick/private/qquickitem_p.h"
12#include "QtQuick/private/qquicktext_p.h"
13#include <private/qquicktext_p_p.h>
14
15#include "QtQuick/private/qquicktextinput_p.h"
16#include "QtQuick/private/qquickaccessibleattached_p.h"
17#include "QtQuick/qquicktextdocument.h"
18#include "QtQuick/qquickrendercontrol.h"
19QT_BEGIN_NAMESPACE
20
21#if QT_CONFIG(accessibility)
22
23class QAccessibleHyperlink : public QAccessibleInterface, public QAccessibleHyperlinkInterface {
24public:
25 QAccessibleHyperlink(QQuickItem *parentTextItem, int linkIndex);
26
27 // check for valid pointers
28 bool isValid() const override;
29 QObject *object() const override;
30 QWindow *window() const override;
31
32 // navigation, hierarchy
33 QAccessibleInterface *parent() const override;
34 QAccessibleInterface *child(int index) const override;
35 int childCount() const override;
36 int indexOfChild(const QAccessibleInterface *iface) const override;
37 QAccessibleInterface *childAt(int x, int y) const override;
38
39 // properties and state
40 QString text(QAccessible::Text) const override;
41 void setText(QAccessible::Text, const QString &text) override;
42 QRect rect() const override;
43 QAccessible::Role role() const override;
44 QAccessible::State state() const override;
45
46 void *interface_cast(QAccessible::InterfaceType t) override;
47
48 // QAccessibleHyperlinkInterface
49 QString anchor() const override
50 {
51 const QList<QQuickTextPrivate::LinkDesc> links = QQuickTextPrivate::get(textItem())->getLinks();
52 if (linkIndex < links.size())
53 return links.at(linkIndex).m_anchor;
54 return QString();
55 }
56
57 QString anchorTarget() const override
58 {
59 const QList<QQuickTextPrivate::LinkDesc> links = QQuickTextPrivate::get(textItem())->getLinks();
60 if (linkIndex < links.size())
61 return links.at(linkIndex).m_anchorTarget;
62 return QString();
63 }
64
65 int startIndex() const override
66 {
67 const QList<QQuickTextPrivate::LinkDesc> links = QQuickTextPrivate::get(textItem())->getLinks();
68 if (linkIndex < links.size())
69 return links.at(linkIndex).m_startIndex;
70 return -1;
71 }
72
73 int endIndex() const override
74 {
75 const QList<QQuickTextPrivate::LinkDesc> links = QQuickTextPrivate::get(textItem())->getLinks();
76 if (linkIndex < links.size())
77 return links.at(linkIndex).m_endIndex;
78 return -1;
79 }
80
81private:
82 QQuickText *textItem() const { return qobject_cast<QQuickText*>(parentTextItem); }
83 QQuickItem *parentTextItem;
84 const int linkIndex;
85
86 friend class QAccessibleQuickItem;
87};
88
89
90QAccessibleHyperlink::QAccessibleHyperlink(QQuickItem *parentTextItem, int linkIndex)
91 : parentTextItem(parentTextItem),
92 linkIndex(linkIndex)
93{
94}
95
96
97bool QAccessibleHyperlink::isValid() const
98{
99 return textItem();
100}
101
102
103QObject *QAccessibleHyperlink::object() const
104{
105 return nullptr;
106}
107
108
109QWindow *QAccessibleHyperlink::window() const
110{
111 return textItem()->window();
112}
113
114
115/* \reimp */
116QRect QAccessibleHyperlink::rect() const
117{
118 const QList<QQuickTextPrivate::LinkDesc> links = QQuickTextPrivate::get(textItem())->getLinks();
119 if (linkIndex < links.size()) {
120 const QPoint tl = itemScreenRect(textItem()).topLeft();
121 return links.at(linkIndex).rect.translated(tl);
122 }
123 return QRect();
124}
125
126/* \reimp */
127QAccessibleInterface *QAccessibleHyperlink::childAt(int, int) const
128{
129 return nullptr;
130}
131
132/* \reimp */
133QAccessibleInterface *QAccessibleHyperlink::parent() const
134{
135 return QAccessible::queryAccessibleInterface(textItem());
136}
137
138/* \reimp */
139QAccessibleInterface *QAccessibleHyperlink::child(int) const
140{
141 return nullptr;
142}
143
144/* \reimp */
145int QAccessibleHyperlink::childCount() const
146{
147 return 0;
148}
149
150/* \reimp */
151int QAccessibleHyperlink::indexOfChild(const QAccessibleInterface *) const
152{
153 return -1;
154}
155
156/* \reimp */
157QAccessible::State QAccessibleHyperlink::state() const
158{
159 QAccessible::State s;
160 s.selectable = true;
161 s.focusable = true;
162 s.selectableText = true;
163 s.selected = false;
164 return s;
165}
166
167/* \reimp */
168QAccessible::Role QAccessibleHyperlink::role() const
169{
170 return QAccessible::Link;
171}
172
173/* \reimp */
174QString QAccessibleHyperlink::text(QAccessible::Text t) const
175{
176 // AT servers have different behaviors:
177 // Wordpad on windows have this behavior:
178 // * Name returns the anchor target (URL)
179 // * Value returns the anchor target (URL)
180
181 // Other AT servers (e.g. MS Edge on Windows) does what seems to be more sensible:
182 // * Name returns the anchor name
183 // * Value returns the anchor target (URL)
184 if (t == QAccessible::Name)
185 return anchor();
186 if (t == QAccessible::Value)
187 return anchorTarget();
188 return QString();
189}
190
191/* \reimp */
192void QAccessibleHyperlink::setText(QAccessible::Text, const QString &)
193{
194
195}
196
197/* \reimp */
198void *QAccessibleHyperlink::interface_cast(QAccessible::InterfaceType t)
199{
200 if (t == QAccessible::HyperlinkInterface)
201 return static_cast<QAccessibleHyperlinkInterface*>(this);
202 return nullptr;
203}
204
205
206/*!
207 * \internal
208 * \brief QAccessibleQuickItem::QAccessibleQuickItem
209 * \param item
210 */
211QAccessibleQuickItem::QAccessibleQuickItem(QQuickItem *item)
212 : QAccessibleObject(item), m_doc(textDocument())
213{
214}
215
216bool QAccessibleQuickItem::isValid() const
217{
218 return item() && !QQuickItemPrivate::get(item())->inDestructor;
219}
220
221
222QWindow *QAccessibleQuickItem::window() const
223{
224 QQuickWindow *window = item()->window();
225
226 // For QQuickWidget the above window will be the offscreen QQuickWindow,
227 // which is not a part of the accessibility tree. Detect this case and
228 // return the window for the QQuickWidget instead.
229 if (window && !window->handle()) {
230 if (QQuickRenderControl *renderControl = QQuickWindowPrivate::get(window)->renderControl) {
231 if (QWindow *renderWindow = renderControl->renderWindow(nullptr))
232 return renderWindow;
233 }
234 }
235
236 return window;
237}
238
239int QAccessibleQuickItem::childCount() const
240{
241 // see comment in QAccessibleQuickItem::child() as to why we do this
242 int cc = 0;
243 if (QQuickText *textItem = qobject_cast<QQuickText*>(item())) {
244 cc = QQuickTextPrivate::get(textItem)->getLinks().size();
245 }
246 cc += childItems().size();
247 return cc;
248}
249
250QRect QAccessibleQuickItem::rect() const
251{
252 const QRect r = itemScreenRect(item());
253 return r;
254}
255
256QRect QAccessibleQuickItem::viewRect() const
257{
258 // ### no window in some cases.
259 if (!item()->window()) {
260 return QRect();
261 }
262
263 QQuickWindow *window = item()->window();
264 QPoint screenPos = window->mapToGlobal(QPoint(0,0));
265 return QRect(screenPos, window->size());
266}
267
268
269bool QAccessibleQuickItem::clipsChildren() const
270{
271 return static_cast<QQuickItem *>(item())->clip();
272}
273
274QAccessibleInterface *QAccessibleQuickItem::childAt(int x, int y) const
275{
276 if (item()->clip()) {
277 if (!rect().contains(x, y))
278 return nullptr;
279 }
280
281 // special case for text interfaces
282 if (QQuickText *textItem = qobject_cast<QQuickText*>(item())) {
283 const auto hyperLinkChildCount = QQuickTextPrivate::get(textItem)->getLinks().size();
284 for (auto i = 0; i < hyperLinkChildCount; i++) {
285 QAccessibleInterface *iface = child(i);
286 if (iface->rect().contains(x,y)) {
287 return iface;
288 }
289 }
290 }
291
292 // general item hit test
293 const QList<QQuickItem*> kids = accessibleUnignoredChildren(item(), true);
294 for (int i = kids.size() - 1; i >= 0; --i) {
295 QAccessibleInterface *childIface = QAccessible::queryAccessibleInterface(kids.at(i));
296 if (QAccessibleInterface *childChild = childIface->childAt(x, y))
297 return childChild;
298 if (childIface && !childIface->state().invisible) {
299 if (childIface->rect().contains(x, y))
300 return childIface;
301 }
302 }
303
304 return nullptr;
305}
306
307QAccessibleInterface *QAccessibleQuickItem::parent() const
308{
309 QQuickItem *parent = item()->parentItem();
310 QQuickWindow *itemWindow = item()->window();
311 QQuickItem *ci = itemWindow ? itemWindow->contentItem() : nullptr;
312 while (parent && !QQuickItemPrivate::get(parent)->isAccessible && parent != ci)
313 parent = parent->parentItem();
314
315 if (parent) {
316 if (parent == ci) {
317 if (auto *iface = itemWindow->accessibleRoot())
318 return iface;
319 // Jump out to the window if the parent is the root item
320 return QAccessible::queryAccessibleInterface(window());
321 } else {
322 while (parent && !parent->d_func()->isAccessible)
323 parent = parent->parentItem();
324 return QAccessible::queryAccessibleInterface(parent);
325 }
326 }
327 return nullptr;
328}
329
330QAccessibleInterface *QAccessibleQuickItem::child(int index) const
331{
332 /* Text with hyperlinks will have dedicated children interfaces representing each hyperlink.
333
334 For the pathological case when a Text node has hyperlinks in its text *and* accessible
335 quick items as children, we put the hyperlink a11y interfaces as the first children, then
336 the other interfaces follows the hyperlink children (as siblings).
337
338 For example, suppose you have two links in the text and an image as a child of the text,
339 it will have the following a11y hierarchy:
340
341 [a11y:TextInterface]
342 |
343 +- [a11y:HyperlinkInterface]
344 +- [a11y:HyperlinkInterface]
345 +- [a11y:ImageInterface]
346
347 Having this order (as opposed to having hyperlink interfaces last) will at least
348 ensure that the child id of hyperlink children is not altered when child is added/removed
349 to the text item and marked accessible.
350 In addition, hyperlink interfaces as children should be the common case, so it is preferred
351 to explore those first when iterating.
352 */
353 if (index < 0)
354 return nullptr;
355
356
357 if (QQuickText *textItem = qobject_cast<QQuickText*>(item())) {
358 const int hyperLinkChildCount = QQuickTextPrivate::get(textItem)->getLinks().size();
359 if (index < hyperLinkChildCount) {
360 auto it = m_childToId.constFind(index);
361 if (it != m_childToId.constEnd())
362 return QAccessible::accessibleInterface(it.value());
363
364 QAccessibleHyperlink *iface = new QAccessibleHyperlink(item(), index);
365 QAccessible::Id id = QAccessible::registerAccessibleInterface(iface);
366 m_childToId.insert(index, id);
367 return iface;
368 }
369 index -= hyperLinkChildCount;
370 }
371
372 QList<QQuickItem *> children = childItems();
373 if (index < children.size()) {
374 QQuickItem *child = children.at(index);
375 return QAccessible::queryAccessibleInterface(child);
376 }
377 return nullptr;
378}
379
380int QAccessibleQuickItem::indexOfChild(const QAccessibleInterface *iface) const
381{
382 int hyperLinkChildCount = 0;
383 if (QQuickText *textItem = qobject_cast<QQuickText*>(item())) {
384 hyperLinkChildCount = QQuickTextPrivate::get(textItem)->getLinks().size();
385 if (QAccessibleHyperlinkInterface *hyperLinkIface = const_cast<QAccessibleInterface *>(iface)->hyperlinkInterface()) {
386 // ### assumes that there is only one subclass implementing QAccessibleHyperlinkInterface
387 // Alternatively, we could simply iterate with child() and do a linear search for it
388 QAccessibleHyperlink *hyperLink = static_cast<QAccessibleHyperlink*>(hyperLinkIface);
389 if (hyperLink->textItem() == static_cast<QQuickText*>(item())) {
390 return hyperLink->linkIndex;
391 }
392 }
393 }
394 QList<QQuickItem*> kids = childItems();
395 int idx = kids.indexOf(static_cast<QQuickItem*>(iface->object()));
396 if (idx >= 0)
397 idx += hyperLinkChildCount;
398 return idx;
399}
400
401static void unignoredChildren(QQuickItem *item, QList<QQuickItem *> *items, bool paintOrder)
402{
403 const QList<QQuickItem*> childItems = paintOrder ? QQuickItemPrivate::get(item)->paintOrderChildItems()
404 : item->childItems();
405 for (QQuickItem *child : childItems) {
406 if (QQuickItemPrivate::get(child)->isAccessible) {
407 items->append(child);
408 } else {
409 unignoredChildren(child, items, paintOrder);
410 }
411 }
412}
413
414QList<QQuickItem *> accessibleUnignoredChildren(QQuickItem *item, bool paintOrder)
415{
416 QList<QQuickItem *> items;
417 unignoredChildren(item, &items, paintOrder);
418 return items;
419}
420
421QList<QQuickItem *> QAccessibleQuickItem::childItems() const
422{
423 return accessibleUnignoredChildren(item());
424}
425
426static bool isTextRole(QAccessible::Role role)
427{
428 return role == QAccessible::EditableText || role == QAccessible::StaticText;
429}
430
431QAccessible::State QAccessibleQuickItem::state() const
432{
433 QAccessible::State state;
434 if (QQuickAccessibleAttached *attached = QQuickAccessibleAttached::attachedProperties(item()))
435 state = attached->state();
436
437 QRect viewRect_ = viewRect();
438 QRect itemRect = rect();
439
440 if (viewRect_.isNull() || itemRect.isNull() || !window() || !window()->isVisible() ||!item()->isVisible() || qFuzzyIsNull(item()->opacity()))
441 state.invisible = true;
442 if (!viewRect_.intersects(itemRect))
443 state.offscreen = true;
444 if ((role() == QAccessible::CheckBox
445 || role() == QAccessible::RadioButton
446 || role() == QAccessible::Switch)
447 && object()->property("checked").toBool())
448 state.checked = true;
449 if (role() == QAccessible::Button && object()->property("expandable").toBool()) {
450 state.expandable = true;
451 state.expanded = object()->property("expanded").toBool();
452 }
453 if (item()->activeFocusOnTab() || isTextRole(role()))
454 state.focusable = true;
455 if (item()->hasActiveFocus())
456 state.focused = true;
457 if (role() == QAccessible::EditableText)
458 if (auto ti = qobject_cast<QQuickTextInput *>(item()))
459 state.passwordEdit = ti->echoMode() != QQuickTextInput::Normal;
460 if (!item()->isEnabled()) {
461 state.focusable = false;
462 state.disabled = true;
463 }
464 if (item()->property("readOnly").toBool() || role() == QAccessible::ProgressBar)
465 state.readOnly = true;
466 return state;
467}
468
469QList<std::pair<QAccessibleInterface *, QAccessible::Relation>>
470QAccessibleQuickItem::relations(QAccessible::Relation match) const
471{
472 QList<std::pair<QAccessibleInterface *, QAccessible::Relation>> rels =
473 QAccessibleObject::relations(match);
474 if (QQuickAccessibleAttached *attached = QQuickAccessibleAttached::attachedProperties(item())) {
475 if (match & QAccessible::Labelled) {
476 if (auto *labelFor = attached->labelFor()) {
477 rels.append({QAccessible::queryAccessibleInterface(labelFor),
478 QAccessible::Labelled});
479 }
480 }
481
482 if (match & QAccessible::Label) {
483 if (auto *labelledBy = attached->labelledBy()) {
484 rels.append({QAccessible::queryAccessibleInterface(labelledBy),
485 QAccessible::Label});
486 }
487 }
488 }
489 return rels;
490}
491
492QAccessible::Role QAccessibleQuickItem::role() const
493{
494 // Workaround for setAccessibleRole() not working for
495 // Text items. Text items are special since they are defined
496 // entirely from C++ (setting the role from QML works.)
497
498 QAccessible::Role role = QAccessible::NoRole;
499 if (item())
500 role = QQuickItemPrivate::get(item())->effectiveAccessibleRole();
501 if (role == QAccessible::NoRole) {
502 if (qobject_cast<QQuickText*>(const_cast<QQuickItem *>(item())))
503 role = QAccessible::StaticText;
504 else if (qobject_cast<QQuickTextInput*>(const_cast<QQuickItem *>(item())))
505 role = QAccessible::EditableText;
506 else
507 role = QAccessible::Client;
508 }
509
510 return role;
511}
512
513bool QAccessibleQuickItem::isAccessible() const
514{
515 return item()->d_func()->isAccessible;
516}
517
518QStringList QAccessibleQuickItem::actionNames() const
519{
520 QStringList actions;
521 switch (role()) {
522 case QAccessible::Button:
523 if (state().expandable)
524 actions << QAccessibleActionInterface::toggleAction();
525 Q_FALLTHROUGH();
526 case QAccessible::Link:
527 case QAccessible::MenuItem:
528 actions << QAccessibleActionInterface::pressAction();
529 break;
530 case QAccessible::RadioButton:
531 case QAccessible::CheckBox:
532 case QAccessible::Switch:
533 actions << QAccessibleActionInterface::toggleAction()
534 << QAccessibleActionInterface::pressAction();
535 break;
536 case QAccessible::Slider:
537 case QAccessible::SpinBox:
538 case QAccessible::ScrollBar:
539 actions << QAccessibleActionInterface::increaseAction()
540 << QAccessibleActionInterface::decreaseAction();
541 break;
542 default:
543 break;
544 }
545 if (state().focusable)
546 actions.append(QAccessibleActionInterface::setFocusAction());
547
548 // ### The following can lead to duplicate action names.
549 if (QQuickAccessibleAttached *attached = QQuickAccessibleAttached::attachedProperties(item()))
550 attached->availableActions(&actions);
551
552 if (QQuickItemPrivate::get(item())->contextMenu()
553 && !actions.contains(QAccessibleActionInterface::showMenuAction())) {
554 actions.append(QAccessibleActionInterface::showMenuAction());
555 }
556
557 return actions;
558}
559
560void QAccessibleQuickItem::doAction(const QString &actionName)
561{
562 bool accepted = false;
563 if (actionName == QAccessibleActionInterface::setFocusAction()) {
564 item()->forceActiveFocus();
565 accepted = true;
566 }
567 if (QQuickAccessibleAttached *attached = QQuickAccessibleAttached::attachedProperties(item()))
568 accepted = attached->doAction(actionName);
569
570 if (accepted)
571 return;
572 // Look for and call the accessible[actionName]Action() function on the item.
573 // This allows for overriding the default action handling.
574 const QByteArray functionName = "accessible" + actionName.toLatin1() + "Action";
575 if (object()->metaObject()->indexOfMethod(QByteArray(functionName + "()")) != -1) {
576 QMetaObject::invokeMethod(object(), functionName);
577 return;
578 }
579
580 // Context menu default behavior
581 if (actionName == QAccessibleActionInterface::showMenuAction()
582 && QQuickItemPrivate::get(item())->contextMenu()) {
583 QContextMenuEvent event(QContextMenuEvent::Other, QPoint(),
584 item()->mapToGlobal(QPointF()).toPoint(),
585 QGuiApplication::keyboardModifiers());
586 QCoreApplication::sendEvent(item(), &event);
587 return;
588 }
589
590 // Role-specific default action handling follows. Items are expected to provide
591 // properties according to role conventions. These will then be read and/or updated
592 // by the accessibility system.
593 // Checkable roles : checked
594 // Value-based roles : (via the value interface: value, minimumValue, maximumValue), stepSize
595 switch (role()) {
596 case QAccessible::RadioButton:
597 case QAccessible::CheckBox:
598 case QAccessible::Switch: {
599 QVariant checked = object()->property("checked");
600 if (checked.isValid()) {
601 if (actionName == QAccessibleActionInterface::toggleAction()
602 || actionName == QAccessibleActionInterface::pressAction()) {
603 object()->setProperty("checked", QVariant(!checked.toBool()));
604 }
605 }
606 break;
607 }
608 case QAccessible::Slider:
609 case QAccessible::SpinBox:
610 case QAccessible::Dial:
611 case QAccessible::ScrollBar: {
612 if (actionName != QAccessibleActionInterface::increaseAction() &&
613 actionName != QAccessibleActionInterface::decreaseAction())
614 break;
615
616 // Update the value using QAccessibleValueInterface, respecting
617 // the minimum and maximum value (if set). Also check for and
618 // use the "stepSize" property on the item
619 if (QAccessibleValueInterface *valueIface = valueInterface()) {
620 QVariant valueV = valueIface->currentValue();
621 qreal newValue = valueV.toReal();
622
623 QVariant stepSizeV = object()->property("stepSize");
624 qreal stepSize = stepSizeV.isValid() ? stepSizeV.toReal() : qreal(1.0);
625 if (actionName == QAccessibleActionInterface::increaseAction()) {
626 newValue += stepSize;
627 } else {
628 newValue -= stepSize;
629 }
630
631 QVariant minimumValueV = valueIface->minimumValue();
632 if (minimumValueV.isValid()) {
633 newValue = qMax(newValue, minimumValueV.toReal());
634 }
635 QVariant maximumValueV = valueIface->maximumValue();
636 if (maximumValueV.isValid()) {
637 newValue = qMin(newValue, maximumValueV.toReal());
638 }
639
640 valueIface->setCurrentValue(QVariant(newValue));
641 }
642 break;
643 }
644 case QAccessible::Button: {
645 QVariant expanded = object()->property("expanded");
646 if (expanded.isValid()) {
647 if (actionName == QAccessibleActionInterface::toggleAction()
648 || actionName == QAccessibleActionInterface::pressAction()) {
649 object()->setProperty("expanded", QVariant(!expanded.toBool()));
650 }
651 }
652 break;
653 }
654 default:
655 break;
656 }
657}
658
659QStringList QAccessibleQuickItem::keyBindingsForAction(const QString &actionName) const
660{
661 Q_UNUSED(actionName);
662 return QStringList();
663}
664
665QString QAccessibleQuickItem::text(QAccessible::Text textType) const
666{
667 // handles generic behavior not specific to an item
668 switch (textType) {
669 case QAccessible::Name: {
670 QVariant accessibleName = QQuickAccessibleAttached::property(object(), "name");
671 if (!accessibleName.isNull())
672 return accessibleName.toString();
673 break;}
674 case QAccessible::Description: {
675 QVariant accessibleDecription = QQuickAccessibleAttached::property(object(), "description");
676 if (!accessibleDecription.isNull())
677 return accessibleDecription.toString();
678 break;}
679 case QAccessible::Identifier: {
680 QVariant accessibleIdentifier = QQuickAccessibleAttached::property(object(), "id");
681 if (!accessibleIdentifier.isNull())
682 return accessibleIdentifier.toString();
683 auto quickItem = item();
684 if (quickItem->isComponentComplete()) {
685 QQmlContext *context = qmlContext(quickItem);
686 if (context) {
687 const auto objectId = context->nameForObject(quickItem);
688 if (!objectId.isEmpty())
689 return objectId;
690 }
691 }
692 break;}
693#ifdef Q_ACCESSIBLE_QUICK_ITEM_ENABLE_DEBUG_DESCRIPTION
694 case QAccessible::DebugDescription: {
695 QString debugString;
696 debugString = QString::fromLatin1(object()->metaObject()->className()) + QLatin1Char(' ');
697 debugString += isAccessible() ? QLatin1String("enabled") : QLatin1String("disabled");
698 return debugString;
699 break; }
700#endif
701 case QAccessible::Value:
702 case QAccessible::Help:
703 case QAccessible::Accelerator:
704 default:
705 break;
706 }
707
708 // the following block handles item-specific behavior
709 if (role() == QAccessible::EditableText) {
710 if (textType == QAccessible::Value) {
711 if (auto textInput = qobject_cast<QQuickTextInput *>(item()))
712 return textInput->displayText();
713 if (QTextDocument *doc = textDocument()) {
714 return doc->toPlainText();
715 }
716 QVariant text = object()->property("text");
717 return text.toString();
718 }
719 }
720
721 return QString();
722}
723
724void QAccessibleQuickItem::setText(QAccessible::Text textType, const QString &text)
725{
726 if (role() != QAccessible::EditableText)
727 return;
728 if (textType != QAccessible::Value)
729 return;
730
731 if (QTextDocument *doc = textDocument()) {
732 doc->setPlainText(text);
733 return;
734 }
735 auto textPropertyName = "text";
736 if (object()->metaObject()->indexOfProperty(textPropertyName) >= 0)
737 object()->setProperty(textPropertyName, text);
738}
739
740void *QAccessibleQuickItem::interface_cast(QAccessible::InterfaceType t)
741{
742 const QAccessible::Role r = role();
743 switch (t) {
744 case QAccessible::ActionInterface:
745 return static_cast<QAccessibleActionInterface*>(this);
746 case QAccessible::AttributesInterface:
747 return static_cast<QAccessibleAttributesInterface *>(this);
748 case QAccessible::ValueInterface:
749 if (r == QAccessible::Slider
750 || r == QAccessible::SpinBox
751 || r == QAccessible::Dial
752 || r == QAccessible::ScrollBar
753 || r == QAccessible::ProgressBar) {
754 return static_cast<QAccessibleValueInterface*>(this);
755 }
756 break;
757 case QAccessible::TextInterface:
758 if (r == QAccessible::EditableText
759 || r == QAccessible::StaticText
760 || r == QAccessible::Heading) {
761 return static_cast<QAccessibleTextInterface*>(this);
762 }
763 break;
764 default:
765 break;
766 }
767
768 return QAccessibleObject::interface_cast(t);
769}
770
771QVariant QAccessibleQuickItem::currentValue() const
772{
773 return item()->property("value");
774}
775
776void QAccessibleQuickItem::setCurrentValue(const QVariant &value)
777{
778 item()->setProperty("value", value);
779}
780
781QVariant QAccessibleQuickItem::maximumValue() const
782{
783 const auto minimumValue = item()->property("minimumValue");
784 const auto maximumValue = item()->property("maximumValue");
785 const auto from = item()->property("from");
786 const auto to = item()->property("to");
787
788 if (minimumValue.isValid() && maximumValue.isValid())
789 return maximumValue;
790
791 if (from.isValid() && to.isValid())
792 return to;
793
794 return QVariant();
795}
796
797QVariant QAccessibleQuickItem::minimumValue() const
798{
799 const auto minimumValue = item()->property("minimumValue");
800 const auto maximumValue = item()->property("maximumValue");
801 const auto from = item()->property("from");
802 const auto to = item()->property("to");
803
804 if (minimumValue.isValid() && maximumValue.isValid())
805 return minimumValue;
806
807 if (from.isValid() && to.isValid())
808 return from;
809
810 return QVariant();
811}
812
813QVariant QAccessibleQuickItem::minimumStepSize() const
814{
815 return item()->property("stepSize");
816}
817
818/*!
819 \internal
820 Shared between QAccessibleQuickItem and QAccessibleQuickView
821*/
822QRect itemScreenRect(QQuickItem *item)
823{
824 // ### no window in some cases.
825 // ### Should we really check for 0 opacity?
826 if (!item->window() ||!item->isVisible() || qFuzzyIsNull(item->opacity())) {
827 return QRect();
828 }
829
830 QSizeF itemSize(item->width(), item->height());
831 // ### If the bounding rect fails, we first try the implicit size, then we go for the
832 // parent size. WE MIGHT HAVE TO REVISIT THESE FALLBACKS.
833 if (itemSize.isEmpty()) {
834 itemSize = QSize(item->implicitWidth(), item->implicitHeight());
835 if (itemSize.isEmpty() && item->parentItem())
836 // ### Seems that the above fallback is not enough, fallback to use the parent size...
837 itemSize = QSize(item->parentItem()->width(), item->parentItem()->height());
838 }
839
840 QRectF sceneRect = item->mapRectToScene(QRectF(QPointF(0, 0), itemSize));
841 QPoint screenPos = item->window()->mapToGlobal(sceneRect.topLeft().toPoint());
842 QSize screenSize = sceneRect.size().toSize();
843 return QRect(screenPos, screenSize);
844}
845
846QTextDocument *QAccessibleQuickItem::textDocument() const
847{
848 QVariant docVariant = item()->property("textDocument");
849 if (docVariant.canConvert<QQuickTextDocument*>()) {
850 QQuickTextDocument *qqdoc = docVariant.value<QQuickTextDocument*>();
851 return qqdoc->textDocument();
852 }
853 return nullptr;
854}
855
856int QAccessibleQuickItem::characterCount() const
857{
858 if (m_doc) {
859 QTextCursor cursor = QTextCursor(m_doc);
860 cursor.movePosition(QTextCursor::End);
861 return cursor.position();
862 }
863
864 if (role() == QAccessible::EditableText)
865 return text(QAccessible::Value).size();
866
867 return text(QAccessible::Name).size();
868}
869
870int QAccessibleQuickItem::cursorPosition() const
871{
872 QVariant pos = item()->property("cursorPosition");
873 return pos.toInt();
874}
875
876void QAccessibleQuickItem::setCursorPosition(int position)
877{
878 item()->setProperty("cursorPosition", position);
879}
880
881QString QAccessibleQuickItem::text(int startOffset, int endOffset) const
882{
883 if (m_doc) {
884 QTextCursor cursor = QTextCursor(m_doc);
885 cursor.setPosition(startOffset);
886 cursor.setPosition(endOffset, QTextCursor::KeepAnchor);
887 return cursor.selectedText();
888 }
889
890 if (role() == QAccessible::EditableText)
891 return text(QAccessible::Value).mid(startOffset, endOffset - startOffset);
892
893 return text(QAccessible::Name).mid(startOffset, endOffset - startOffset);
894}
895
896QString QAccessibleQuickItem::textBeforeOffset(int offset, QAccessible::TextBoundaryType boundaryType,
897 int *startOffset, int *endOffset) const
898{
899 Q_ASSERT(startOffset);
900 Q_ASSERT(endOffset);
901
902 if (m_doc) {
903 return qt_accTextBeforeOffsetHelper(*this, QTextCursor(m_doc), offset, boundaryType, startOffset, endOffset);
904 } else {
905 return QAccessibleTextInterface::textBeforeOffset(offset, boundaryType, startOffset, endOffset);
906 }
907}
908
909QString QAccessibleQuickItem::textAfterOffset(int offset, QAccessible::TextBoundaryType boundaryType,
910 int *startOffset, int *endOffset) const
911{
912 Q_ASSERT(startOffset);
913 Q_ASSERT(endOffset);
914
915 if (m_doc) {
916 return qt_accTextAfterOffsetHelper(*this, QTextCursor(m_doc), offset, boundaryType, startOffset, endOffset);
917 } else {
918 return QAccessibleTextInterface::textAfterOffset(offset, boundaryType, startOffset, endOffset);
919 }
920}
921
922QString QAccessibleQuickItem::textAtOffset(int offset, QAccessible::TextBoundaryType boundaryType,
923 int *startOffset, int *endOffset) const
924{
925 Q_ASSERT(startOffset);
926 Q_ASSERT(endOffset);
927
928 if (m_doc) {
929 return qt_accTextAtOffsetHelper(*this, QTextCursor(m_doc), offset, boundaryType, startOffset, endOffset);
930 } else {
931 return QAccessibleTextInterface::textAtOffset(offset, boundaryType, startOffset, endOffset);
932 }
933}
934
935void QAccessibleQuickItem::selection(int selectionIndex, int *startOffset, int *endOffset) const
936{
937 if (selectionIndex == 0) {
938 *startOffset = item()->property("selectionStart").toInt();
939 *endOffset = item()->property("selectionEnd").toInt();
940 } else {
941 *startOffset = 0;
942 *endOffset = 0;
943 }
944}
945
946int QAccessibleQuickItem::selectionCount() const
947{
948 if (item()->property("selectionStart").toInt() != item()->property("selectionEnd").toInt())
949 return 1;
950 return 0;
951}
952
953void QAccessibleQuickItem::addSelection(int startOffset, int endOffset)
954{
955 setSelection(0, startOffset, endOffset);
956}
957void QAccessibleQuickItem::removeSelection(int /* selectionIndex */)
958{
959
960}
961void QAccessibleQuickItem::setSelection(int /* selectionIndex */, int /* startOffset */, int /* endOffset */)
962{
963
964}
965
966QList<QAccessible::Attribute> QAccessibleQuickItem::attributeKeys() const
967{
968 QList<QAccessible::Attribute> attributes;
969
970 if (attributeValue(QAccessible::Attribute::Locale).isValid())
971 attributes << QAccessible::Attribute::Locale;
972
973 if (attributeValue(QAccessible::Attribute::Level).isValid())
974 attributes << QAccessible::Attribute::Level;
975
976 return attributes;
977}
978
979QVariant QAccessibleQuickItem::attributeValue(QAccessible::Attribute key) const
980{
981 if (key == QAccessible::Attribute::Locale) {
982 QAccessibleInterface *windowIface = QAccessible::queryAccessibleInterface(window());
983 if (!windowIface || !windowIface->attributesInterface())
984 return QVariant();
985
986 return windowIface->attributesInterface()->attributeValue(QAccessible::Attribute::Locale);
987 }
988
989 if (key == QAccessible::Attribute::Level) {
990 const auto levelAttribute = QQuickAccessibleAttached::property(object(), "levelAttribute");
991 if (!levelAttribute.isNull())
992 return levelAttribute;
993 }
994
995 return QVariant();
996}
997
998#endif // accessibility
999
1000QT_END_NAMESPACE