Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat(themes): make themes follow standard paths
Browse files Browse the repository at this point in the history
  • Loading branch information
sudden6 committed Oct 24, 2018
1 parent 5033fc3 commit 133ac8d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
9 changes: 0 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ include_directories(${CMAKE_SOURCE_DIR})

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
Expand Down
48 changes: 27 additions & 21 deletions src/widget/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <QMap>
#include <QPainter>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QStringBuilder>
#include <QStyle>
#include <QSvgRenderer>
Expand Down Expand Up @@ -56,8 +57,16 @@
*
* @var SmallLight
* @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
QFont appFont(int pixelSize, int weight)
{
Expand Down Expand Up @@ -98,22 +107,19 @@ QString Style::getThemeName()
return QStringLiteral("default");
}

QString Style::getThemePath()
QString Style::getThemeFolder()
{
const QString themeName = getThemeName();
const QString homePath = QDir::homePath();

#if defined(Q_OS_UNIX) and not defined(Q_OS_MACOS)
const QString themePath = homePath % QLatin1String("/.config/qtox/themes/") % themeName % '/';
#elif defined(Q_OS_MACOS)
const QString themePath = homePath % QLatin1String("/Library/Application Support/qtox/themes/")
% themeName % '/';
#elif defined(Q_OS_WIN32)
const QString themePath = homePath % QLatin1String("/AppData/roaming/qtox/themes/")
% themeName % '/';
#endif

return themePath;
const QString themeFolder = ThemeSubFolder % themeName;
const QString fullPath = QStandardPaths::locate(QStandardPaths::AppDataLocation,
themeFolder, QStandardPaths::LocateDirectory);

// No themes available, fallback to builtin
if(fullPath.isEmpty()) {
return BuiltinThemePath;
}

return fullPath % QDir::separator();
}

QList<QColor> Style::themeColorColors = {QColor(), QColor("#004aa4"), QColor("#97ba00"),
Expand All @@ -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 fullPath = getThemePath() + filename;
const QString fullPath = getThemeFolder() + filename;
const std::pair<const QString, const QFont> cacheKey(fullPath, baseFont);
auto it = stylesheetsCache.find(cacheKey);
if (it != stylesheetsCache.end())
Expand All @@ -142,7 +148,7 @@ const QString Style::getStylesheet(const QString& filename, const QFont& baseFon
static QStringList existingImagesCache;
const QString Style::getImagePath(const QString& filename)
{
QString fullPath = getThemePath() + filename;
QString fullPath = getThemeFolder() + filename;

// search for image in cache
if (existingImagesCache.contains(fullPath)) {
Expand All @@ -156,7 +162,7 @@ const QString Style::getImagePath(const QString& filename)
} else {
qWarning() << "Failed to open file (using defaults):" << fullPath;

fullPath = QLatin1String(":themes/default/") % filename;
fullPath = BuiltinThemePath % filename;

if (QFileInfo::exists(fullPath)) {
return fullPath;
Expand Down Expand Up @@ -194,7 +200,7 @@ QFont Style::getFont(Style::Font font)

const QString Style::resolve(const QString& filename, const QFont& baseFont)
{
QString themePath = getThemePath();
QString themePath = getThemeFolder();
QString fullPath = themePath + filename;
QString qss;

Expand All @@ -204,7 +210,7 @@ const QString Style::resolve(const QString& filename, const QFont& baseFont)
} else {
qWarning() << "Failed to open file (using defaults):" << fullPath;

fullPath = QLatin1String(":themes/default/") % filename;
fullPath = BuiltinThemePath;
QFile file{fullPath};

if (file.open(QFile::ReadOnly | QFile::Text)) {
Expand Down Expand Up @@ -260,14 +266,14 @@ const QString Style::resolve(const QString& filename, const QFont& baseFont)
path.remove(QStringLiteral("@getImagePath("));
path.chop(1);

QString fullImagePath = getThemePath() + path;
QString fullImagePath = getThemeFolder() + path;
// image not in cache
if (!existingImagesCache.contains(fullPath)) {
if (QFileInfo::exists(fullImagePath)) {
existingImagesCache << fullImagePath;
} else {
qWarning() << "Failed to open file (using defaults):" << fullImagePath;
fullImagePath = QLatin1String(":themes/default/") % path;
fullImagePath = BuiltinThemePath % path;
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/widget/style.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Style
static QStringList getThemeColorNames();
static const QString getStylesheet(const QString& filename, const QFont& baseFont = QFont());
static const QString getImagePath(const QString& filename);
static QString getThemePath();
static QString getThemeFolder();
static QString getThemeName();
static QColor getColor(ColorPalette entry);
static QFont getFont(Font font);
Expand All @@ -72,14 +72,15 @@ class Style
static void applyTheme();
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:
void themeChanged();

private:
Style();

private:
static QList<QColor> themeColorColors;
static std::map<std::pair<const QString, const QFont>, const QString> stylesheetsCache;
};

#endif // STYLE_H

0 comments on commit 133ac8d

Please sign in to comment.