summaryrefslogtreecommitdiffstats
path: root/logic/IconListModel.cpp
blob: b531df434770aa6ff8073e31ff0fcabbc86ff45a (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "IconListModel.h"
#include <pathutils.h>
#include <QMap>
#include <QEventLoop>
#include <QDir>

#define MAX_SIZE 1024
IconList* IconList::m_Instance = 0;
QMutex IconList::mutex;

struct entry
{
	QString key;
	QString name;
	QIcon icon;
	bool is_builtin;
};

class Private : public QObject
{
	Q_OBJECT
public:
	QMap<QString, int> index;
	QVector<entry> icons;
	Private()
	{
	}
};


IconList::IconList() : QAbstractListModel(), d(new Private())
{
	QDir instance_icons(":/icons/instances/");
	auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name);
	for(auto file_info: file_info_list)
	{
		QString key = file_info.baseName();
		addIcon(key, key, file_info.absoluteFilePath(), true);
	}
	
	// FIXME: get from settings
	ensureFolderPathExists("icons");
	QDir user_icons("icons");
	file_info_list = user_icons.entryInfoList(QDir::Files, QDir::Name);
	for(auto file_info: file_info_list)
	{
		QString filename = file_info.absoluteFilePath();
		QString key = file_info.baseName();
		addIcon(key, key, filename);
	}
}

IconList::~IconList()
{
	delete d;
	d = nullptr;
}

QVariant IconList::data ( const QModelIndex& index, int role ) const
{
	if(!index.isValid())
		return QVariant();
	
	int row = index.row();
	
	if(row < 0 || row >= d->icons.size())
		return QVariant();
	
	switch(role)
	{
		case Qt::DecorationRole:
			return d->icons[row].icon;
		case Qt::DisplayRole:
			return d->icons[row].name;
		case Qt::UserRole:
			return d->icons[row].key;
		default:
			return QVariant();
	}
}

int IconList::rowCount ( const QModelIndex& parent ) const
{
	return d->icons.size();
}

bool IconList::addIcon ( QString key, QString name, QString path, bool is_builtin )
{
	auto iter = d->index.find(key);
	if(iter != d->index.end())
	{
		if(d->icons[*iter].is_builtin) return false;
		
		QIcon icon(path);
		if(icon.isNull()) return false;
		
		// replace the icon
		d->icons[*iter] = {key, name, icon, is_builtin};
		return true;
	}
	else
	{
		QIcon icon(path);
		if(icon.isNull()) return false;
		
		// add a new icon
		d->icons.push_back({key, name, icon, is_builtin});
		d->index[key] = d->icons.size() - 1;
		return true;
	}
}


QIcon IconList::getIcon ( QString key )
{
	int icon_index = getIconIndex(key);

	if(icon_index != -1)
		return d->icons[icon_index].icon;
	
	// Fallback for icons that don't exist.
	icon_index = getIconIndex("infinity");
	
	if(icon_index != -1)
		return d->icons[icon_index].icon;
	return QIcon();
}

int IconList::getIconIndex ( QString key )
{
	if(key == "default")
		key = "infinity";
	
	auto iter = d->index.find(key);
	if(iter != d->index.end())
		return *iter;
	
	
	return -1;
}


void IconList::drop()
{
	mutex.lock();
	delete m_Instance;
	m_Instance = 0;
	mutex.unlock();
}

IconList* IconList::instance()
{
	if ( !m_Instance )
	{
		mutex.lock();
		if ( !m_Instance )
			m_Instance = new IconList;
		mutex.unlock();
	}
	return m_Instance;
}

#include "IconListModel.moc"