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
93 QML_NAMED_ELEMENT(Accessible)
94 QML_UNCREATABLE("Accessible is only available via attached properties.")
95 QML_ATTACHED(QQuickAccessibleAttached)
96
97public:
98 Q_ENUMS(QAccessible::Role QAccessible::Event)
99 STATE_PROPERTY(checkable)
100 STATE_PROPERTY(checked)
101 STATE_PROPERTY(editable)
102 STATE_PROPERTY(focusable)
103 STATE_PROPERTY(focused)
104 STATE_PROPERTY(multiLine)
105 STATE_PROPERTY(readOnly)
106 STATE_PROPERTY(selected)
107 STATE_PROPERTY(selectable)
108 STATE_PROPERTY(pressed)
109 STATE_PROPERTY(checkStateMixed)
110 STATE_PROPERTY(defaultButton)
111 STATE_PROPERTY(passwordEdit)
112 STATE_PROPERTY(selectableText)
113 STATE_PROPERTY(searchEdit)
114
115 QQuickAccessibleAttached(QObject *parent);
116 ~QQuickAccessibleAttached();
117
118 QAccessible::Role role() const { return m_role; }
119 void setRole(QAccessible::Role role);
120 QString name() const {
121 if (m_state.passwordEdit)
122 return QString();
123 return m_name;
124 }
125
126 bool wasNameExplicitlySet() const;
127 void setName(const QString &name) {
128 m_nameExplicitlySet = true;
129 if (name != m_name) {
130 m_name = name;
131 Q_EMIT nameChanged();
132 QAccessibleEvent ev(parent(), QAccessible::NameChanged);
133 QAccessible::updateAccessibility(event: &ev);
134 }
135 }
136 void setNameImplicitly(const QString &name);
137
138 QString description() const { return m_description; }
139 void setDescription(const QString &description)
140 {
141 if (m_description != description) {
142 m_description = description;
143 Q_EMIT descriptionChanged();
144 QAccessibleEvent ev(parent(), QAccessible::DescriptionChanged);
145 QAccessible::updateAccessibility(event: &ev);
146 }
147 }
148
149 // Factory function
150 static QQuickAccessibleAttached *qmlAttachedProperties(QObject *obj);
151
152 static QQuickAccessibleAttached *attachedProperties(const QObject *obj)
153 {
154 return qobject_cast<QQuickAccessibleAttached*>(object: qmlAttachedPropertiesObject<QQuickAccessibleAttached>(obj, create: false));
155 }
156
157 // Property getter
158 static QVariant property(const QObject *object, const char *propertyName)
159 {
160 if (QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(obj: object))
161 return attachedObject->property(name: propertyName);
162 return QVariant();
163 }
164
165 static bool setProperty(QObject *object, const char *propertyName, const QVariant &value)
166 {
167 QObject *obj = qmlAttachedPropertiesObject<QQuickAccessibleAttached>(obj: object, create: true);
168 if (!obj) {
169 qWarning(msg: "cannot set property Accessible.%s of QObject %s", propertyName, object->metaObject()->className());
170 return false;
171 }
172 return obj->setProperty(name: propertyName, value);
173 }
174
175 static QObject *findAccessible(QObject *object, QAccessible::Role role = QAccessible::NoRole)
176 {
177 while (object) {
178 QQuickAccessibleAttached *att = QQuickAccessibleAttached::attachedProperties(obj: object);
179 if (att && (role == QAccessible::NoRole || att->role() == role)) {
180 break;
181 }
182 object = object->parent();
183 }
184 return object;
185 }
186
187 QAccessible::State state() const { return m_state; }
188 bool ignored() const;
189 bool doAction(const QString &actionName);
190 void availableActions(QStringList *actions) const;
191
192public Q_SLOTS:
193 void valueChanged() {
194 QAccessibleValueChangeEvent ev(parent(), parent()->property(name: "value"));
195 QAccessible::updateAccessibility(event: &ev);
196 }
197 void cursorPositionChanged() {
198 QAccessibleTextCursorEvent ev(parent(), parent()->property(name: "cursorPosition").toInt());
199 QAccessible::updateAccessibility(event: &ev);
200 }
201
202 void setIgnored(bool ignored);
203
204Q_SIGNALS:
205 void roleChanged();
206 void nameChanged();
207 void descriptionChanged();
208 void ignoredChanged();
209 void pressAction();
210 void toggleAction();
211 void increaseAction();
212 void decreaseAction();
213 void scrollUpAction();
214 void scrollDownAction();
215 void scrollLeftAction();
216 void scrollRightAction();
217 void previousPageAction();
218 void nextPageAction();
219
220private:
221 QQuickItem *item() const { return qobject_cast<QQuickItem*>(object: parent()); }
222
223 QAccessible::Role m_role;
224 QAccessible::State m_state;
225 QAccessible::State m_stateExplicitlySet;
226 QString m_name;
227 bool m_nameExplicitlySet = false;
228 QString m_description;
229
230 static QMetaMethod sigPress;
231 static QMetaMethod sigToggle;
232 static QMetaMethod sigIncrease;
233 static QMetaMethod sigDecrease;
234 static QMetaMethod sigScrollUp;
235 static QMetaMethod sigScrollDown;
236 static QMetaMethod sigScrollLeft;
237 static QMetaMethod sigScrollRight;
238 static QMetaMethod sigPreviousPage;
239 static QMetaMethod sigNextPage;
240
241public:
242 using QObject::property;
243};
244
245
246QT_END_NAMESPACE
247
248QML_DECLARE_TYPE(QQuickAccessibleAttached)
249
250#endif // accessibility
251
252#endif
253

source code of qtdeclarative/src/quick/items/qquickaccessibleattached_p.h