Browse Source

Allow changing GUI icons from theme bundles

adaptive-webui-19844
jagannatharjun 5 years ago
parent
commit
3c733ddf0c
  1. 53
      src/gui/uithememanager.cpp
  2. 5
      src/gui/uithememanager.h

53
src/gui/uithememanager.cpp

@ -37,14 +37,28 @@
#include <QPalette> #include <QPalette>
#include <QResource> #include <QResource>
#include "base/iconprovider.h"
#include "base/logger.h" #include "base/logger.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
namespace namespace
{ {
const QString ICONS_DIR = QStringLiteral(":icons/");
const QString THEME_ICONS_DIR = QStringLiteral(":uitheme/icons/");
const QString CONFIG_FILE_NAME = QStringLiteral(":uitheme/config.json"); const QString CONFIG_FILE_NAME = QStringLiteral(":uitheme/config.json");
QString findIcon(const QString &iconId, const QString &dir)
{
const QString pathSvg = dir + iconId + QLatin1String(".svg");
if (QFile::exists(pathSvg))
return pathSvg;
const QString pathPng = dir + iconId + QLatin1String(".png");
if (QFile::exists(pathPng))
return pathPng;
return {};
}
} }
UIThemeManager *UIThemeManager::m_instance = nullptr; UIThemeManager *UIThemeManager::m_instance = nullptr;
@ -62,9 +76,10 @@ void UIThemeManager::initInstance()
} }
UIThemeManager::UIThemeManager() UIThemeManager::UIThemeManager()
: m_useCustomTheme(Preferences::instance()->useCustomUITheme())
{ {
const Preferences *const pref = Preferences::instance(); const Preferences *const pref = Preferences::instance();
if (pref->useCustomUITheme()) { if (m_useCustomTheme) {
if (!QResource::registerResource(pref->customUIThemePath(), "/uitheme")) { if (!QResource::registerResource(pref->customUIThemePath(), "/uitheme")) {
LogMsg(tr("Failed to load UI theme from file: \"%1\"").arg(pref->customUIThemePath()), Log::WARNING); LogMsg(tr("Failed to load UI theme from file: \"%1\"").arg(pref->customUIThemePath()), Log::WARNING);
} }
@ -86,7 +101,7 @@ UIThemeManager *UIThemeManager::instance()
void UIThemeManager::applyStyleSheet() const void UIThemeManager::applyStyleSheet() const
{ {
if (!Preferences::instance()->useCustomUITheme()) { if (!m_useCustomTheme) {
qApp->setStyleSheet({}); qApp->setStyleSheet({});
return; return;
} }
@ -102,22 +117,15 @@ void UIThemeManager::applyStyleSheet() const
qApp->setStyleSheet(qssFile.readAll()); qApp->setStyleSheet(qssFile.readAll());
} }
QIcon UIThemeManager::getIcon(const QString &iconId) const
{
return getIcon(iconId, iconId);
}
QIcon UIThemeManager::getIcon(const QString &iconId, const QString &fallback) const QIcon UIThemeManager::getIcon(const QString &iconId, const QString &fallback) const
{ {
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
if (m_useSystemTheme) { if (m_useSystemTheme) {
QIcon icon = QIcon::fromTheme(iconId); QIcon icon = QIcon::fromTheme(iconId);
if (icon.name() != iconId) if (icon.name() != iconId)
icon = QIcon::fromTheme(fallback, QIcon(IconProvider::instance()->getIconPath(iconId))); icon = QIcon::fromTheme(fallback, QIcon(getIconPathFromResources(iconId, fallback)));
return icon; return icon;
} }
#else
Q_UNUSED(fallback)
#endif #endif
// cache to avoid rescaling svg icons // cache to avoid rescaling svg icons
static QHash<QString, QIcon> iconCache; static QHash<QString, QIcon> iconCache;
@ -125,7 +133,7 @@ QIcon UIThemeManager::getIcon(const QString &iconId, const QString &fallback) co
if (iter != iconCache.end()) if (iter != iconCache.end())
return *iter; return *iter;
const QIcon icon {IconProvider::instance()->getIconPath(iconId)}; const QIcon icon {getIconPathFromResources(iconId, fallback)};
iconCache[iconId] = icon; iconCache[iconId] = icon;
return icon; return icon;
} }
@ -151,13 +159,30 @@ QString UIThemeManager::getIconPath(const QString &iconId) const
if (!icon.isNull()) if (!icon.isNull())
icon.pixmap(32).save(path); icon.pixmap(32).save(path);
else else
path = IconProvider::instance()->getIconPath(iconId); path = getIconPathFromResources(iconId);
} }
return path; return path;
} }
#endif #endif
return IconProvider::instance()->getIconPath(iconId); return getIconPathFromResources(iconId, {});
}
QString UIThemeManager::getIconPathFromResources(const QString &iconId, const QString &fallback) const
{
if (m_useCustomTheme) {
const QString customIcon = findIcon(iconId, THEME_ICONS_DIR);
if (!customIcon.isEmpty())
return customIcon;
if (!fallback.isEmpty()) {
const QString fallbackIcon = findIcon(fallback, THEME_ICONS_DIR);
if (!fallbackIcon.isEmpty())
return fallbackIcon;
}
}
return findIcon(iconId, ICONS_DIR);
} }
void UIThemeManager::loadColorsFromJSONConfig() void UIThemeManager::loadColorsFromJSONConfig()

5
src/gui/uithememanager.h

@ -47,19 +47,20 @@ public:
void applyStyleSheet() const; void applyStyleSheet() const;
QString getIconPath(const QString &iconId) const; QString getIconPath(const QString &iconId) const;
QIcon getIcon(const QString &iconId) const; QIcon getIcon(const QString &iconId, const QString &fallback = {}) const;
QIcon getIcon(const QString &iconId, const QString &fallback) const;
QIcon getFlagIcon(const QString &countryIsoCode) const; QIcon getFlagIcon(const QString &countryIsoCode) const;
QColor getColor(const QString &id, const QColor &defaultColor) const; QColor getColor(const QString &id, const QColor &defaultColor) const;
private: private:
UIThemeManager(); // singleton class UIThemeManager(); // singleton class
QString getIconPathFromResources(const QString &iconId, const QString &fallback = {}) const;
void loadColorsFromJSONConfig(); void loadColorsFromJSONConfig();
void applyPalette() const; void applyPalette() const;
static UIThemeManager *m_instance; static UIThemeManager *m_instance;
QHash<QString, QColor> m_colors; QHash<QString, QColor> m_colors;
const bool m_useCustomTheme;
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
bool m_useSystemTheme; bool m_useSystemTheme;
#endif #endif

Loading…
Cancel
Save