Multiple icon themes!
Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
35
launcher/ui/themes/IconTheme.cpp
Normal file
35
launcher/ui/themes/IconTheme.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include "IconTheme.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QSettings>
|
||||
|
||||
IconTheme::IconTheme(const QString& id, const QString& path) : m_id(id), m_path(path) {}
|
||||
|
||||
bool IconTheme::load()
|
||||
{
|
||||
QString path = m_path + "/index.theme";
|
||||
|
||||
if (!QFile::exists(path))
|
||||
return false;
|
||||
|
||||
QSettings settings(path, QSettings::IniFormat);
|
||||
settings.beginGroup("Icon Theme");
|
||||
m_name = settings.value("Name").toString();
|
||||
settings.endGroup();
|
||||
return !m_name.isNull();
|
||||
}
|
||||
|
||||
QString IconTheme::id()
|
||||
{
|
||||
return m_id;
|
||||
}
|
||||
|
||||
QString IconTheme::path()
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
QString IconTheme::name()
|
||||
{
|
||||
return m_name;
|
||||
}
|
18
launcher/ui/themes/IconTheme.h
Normal file
18
launcher/ui/themes/IconTheme.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
class IconTheme {
|
||||
public:
|
||||
IconTheme(const QString& id, const QString& path);
|
||||
|
||||
bool load();
|
||||
QString id();
|
||||
QString path();
|
||||
QString name();
|
||||
|
||||
private:
|
||||
QString m_id;
|
||||
QString m_path;
|
||||
QString m_name;
|
||||
};
|
@ -55,50 +55,84 @@ ITheme* ThemeManager::getTheme(QString themeId)
|
||||
void ThemeManager::initializeThemes()
|
||||
{
|
||||
// Icon themes
|
||||
{
|
||||
// TODO: icon themes and instance icons do not mesh well together. Rearrange and fix discrepancies!
|
||||
// set icon theme search path!
|
||||
auto searchPaths = QIcon::themeSearchPaths();
|
||||
searchPaths.append("iconthemes");
|
||||
QIcon::setThemeSearchPaths(searchPaths);
|
||||
themeDebugLog() << "<> Icon themes initialized.";
|
||||
}
|
||||
initializeIcons();
|
||||
|
||||
// Initialize widget themes
|
||||
{
|
||||
themeDebugLog() << "<> Initializing Widget Themes";
|
||||
themeDebugLog() << "Loading Built-in Theme:" << addTheme(std::make_unique<SystemTheme>());
|
||||
auto darkThemeId = addTheme(std::make_unique<DarkTheme>());
|
||||
themeDebugLog() << "Loading Built-in Theme:" << darkThemeId;
|
||||
themeDebugLog() << "Loading Built-in Theme:" << addTheme(std::make_unique<BrightTheme>());
|
||||
initializeWidgets();
|
||||
}
|
||||
|
||||
// TODO: need some way to differentiate same name themes in different subdirectories (maybe smaller grey text next to theme name in
|
||||
// dropdown?)
|
||||
QString themeFolder = QDir("./themes/").absoluteFilePath("");
|
||||
themeDebugLog() << "Theme Folder Path: " << themeFolder;
|
||||
void ThemeManager::initializeIcons()
|
||||
{
|
||||
// TODO: icon themes and instance icons do not mesh well together. Rearrange and fix discrepancies!
|
||||
// set icon theme search path!
|
||||
|
||||
QDirIterator directoryIterator(themeFolder, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
|
||||
while (directoryIterator.hasNext()) {
|
||||
QDir dir(directoryIterator.next());
|
||||
QFileInfo themeJson(dir.absoluteFilePath("theme.json"));
|
||||
if (themeJson.exists()) {
|
||||
// Load "theme.json" based themes
|
||||
themeDebugLog() << "Loading JSON Theme from:" << themeJson.absoluteFilePath();
|
||||
addTheme(std::make_unique<CustomTheme>(getTheme(darkThemeId), themeJson, true));
|
||||
} else {
|
||||
// Load pure QSS Themes
|
||||
QDirIterator stylesheetFileIterator(dir.absoluteFilePath(""), { "*.qss", "*.css" }, QDir::Files);
|
||||
while (stylesheetFileIterator.hasNext()) {
|
||||
QFile customThemeFile(stylesheetFileIterator.next());
|
||||
QFileInfo customThemeFileInfo(customThemeFile);
|
||||
themeDebugLog() << "Loading QSS Theme from:" << customThemeFileInfo.absoluteFilePath();
|
||||
addTheme(std::make_unique<CustomTheme>(getTheme(darkThemeId), customThemeFileInfo, false));
|
||||
}
|
||||
}
|
||||
QString themeFolder = "iconthemes";
|
||||
|
||||
auto searchPaths = QIcon::themeSearchPaths();
|
||||
searchPaths.append(themeFolder);
|
||||
QIcon::setThemeSearchPaths(searchPaths);
|
||||
|
||||
themeDebugLog() << "<> Initializing Icon Themes";
|
||||
|
||||
for (const QString& id : builtinIcons) {
|
||||
IconTheme theme(id, QString(":/icons/%1").arg(id));
|
||||
if (!theme.load()) {
|
||||
themeWarningLog() << "Couldn't load built-in icon theme" << id;
|
||||
continue;
|
||||
}
|
||||
|
||||
themeDebugLog() << "<> Widget themes initialized.";
|
||||
m_icons.append(theme);
|
||||
themeDebugLog() << "Loaded Built-In Icon Theme" << id;
|
||||
}
|
||||
|
||||
QDirIterator directoryIterator(themeFolder, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
|
||||
while (directoryIterator.hasNext()) {
|
||||
QDir dir(directoryIterator.next());
|
||||
IconTheme theme(dir.dirName(), dir.path());
|
||||
if (!theme.load())
|
||||
continue;
|
||||
|
||||
m_icons.append(theme);
|
||||
themeDebugLog() << "Loaded Custom Icon Theme from" << dir.path();
|
||||
}
|
||||
|
||||
themeDebugLog() << "<> Icon themes initialized.";
|
||||
}
|
||||
|
||||
void ThemeManager::initializeWidgets()
|
||||
{
|
||||
themeDebugLog() << "<> Initializing Widget Themes";
|
||||
themeDebugLog() << "Loading Built-in Theme:" << addTheme(std::make_unique<SystemTheme>());
|
||||
auto darkThemeId = addTheme(std::make_unique<DarkTheme>());
|
||||
themeDebugLog() << "Loading Built-in Theme:" << darkThemeId;
|
||||
themeDebugLog() << "Loading Built-in Theme:" << addTheme(std::make_unique<BrightTheme>());
|
||||
|
||||
// TODO: need some way to differentiate same name themes in different subdirectories (maybe smaller grey text next to theme name in
|
||||
// dropdown?)
|
||||
QString themeFolder = QDir("./themes/").absoluteFilePath("");
|
||||
themeDebugLog() << "Theme Folder Path: " << themeFolder;
|
||||
|
||||
QDirIterator directoryIterator(themeFolder, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
|
||||
while (directoryIterator.hasNext()) {
|
||||
QDir dir(directoryIterator.next());
|
||||
QFileInfo themeJson(dir.absoluteFilePath("theme.json"));
|
||||
if (themeJson.exists()) {
|
||||
// Load "theme.json" based themes
|
||||
themeDebugLog() << "Loading JSON Theme from:" << themeJson.absoluteFilePath();
|
||||
addTheme(std::make_unique<CustomTheme>(getTheme(darkThemeId), themeJson, true));
|
||||
} else {
|
||||
// Load pure QSS Themes
|
||||
QDirIterator stylesheetFileIterator(dir.absoluteFilePath(""), { "*.qss", "*.css" }, QDir::Files);
|
||||
while (stylesheetFileIterator.hasNext()) {
|
||||
QFile customThemeFile(stylesheetFileIterator.next());
|
||||
QFileInfo customThemeFileInfo(customThemeFile);
|
||||
themeDebugLog() << "Loading QSS Theme from:" << customThemeFileInfo.absoluteFilePath();
|
||||
addTheme(std::make_unique<CustomTheme>(getTheme(darkThemeId), customThemeFileInfo, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
themeDebugLog() << "<> Widget themes initialized.";
|
||||
}
|
||||
|
||||
QList<ITheme*> ThemeManager::getValidApplicationThemes()
|
||||
@ -111,6 +145,15 @@ QList<ITheme*> ThemeManager::getValidApplicationThemes()
|
||||
return ret;
|
||||
}
|
||||
|
||||
QList<IconTheme*> ThemeManager::getValidIconThemes()
|
||||
{
|
||||
QList<IconTheme*> ret;
|
||||
ret.reserve(m_icons.size());
|
||||
for (IconTheme& theme : m_icons)
|
||||
ret.append(&theme);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ThemeManager::setIconTheme(const QString& name)
|
||||
{
|
||||
QIcon::setThemeName(name);
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include "IconTheme.h"
|
||||
#include "ui/MainWindow.h"
|
||||
#include "ui/themes/ITheme.h"
|
||||
|
||||
@ -36,6 +37,7 @@ class ThemeManager {
|
||||
ThemeManager(MainWindow* mainWindow);
|
||||
|
||||
QList<ITheme*> getValidApplicationThemes();
|
||||
QList<IconTheme*> getValidIconThemes();
|
||||
void setIconTheme(const QString& name);
|
||||
void applyCurrentlySelectedTheme(bool initial = false);
|
||||
void setApplicationTheme(const QString& name, bool initial = false);
|
||||
@ -49,9 +51,15 @@ class ThemeManager {
|
||||
|
||||
private:
|
||||
std::map<QString, std::unique_ptr<ITheme>> m_themes;
|
||||
QList<IconTheme> m_icons;
|
||||
MainWindow* m_mainWindow;
|
||||
|
||||
void initializeThemes();
|
||||
QString addTheme(std::unique_ptr<ITheme> theme);
|
||||
ITheme* getTheme(QString themeId);
|
||||
void initializeIcons();
|
||||
void initializeWidgets();
|
||||
|
||||
const QStringList builtinIcons{ "pe_colored", "pe_light", "pe_dark", "pe_blue", "breeze_light", "breeze_dark",
|
||||
"OSX", "iOS", "flat", "flat_white", "multimc" };
|
||||
};
|
||||
|
Reference in New Issue
Block a user