1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module 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#ifndef QQUICKACCESSIBLEATTACHED_H
41#define QQUICKACCESSIBLEATTACHED_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtQuick/qquickitem.h>
55
56#include <QtCore/qobject.h>
57#include <QtCore/qstring.h>
58
59#if QT_CONFIG(accessibility)
60
61#include <QtGui/qaccessible.h>
62#include <private/qtquickglobal_p.h>
63
64QT_BEGIN_NAMESPACE
65
66
67#define STATE_PROPERTY(P) \
68 Q_PROPERTY(bool P READ P WRITE set_ ## P NOTIFY P ## Changed FINAL) \
69 bool P() const { return m_state.P ; } \
70 void set_ ## P(bool arg) \
71 { \
72 m_stateExplicitlySet.P = true; \
73 if (m_state.P == arg) \
74 return; \
75 m_state.P = arg; \
76 emit P ## Changed(arg); \
77 QAccessible::State changedState; \
78 changedState.P = true; \
79 QAccessibleStateChangeEvent ev(parent(), changedState); \
80 QAccessible::updateAccessibility(&ev); \
81 } \
82 Q_SIGNAL void P ## Changed(bool arg);
83
84
85class Q_QUICK_PRIVATE_EXPORT QQuickAccessibleAttached : public QObject
86{
87 Q_OBJECT
88 Q_PROPERTY(QAccessible::Role role READ role WRITE setRole NOTIFY roleChanged FINAL)
89 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL)
90 Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged FINAL)
91 Q_PROPERTY(bool ignored READ ignored WRITE setIgnored NOTIFY ignoredChanged FINAL)
92
93public:
94 Q_ENUMS(QAccessible::Role QAccessible::Event)
95 STATE_PROPERTY(checkable)
96 STATE_PROPERTY(checked)
97 STATE_PROPERTY(editable)
98 STATE_PROPERTY(focusable)
99 STATE_PROPERTY(focused)
100 STATE_PROPERTY(multiLine)
101 STATE_PROPERTY(readOnly)
102 STATE_PROPERTY(selected)
103 STATE_PROPERTY(selectable)
104 STATE_PROPERTY(pressed)
105 STATE_PROPERTY(checkStateMixed)
106 STATE_PROPERTY(defaultButton)
107 STATE_PROPERTY(passwordEdit)
108 STATE_PROPERTY(selectableText)
109 STATE_PROPERTY(searchEdit)
110
111 QQuickAccessibleAttached(QObject *parent);
112 ~QQuickAccessibleAttached();
113
114 QAccessible::Role role() const { return m_role; }
115 void setRole(QAccessible::Role role);
116 QString name() const {
117 if (m_state.passwordEdit)
118 return QString();
119 return m_name;
120 }
121 void setName(const QString &name) {
122 if (name != m_name) {
123 m_name = name;
124 Q_EMIT nameChanged();
125 QAccessibleEvent ev(parent(), QAccessible::NameChanged);
126 QAccessible::updateAccessibility(&ev);
127 }
128 }
129
130 QString description() const { return m_description; }
131 void setDescription(const QString &description)
132 {
133 if (m_description != description) {
134 m_description = description;
135 Q_EMIT descriptionChanged();
136 QAccessibleEvent ev(parent(), QAccessible::DescriptionChanged);
137 QAccessible::updateAccessibility(&ev);
138 }
139 }
140
141 // Factory function
142 static QQuickAccessibleAttached *qmlAttachedProperties(QObject *obj);
143
144 static QQuickAccessibleAttached *attachedProperties(const QObject *obj)
145 {
146 return qobject_cast<QQuickAccessibleAttached*>(qmlAttachedPropertiesObject<QQuickAccessibleAttached>(obj, false));
147 }
148
149 // Property getter
150 static QVariant property(const QObject *object, const char *propertyName)
151 {
152 if (QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(object))
153 return attachedObject->property(propertyName);
154 return QVariant();
155 }
156
157 static bool setProperty(QObject *object, const char *propertyName, const QVariant &value)
158 {
159 QObject *obj = qmlAttachedPropertiesObject<QQuickAccessibleAttached>(object, true);
160 if (!obj) {
161 qWarning("cannot set property Accessible.%s of QObject %s", propertyName, object->metaObject()->className());
162 return false;
163 }
164 return obj->setProperty(propertyName, value);
165 }
166
167 static QObject *findAccessible(QObject *object, QAccessible::Role role = QAccessible::NoRole)
168 {
169 while (object) {
170 QQuickAccessibleAttached *att = QQuickAccessibleAttached::attachedProperties(object);
171 if (att && (role == QAccessible::NoRole || att->role() == role)) {
172 break;
173 }
174 object = object->parent();
175 }
176 return object;
177 }
178
179 QAccessible::State state() const { return m_state; }
180 bool ignored() const;
181 bool doAction(const QString &actionName);
182 void availableActions(QStringList *actions) const;
183
184public Q_SLOTS:
185 void valueChanged() {
186 QAccessibleValueChangeEvent ev(parent(), parent()->property("value"));
187 QAccessible::updateAccessibility(&ev);
188 }
189 void cursorPositionChanged() {
190 QAccessibleTextCursorEvent ev(parent(), parent()->property("cursorPosition").toInt());
191 QAccessible::updateAccessibility(&ev);
192 }
193
194 void setIgnored(bool ignored);
195
196Q_SIGNALS:
197 void roleChanged();
198 void nameChanged();
199 void descriptionChanged();
200 void ignoredChanged();
201 void pressAction();
202 void toggleAction();
203 void increaseAction();
204 void decreaseAction();
205 void scrollUpAction();
206 void scrollDownAction();
207 void scrollLeftAction();
208 void scrollRightAction();
209 void previousPageAction();
210 void nextPageAction();
211
212private:
213 QQuickItem *item() const { return static_cast<QQuickItem*>(parent()); }
214
215 QAccessible::Role m_role;
216 QAccessible::State m_state;
217 QAccessible::State m_stateExplicitlySet;
218 QString m_name;
219 QString m_description;
220
221 static QMetaMethod sigPress;
222 static QMetaMethod sigToggle;
223 static QMetaMethod sigIncrease;
224 static QMetaMethod sigDecrease;
225 static QMetaMethod sigScrollUp;
226 static QMetaMethod sigScrollDown;
227 static QMetaMethod sigScrollLeft;
228 static QMetaMethod sigScrollRight;
229 static QMetaMethod sigPreviousPage;
230 static QMetaMethod sigNextPage;
231
232public:
233 using QObject::property;
234};
235
236
237QT_END_NAMESPACE
238
239QML_DECLARE_TYPE(QQuickAccessibleAttached)
240QML_DECLARE_TYPEINFO(QQuickAccessibleAttached, QML_HAS_ATTACHED_PROPERTIES)
241
242#endif // accessibility
243
244#endif
245