6#include <private/qabstractobjectregistryref_p.h>
7#include <private/qqmldata_p.h>
9#include <QtQml/qqmlengine.h>
13QObjectRegistrySingleton::QObjectRegistrySingleton(QObject *parent)
18QObjectRegistrySingleton::~QObjectRegistrySingleton()
20 for (
const auto &guards : std::as_const(m_objects))
24void QObjectRegistrySingleton::add(
const QString &key, QObject *obj)
26 if (key.isEmpty() || !obj)
29 if (QQmlData::wasDeleted(obj))
32 auto &guards = m_objects[key];
33 if (guards.contains(obj))
36 guards.insert(obj,
new ObjectGuard(
this, key, obj));
38 notifyRefs(key, obj, Notification::ObjectAdded);
41void QObjectRegistrySingleton::remove(
const QString &key, QObject *obj)
43 if (key.isEmpty() || !obj)
46 const auto keyIt = m_objects.find(key);
47 if (keyIt == m_objects.end())
50 const std::unique_ptr<ObjectGuard> guard(keyIt->take(obj));
55 m_objects.erase(keyIt);
57 notifyRefs(key, obj, Notification::ObjectRemoved);
60void QObjectRegistrySingleton::notifyRefs(
const QString &key, QObject *obj,
61 Notification notification)
65 const auto refs = m_refs.value(key);
66 for (
const auto &ref : refs) {
67 const auto keyIt = m_refs.constFind(key);
68 if (keyIt == m_refs.cend() || !keyIt->contains(ref))
71 if (notification == Notification::ObjectAdded)
72 ref->handleObjectAdded(obj);
74 ref->handleObjectRemoved(obj);
78QSet<QObject*> QObjectRegistrySingleton::objects(
const QString &key)
const
80 const auto keyIt = m_objects.constFind(key);
81 if (keyIt == m_objects.cend())
84 return QSet<QObject *>(keyIt->keyBegin(), keyIt->keyEnd());
87void QObjectRegistrySingleton::registerRef(QAbstractObjectRegistryRefPrivate *ref)
92 m_refs[ref->key()].insert(ref);
95void QObjectRegistrySingleton::deregisterRef(QAbstractObjectRegistryRefPrivate *ref)
100 if (m_refs.contains(ref->key())) {
101 m_refs[ref->key()].remove(ref);
102 if (m_refs[ref->key()].isEmpty())
103 m_refs.remove(ref->key());
107QObjectRegistrySingleton *QObjectRegistrySingleton::registryForObject(QObject *obj)
109 return registryForEngine(qmlEngine(obj));
112QObjectRegistrySingleton *QObjectRegistrySingleton::registryForEngine(QQmlEngine *engine)
115 static int typeId = qmlTypeId(
"QtQml.DesignSupport",
118 "InternalObjectRegistry");
120 return engine->singletonInstance<QObjectRegistrySingleton *>(typeId);
Combined button and popup list for selecting options.