1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include <QtGui/qtguiglobal.h>
41 #if QT_CONFIG(accessibility)
42 
43 #include "qwinrtuiavalueprovider.h"
44 #include "qwinrtuiametadatacache.h"
45 #include "qwinrtuiautils.h"
46 
47 #include <QtGui/QAccessible>
48 #include <QtGui/QAccessibleInterface>
49 #include <QtCore/QLoggingCategory>
50 #include <QtCore/QString>
51 #include <QtCore/private/qeventdispatcher_winrt_p.h>
52 
53 QT_BEGIN_NAMESPACE
54 
55 using namespace QWinRTUiAutomation;
56 using namespace ABI::Windows::UI::Xaml::Automation;
57 using namespace ABI::Windows::UI::Xaml::Automation::Provider;
58 
QWinRTUiaValueProvider(QAccessible::Id id)59 QWinRTUiaValueProvider::QWinRTUiaValueProvider(QAccessible::Id id) :
60     QWinRTUiaBaseProvider(id)
61 {
62     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
63 }
64 
~QWinRTUiaValueProvider()65 QWinRTUiaValueProvider::~QWinRTUiaValueProvider()
66 {
67     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
68 }
69 
70 // True for read-only controls.
get_IsReadOnly(boolean * value)71 HRESULT STDMETHODCALLTYPE QWinRTUiaValueProvider::get_IsReadOnly(boolean *value)
72 {
73     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
74 
75     if (!value)
76         return E_INVALIDARG;
77     QSharedPointer<QWinRTUiaControlMetadata> metadata = QWinRTUiaMetadataCache::instance()->metadataForId(id());
78     *value = (metadata->state().readOnly != 0);
79     return S_OK;
80 }
81 
82 // Returns the value in text form.
get_Value(HSTRING * value)83 HRESULT STDMETHODCALLTYPE QWinRTUiaValueProvider::get_Value(HSTRING *value)
84 {
85     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
86 
87     if (!value)
88         return E_INVALIDARG;
89     QSharedPointer<QWinRTUiaControlMetadata> metadata = QWinRTUiaMetadataCache::instance()->metadataForId(id());
90     return qHString(metadata->value(), value);
91 }
92 
93 // Sets the value associated with the control.
SetValue(HSTRING value)94 HRESULT STDMETHODCALLTYPE QWinRTUiaValueProvider::SetValue(HSTRING value)
95 {
96     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
97 
98     auto accid = id();
99 
100     QString tmpValue = hStrToQStr(value);
101 
102     QEventDispatcherWinRT::runOnMainThread([accid, tmpValue]() {
103 
104         if (QAccessibleInterface *accessible = accessibleForId(accid)) {
105 
106             // First sets the value as a text.
107             accessible->setText(QAccessible::Value, tmpValue);
108 
109             // Then, if the control supports the value interface (range value)
110             // and the supplied text can be converted to a number, and that number
111             // lies within the min/max limits, sets it as the control's current (numeric) value.
112             if (QAccessibleValueInterface *valueInterface = accessible->valueInterface()) {
113                 bool ok = false;
114                 double numval = tmpValue.toDouble(&ok);
115                 if (ok) {
116                     double minimum = valueInterface->minimumValue().toDouble();
117                     double maximum = valueInterface->maximumValue().toDouble();
118                     if ((numval >= minimum) && (numval <= maximum)) {
119                         valueInterface->setCurrentValue(QVariant(numval));
120                     }
121                 }
122             }
123         }
124         QWinRTUiaMetadataCache::instance()->load(accid);
125         return S_OK;
126     }, 0);
127 
128     return S_OK;
129 }
130 
131 QT_END_NAMESPACE
132 
133 #endif // QT_CONFIG(accessibility)
134