summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/windows/uiautomation/qwindowsuiaselectionitemprovider.cpp
blob: 14ec29c75b639e634940854e4cf8870554bd8c91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QtGui/qtguiglobal.h>
#if QT_CONFIG(accessibility)

#include "qwindowsuiaselectionitemprovider.h"
#include "qwindowsuiamainprovider.h"
#include "qwindowsuiautils.h"
#include "qwindowscontext.h"

#include <QtGui/qaccessible.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qstring.h>

QT_BEGIN_NAMESPACE

using namespace QWindowsUiAutomation;


QWindowsUiaSelectionItemProvider::QWindowsUiaSelectionItemProvider(QAccessible::Id id) :
    QWindowsUiaBaseProvider(id)
{
}

QWindowsUiaSelectionItemProvider::~QWindowsUiaSelectionItemProvider()
{
}

// Selects the element (deselecting all others).
HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::Select()
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleActionInterface *actionInterface = accessible->actionInterface();
    if (!actionInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    if (accessible->role() == QAccessible::RadioButton) {
        // For radio buttons we just invoke the selection action; others are automatically deselected.
        actionInterface->doAction(QAccessibleActionInterface::pressAction());
    } else {
        // Toggle list item if not already selected. It must be done first to support all selection modes.
        if (!accessible->state().selected) {
            actionInterface->doAction(QAccessibleActionInterface::toggleAction());
        }
        // Toggle selected siblings.
        if (QAccessibleInterface *parent = accessible->parent()) {
            for (int i = 0; i < parent->childCount(); ++i) {
                if (QAccessibleInterface *sibling = parent->child(i)) {
                    if ((sibling != accessible) && (sibling->state().selected)) {
                        if (QAccessibleActionInterface *siblingAction = sibling->actionInterface()) {
                            siblingAction->doAction(QAccessibleActionInterface::toggleAction());
                        }
                    }
                }
            }
        }
    }
    return S_OK;
}

// Adds the element to the list of selected elements.
HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::AddToSelection()
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleActionInterface *actionInterface = accessible->actionInterface();
    if (!actionInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    if (accessible->role() == QAccessible::RadioButton) {
        // For radio buttons we invoke the selection action.
        actionInterface->doAction(QAccessibleActionInterface::pressAction());
    } else {
        // Toggle list item if not already selected.
        if (!accessible->state().selected) {
            actionInterface->doAction(QAccessibleActionInterface::toggleAction());
        }
    }
    return S_OK;
}

// Removes a list item from selection.
HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::RemoveFromSelection()
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleActionInterface *actionInterface = accessible->actionInterface();
    if (!actionInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    if (accessible->role() != QAccessible::RadioButton) {
        if (accessible->state().selected) {
            actionInterface->doAction(QAccessibleActionInterface::toggleAction());
        }
    }

    return S_OK;
}

// Returns true if element is currently selected.
HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::get_IsSelected(BOOL *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

    if (!pRetVal)
        return E_INVALIDARG;
    *pRetVal = FALSE;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    if (accessible->role() == QAccessible::RadioButton)
        *pRetVal = accessible->state().checked;
    else
        *pRetVal = accessible->state().selected;
    return S_OK;
}

// Returns the provider for the container element (e.g., the list for the list item).
HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionItemProvider::get_SelectionContainer(IRawElementProviderSimple **pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

    if (!pRetVal)
        return E_INVALIDARG;
    *pRetVal = nullptr;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleActionInterface *actionInterface = accessible->actionInterface();
    if (!actionInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    // Radio buttons do not require a container.
    if (accessible->role() == QAccessible::ListItem) {
        if (QAccessibleInterface *parent = accessible->parent()) {
            if (parent->role() == QAccessible::List) {
                *pRetVal = QWindowsUiaMainProvider::providerForAccessible(parent);
            }
        }
    }
    return S_OK;
}

QT_END_NAMESPACE

#endif // QT_CONFIG(accessibility)