summaryrefslogtreecommitdiffstats
path: root/backend
diff options
context:
space:
mode:
Diffstat (limited to 'backend')
-rw-r--r--backend/CMakeLists.txt2
-rw-r--r--backend/IconListModel.cpp163
-rw-r--r--backend/IconListModel.h33
3 files changed, 198 insertions, 0 deletions
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt
index ea19fbbf..0d0df5e5 100644
--- a/backend/CMakeLists.txt
+++ b/backend/CMakeLists.txt
@@ -52,6 +52,7 @@ lists/InstanceList.h
lists/InstVersionList.h
lists/MinecraftVersionList.h
lists/LwjglVersionList.h
+IconListModel.h
# Tasks
tasks/Task.h
@@ -87,6 +88,7 @@ lists/InstanceList.cpp
lists/InstVersionList.cpp
lists/MinecraftVersionList.cpp
lists/LwjglVersionList.cpp
+IconListModel.cpp
# Tasks
tasks/Task.cpp
diff --git a/backend/IconListModel.cpp b/backend/IconListModel.cpp
new file mode 100644
index 00000000..2d2fb6cf
--- /dev/null
+++ b/backend/IconListModel.cpp
@@ -0,0 +1,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
+ ensurePathExists("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" \ No newline at end of file
diff --git a/backend/IconListModel.h b/backend/IconListModel.h
new file mode 100644
index 00000000..31b05e64
--- /dev/null
+++ b/backend/IconListModel.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <QMutex>
+#include <QAbstractListModel>
+#include <QtGui/QIcon>
+
+class Private;
+
+class IconList : public QAbstractListModel
+{
+public:
+ static IconList* instance();
+ static void drop();
+ QIcon getIcon ( QString key );
+ int getIconIndex ( QString key );
+
+ virtual QVariant data ( const QModelIndex& index, int role = Qt::DisplayRole ) const;
+ virtual int rowCount ( const QModelIndex& parent = QModelIndex() ) const;
+
+ bool addIcon(QString key, QString name, QString path, bool is_builtin = false);
+
+
+private:
+ virtual ~IconList();
+ IconList();
+ // hide copy constructor
+ IconList ( const IconList & ) = delete;
+ // hide assign op
+ IconList& operator= ( const IconList & ) = delete;
+ static IconList* m_Instance;
+ static QMutex mutex;
+ Private* d;
+};