| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | #include "qplatformaccessibility.h" |
| 4 | #include <private/qfactoryloader_p.h> |
| 5 | #include "qaccessibleplugin.h" |
| 6 | #include "qaccessibleobject.h" |
| 7 | #include "qaccessiblebridge.h" |
| 8 | #include <QtGui/QGuiApplication> |
| 9 | |
| 10 | #include <QDebug> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | using namespace Qt::StringLiterals; |
| 15 | |
| 16 | #if QT_CONFIG(accessibility) |
| 17 | |
| 18 | /* accessiblebridge plugin discovery stuff */ |
| 19 | Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, bridgeloader, |
| 20 | (QAccessibleBridgeFactoryInterface_iid, "/accessiblebridge"_L1)) |
| 21 | |
| 22 | Q_GLOBAL_STATIC(QList<QAccessibleBridge *>, bridges) |
| 23 | |
| 24 | /*! |
| 25 | \class QPlatformAccessibility |
| 26 | \since 5.0 |
| 27 | \internal |
| 28 | \preliminary |
| 29 | \ingroup qpa |
| 30 | \ingroup accessibility |
| 31 | |
| 32 | \brief The QPlatformAccessibility class is the base class for |
| 33 | integrating accessibility backends |
| 34 | |
| 35 | \sa QAccessible |
| 36 | */ |
| 37 | QPlatformAccessibility::QPlatformAccessibility() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | QPlatformAccessibility::~QPlatformAccessibility() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void QPlatformAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event) |
| 46 | { |
| 47 | initialize(); |
| 48 | |
| 49 | if (!bridges() || bridges()->isEmpty()) |
| 50 | return; |
| 51 | |
| 52 | for (int i = 0; i < bridges()->size(); ++i) |
| 53 | bridges()->at(i)->notifyAccessibilityUpdate(event); |
| 54 | } |
| 55 | |
| 56 | void QPlatformAccessibility::setRootObject(QObject *o) |
| 57 | { |
| 58 | initialize(); |
| 59 | if (bridges()->isEmpty()) |
| 60 | return; |
| 61 | |
| 62 | if (!o) |
| 63 | return; |
| 64 | |
| 65 | for (int i = 0; i < bridges()->size(); ++i) { |
| 66 | QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(o); |
| 67 | bridges()->at(i)->setRootObject(iface); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void QPlatformAccessibility::initialize() |
| 72 | { |
| 73 | static bool isInit = false; |
| 74 | if (isInit) |
| 75 | return; |
| 76 | isInit = true; // ### not atomic |
| 77 | |
| 78 | typedef QMultiMap<int, QString> PluginKeyMap; |
| 79 | typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; |
| 80 | |
| 81 | const PluginKeyMap keyMap = bridgeloader()->keyMap(); |
| 82 | QAccessibleBridgePlugin *factory = nullptr; |
| 83 | int i = -1; |
| 84 | const PluginKeyMapConstIterator cend = keyMap.constEnd(); |
| 85 | for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) { |
| 86 | if (it.key() != i) { |
| 87 | i = it.key(); |
| 88 | factory = qobject_cast<QAccessibleBridgePlugin*>(object: bridgeloader()->instance(index: i)); |
| 89 | } |
| 90 | if (factory) |
| 91 | if (QAccessibleBridge *bridge = factory->create(key: it.value())) |
| 92 | bridges()->append(t: bridge); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void QPlatformAccessibility::cleanup() |
| 97 | { |
| 98 | qDeleteAll(c: *bridges()); |
| 99 | } |
| 100 | |
| 101 | void qAccessibleNotifyActivationObservers(bool active); // qaccessible.cpp |
| 102 | |
| 103 | void QPlatformAccessibility::setActive(bool active) |
| 104 | { |
| 105 | m_active = active; |
| 106 | |
| 107 | // Send activeChanged notifications if the new active status differs from |
| 108 | // the notifed one. |
| 109 | if ((active && m_activeNotificationState != std::optional<bool>{true}) || |
| 110 | (!active && m_activeNotificationState != std::optional<bool>{false})) { |
| 111 | qAccessibleNotifyActivationObservers(active); |
| 112 | } |
| 113 | |
| 114 | m_activeNotificationState = active; |
| 115 | } |
| 116 | |
| 117 | void QPlatformAccessibility::clearActiveNotificationState() |
| 118 | { |
| 119 | m_activeNotificationState = std::nullopt; |
| 120 | } |
| 121 | |
| 122 | #endif // QT_CONFIG(accessibility) |
| 123 | |
| 124 | QT_END_NAMESPACE |
| 125 |
