GH-1559 Fix FTB icons

This was caused by separation of GUI and logic. Now logic has an interface that GUI implements.
It should be expanded upon later.
This commit is contained in:
Petr Mrázek 2016-05-03 00:27:28 +02:00
parent c50b3cdeec
commit e1a530f84d
10 changed files with 86 additions and 52 deletions

View File

@ -33,7 +33,7 @@ IconList::IconList(QString builtinPath, QString path, QObject *parent) : QAbstra
for (auto file_info : file_info_list)
{
QString key = file_info.baseName();
addIcon(key, key, file_info.absoluteFilePath(), MMCIcon::Builtin);
addIcon(key, key, file_info.absoluteFilePath(), IconType::Builtin);
}
m_watcher.reset(new QFileSystemWatcher());
@ -70,9 +70,9 @@ void IconList::directoryChanged(const QString &path)
QList<QString> current_list;
for (auto &it : icons)
{
if (!it.has(MMCIcon::FileBased))
if (!it.has(IconType::FileBased))
continue;
current_list.push_back(it.m_images[MMCIcon::FileBased].filename);
current_list.push_back(it.m_images[IconType::FileBased].filename);
}
QSet<QString> current_set = current_list.toSet();
@ -90,8 +90,8 @@ void IconList::directoryChanged(const QString &path)
int idx = getIconIndex(key);
if (idx == -1)
continue;
icons[idx].remove(MMCIcon::FileBased);
if (icons[idx].type() == MMCIcon::ToBeDeleted)
icons[idx].remove(IconType::FileBased);
if (icons[idx].type() == IconType::ToBeDeleted)
{
beginRemoveRows(QModelIndex(), idx, idx);
icons.remove(idx);
@ -111,7 +111,7 @@ void IconList::directoryChanged(const QString &path)
qDebug() << "Adding " << add;
QFileInfo addfile(add);
QString key = addfile.baseName();
if (addIcon(key, QString(), addfile.filePath(), MMCIcon::FileBased))
if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased))
{
m_watcher->addPath(add);
emit iconUpdated(key);
@ -133,7 +133,7 @@ void IconList::fileChanged(const QString &path)
if (!icon.availableSizes().size())
return;
icons[idx].m_images[MMCIcon::FileBased].icon = icon;
icons[idx].m_images[IconType::FileBased].icon = icon;
dataChanged(index(idx), index(idx));
emit iconUpdated(key);
}
@ -268,7 +268,7 @@ bool IconList::iconFileExists(QString key)
{
return false;
}
return iconEntry->has(MMCIcon::FileBased);
return iconEntry->has(IconType::FileBased);
}
const MMCIcon *IconList::icon(QString key)
@ -285,14 +285,14 @@ bool IconList::deleteIcon(QString key)
if (iconIdx == -1)
return false;
auto &iconEntry = icons[iconIdx];
if (iconEntry.has(MMCIcon::FileBased))
if (iconEntry.has(IconType::FileBased))
{
return QFile::remove(iconEntry.m_images[MMCIcon::FileBased].filename);
return QFile::remove(iconEntry.m_images[IconType::FileBased].filename);
}
return false;
}
bool IconList::addIcon(QString key, QString name, QString path, MMCIcon::Type type)
bool IconList::addIcon(QString key, QString name, QString path, IconType type)
{
// replace the icon even? is the input valid?
QIcon icon(path);

View File

@ -24,12 +24,13 @@
#include "MMCIcon.h"
#include "settings/Setting.h"
#include "Env.h" // there is a global icon list inside Env.
#include <icons/IIconList.h>
#include "multimc_gui_export.h"
class QFileSystemWatcher;
class MULTIMC_GUI_EXPORT IconList : public QAbstractListModel
class MULTIMC_GUI_EXPORT IconList : public QAbstractListModel, public IIconList
{
Q_OBJECT
public:
@ -40,18 +41,17 @@ public:
QIcon getBigIcon(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;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool addIcon(QString key, QString name, QString path, MMCIcon::Type type);
virtual bool addIcon(QString key, QString name, QString path, IconType type) override;
bool deleteIcon(QString key);
bool iconFileExists(QString key);
virtual QStringList mimeTypes() const;
virtual Qt::DropActions supportedDropActions() const;
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
const QModelIndex &parent);
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
virtual QStringList mimeTypes() const override;
virtual Qt::DropActions supportedDropActions() const override;
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
void installIcons(QStringList iconFiles);

View File

@ -16,19 +16,19 @@
#include "MMCIcon.h"
#include <QFileInfo>
MMCIcon::Type operator--(MMCIcon::Type &t, int)
IconType operator--(IconType &t, int)
{
MMCIcon::Type temp = t;
IconType temp = t;
switch (t)
{
case MMCIcon::Type::Builtin:
t = MMCIcon::Type::ToBeDeleted;
case IconType::Builtin:
t = IconType::ToBeDeleted;
break;
case MMCIcon::Type::Transient:
t = MMCIcon::Type::Builtin;
case IconType::Transient:
t = IconType::Builtin;
break;
case MMCIcon::Type::FileBased:
t = MMCIcon::Type::Transient;
case IconType::FileBased:
t = IconType::Transient;
break;
default:
{
@ -37,7 +37,7 @@ MMCIcon::Type operator--(MMCIcon::Type &t, int)
return temp;
}
MMCIcon::Type MMCIcon::type() const
IconType MMCIcon::type() const
{
return m_current_type;
}
@ -49,23 +49,23 @@ QString MMCIcon::name() const
return m_key;
}
bool MMCIcon::has(MMCIcon::Type _type) const
bool MMCIcon::has(IconType _type) const
{
return m_images[_type].present();
}
QIcon MMCIcon::icon() const
{
if (m_current_type == Type::ToBeDeleted)
if (m_current_type == IconType::ToBeDeleted)
return QIcon();
return m_images[m_current_type].icon;
}
void MMCIcon::remove(Type rm_type)
void MMCIcon::remove(IconType rm_type)
{
m_images[rm_type].filename = QString();
m_images[rm_type].icon = QIcon();
for (auto iter = rm_type; iter != Type::ToBeDeleted; iter--)
for (auto iter = rm_type; iter != IconType::ToBeDeleted; iter--)
{
if (m_images[iter].present())
{
@ -73,13 +73,13 @@ void MMCIcon::remove(Type rm_type)
return;
}
}
m_current_type = Type::ToBeDeleted;
m_current_type = IconType::ToBeDeleted;
}
void MMCIcon::replace(MMCIcon::Type new_type, QIcon icon, QString path)
void MMCIcon::replace(IconType new_type, QIcon icon, QString path)
{
QFileInfo foo(path);
if (new_type > m_current_type || m_current_type == MMCIcon::ToBeDeleted)
if (new_type > m_current_type || m_current_type == IconType::ToBeDeleted)
{
m_current_type = new_type;
}

View File

@ -17,6 +17,7 @@
#include <QString>
#include <QDateTime>
#include <QIcon>
#include <icons/IIconList.h>
#include "multimc_gui_export.h"
@ -33,23 +34,15 @@ struct MULTIMC_GUI_EXPORT MMCImage
struct MULTIMC_GUI_EXPORT MMCIcon
{
enum Type : unsigned
{
Builtin,
Transient,
FileBased,
ICONS_TOTAL,
ToBeDeleted
};
QString m_key;
QString m_name;
MMCImage m_images[ICONS_TOTAL];
Type m_current_type = ToBeDeleted;
IconType m_current_type = ToBeDeleted;
Type type() const;
IconType type() const;
QString name() const;
bool has(Type _type) const;
bool has(IconType _type) const;
QIcon icon() const;
void remove(Type rm_type);
void replace(Type new_type, QIcon icon, QString path = QString());
void remove(IconType rm_type);
void replace(IconType new_type, QIcon icon, QString path = QString());
};

View File

@ -43,6 +43,16 @@ std::shared_ptr< QNetworkAccessManager > Env::qnam()
return m_qnam;
}
std::shared_ptr<IIconList> Env::icons()
{
return m_iconlist;
}
void Env::registerIconList(std::shared_ptr<IIconList> iconlist)
{
m_iconlist = iconlist;
}
/*
class NullVersion : public BaseVersion
{

View File

@ -1,6 +1,7 @@
#pragma once
#include <memory>
#include "icons/IIconList.h"
#include <QString>
#include <QMap>
@ -32,6 +33,8 @@ public:
std::shared_ptr<HttpMetaCache> metacache();
std::shared_ptr<IIconList> icons();
/// init the cache. FIXME: possible future hook point
void initHttpMetaCache();
@ -46,6 +49,8 @@ public:
void registerVersionList(QString name, std::shared_ptr<BaseVersionList> vlist);
void registerIconList(std::shared_ptr<IIconList> iconlist);
std::shared_ptr<WonkoIndex> wonkoIndex();
QString wonkoRootUrl() const { return m_wonkoRootUrl; }
@ -54,6 +59,7 @@ public:
protected:
std::shared_ptr<QNetworkAccessManager> m_qnam;
std::shared_ptr<HttpMetaCache> m_metacache;
std::shared_ptr<IIconList> m_iconlist;
QMap<QString, std::shared_ptr<BaseVersionList>> m_versionLists;
std::shared_ptr<WonkoIndex> m_wonkoIndex;
QString m_wonkoRootUrl;

View File

@ -0,0 +1,20 @@
#pragma once
#include <QString>
enum IconType : unsigned
{
Builtin,
Transient,
FileBased,
ICONS_TOTAL,
ToBeDeleted
};
class IIconList
{
public:
virtual ~IIconList(){}
virtual bool addIcon(QString key, QString name, QString path, IconType type) = 0;
};

View File

@ -257,7 +257,11 @@ void FTBPlugin::loadInstances(SettingsObjectPtr globalSettings, QMap<QString, QS
{
qDebug() << "Loading FTB instance from " << record.instanceDir;
QString iconKey = record.iconKey;
// MMC->icons()->addIcon(iconKey, iconKey, FS::PathCombine(record.templateDir, record.logo), MMCIcon::Transient);
auto icons = ENV.icons();
if(icons)
{
icons->addIcon(iconKey, iconKey, FS::PathCombine(record.templateDir, record.logo), IconType::Transient);
}
auto settingsFilePath = FS::PathCombine(record.instanceDir, "instance.cfg");
qDebug() << "ICON get!";

View File

@ -344,6 +344,7 @@ void MultiMC::initIcons()
{
m_icons->directoryChanged(value.toString());
});
ENV.registerIconList(m_icons);
}

View File

@ -346,8 +346,8 @@ void SaveIcon(InstancePtr m_instance)
bool saveIcon = false;
switch(mmcIcon->type())
{
case MMCIcon::FileBased:
case MMCIcon::Transient:
case IconType::FileBased:
case IconType::Transient:
saveIcon = true;
default:
break;