summaryrefslogtreecommitdiffstats
path: root/logic/resources/ResourceObserver.cpp
blob: 4f168fd24205459e93fe28001a527586319e8e44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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);
}