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 QtGui 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#include "qaccessiblecache_p.h"
41#include <QtCore/qdebug.h>
42#include <QtCore/qloggingcategory.h>
43
44#ifndef QT_NO_ACCESSIBILITY
45
46QT_BEGIN_NAMESPACE
47
48Q_LOGGING_CATEGORY(lcAccessibilityCache, "qt.accessibility.cache");
49
50/*!
51 \class QAccessibleCache
52 \internal
53 \brief Maintains a cache of accessible interfaces.
54*/
55
56static QAccessibleCache *accessibleCache = nullptr;
57
58static void cleanupAccessibleCache()
59{
60 delete accessibleCache;
61 accessibleCache = nullptr;
62}
63
64QAccessibleCache::~QAccessibleCache()
65{
66 for (QAccessible::Id id: idToInterface.keys())
67 deleteInterface(id);
68}
69
70QAccessibleCache *QAccessibleCache::instance()
71{
72 if (!accessibleCache) {
73 accessibleCache = new QAccessibleCache;
74 qAddPostRoutine(cleanupAccessibleCache);
75 }
76 return accessibleCache;
77}
78
79/*
80 The ID is always in the range [INT_MAX+1, UINT_MAX].
81 This makes it easy on windows to reserve the positive integer range
82 for the index of a child and not clash with the unique ids.
83*/
84QAccessible::Id QAccessibleCache::acquireId() const
85{
86 static const QAccessible::Id FirstId = QAccessible::Id(INT_MAX) + 1;
87 static QAccessible::Id lastUsedId = FirstId;
88
89 while (idToInterface.contains(akey: lastUsedId)) {
90 // (wrap back when when we reach UINT_MAX - 1)
91 // -1 because on Android -1 is taken for the "View" so just avoid it completely for consistency
92 if (lastUsedId == UINT_MAX - 1)
93 lastUsedId = FirstId;
94 else
95 ++lastUsedId;
96 }
97
98 return lastUsedId;
99}
100
101QAccessibleInterface *QAccessibleCache::interfaceForId(QAccessible::Id id) const
102{
103 return idToInterface.value(akey: id);
104}
105
106QAccessible::Id QAccessibleCache::idForInterface(QAccessibleInterface *iface) const
107{
108 return interfaceToId.value(akey: iface);
109}
110
111QAccessible::Id QAccessibleCache::insert(QObject *object, QAccessibleInterface *iface) const
112{
113 Q_ASSERT(iface);
114 Q_UNUSED(object)
115
116 // object might be 0
117 Q_ASSERT(!objectToId.contains(object));
118 Q_ASSERT_X(!interfaceToId.contains(iface), "", "Accessible interface inserted into cache twice!");
119
120 QAccessible::Id id = acquireId();
121 QObject *obj = iface->object();
122 Q_ASSERT(object == obj);
123 if (obj) {
124 objectToId.insert(akey: obj, avalue: id);
125 connect(sender: obj, signal: &QObject::destroyed, receiver: this, slot: &QAccessibleCache::objectDestroyed);
126 }
127 idToInterface.insert(akey: id, avalue: iface);
128 interfaceToId.insert(akey: iface, avalue: id);
129 qCDebug(lcAccessibilityCache) << "insert - id:" << id << " iface:" << iface;
130 return id;
131}
132
133void QAccessibleCache::objectDestroyed(QObject* obj)
134{
135 QAccessible::Id id = objectToId.value(akey: obj);
136 if (id) {
137 Q_ASSERT_X(idToInterface.contains(id), "", "QObject with accessible interface deleted, where interface not in cache!");
138 deleteInterface(id, obj);
139 }
140}
141
142void QAccessibleCache::deleteInterface(QAccessible::Id id, QObject *obj)
143{
144 QAccessibleInterface *iface = idToInterface.take(akey: id);
145 qCDebug(lcAccessibilityCache) << "delete - id:" << id << " iface:" << iface;
146 if (!iface) // the interface may be deleted already
147 return;
148 interfaceToId.take(akey: iface);
149 if (!obj)
150 obj = iface->object();
151 if (obj)
152 objectToId.remove(akey: obj);
153 delete iface;
154
155#ifdef Q_OS_MAC
156 removeCocoaElement(id);
157#endif
158}
159
160QT_END_NAMESPACE
161
162#endif
163

source code of qtbase/src/gui/accessible/qaccessiblecache.cpp