mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2025-06-13 05:37:42 +02:00
Added search and support for subdirectories to icon picker
Signed-off-by: QazCetelic <qaz.cetelic@protonmail.com>
This commit is contained in:
@ -56,7 +56,7 @@ IconList::IconList(const QStringList& builtinPaths, QString path, QObject* paren
|
|||||||
QDir instance_icons(builtinPath);
|
QDir instance_icons(builtinPath);
|
||||||
auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name);
|
auto file_info_list = instance_icons.entryInfoList(QDir::Files, QDir::Name);
|
||||||
for (auto file_info : file_info_list) {
|
for (auto file_info : file_info_list) {
|
||||||
builtinNames.insert(file_info.completeBaseName());
|
builtinNames.insert(file_info.baseName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto& builtinName : builtinNames) {
|
for (auto& builtinName : builtinNames) {
|
||||||
@ -77,10 +77,95 @@ IconList::IconList(const QStringList& builtinPaths, QString path, QObject* paren
|
|||||||
void IconList::sortIconList()
|
void IconList::sortIconList()
|
||||||
{
|
{
|
||||||
qDebug() << "Sorting icon list...";
|
qDebug() << "Sorting icon list...";
|
||||||
std::sort(icons.begin(), icons.end(), [](const MMCIcon& a, const MMCIcon& b) { return a.m_key.localeAwareCompare(b.m_key) < 0; });
|
std::sort(icons.begin(), icons.end(), [](const MMCIcon& a, const MMCIcon& b) {
|
||||||
|
bool aIsSubdir = a.m_key.contains(std::filesystem::path::preferred_separator);
|
||||||
|
bool bIsSubdir = b.m_key.contains(std::filesystem::path::preferred_separator);
|
||||||
|
if (aIsSubdir != bIsSubdir) {
|
||||||
|
return !aIsSubdir; // root-level icons come first
|
||||||
|
}
|
||||||
|
return a.m_key.localeAwareCompare(b.m_key) < 0;
|
||||||
|
});
|
||||||
reindex();
|
reindex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to add directories recursively
|
||||||
|
bool IconList::addPathRecursively(const QString& path)
|
||||||
|
{
|
||||||
|
QDir dir(path);
|
||||||
|
if (!dir.exists())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
bool watching = false;
|
||||||
|
|
||||||
|
// Add the directory itself
|
||||||
|
watching = m_watcher->addPath(path);
|
||||||
|
|
||||||
|
// Add all subdirectories
|
||||||
|
QFileInfoList entries = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
|
||||||
|
for (const QFileInfo& entry : entries)
|
||||||
|
{
|
||||||
|
if (addPathRecursively(entry.absoluteFilePath()))
|
||||||
|
{
|
||||||
|
watching = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return watching;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconList::removePathRecursively(const QString& path)
|
||||||
|
{
|
||||||
|
QFileInfo file_info(path);
|
||||||
|
if (file_info.isFile()) {
|
||||||
|
// Remove the icon belonging to the file
|
||||||
|
QString key = m_dir.relativeFilePath(file_info.absoluteFilePath());
|
||||||
|
int idx = getIconIndex(key);
|
||||||
|
if (idx == -1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (file_info.isDir()) {
|
||||||
|
// Remove the directory itself
|
||||||
|
m_watcher->removePath(path);
|
||||||
|
|
||||||
|
const QDir dir(path);
|
||||||
|
// Remove all files within the directory
|
||||||
|
for (const QFileInfo& file : dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot))
|
||||||
|
{
|
||||||
|
removePathRecursively(file.absoluteFilePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList IconList::getIconFilePaths() const
|
||||||
|
{
|
||||||
|
QStringList icon_files {};
|
||||||
|
QStringList directories {m_dir.absolutePath()};
|
||||||
|
while (!directories.isEmpty()) {
|
||||||
|
QString first = directories.takeFirst();
|
||||||
|
QDir dir(first);
|
||||||
|
for (QString& file_name : dir.entryList(QDir::AllDirs | QDir::Files |QDir::NoDotAndDotDot, QDir::Name)) {
|
||||||
|
QString full_path = dir.filePath(file_name); // Convert to full path
|
||||||
|
QFileInfo file_info(full_path);
|
||||||
|
if (file_info.isDir())
|
||||||
|
directories.push_back(full_path);
|
||||||
|
else
|
||||||
|
icon_files.push_back(full_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return icon_files;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString formatName(const QDir& icons_dir, const QFileInfo& file)
|
||||||
|
{
|
||||||
|
if (file.dir() == icons_dir) {
|
||||||
|
return file.baseName();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const QString delimiter = " » ";
|
||||||
|
return (icons_dir.relativeFilePath(file.dir().path()) + std::filesystem::path::preferred_separator + file.baseName()).replace(std::filesystem::path::preferred_separator, delimiter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void IconList::directoryChanged(const QString& path)
|
void IconList::directoryChanged(const QString& path)
|
||||||
{
|
{
|
||||||
QDir new_dir(path);
|
QDir new_dir(path);
|
||||||
@ -95,13 +180,9 @@ void IconList::directoryChanged(const QString& path)
|
|||||||
if (!FS::ensureFolderPathExists(m_dir.absolutePath()))
|
if (!FS::ensureFolderPathExists(m_dir.absolutePath()))
|
||||||
return;
|
return;
|
||||||
m_dir.refresh();
|
m_dir.refresh();
|
||||||
auto new_list = m_dir.entryList(QDir::Files, QDir::Name);
|
QStringList new_file_names_list = getIconFilePaths();
|
||||||
for (auto it = new_list.begin(); it != new_list.end(); it++) {
|
|
||||||
QString& foo = (*it);
|
|
||||||
foo = m_dir.filePath(foo);
|
|
||||||
}
|
|
||||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
||||||
QSet<QString> new_set(new_list.begin(), new_list.end());
|
QSet<QString> new_set(new_file_names_list.begin(), new_file_names_list.end());
|
||||||
#else
|
#else
|
||||||
auto new_set = new_list.toSet();
|
auto new_set = new_list.toSet();
|
||||||
#endif
|
#endif
|
||||||
@ -123,21 +204,16 @@ void IconList::directoryChanged(const QString& path)
|
|||||||
QSet<QString> to_add = new_set;
|
QSet<QString> to_add = new_set;
|
||||||
to_add -= current_set;
|
to_add -= current_set;
|
||||||
|
|
||||||
for (auto remove : to_remove) {
|
for (const auto& remove : to_remove) {
|
||||||
qDebug() << "Removing " << remove;
|
qDebug() << "Removing " << remove;
|
||||||
QFileInfo rmfile(remove);
|
QFileInfo removed_file(remove);
|
||||||
QString key = rmfile.completeBaseName();
|
QString key = m_dir.relativeFilePath(removed_file.absoluteFilePath());
|
||||||
|
|
||||||
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 (!IconUtils::isIconSuffix(suffix))
|
|
||||||
key = rmfile.fileName();
|
|
||||||
|
|
||||||
int idx = getIconIndex(key);
|
int idx = getIconIndex(key);
|
||||||
if (idx == -1)
|
if (idx == -1)
|
||||||
continue;
|
continue;
|
||||||
icons[idx].remove(IconType::FileBased);
|
icons[idx].remove(FileBased);
|
||||||
if (icons[idx].type() == IconType::ToBeDeleted) {
|
if (icons[idx].type() == ToBeDeleted) {
|
||||||
beginRemoveRows(QModelIndex(), idx, idx);
|
beginRemoveRows(QModelIndex(), idx, idx);
|
||||||
icons.remove(idx);
|
icons.remove(idx);
|
||||||
reindex();
|
reindex();
|
||||||
@ -149,18 +225,14 @@ void IconList::directoryChanged(const QString& path)
|
|||||||
emit iconUpdated(key);
|
emit iconUpdated(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto add : to_add) {
|
for (const auto& add : to_add) {
|
||||||
qDebug() << "Adding " << add;
|
qDebug() << "Adding " << add;
|
||||||
|
|
||||||
QFileInfo addfile(add);
|
QFileInfo addfile(add);
|
||||||
QString key = addfile.completeBaseName();
|
QString key = m_dir.relativeFilePath(addfile.absoluteFilePath());
|
||||||
|
QString name = formatName(m_dir, addfile);
|
||||||
|
|
||||||
QString suffix = addfile.suffix();
|
if (addIcon(key, name, addfile.filePath(), IconType::FileBased)) {
|
||||||
// The icon doesnt have a suffix, but it can have other .s in the name, so we account for those as well
|
|
||||||
if (!IconUtils::isIconSuffix(suffix))
|
|
||||||
key = addfile.fileName();
|
|
||||||
|
|
||||||
if (addIcon(key, QString(), addfile.filePath(), IconType::FileBased)) {
|
|
||||||
m_watcher->addPath(add);
|
m_watcher->addPath(add);
|
||||||
emit iconUpdated(key);
|
emit iconUpdated(key);
|
||||||
}
|
}
|
||||||
@ -175,7 +247,7 @@ void IconList::fileChanged(const QString& path)
|
|||||||
QFileInfo checkfile(path);
|
QFileInfo checkfile(path);
|
||||||
if (!checkfile.exists())
|
if (!checkfile.exists())
|
||||||
return;
|
return;
|
||||||
QString key = checkfile.completeBaseName();
|
QString key = m_dir.relativeFilePath(checkfile.absoluteFilePath());
|
||||||
int idx = getIconIndex(key);
|
int idx = getIconIndex(key);
|
||||||
if (idx == -1)
|
if (idx == -1)
|
||||||
return;
|
return;
|
||||||
@ -200,7 +272,7 @@ void IconList::startWatching()
|
|||||||
{
|
{
|
||||||
auto abs_path = m_dir.absolutePath();
|
auto abs_path = m_dir.absolutePath();
|
||||||
FS::ensureFolderPathExists(abs_path);
|
FS::ensureFolderPathExists(abs_path);
|
||||||
is_watching = m_watcher->addPath(abs_path);
|
is_watching = addPathRecursively(abs_path);
|
||||||
if (is_watching) {
|
if (is_watching) {
|
||||||
qDebug() << "Started watching " << abs_path;
|
qDebug() << "Started watching " << abs_path;
|
||||||
} else {
|
} else {
|
||||||
@ -312,6 +384,7 @@ bool IconList::iconFileExists(const QString& key) const
|
|||||||
return iconEntry && iconEntry->has(IconType::FileBased);
|
return iconEntry && iconEntry->has(IconType::FileBased);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the icon with the given key or nullptr if it doesn't exist.
|
||||||
const MMCIcon* IconList::icon(const QString& key) const
|
const MMCIcon* IconList::icon(const QString& key) const
|
||||||
{
|
{
|
||||||
int iconIdx = getIconIndex(key);
|
int iconIdx = getIconIndex(key);
|
||||||
@ -394,6 +467,7 @@ void IconList::reindex()
|
|||||||
for (auto& iter : icons) {
|
for (auto& iter : icons) {
|
||||||
name_index[iter.m_key] = i;
|
name_index[iter.m_key] = i;
|
||||||
i++;
|
i++;
|
||||||
|
emit iconUpdated(iter.m_key); // prevents incorrect indices with proxy model
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,3 +499,15 @@ QString IconList::getDirectory() const
|
|||||||
{
|
{
|
||||||
return m_dir.absolutePath();
|
return m_dir.absolutePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the directory of the icon with the given key or the default directory if it's a builtin icon.
|
||||||
|
QString IconList::iconDirectory(const QString& key) const
|
||||||
|
{
|
||||||
|
for (auto mmc_icon : icons) {
|
||||||
|
if (mmc_icon.m_key == key && mmc_icon.has(IconType::FileBased)) {
|
||||||
|
QFileInfo icon_file(mmc_icon.getFilePath());
|
||||||
|
return icon_file.dir().path();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getDirectory();
|
||||||
|
}
|
||||||
|
@ -72,6 +72,7 @@ class IconList : public QAbstractListModel {
|
|||||||
bool deleteIcon(const QString& key);
|
bool deleteIcon(const QString& key);
|
||||||
bool trashIcon(const QString& key);
|
bool trashIcon(const QString& key);
|
||||||
bool iconFileExists(const QString& key) const;
|
bool iconFileExists(const QString& key) const;
|
||||||
|
QString iconDirectory(const QString& key) const;
|
||||||
|
|
||||||
void installIcons(const QStringList& iconFiles);
|
void installIcons(const QStringList& iconFiles);
|
||||||
void installIcon(const QString& file, const QString& name);
|
void installIcon(const QString& file, const QString& name);
|
||||||
@ -91,6 +92,9 @@ class IconList : public QAbstractListModel {
|
|||||||
IconList& operator=(const IconList&) = delete;
|
IconList& operator=(const IconList&) = delete;
|
||||||
void reindex();
|
void reindex();
|
||||||
void sortIconList();
|
void sortIconList();
|
||||||
|
bool addPathRecursively(const QString& path);
|
||||||
|
void removePathRecursively(const QString& path);
|
||||||
|
QStringList getIconFilePaths() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void directoryChanged(const QString& path);
|
void directoryChanged(const QString& path);
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
|
#include <QSortFilterProxyModel>
|
||||||
|
#include <QLineEdit>
|
||||||
|
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
|
|
||||||
@ -33,6 +35,15 @@ IconPickerDialog::IconPickerDialog(QWidget* parent) : QDialog(parent), ui(new Ui
|
|||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
setWindowModality(Qt::WindowModal);
|
setWindowModality(Qt::WindowModal);
|
||||||
|
|
||||||
|
searchBar = new QLineEdit(this);
|
||||||
|
searchBar->setPlaceholderText(tr("Search..."));
|
||||||
|
ui->verticalLayout->insertWidget(0, searchBar);
|
||||||
|
|
||||||
|
proxyModel = new QSortFilterProxyModel(this);
|
||||||
|
proxyModel->setSourceModel(APPLICATION->icons().get());
|
||||||
|
proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
|
||||||
|
ui->iconView->setModel(proxyModel);
|
||||||
|
|
||||||
auto contentsWidget = ui->iconView;
|
auto contentsWidget = ui->iconView;
|
||||||
contentsWidget->setViewMode(QListView::IconMode);
|
contentsWidget->setViewMode(QListView::IconMode);
|
||||||
contentsWidget->setFlow(QListView::LeftToRight);
|
contentsWidget->setFlow(QListView::LeftToRight);
|
||||||
@ -57,7 +68,7 @@ IconPickerDialog::IconPickerDialog(QWidget* parent) : QDialog(parent), ui(new Ui
|
|||||||
|
|
||||||
contentsWidget->installEventFilter(this);
|
contentsWidget->installEventFilter(this);
|
||||||
|
|
||||||
contentsWidget->setModel(APPLICATION->icons().get());
|
contentsWidget->setModel(proxyModel);
|
||||||
|
|
||||||
// NOTE: ResetRole forces the button to be on the left, while the OK/Cancel ones are on the right. We win.
|
// NOTE: ResetRole forces the button to be on the left, while the OK/Cancel ones are on the right. We win.
|
||||||
auto buttonAdd = ui->buttonBox->addButton(tr("Add Icon"), QDialogButtonBox::ResetRole);
|
auto buttonAdd = ui->buttonBox->addButton(tr("Add Icon"), QDialogButtonBox::ResetRole);
|
||||||
@ -73,6 +84,9 @@ IconPickerDialog::IconPickerDialog(QWidget* parent) : QDialog(parent), ui(new Ui
|
|||||||
|
|
||||||
auto buttonFolder = ui->buttonBox->addButton(tr("Open Folder"), QDialogButtonBox::ResetRole);
|
auto buttonFolder = ui->buttonBox->addButton(tr("Open Folder"), QDialogButtonBox::ResetRole);
|
||||||
connect(buttonFolder, &QPushButton::clicked, this, &IconPickerDialog::openFolder);
|
connect(buttonFolder, &QPushButton::clicked, this, &IconPickerDialog::openFolder);
|
||||||
|
connect(searchBar, &QLineEdit::textChanged, this, &IconPickerDialog::filterIcons);
|
||||||
|
// Prevent incorrect indices from e.g. filesystem changes
|
||||||
|
connect(APPLICATION->icons().get(), &IconList::iconUpdated, this, [this]() { proxyModel->invalidate(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IconPickerDialog::eventFilter(QObject* obj, QEvent* evt)
|
bool IconPickerDialog::eventFilter(QObject* obj, QEvent* evt)
|
||||||
@ -159,5 +173,10 @@ IconPickerDialog::~IconPickerDialog()
|
|||||||
|
|
||||||
void IconPickerDialog::openFolder()
|
void IconPickerDialog::openFolder()
|
||||||
{
|
{
|
||||||
DesktopServices::openPath(APPLICATION->icons()->getDirectory(), true);
|
DesktopServices::openPath(APPLICATION->icons()->iconDirectory(selectedIconKey), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IconPickerDialog::filterIcons(const QString& query)
|
||||||
|
{
|
||||||
|
proxyModel->setFilterFixedString(query);
|
||||||
|
}
|
@ -16,6 +16,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
#include <QItemSelection>
|
#include <QItemSelection>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QSortFilterProxyModel>
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class IconPickerDialog;
|
class IconPickerDialog;
|
||||||
@ -36,6 +38,8 @@ class IconPickerDialog : public QDialog {
|
|||||||
private:
|
private:
|
||||||
Ui::IconPickerDialog* ui;
|
Ui::IconPickerDialog* ui;
|
||||||
QPushButton* buttonRemove;
|
QPushButton* buttonRemove;
|
||||||
|
QLineEdit* searchBar;
|
||||||
|
QSortFilterProxyModel* proxyModel;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void selectionChanged(QItemSelection, QItemSelection);
|
void selectionChanged(QItemSelection, QItemSelection);
|
||||||
@ -44,4 +48,5 @@ class IconPickerDialog : public QDialog {
|
|||||||
void addNewIcon();
|
void addNewIcon();
|
||||||
void removeSelectedIcon();
|
void removeSelectedIcon();
|
||||||
void openFolder();
|
void openFolder();
|
||||||
|
void filterIcons(const QString& text);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user