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
qwindowsuiaselectionitemprovider.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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
5#include <QtGui/qtguiglobal.h>
6#if QT_CONFIG(accessibility)
7
8#include "qwindowsuiaselectionitemprovider.h"
9#include "qwindowsuiamainprovider.h"
10#include "qwindowsuiautils.h"
11#include "qwindowscontext.h"
12
13#include <QtGui/qaccessible.h>
14#include <QtCore/qloggingcategory.h>
15#include <QtCore/qstring.h>
16
17QT_BEGIN_NAMESPACE
18
19using namespace QWindowsUiAutomation;
20
21
22QWindowsUiaSelectionItemProvider::QWindowsUiaSelectionItemProvider(QAccessible::Id id) :
23 QWindowsUiaBaseProvider(id)
24{
25}
26
27QWindowsUiaSelectionItemProvider::~QWindowsUiaSelectionItemProvider()
28{
29}
30
31// Selects the element (deselecting all others).
32HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::Select()
33{
34 qCDebug(lcQpaUiAutomation) << __FUNCTION__;
35
36 QAccessibleInterface *accessible = accessibleInterface();
37 if (!accessible)
38 return UIA_E_ELEMENTNOTAVAILABLE;
39
40 if (QAccessibleInterface *parent = accessible->parent()) {
41 if (QAccessibleSelectionInterface *selectionInterface = parent->selectionInterface()) {
42 selectionInterface->clear();
43 bool ok = selectionInterface->select(accessible);
44 return ok ? S_OK : S_FALSE;
45 }
46 }
47
48 QAccessibleActionInterface *actionInterface = accessible->actionInterface();
49 if (!actionInterface)
50 return UIA_E_ELEMENTNOTAVAILABLE;
51
52 if (accessible->role() == QAccessible::RadioButton || accessible->role() == QAccessible::PageTab) {
53 // For radio buttons/tabs we just invoke the selection action; others are automatically deselected.
54 actionInterface->doAction(QAccessibleActionInterface::pressAction());
55 } else {
56 // Toggle list item if not already selected. It must be done first to support all selection modes.
57 if (!accessible->state().selected) {
58 actionInterface->doAction(QAccessibleActionInterface::toggleAction());
59 }
60 // Toggle selected siblings.
61 if (QAccessibleInterface *parent = accessible->parent()) {
62 for (int i = 0; i < parent->childCount(); ++i) {
63 if (QAccessibleInterface *sibling = parent->child(i)) {
64 if ((sibling != accessible) && (sibling->state().selected)) {
65 if (QAccessibleActionInterface *siblingAction = sibling->actionInterface()) {
66 siblingAction->doAction(QAccessibleActionInterface::toggleAction());
67 }
68 }
69 }
70 }
71 }
72 }
73 return S_OK;
74}
75
76// Adds the element to the list of selected elements.
77HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::AddToSelection()
78{
79 qCDebug(lcQpaUiAutomation) << __FUNCTION__;
80
81 QAccessibleInterface *accessible = accessibleInterface();
82 if (!accessible)
83 return UIA_E_ELEMENTNOTAVAILABLE;
84
85 if (QAccessibleInterface *parent = accessible->parent()) {
86 if (QAccessibleSelectionInterface *selectionInterface = parent->selectionInterface()) {
87 bool ok = selectionInterface->select(accessible);
88 return ok ? S_OK : S_FALSE;
89 }
90 }
91
92 QAccessibleActionInterface *actionInterface = accessible->actionInterface();
93 if (!actionInterface)
94 return UIA_E_ELEMENTNOTAVAILABLE;
95
96 if (accessible->role() == QAccessible::RadioButton || accessible->role() == QAccessible::PageTab) {
97 // For radio buttons and tabs we invoke the selection action.
98 actionInterface->doAction(QAccessibleActionInterface::pressAction());
99 } else {
100 // Toggle list item if not already selected.
101 if (!accessible->state().selected) {
102 actionInterface->doAction(QAccessibleActionInterface::toggleAction());
103 }
104 }
105 return S_OK;
106}
107
108// Removes a list item from selection.
109HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::RemoveFromSelection()
110{
111 qCDebug(lcQpaUiAutomation) << __FUNCTION__;
112
113 QAccessibleInterface *accessible = accessibleInterface();
114 if (!accessible)
115 return UIA_E_ELEMENTNOTAVAILABLE;
116
117 if (QAccessibleInterface *parent = accessible->parent()) {
118 if (QAccessibleSelectionInterface *selectionInterface = parent->selectionInterface()) {
119 bool ok = selectionInterface->unselect(accessible);
120 return ok ? S_OK : S_FALSE;
121 }
122 }
123
124 QAccessibleActionInterface *actionInterface = accessible->actionInterface();
125 if (!actionInterface)
126 return UIA_E_ELEMENTNOTAVAILABLE;
127
128 if (accessible->role() != QAccessible::RadioButton && accessible->role() != QAccessible::PageTab) {
129 if (accessible->state().selected) {
130 actionInterface->doAction(QAccessibleActionInterface::toggleAction());
131 }
132 }
133
134 return S_OK;
135}
136
137// Returns true if element is currently selected.
138HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::get_IsSelected(BOOL *pRetVal)
139{
140 qCDebug(lcQpaUiAutomation) << __FUNCTION__;
141
142 if (!pRetVal)
143 return E_INVALIDARG;
144 *pRetVal = FALSE;
145
146 QAccessibleInterface *accessible = accessibleInterface();
147 if (!accessible)
148 return UIA_E_ELEMENTNOTAVAILABLE;
149
150 if (QAccessibleInterface *parent = accessible->parent()) {
151 if (QAccessibleSelectionInterface *selectionInterface = parent->selectionInterface()) {
152 bool selected = selectionInterface->isSelected(accessible);
153 *pRetVal = selected ? TRUE : FALSE;
154 return S_OK;
155 }
156 }
157
158 if (accessible->role() == QAccessible::RadioButton)
159 *pRetVal = accessible->state().checked;
160 else if (accessible->role() == QAccessible::PageTab)
161 *pRetVal = accessible->state().focused;
162 else
163 *pRetVal = accessible->state().selected;
164 return S_OK;
165}
166
167// Returns the provider for the container element (e.g., the list for the list item).
168HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::get_SelectionContainer(IRawElementProviderSimple **pRetVal)
169{
170 qCDebug(lcQpaUiAutomation) << __FUNCTION__;
171
172 if (!pRetVal)
173 return E_INVALIDARG;
174 *pRetVal = nullptr;
175
176 QAccessibleInterface *accessible = accessibleInterface();
177 if (!accessible)
178 return UIA_E_ELEMENTNOTAVAILABLE;
179
180 QAccessibleInterface *parent = accessible->parent();
181 if (parent && parent->selectionInterface()) {
182 *pRetVal = QWindowsUiaMainProvider::providerForAccessible(parent).Detach();
183 return S_OK;
184 }
185
186 QAccessibleActionInterface *actionInterface = accessible->actionInterface();
187 if (!actionInterface)
188 return UIA_E_ELEMENTNOTAVAILABLE;
189
190 // Radio buttons do not require a container.
191 if (parent) {
192 if ((accessible->role() == QAccessible::ListItem && parent->role() == QAccessible::List)
193 || (accessible->role() == QAccessible::PageTab && parent->role() == QAccessible::PageTabList)) {
194 *pRetVal = QWindowsUiaMainProvider::providerForAccessible(parent).Detach();
195 }
196 }
197 return S_OK;
198}
199
200QT_END_NAMESPACE
201
202#endif // QT_CONFIG(accessibility)