2022-05-26 22:18:54 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2023-08-04 18:41:47 +01:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-05-26 22:18:54 +01:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2013-12-31 00:24:28 +00:00
|
|
|
*
|
2022-05-26 22:18:54 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
2013-12-31 00:24:28 +00:00
|
|
|
*
|
2022-05-26 22:18:54 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-12-31 00:24:28 +00:00
|
|
|
*
|
2022-05-26 22:18:54 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-12-31 00:24:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IconList.h"
|
2015-10-05 00:47:27 +01:00
|
|
|
#include <FileSystem.h>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include <QDebug>
|
2013-12-31 00:24:28 +00:00
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QFileSystemWatcher>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include <QMap>
|
|
|
|
#include <QMimeData>
|
2015-02-01 10:44:47 +00:00
|
|
|
#include <QSet>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include <QUrl>
|
2013-12-31 00:24:28 +00:00
|
|
|
|
|
|
|
#define MAX_SIZE 1024
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
IconList::IconList(const QStringList& builtinPaths, QString path, QObject* parent) : QAbstractListModel(parent)
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QSet<QString> builtinNames;
|
|
|
|
|
|
|
|
// add builtin icons
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto& builtinPath : builtinPaths) {
|
2018-07-15 13:51:05 +01:00
|
|
|
QDir instance_icons(builtinPath);
|
|
|
|
auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name);
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto file_info : file_info_list) {
|
2022-05-13 21:21:35 +01:00
|
|
|
builtinNames.insert(file_info.completeBaseName());
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto& builtinName : builtinNames) {
|
2018-07-15 13:51:05 +01:00
|
|
|
addThemeIcon(builtinName);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_watcher.reset(new QFileSystemWatcher());
|
|
|
|
is_watching = false;
|
2023-04-18 01:51:34 +01:00
|
|
|
connect(m_watcher.get(), &QFileSystemWatcher::directoryChanged, this, &IconList::directoryChanged);
|
|
|
|
connect(m_watcher.get(), &QFileSystemWatcher::fileChanged, this, &IconList::fileChanged);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
|
|
|
directoryChanged(path);
|
2022-05-13 21:21:35 +01:00
|
|
|
|
|
|
|
// Forces the UI to update, so that lengthy icon names are shown properly from the start
|
|
|
|
emit iconUpdated({});
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 18:12:50 +01:00
|
|
|
void IconList::sortIconList()
|
|
|
|
{
|
|
|
|
qDebug() << "Sorting icon list...";
|
2023-08-02 17:35:35 +01:00
|
|
|
std::sort(icons.begin(), icons.end(), [](const MMCIcon& a, const MMCIcon& b) { return a.m_key.localeAwareCompare(b.m_key) < 0; });
|
2022-06-06 18:12:50 +01:00
|
|
|
reindex();
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void IconList::directoryChanged(const QString& path)
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
QDir new_dir(path);
|
|
|
|
if (m_dir.absolutePath() != new_dir.absolutePath()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_dir.setPath(path);
|
|
|
|
m_dir.refresh();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (is_watching)
|
2018-07-15 13:51:05 +01:00
|
|
|
stopWatching();
|
|
|
|
startWatching();
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_dir.exists())
|
|
|
|
if (!FS::ensureFolderPathExists(m_dir.absolutePath()))
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
m_dir.refresh();
|
|
|
|
auto new_list = m_dir.entryList(QDir::Files, QDir::Name);
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto it = new_list.begin(); it != new_list.end(); it++) {
|
|
|
|
QString& foo = (*it);
|
2018-07-15 13:51:05 +01:00
|
|
|
foo = m_dir.filePath(foo);
|
|
|
|
}
|
2022-05-02 18:10:45 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
QSet<QString> new_set(new_list.begin(), new_list.end());
|
|
|
|
#else
|
2018-07-15 13:51:05 +01:00
|
|
|
auto new_set = new_list.toSet();
|
2022-05-02 18:10:45 +01:00
|
|
|
#endif
|
2018-07-15 13:51:05 +01:00
|
|
|
QList<QString> current_list;
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto& it : icons) {
|
2018-07-15 13:51:05 +01:00
|
|
|
if (!it.has(IconType::FileBased))
|
|
|
|
continue;
|
|
|
|
current_list.push_back(it.m_images[IconType::FileBased].filename);
|
|
|
|
}
|
2022-05-02 18:10:45 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
QSet<QString> current_set(current_list.begin(), current_list.end());
|
|
|
|
#else
|
2018-07-15 13:51:05 +01:00
|
|
|
QSet<QString> current_set = current_list.toSet();
|
2022-05-02 18:10:45 +01:00
|
|
|
#endif
|
2018-07-15 13:51:05 +01:00
|
|
|
|
|
|
|
QSet<QString> to_remove = current_set;
|
|
|
|
to_remove -= new_set;
|
|
|
|
|
|
|
|
QSet<QString> to_add = new_set;
|
|
|
|
to_add -= current_set;
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto remove : to_remove) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Removing " << remove;
|
|
|
|
QFileInfo rmfile(remove);
|
2022-05-13 21:21:35 +01:00
|
|
|
QString key = rmfile.completeBaseName();
|
|
|
|
|
|
|
|
QString suffix = rmfile.suffix();
|
|
|
|
// The icon doesnt have a suffix, but it can have other .s in the name, so we account for those as well
|
|
|
|
if (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif")
|
|
|
|
key = rmfile.fileName();
|
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
int idx = getIconIndex(key);
|
|
|
|
if (idx == -1)
|
|
|
|
continue;
|
|
|
|
icons[idx].remove(IconType::FileBased);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (icons[idx].type() == IconType::ToBeDeleted) {
|
2018-07-15 13:51:05 +01:00
|
|
|
beginRemoveRows(QModelIndex(), idx, idx);
|
|
|
|
icons.remove(idx);
|
|
|
|
reindex();
|
|
|
|
endRemoveRows();
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
dataChanged(index(idx), index(idx));
|
|
|
|
}
|
|
|
|
m_watcher->removePath(remove);
|
|
|
|
emit iconUpdated(key);
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto add : to_add) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Adding " << add;
|
2022-05-13 21:21:35 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
QFileInfo addfile(add);
|
2022-05-13 21:21:35 +01:00
|
|
|
QString key = addfile.completeBaseName();
|
|
|
|
|
|
|
|
QString suffix = addfile.suffix();
|
|
|
|
// The icon doesnt have a suffix, but it can have other .s in the name, so we account for those as well
|
|
|
|
if (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif")
|
|
|
|
key = addfile.fileName();
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_watcher->addPath(add);
|
|
|
|
emit iconUpdated(key);
|
|
|
|
}
|
|
|
|
}
|
2022-06-06 18:12:50 +01:00
|
|
|
|
|
|
|
sortIconList();
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void IconList::fileChanged(const QString& path)
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Checking " << path;
|
|
|
|
QFileInfo checkfile(path);
|
|
|
|
if (!checkfile.exists())
|
|
|
|
return;
|
2022-05-13 21:21:35 +01:00
|
|
|
QString key = checkfile.completeBaseName();
|
2018-07-15 13:51:05 +01:00
|
|
|
int idx = getIconIndex(key);
|
|
|
|
if (idx == -1)
|
|
|
|
return;
|
|
|
|
QIcon icon(path);
|
|
|
|
if (!icon.availableSizes().size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
icons[idx].m_images[IconType::FileBased].icon = icon;
|
|
|
|
dataChanged(index(idx), index(idx));
|
|
|
|
emit iconUpdated(key);
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void IconList::SettingChanged(const Setting& setting, QVariant value)
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (setting.id() != "IconsDir")
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
2013-12-31 00:24:28 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
directoryChanged(value.toString());
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconList::startWatching()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
auto abs_path = m_dir.absolutePath();
|
|
|
|
FS::ensureFolderPathExists(abs_path);
|
|
|
|
is_watching = m_watcher->addPath(abs_path);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (is_watching) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Started watching " << abs_path;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Failed to start watching " << abs_path;
|
|
|
|
}
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconList::stopWatching()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_watcher->removePaths(m_watcher->files());
|
|
|
|
m_watcher->removePaths(m_watcher->directories());
|
|
|
|
is_watching = false;
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList IconList::mimeTypes() const
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QStringList types;
|
|
|
|
types << "text/uri-list";
|
|
|
|
return types;
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
Qt::DropActions IconList::supportedDropActions() const
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return Qt::CopyAction;
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool IconList::dropMimeData(const QMimeData* data,
|
|
|
|
Qt::DropAction action,
|
|
|
|
[[maybe_unused]] int row,
|
|
|
|
[[maybe_unused]] int column,
|
|
|
|
[[maybe_unused]] const QModelIndex& parent)
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
if (action == Qt::IgnoreAction)
|
|
|
|
return true;
|
|
|
|
// check if the action is supported
|
|
|
|
if (!data || !(action & supportedDropActions()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// files dropped from outside?
|
2023-08-02 17:35:35 +01:00
|
|
|
if (data->hasUrls()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
auto urls = data->urls();
|
|
|
|
QStringList iconFiles;
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto url : urls) {
|
2018-07-15 13:51:05 +01:00
|
|
|
// only local files may be dropped...
|
|
|
|
if (!url.isLocalFile())
|
|
|
|
continue;
|
|
|
|
iconFiles += url.toLocalFile();
|
|
|
|
}
|
|
|
|
installIcons(iconFiles);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
Qt::ItemFlags IconList::flags(const QModelIndex& index) const
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
|
|
|
|
if (index.isValid())
|
|
|
|
return Qt::ItemIsDropEnabled | defaultFlags;
|
|
|
|
else
|
|
|
|
return Qt::ItemIsDropEnabled | defaultFlags;
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QVariant IconList::data(const QModelIndex& index, int role) const
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
int row = index.row();
|
|
|
|
|
|
|
|
if (row < 0 || row >= icons.size())
|
|
|
|
return QVariant();
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
return icons[row].icon();
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return icons[row].name();
|
|
|
|
case Qt::UserRole:
|
|
|
|
return icons[row].m_key;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
int IconList::rowCount(const QModelIndex& parent) const
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2022-11-13 14:35:55 +00:00
|
|
|
return parent.isValid() ? 0 : icons.size();
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void IconList::installIcons(const QStringList& iconFiles)
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
for (QString file : iconFiles) {
|
2018-07-15 13:51:05 +01:00
|
|
|
QFileInfo fileinfo(file);
|
|
|
|
if (!fileinfo.isReadable() || !fileinfo.isFile())
|
|
|
|
continue;
|
2022-05-27 13:15:32 +01:00
|
|
|
QString target = FS::PathCombine(getDirectory(), fileinfo.fileName());
|
2018-07-15 13:51:05 +01:00
|
|
|
|
|
|
|
QString suffix = fileinfo.suffix();
|
2018-10-31 20:54:22 +00:00
|
|
|
if (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif")
|
2018-07-15 13:51:05 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!QFile::copy(file, target))
|
|
|
|
continue;
|
|
|
|
}
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void IconList::installIcon(const QString& file, const QString& name)
|
2018-04-07 15:15:58 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QFileInfo fileinfo(file);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!fileinfo.isReadable() || !fileinfo.isFile())
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
2018-04-07 15:15:58 +01:00
|
|
|
|
2022-05-27 13:15:32 +01:00
|
|
|
QString target = FS::PathCombine(getDirectory(), name);
|
2018-04-07 15:15:58 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
QFile::copy(file, target);
|
2018-04-07 15:15:58 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool IconList::iconFileExists(const QString& key) const
|
2015-06-01 00:19:12 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
auto iconEntry = icon(key);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!iconEntry) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return iconEntry->has(IconType::FileBased);
|
2015-06-01 00:19:12 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
const MMCIcon* IconList::icon(const QString& key) const
|
2015-06-01 00:19:12 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
int iconIdx = getIconIndex(key);
|
|
|
|
if (iconIdx == -1)
|
|
|
|
return nullptr;
|
|
|
|
return &icons[iconIdx];
|
2015-06-01 00:19:12 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool IconList::deleteIcon(const QString& key)
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2022-12-06 23:25:42 +00:00
|
|
|
if (!iconFileExists(key))
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
2022-12-06 23:25:42 +00:00
|
|
|
|
2022-12-10 13:31:30 +00:00
|
|
|
return QFile::remove(icon(key)->getFilePath());
|
2022-12-06 23:25:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool IconList::trashIcon(const QString& key)
|
2022-12-06 23:25:42 +00:00
|
|
|
{
|
|
|
|
if (!iconFileExists(key))
|
|
|
|
return false;
|
|
|
|
|
2022-12-10 13:31:44 +00:00
|
|
|
return FS::trash(icon(key)->getFilePath(), nullptr);
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 01:54:53 +00:00
|
|
|
bool IconList::addThemeIcon(const QString& key)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
auto iter = name_index.find(key);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (iter != name_index.end()) {
|
|
|
|
auto& oldOne = icons[*iter];
|
2018-07-15 13:51:05 +01:00
|
|
|
oldOne.replace(Builtin, key);
|
|
|
|
dataChanged(index(*iter), index(*iter));
|
|
|
|
return true;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
// add a new icon
|
|
|
|
beginInsertRows(QModelIndex(), icons.size(), icons.size());
|
|
|
|
{
|
|
|
|
MMCIcon mmc_icon;
|
|
|
|
mmc_icon.m_name = key;
|
|
|
|
mmc_icon.m_key = key;
|
|
|
|
mmc_icon.replace(Builtin, key);
|
|
|
|
icons.push_back(mmc_icon);
|
|
|
|
name_index[key] = icons.size() - 1;
|
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-10 01:54:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool IconList::addIcon(const QString& key, const QString& name, const QString& path, const IconType type)
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
// replace the icon even? is the input valid?
|
|
|
|
QIcon icon(path);
|
|
|
|
if (icon.isNull())
|
|
|
|
return false;
|
|
|
|
auto iter = name_index.find(key);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (iter != name_index.end()) {
|
|
|
|
auto& oldOne = icons[*iter];
|
2018-07-15 13:51:05 +01:00
|
|
|
oldOne.replace(type, icon, path);
|
|
|
|
dataChanged(index(*iter), index(*iter));
|
|
|
|
return true;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
// add a new icon
|
|
|
|
beginInsertRows(QModelIndex(), icons.size(), icons.size());
|
|
|
|
{
|
|
|
|
MMCIcon mmc_icon;
|
|
|
|
mmc_icon.m_name = name;
|
|
|
|
mmc_icon.m_key = key;
|
|
|
|
mmc_icon.replace(type, icon, path);
|
|
|
|
icons.push_back(mmc_icon);
|
|
|
|
name_index[key] = icons.size() - 1;
|
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void IconList::saveIcon(const QString& key, const QString& path, const char* format) const
|
2016-10-20 00:02:28 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
auto icon = getIcon(key);
|
|
|
|
auto pixmap = icon.pixmap(128, 128);
|
|
|
|
pixmap.save(path, format);
|
2016-10-20 00:02:28 +01:00
|
|
|
}
|
|
|
|
|
2013-12-31 00:24:28 +00:00
|
|
|
void IconList::reindex()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
name_index.clear();
|
|
|
|
int i = 0;
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto& iter : icons) {
|
2018-07-15 13:51:05 +01:00
|
|
|
name_index[iter.m_key] = i;
|
|
|
|
i++;
|
|
|
|
}
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QIcon IconList::getIcon(const QString& key) const
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
int icon_index = getIconIndex(key);
|
2013-12-31 00:24:28 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if (icon_index != -1)
|
|
|
|
return icons[icon_index].icon();
|
2013-12-31 00:24:28 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
// Fallback for icons that don't exist.
|
2021-10-15 23:42:01 +01:00
|
|
|
icon_index = getIconIndex("grass");
|
2013-12-31 00:24:28 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
if (icon_index != -1)
|
|
|
|
return icons[icon_index].icon();
|
|
|
|
return QIcon();
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
int IconList::getIconIndex(const QString& key) const
|
2013-12-31 00:24:28 +00:00
|
|
|
{
|
2021-10-15 23:42:01 +01:00
|
|
|
auto iter = name_index.find(key == "default" ? "grass" : key);
|
2018-07-15 13:51:05 +01:00
|
|
|
if (iter != name_index.end())
|
|
|
|
return *iter;
|
2013-12-31 00:24:28 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
return -1;
|
2013-12-31 00:24:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 01:01:12 +00:00
|
|
|
QString IconList::getDirectory() const
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return m_dir.absolutePath();
|
2018-02-05 01:01:12 +00:00
|
|
|
}
|
|
|
|
|
2014-01-02 17:51:40 +00:00
|
|
|
//#include "IconList.moc"
|