1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

feat(themes): make themes follow standard paths

This commit is contained in:
sudden6 2018-10-15 11:08:01 +02:00
parent 5033fc3f5d
commit 133ac8def8
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
3 changed files with 30 additions and 32 deletions

View File

@ -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

View File

@ -28,6 +28,7 @@
#include <QMap>
#include <QPainter>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QStringBuilder>
#include <QStyle>
#include <QSvgRenderer>
@ -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)
{
@ -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();
const QString themeFolder = ThemeSubFolder % themeName;
const QString fullPath = QStandardPaths::locate(QStandardPaths::AppDataLocation,
themeFolder, QStandardPaths::LocateDirectory);
#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
// No themes available, fallback to builtin
if(fullPath.isEmpty()) {
return BuiltinThemePath;
}
return themePath;
return fullPath % QDir::separator();
}
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 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())
@ -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)) {
@ -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;
@ -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;
@ -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)) {
@ -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;
}
}

View File

@ -61,7 +61,7 @@ public:
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);
@ -72,14 +72,15 @@ public:
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