summaryrefslogtreecommitdiffstats
path: root/logic/resources/IconResourceHandler.cpp
blob: 99353b6c88b1a41e48b9ef7114b695b89f2782f2 (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
56
57
58
59
60
61
62
#include "IconResourceHandler.h"

#include <QDir>
#include <QDebug>

QString IconResourceHandler::m_theme = "multimc";
QList<std::weak_ptr<IconResourceHandler>> IconResourceHandler::m_iconHandlers;

IconResourceHandler::IconResourceHandler(const QString &key)
	: m_key(key)
{
}

void IconResourceHandler::setTheme(const QString &theme)
{
	m_theme = theme;

	// notify everyone
	for (auto handler : m_iconHandlers)
	{
		std::shared_ptr<IconResourceHandler> ptr = handler.lock();
		if (ptr)
		{
			ptr->setResult(ptr->get());
		}
	}
}

void IconResourceHandler::init(std::shared_ptr<ResourceHandler> &ptr)
{
	m_iconHandlers.append(std::dynamic_pointer_cast<IconResourceHandler>(ptr));
	// we always have a result, so lets report it now!
	setResult(get());
}

QVariant IconResourceHandler::get() const
{
	const QDir iconsDir = QDir(":/icons/" + m_theme);

	QVariantMap out;
	for (const QFileInfo &sizeInfo : iconsDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot))
	{
		const QDir dir = QDir(sizeInfo.absoluteFilePath());
		const QString dirName = sizeInfo.fileName();
		const int size = dirName.left(dirName.indexOf('x')).toInt();
		if (dir.exists(m_key + ".png") && dirName != "scalable")
		{
			out.insert(dir.absoluteFilePath(m_key + ".png"), size);
		}
		else if (dir.exists(m_key + ".svg") && dirName == "scalable")
		{
			out.insert(dir.absoluteFilePath(m_key + ".svg"), size);
		}
	}

	if (out.isEmpty())
	{
		qWarning() << "Couldn't find any icons for" << m_key;
	}

	return out;
}