GH-1031 include icon in exported instance if it is custom

This commit is contained in:
Petr Mrázek 2015-06-01 01:19:12 +02:00
parent 96c497f654
commit 405cea1778
6 changed files with 92 additions and 7 deletions

View File

@ -1137,7 +1137,27 @@ void MainWindow::instanceFromZipPack(QString instName, QString instGroup, QStrin
} }
newInstance->setName(instName); newInstance->setName(instName);
newInstance->setIconKey(instIcon); if(instIcon != "default")
{
newInstance->setIconKey(instIcon);
}
else
{
instIcon = newInstance->iconKey();
auto importIconPath = PathCombine(newInstance->instanceRoot(), instIcon + ".png");
if (QFile::exists(importIconPath))
{
// import icon
auto iconList = ENV.icons();
// FIXME: check if the file is OK before removing the existing one...
if(iconList->iconFileExists(instIcon))
{
//FIXME: ask if icon should be overwritten. Show difference in the question dialog.
iconList->deleteIcon(instIcon);
}
iconList->installIcons({importIconPath});
}
}
newInstance->setGroupInitial(instGroup); newInstance->setGroupInitial(instGroup);
MMC->instances()->add(InstancePtr(newInstance)); MMC->instances()->add(InstancePtr(newInstance));
MMC->instances()->saveGroupList(); MMC->instances()->saveGroupList();
@ -1225,7 +1245,6 @@ void MainWindow::on_actionAddInstance_triggered()
const QUrl modpackUrl = newInstDlg.modpackUrl(); const QUrl modpackUrl = newInstDlg.modpackUrl();
if (modpackUrl.isValid()) if (modpackUrl.isValid())
{ {
instanceFromZipPack(newInstDlg.instName(), newInstDlg.instGroup(), newInstDlg.iconKey(), modpackUrl); instanceFromZipPack(newInstDlg.instName(), newInstDlg.instGroup(), newInstDlg.iconKey(), modpackUrl);

View File

@ -28,6 +28,8 @@
#include <QSaveFile> #include <QSaveFile>
#include "MMCStrings.h" #include "MMCStrings.h"
#include "SeparatorPrefixTree.h" #include "SeparatorPrefixTree.h"
#include "Env.h"
#include <icons/IconList.h>
class PackIgnoreProxy : public QSortFilterProxyModel class PackIgnoreProxy : public QSortFilterProxyModel
{ {
@ -333,6 +335,51 @@ ExportInstanceDialog::~ExportInstanceDialog()
delete ui; delete ui;
} }
/// Save icon to instance's folder is needed
void SaveIcon(InstancePtr m_instance)
{
auto iconKey = m_instance->iconKey();
auto iconList = ENV.icons();
auto mmcIcon = iconList->icon(iconKey);
if(mmcIcon)
{
bool saveIcon = false;
switch(mmcIcon->type())
{
case MMCIcon::FileBased:
case MMCIcon::Transient:
saveIcon = true;
default:
break;
}
if(saveIcon)
{
auto & image = mmcIcon->m_images[mmcIcon->type()];
auto & icon = image.icon;
auto sizes = icon.availableSizes();
if(sizes.size() == 0)
{
return;
}
auto areaOf = [](QSize size)
{
return size.width() * size.height();
};
QSize largest = sizes[0];
// find variant with largest area
for(auto size: sizes)
{
if(areaOf(largest) < areaOf(size))
{
largest = size;
}
}
auto pixmap = icon.pixmap(largest);
pixmap.save(PathCombine(m_instance->instanceRoot(), iconKey + ".png"));
}
}
}
bool ExportInstanceDialog::doExport() bool ExportInstanceDialog::doExport()
{ {
auto name = RemoveInvalidFilenameChars(m_instance->name()); auto name = RemoveInvalidFilenameChars(m_instance->name());
@ -356,6 +403,8 @@ bool ExportInstanceDialog::doExport()
} }
} }
SaveIcon(m_instance);
if (!MMCZip::compressDir(output, m_instance->instanceRoot(), name, &proxyModel->blockedPaths())) if (!MMCZip::compressDir(output, m_instance->instanceRoot(), name, &proxyModel->blockedPaths()))
{ {
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance")); QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));

View File

@ -65,7 +65,7 @@ NewInstanceDialog::NewInstanceDialog(QWidget *parent)
layout()->setSizeConstraint(QLayout::SetFixedSize); layout()->setSizeConstraint(QLayout::SetFixedSize);
setSelectedVersion(MMC->minecraftlist()->getRecommended(), true); setSelectedVersion(MMC->minecraftlist()->getRecommended(), true);
InstIconKey = "infinity"; InstIconKey = "default";
ui->iconButton->setIcon(ENV.icons()->getIcon(InstIconKey)); ui->iconButton->setIcon(ENV.icons()->getIcon(InstIconKey));
ui->modpackEdit->setValidator(new UrlValidator(ui->modpackEdit)); ui->modpackEdit->setValidator(new UrlValidator(ui->modpackEdit));

View File

@ -41,10 +41,6 @@
</item> </item>
<item> <item>
<widget class="QToolButton" name="iconButton"> <widget class="QToolButton" name="iconButton">
<property name="icon">
<iconset>
<normaloff>:/icons/instances/infinity</normaloff>:/icons/instances/infinity</iconset>
</property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
<width>80</width> <width>80</width>

View File

@ -261,6 +261,24 @@ void IconList::installIcons(QStringList iconFiles)
} }
} }
bool IconList::iconFileExists(QString key)
{
auto iconEntry = icon(key);
if(!iconEntry)
{
return false;
}
return iconEntry->has(MMCIcon::FileBased);
}
const MMCIcon *IconList::icon(QString key)
{
int iconIdx = getIconIndex(key);
if (iconIdx == -1)
return nullptr;
return &icons[iconIdx];
}
bool IconList::deleteIcon(QString key) bool IconList::deleteIcon(QString key)
{ {
int iconIdx = getIconIndex(key); int iconIdx = getIconIndex(key);

View File

@ -43,6 +43,7 @@ public:
bool addIcon(QString key, QString name, QString path, MMCIcon::Type type); bool addIcon(QString key, QString name, QString path, MMCIcon::Type type);
bool deleteIcon(QString key); bool deleteIcon(QString key);
bool iconFileExists(QString key);
virtual QStringList mimeTypes() const; virtual QStringList mimeTypes() const;
virtual Qt::DropActions supportedDropActions() const; virtual Qt::DropActions supportedDropActions() const;
@ -52,6 +53,8 @@ public:
void installIcons(QStringList iconFiles); void installIcons(QStringList iconFiles);
const MMCIcon * icon(QString key);
void startWatching(); void startWatching();
void stopWatching(); void stopWatching();