Browse Source

feat(themes): make themes follow standard paths

reviewable/pr5404/r7
sudden6 7 years ago
parent
commit
133ac8def8
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
  1. 9
      CMakeLists.txt
  2. 48
      src/widget/style.cpp
  3. 9
      src/widget/style.h

9
CMakeLists.txt

@ -100,15 +100,6 @@ include_directories(${CMAKE_SOURCE_DIR})
include(Dependencies) include(Dependencies)
# Copy themes
if (UNIX AND NOT APPLE)
file(COPY ${CMAKE_SOURCE_DIR}/themes DESTINATION $ENV{HOME}/.config/qtox)
elseif (APPLE)
file(COPY ${CMAKE_SOURCE_DIR}/themes DESTINATION "$ENV{HOME}/Library/Application Support/qtox")
elseif (WIN32)
file(COPY ${CMAKE_SOURCE_DIR}/themes DESTINATION C:\\users\\%username%\\AppData\\roaming\\qtox)
endif()
################################################################################ ################################################################################
# #
# :: qTox main library sources # :: qTox main library sources

48
src/widget/style.cpp

@ -28,6 +28,7 @@
#include <QMap> #include <QMap>
#include <QPainter> #include <QPainter>
#include <QRegularExpression> #include <QRegularExpression>
#include <QStandardPaths>
#include <QStringBuilder> #include <QStringBuilder>
#include <QStyle> #include <QStyle>
#include <QSvgRenderer> #include <QSvgRenderer>
@ -56,8 +57,16 @@
* *
* @var SmallLight * @var SmallLight
* @brief [SystemDefault - 2]px, light * @brief [SystemDefault - 2]px, light
*
* @var BuiltinThemePath
* @brief Path to the theme built into the application binary
*/ */
namespace {
const QLatin1Literal ThemeSubFolder{"themes/"};
const QLatin1Literal BuiltinThemePath{":themes/default/"};
}
// helper functions // helper functions
QFont appFont(int pixelSize, int weight) QFont appFont(int pixelSize, int weight)
{ {
@ -98,22 +107,19 @@ QString Style::getThemeName()
return QStringLiteral("default"); return QStringLiteral("default");
} }
QString Style::getThemePath() QString Style::getThemeFolder()
{ {
const QString themeName = getThemeName(); const QString themeName = getThemeName();
const QString homePath = QDir::homePath(); const QString themeFolder = ThemeSubFolder % themeName;
const QString fullPath = QStandardPaths::locate(QStandardPaths::AppDataLocation,
#if defined(Q_OS_UNIX) and not defined(Q_OS_MACOS) themeFolder, QStandardPaths::LocateDirectory);
const QString themePath = homePath % QLatin1String("/.config/qtox/themes/") % themeName % '/';
#elif defined(Q_OS_MACOS) // No themes available, fallback to builtin
const QString themePath = homePath % QLatin1String("/Library/Application Support/qtox/themes/") if(fullPath.isEmpty()) {
% themeName % '/'; return BuiltinThemePath;
#elif defined(Q_OS_WIN32) }
const QString themePath = homePath % QLatin1String("/AppData/roaming/qtox/themes/")
% themeName % '/'; return fullPath % QDir::separator();
#endif
return themePath;
} }
QList<QColor> Style::themeColorColors = {QColor(), QColor("#004aa4"), QColor("#97ba00"), QList<QColor> Style::themeColorColors = {QColor(), QColor("#004aa4"), QColor("#97ba00"),
@ -125,7 +131,7 @@ std::map<std::pair<const QString, const QFont>, const QString> Style::stylesheet
const QString Style::getStylesheet(const QString& filename, const QFont& baseFont) const QString Style::getStylesheet(const QString& filename, const QFont& baseFont)
{ {
const QString fullPath = getThemePath() + filename; const QString fullPath = getThemeFolder() + filename;
const std::pair<const QString, const QFont> cacheKey(fullPath, baseFont); const std::pair<const QString, const QFont> cacheKey(fullPath, baseFont);
auto it = stylesheetsCache.find(cacheKey); auto it = stylesheetsCache.find(cacheKey);
if (it != stylesheetsCache.end()) if (it != stylesheetsCache.end())
@ -142,7 +148,7 @@ const QString Style::getStylesheet(const QString& filename, const QFont& baseFon
static QStringList existingImagesCache; static QStringList existingImagesCache;
const QString Style::getImagePath(const QString& filename) const QString Style::getImagePath(const QString& filename)
{ {
QString fullPath = getThemePath() + filename; QString fullPath = getThemeFolder() + filename;
// search for image in cache // search for image in cache
if (existingImagesCache.contains(fullPath)) { if (existingImagesCache.contains(fullPath)) {
@ -156,7 +162,7 @@ const QString Style::getImagePath(const QString& filename)
} else { } else {
qWarning() << "Failed to open file (using defaults):" << fullPath; qWarning() << "Failed to open file (using defaults):" << fullPath;
fullPath = QLatin1String(":themes/default/") % filename; fullPath = BuiltinThemePath % filename;
if (QFileInfo::exists(fullPath)) { if (QFileInfo::exists(fullPath)) {
return fullPath; return fullPath;
@ -194,7 +200,7 @@ QFont Style::getFont(Style::Font font)
const QString Style::resolve(const QString& filename, const QFont& baseFont) const QString Style::resolve(const QString& filename, const QFont& baseFont)
{ {
QString themePath = getThemePath(); QString themePath = getThemeFolder();
QString fullPath = themePath + filename; QString fullPath = themePath + filename;
QString qss; QString qss;
@ -204,7 +210,7 @@ const QString Style::resolve(const QString& filename, const QFont& baseFont)
} else { } else {
qWarning() << "Failed to open file (using defaults):" << fullPath; qWarning() << "Failed to open file (using defaults):" << fullPath;
fullPath = QLatin1String(":themes/default/") % filename; fullPath = BuiltinThemePath;
QFile file{fullPath}; QFile file{fullPath};
if (file.open(QFile::ReadOnly | QFile::Text)) { if (file.open(QFile::ReadOnly | QFile::Text)) {
@ -260,14 +266,14 @@ const QString Style::resolve(const QString& filename, const QFont& baseFont)
path.remove(QStringLiteral("@getImagePath(")); path.remove(QStringLiteral("@getImagePath("));
path.chop(1); path.chop(1);
QString fullImagePath = getThemePath() + path; QString fullImagePath = getThemeFolder() + path;
// image not in cache // image not in cache
if (!existingImagesCache.contains(fullPath)) { if (!existingImagesCache.contains(fullPath)) {
if (QFileInfo::exists(fullImagePath)) { if (QFileInfo::exists(fullImagePath)) {
existingImagesCache << fullImagePath; existingImagesCache << fullImagePath;
} else { } else {
qWarning() << "Failed to open file (using defaults):" << fullImagePath; qWarning() << "Failed to open file (using defaults):" << fullImagePath;
fullImagePath = QLatin1String(":themes/default/") % path; fullImagePath = BuiltinThemePath % path;
} }
} }

9
src/widget/style.h

@ -61,7 +61,7 @@ public:
static QStringList getThemeColorNames(); static QStringList getThemeColorNames();
static const QString getStylesheet(const QString& filename, const QFont& baseFont = QFont()); static const QString getStylesheet(const QString& filename, const QFont& baseFont = QFont());
static const QString getImagePath(const QString& filename); static const QString getImagePath(const QString& filename);
static QString getThemePath(); static QString getThemeFolder();
static QString getThemeName(); static QString getThemeName();
static QColor getColor(ColorPalette entry); static QColor getColor(ColorPalette entry);
static QFont getFont(Font font); static QFont getFont(Font font);
@ -72,14 +72,15 @@ public:
static void applyTheme(); static void applyTheme();
static QPixmap scaleSvgImage(const QString& path, uint32_t width, uint32_t height); static QPixmap scaleSvgImage(const QString& path, uint32_t width, uint32_t height);
static QList<QColor> themeColorColors;
static std::map<std::pair<const QString, const QFont>, const QString> stylesheetsCache;
signals: signals:
void themeChanged(); void themeChanged();
private: private:
Style(); Style();
private:
static QList<QColor> themeColorColors;
static std::map<std::pair<const QString, const QFont>, const QString> stylesheetsCache;
}; };
#endif // STYLE_H #endif // STYLE_H

Loading…
Cancel
Save