summaryrefslogtreecommitdiffstats
path: root/api/logic/resources/ResourceObserver.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'api/logic/resources/ResourceObserver.cpp')
-rw-r--r--api/logic/resources/ResourceObserver.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/api/logic/resources/ResourceObserver.cpp b/api/logic/resources/ResourceObserver.cpp
new file mode 100644
index 00000000..4f168fd2
--- /dev/null
+++ b/api/logic/resources/ResourceObserver.cpp
@@ -0,0 +1,55 @@
+#include "ResourceObserver.h"
+
+#include <QDebug>
+
+#include "Resource.h"
+
+static const char *defaultPropertyForTarget(QObject *target)
+{
+ if (target->inherits("QLabel"))
+ {
+ return "pixmap";
+ }
+ else if (target->inherits("QAction") ||
+ target->inherits("QMenu") ||
+ target->inherits("QAbstractButton"))
+ {
+ return "icon";
+ }
+ // for unit tests
+ else if (target->inherits("DummyObserverObject"))
+ {
+ return "property";
+ }
+ else
+ {
+ Q_ASSERT_X(false, "ResourceObserver.cpp: defaultPropertyForTarget", "Unrecognized QObject subclass");
+ return nullptr;
+ }
+}
+
+QObjectResourceObserver::QObjectResourceObserver(QObject *target, const char *property)
+ : QObject(target), m_target(target)
+{
+ const QMetaObject *mo = m_target->metaObject();
+ m_property = mo->property(mo->indexOfProperty(
+ property ?
+ property
+ : defaultPropertyForTarget(target)));
+}
+void QObjectResourceObserver::resourceUpdated()
+{
+ m_property.write(m_target, getInternal(m_property.type()));
+}
+
+
+ResourceObserver::~ResourceObserver()
+{
+ m_resource->notifyObserverDeleted(this);
+}
+
+QVariant ResourceObserver::getInternal(const int typeId) const
+{
+ Q_ASSERT(m_resource);
+ return m_resource->getResourceInternal(typeId);
+}