clang_format for PR files

I'm getting a rendezvous... I thought I did this already

Signed-off-by: Tayou <tayou@gmx.net>
This commit is contained in:
Tayou 2022-11-03 05:05:07 +01:00
parent e9bfd43ff7
commit c2f37716e5
No known key found for this signature in database
GPG Key ID: 02CA43C1CB6E9887
4 changed files with 87 additions and 112 deletions

View File

@ -33,19 +33,24 @@
* limitations under the License. * limitations under the License.
*/ */
#include "CustomTheme.h" #include "CustomTheme.h"
#include <Json.h>
#include <FileSystem.h> #include <FileSystem.h>
#include <Json.h>
#include "ThemeManager.h" #include "ThemeManager.h"
const char * themeFile = "theme.json"; const char* themeFile = "theme.json";
static bool readThemeJson(const QString &path, QPalette &palette, double &fadeAmount, QColor &fadeColor, QString &name, QString &widgets, QString &qssFilePath, bool &dataIncomplete) static bool readThemeJson(const QString& path,
QPalette& palette,
double& fadeAmount,
QColor& fadeColor,
QString& name,
QString& widgets,
QString& qssFilePath,
bool& dataIncomplete)
{ {
QFileInfo pathInfo(path); QFileInfo pathInfo(path);
if(pathInfo.exists() && pathInfo.isFile()) if (pathInfo.exists() && pathInfo.isFile()) {
{ try {
try
{
auto doc = Json::requireDocument(path, "Theme JSON file"); auto doc = Json::requireDocument(path, "Theme JSON file");
const QJsonObject root = doc.object(); const QJsonObject root = doc.object();
dataIncomplete = !root.contains("qssFilePath"); dataIncomplete = !root.contains("qssFilePath");
@ -53,14 +58,11 @@ static bool readThemeJson(const QString &path, QPalette &palette, double &fadeAm
widgets = Json::requireString(root, "widgets", "Qt widget theme"); widgets = Json::requireString(root, "widgets", "Qt widget theme");
qssFilePath = Json::ensureString(root, "qssFilePath", "themeStyle.css"); qssFilePath = Json::ensureString(root, "qssFilePath", "themeStyle.css");
auto colorsRoot = Json::requireObject(root, "colors", "colors object"); auto colorsRoot = Json::requireObject(root, "colors", "colors object");
auto readColor = [&](QString colorName) -> QColor auto readColor = [&](QString colorName) -> QColor {
{
auto colorValue = Json::ensureString(colorsRoot, colorName, QString()); auto colorValue = Json::ensureString(colorsRoot, colorName, QString());
if(!colorValue.isEmpty()) if (!colorValue.isEmpty()) {
{
QColor color(colorValue); QColor color(colorValue);
if(!color.isValid()) if (!color.isValid()) {
{
themeWarningLog() << "Color value" << colorValue << "for" << colorName << "was not recognized."; themeWarningLog() << "Color value" << colorValue << "for" << colorName << "was not recognized.";
return QColor(); return QColor();
} }
@ -68,15 +70,11 @@ static bool readThemeJson(const QString &path, QPalette &palette, double &fadeAm
} }
return QColor(); return QColor();
}; };
auto readAndSetColor = [&](QPalette::ColorRole role, QString colorName) auto readAndSetColor = [&](QPalette::ColorRole role, QString colorName) {
{
auto color = readColor(colorName); auto color = readColor(colorName);
if(color.isValid()) if (color.isValid()) {
{
palette.setColor(role, color); palette.setColor(role, color);
} } else {
else
{
themeDebugLog() << "Color value for" << colorName << "was not present."; themeDebugLog() << "Color value for" << colorName << "was not present.";
} }
}; };
@ -96,26 +94,28 @@ static bool readThemeJson(const QString &path, QPalette &palette, double &fadeAm
readAndSetColor(QPalette::Highlight, "Highlight"); readAndSetColor(QPalette::Highlight, "Highlight");
readAndSetColor(QPalette::HighlightedText, "HighlightedText"); readAndSetColor(QPalette::HighlightedText, "HighlightedText");
//fade // fade
fadeColor = readColor("fadeColor"); fadeColor = readColor("fadeColor");
fadeAmount = Json::ensureDouble(colorsRoot, "fadeAmount", 0.5, "fade amount"); fadeAmount = Json::ensureDouble(colorsRoot, "fadeAmount", 0.5, "fade amount");
} } catch (const Exception& e) {
catch (const Exception &e)
{
themeWarningLog() << "Couldn't load theme json: " << e.cause(); themeWarningLog() << "Couldn't load theme json: " << e.cause();
return false; return false;
} }
} } else {
else
{
themeDebugLog() << "No theme json present."; themeDebugLog() << "No theme json present.";
return false; return false;
} }
return true; return true;
} }
static bool writeThemeJson(const QString &path, const QPalette &palette, double fadeAmount, QColor fadeColor, QString name, QString widgets, QString qssFilePath) static bool writeThemeJson(const QString& path,
const QPalette& palette,
double fadeAmount,
QColor fadeColor,
QString name,
QString widgets,
QString qssFilePath)
{ {
QJsonObject rootObj; QJsonObject rootObj;
rootObj.insert("name", name); rootObj.insert("name", name);
@ -123,10 +123,7 @@ static bool writeThemeJson(const QString &path, const QPalette &palette, double
rootObj.insert("qssFilePath", qssFilePath); rootObj.insert("qssFilePath", qssFilePath);
QJsonObject colorsObj; QJsonObject colorsObj;
auto insertColor = [&](QPalette::ColorRole role, QString colorName) auto insertColor = [&](QPalette::ColorRole role, QString colorName) { colorsObj.insert(colorName, palette.color(role).name()); };
{
colorsObj.insert(colorName, palette.color(role).name());
};
// palette // palette
insertColor(QPalette::Window, "Window"); insertColor(QPalette::Window, "Window");
@ -148,13 +145,10 @@ static bool writeThemeJson(const QString &path, const QPalette &palette, double
colorsObj.insert("fadeAmount", fadeAmount); colorsObj.insert("fadeAmount", fadeAmount);
rootObj.insert("colors", colorsObj); rootObj.insert("colors", colorsObj);
try try {
{
Json::write(rootObj, path); Json::write(rootObj, path);
return true; return true;
} } catch (const Exception& e) {
catch (const Exception &e)
{
themeWarningLog() << "Failed to write theme json to" << path; themeWarningLog() << "Failed to write theme json to" << path;
return false; return false;
} }
@ -171,8 +165,7 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
QString path = FS::PathCombine("themes", m_id); QString path = FS::PathCombine("themes", m_id);
QString pathResources = FS::PathCombine("themes", m_id, "resources"); QString pathResources = FS::PathCombine("themes", m_id, "resources");
if(!FS::ensureFolderPathExists(path) || !FS::ensureFolderPathExists(pathResources)) if (!FS::ensureFolderPathExists(path) || !FS::ensureFolderPathExists(pathResources)) {
{
themeWarningLog() << "couldn't create folder for theme!"; themeWarningLog() << "couldn't create folder for theme!";
m_palette = baseTheme->colorScheme(); m_palette = baseTheme->colorScheme();
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
@ -184,8 +177,7 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
bool jsonDataIncomplete = false; bool jsonDataIncomplete = false;
m_palette = baseTheme->colorScheme(); m_palette = baseTheme->colorScheme();
if (!readThemeJson(themeFilePath, m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath, jsonDataIncomplete)) if (!readThemeJson(themeFilePath, m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath, jsonDataIncomplete)) {
{
themeDebugLog() << "Did not read theme json file correctly, writing new one to: " << themeFilePath; themeDebugLog() << "Did not read theme json file correctly, writing new one to: " << themeFilePath;
m_name = "Custom"; m_name = "Custom";
m_palette = baseTheme->colorScheme(); m_palette = baseTheme->colorScheme();
@ -193,42 +185,30 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
m_fadeAmount = baseTheme->fadeAmount(); m_fadeAmount = baseTheme->fadeAmount();
m_widgets = baseTheme->qtTheme(); m_widgets = baseTheme->qtTheme();
m_qssFilePath = "themeStyle.css"; m_qssFilePath = "themeStyle.css";
} } else {
else
{
m_palette = fadeInactive(m_palette, m_fadeAmount, m_fadeColor); m_palette = fadeInactive(m_palette, m_fadeAmount, m_fadeColor);
} }
if(jsonDataIncomplete) if (jsonDataIncomplete) {
{
writeThemeJson(fileInfo.absoluteFilePath(), m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath); writeThemeJson(fileInfo.absoluteFilePath(), m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath);
} }
auto qssFilePath = FS::PathCombine(path, m_qssFilePath); auto qssFilePath = FS::PathCombine(path, m_qssFilePath);
QFileInfo info (qssFilePath); QFileInfo info(qssFilePath);
if(info.isFile()) if (info.isFile()) {
{ try {
try
{
// TODO: validate css? // TODO: validate css?
m_styleSheet = QString::fromUtf8(FS::read(qssFilePath)); m_styleSheet = QString::fromUtf8(FS::read(qssFilePath));
} } catch (const Exception& e) {
catch (const Exception &e)
{
themeWarningLog() << "Couldn't load css:" << e.cause() << "from" << qssFilePath; themeWarningLog() << "Couldn't load css:" << e.cause() << "from" << qssFilePath;
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
} }
} } else {
else
{
themeDebugLog() << "No theme css present."; themeDebugLog() << "No theme css present.";
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
try try {
{
FS::write(qssFilePath, m_styleSheet.toUtf8()); FS::write(qssFilePath, m_styleSheet.toUtf8());
} } catch (const Exception& e) {
catch (const Exception &e)
{
themeWarningLog() << "Couldn't write css:" << e.cause() << "to" << qssFilePath; themeWarningLog() << "Couldn't write css:" << e.cause() << "to" << qssFilePath;
} }
} }
@ -236,12 +216,11 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
m_id = fileInfo.fileName(); m_id = fileInfo.fileName();
m_name = fileInfo.baseName(); m_name = fileInfo.baseName();
QString path = fileInfo.filePath(); QString path = fileInfo.filePath();
//themeDebugLog << "Theme ID: " << m_id; // themeDebugLog << "Theme ID: " << m_id;
//themeDebugLog << "Theme Name: " << m_name; // themeDebugLog << "Theme Name: " << m_name;
//themeDebugLog << "Theme Path: " << path; // themeDebugLog << "Theme Path: " << path;
if(!FS::ensureFilePathExists(path)) if (!FS::ensureFilePathExists(path)) {
{
themeWarningLog() << m_name << " Theme file path doesn't exist!"; themeWarningLog() << m_name << " Theme file path doesn't exist!";
m_palette = baseTheme->colorScheme(); m_palette = baseTheme->colorScheme();
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
@ -249,13 +228,10 @@ CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest
} }
m_palette = baseTheme->colorScheme(); m_palette = baseTheme->colorScheme();
try try {
{
// TODO: validate qss? // TODO: validate qss?
m_styleSheet = QString::fromUtf8(FS::read(path)); m_styleSheet = QString::fromUtf8(FS::read(path));
} } catch (const Exception& e) {
catch (const Exception &e)
{
themeWarningLog() << "Couldn't load qss:" << e.cause() << "from" << path; themeWarningLog() << "Couldn't load qss:" << e.cause() << "from" << path;
m_styleSheet = baseTheme->appStyleSheet(); m_styleSheet = baseTheme->appStyleSheet();
} }
@ -267,7 +243,6 @@ QStringList CustomTheme::searchPaths()
return { FS::PathCombine("themes", m_id, "resources") }; return { FS::PathCombine("themes", m_id, "resources") };
} }
QString CustomTheme::id() QString CustomTheme::id()
{ {
return m_id; return m_id;

View File

@ -34,13 +34,12 @@
*/ */
#pragma once #pragma once
#include "ITheme.h"
#include <QFileInfo> #include <QFileInfo>
#include "ITheme.h"
class CustomTheme: public ITheme class CustomTheme : public ITheme {
{ public:
public: CustomTheme(ITheme* baseTheme, QFileInfo& file, bool isManifest);
CustomTheme(ITheme * baseTheme, QFileInfo& file, bool isManifest);
virtual ~CustomTheme() {} virtual ~CustomTheme() {}
QString id() override; QString id() override;
@ -54,7 +53,7 @@ public:
QString qtTheme() override; QString qtTheme() override;
QStringList searchPaths() override; QStringList searchPaths() override;
private: /* data */ private: /* data */
QPalette m_palette; QPalette m_palette;
QColor m_fadeColor; QColor m_fadeColor;
double m_fadeAmount; double m_fadeAmount;
@ -64,4 +63,3 @@ private: /* data */
QString m_widgets; QString m_widgets;
QString m_qssFilePath; QString m_qssFilePath;
}; };

View File

@ -17,26 +17,27 @@
*/ */
#include "ThemeManager.h" #include "ThemeManager.h"
#include "ui/themes/SystemTheme.h" #include <QApplication>
#include "ui/themes/DarkTheme.h"
#include "ui/themes/BrightTheme.h"
#include "ui/themes/CustomTheme.h"
#include <QDir> #include <QDir>
#include <QDirIterator> #include <QDirIterator>
#include <QIcon> #include <QIcon>
#include <QApplication> #include "ui/themes/BrightTheme.h"
#include "ui/themes/CustomTheme.h"
#include "ui/themes/DarkTheme.h"
#include "ui/themes/SystemTheme.h"
#include "Application.h" #include "Application.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <windows.h> #include <windows.h>
// this is needed for versionhelpers.h, it is also included in WinDarkmode, but we can't rely on that. // this is needed for versionhelpers.h, it is also included in WinDarkmode, but we can't rely on that.
// Ultimately this should be included in versionhelpers, but that is outside of the project. // Ultimately this should be included in versionhelpers, but that is outside of the project.
#include "ui/WinDarkmode.h" #include "ui/WinDarkmode.h"
#include <versionhelpers.h> #include <versionhelpers.h>
#endif #endif
ThemeManager::ThemeManager(MainWindow* mainWindow) { ThemeManager::ThemeManager(MainWindow* mainWindow)
{
m_mainWindow = mainWindow; m_mainWindow = mainWindow;
InitializeThemes(); InitializeThemes();
} }
@ -44,7 +45,8 @@ ThemeManager::ThemeManager(MainWindow* mainWindow) {
/// @brief Adds the Theme to the list of themes /// @brief Adds the Theme to the list of themes
/// @param theme The Theme to add /// @param theme The Theme to add
/// @return Theme ID /// @return Theme ID
QString ThemeManager::AddTheme(std::unique_ptr<ITheme> theme) { QString ThemeManager::AddTheme(std::unique_ptr<ITheme> theme)
{
QString id = theme->id(); QString id = theme->id();
m_themes.emplace(id, std::move(theme)); m_themes.emplace(id, std::move(theme));
return id; return id;
@ -53,13 +55,13 @@ QString ThemeManager::AddTheme(std::unique_ptr<ITheme> theme) {
/// @brief Gets the Theme from the List via ID /// @brief Gets the Theme from the List via ID
/// @param themeId Theme ID of theme to fetch /// @param themeId Theme ID of theme to fetch
/// @return Theme at themeId /// @return Theme at themeId
ITheme* ThemeManager::GetTheme(QString themeId) { ITheme* ThemeManager::GetTheme(QString themeId)
{
return m_themes[themeId].get(); return m_themes[themeId].get();
} }
void ThemeManager::InitializeThemes() { void ThemeManager::InitializeThemes()
{
// Icon themes // Icon themes
{ {
// TODO: icon themes and instance icons do not mesh well together. Rearrange and fix discrepancies! // TODO: icon themes and instance icons do not mesh well together. Rearrange and fix discrepancies!
@ -78,7 +80,8 @@ void ThemeManager::InitializeThemes() {
themeDebugLog() << "Loading Built-in Theme:" << darkThemeId; themeDebugLog() << "Loading Built-in Theme:" << darkThemeId;
themeDebugLog() << "Loading Built-in Theme:" << AddTheme(std::make_unique<BrightTheme>()); 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?) // 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(""); QString themeFolder = QDir("./themes/").absoluteFilePath("");
themeDebugLog() << "Theme Folder Path: " << themeFolder; themeDebugLog() << "Theme Folder Path: " << themeFolder;
@ -92,7 +95,7 @@ void ThemeManager::InitializeThemes() {
AddTheme(std::make_unique<CustomTheme>(GetTheme(darkThemeId), themeJson, true)); AddTheme(std::make_unique<CustomTheme>(GetTheme(darkThemeId), themeJson, true));
} else { } else {
// Load pure QSS Themes // Load pure QSS Themes
QDirIterator stylesheetFileIterator(dir.absoluteFilePath(""), {"*.qss", "*.css"}, QDir::Files); QDirIterator stylesheetFileIterator(dir.absoluteFilePath(""), { "*.qss", "*.css" }, QDir::Files);
while (stylesheetFileIterator.hasNext()) { while (stylesheetFileIterator.hasNext()) {
QFile customThemeFile(stylesheetFileIterator.next()); QFile customThemeFile(stylesheetFileIterator.next());
QFileInfo customThemeFileInfo(customThemeFile); QFileInfo customThemeFileInfo(customThemeFile);
@ -121,7 +124,8 @@ void ThemeManager::setIconTheme(const QString& name)
QIcon::setThemeName(name); QIcon::setThemeName(name);
} }
void ThemeManager::applyCurrentlySelectedTheme() { void ThemeManager::applyCurrentlySelectedTheme()
{
setIconTheme(APPLICATION->settings()->get("IconTheme").toString()); setIconTheme(APPLICATION->settings()->get("IconTheme").toString());
themeDebugLog() << "<> Icon theme set."; themeDebugLog() << "<> Icon theme set.";
setApplicationTheme(APPLICATION->settings()->get("ApplicationTheme").toString(), true); setApplicationTheme(APPLICATION->settings()->get("ApplicationTheme").toString(), true);
@ -132,23 +136,20 @@ void ThemeManager::setApplicationTheme(const QString& name, bool initial)
{ {
auto systemPalette = qApp->palette(); auto systemPalette = qApp->palette();
auto themeIter = m_themes.find(name); auto themeIter = m_themes.find(name);
if(themeIter != m_themes.end()) if (themeIter != m_themes.end()) {
{ auto& theme = themeIter->second;
auto & theme = themeIter->second;
themeDebugLog() << "applying theme" << theme->name(); themeDebugLog() << "applying theme" << theme->name();
theme->apply(initial); theme->apply(initial);
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
if (m_mainWindow && IsWindows10OrGreater()) { if (m_mainWindow && IsWindows10OrGreater()) {
if (QString::compare(theme->id(), "dark") == 0) { if (QString::compare(theme->id(), "dark") == 0) {
WinDarkmode::setDarkWinTitlebar(m_mainWindow->winId(), true); WinDarkmode::setDarkWinTitlebar(m_mainWindow->winId(), true);
} else { } else {
WinDarkmode::setDarkWinTitlebar(m_mainWindow->winId(), false); WinDarkmode::setDarkWinTitlebar(m_mainWindow->winId(), false);
} }
} }
#endif #endif
} } else {
else
{
themeWarningLog() << "Tried to set invalid theme:" << name; themeWarningLog() << "Tried to set invalid theme:" << name;
} }
} }

View File

@ -19,18 +19,20 @@
#include <QString> #include <QString>
#include "ui/themes/ITheme.h"
#include "ui/MainWindow.h" #include "ui/MainWindow.h"
#include "ui/themes/ITheme.h"
inline auto themeDebugLog() { inline auto themeDebugLog()
return qDebug() << "[Theme]"; {
return qDebug() << "[Theme]";
} }
inline auto themeWarningLog() { inline auto themeWarningLog()
return qWarning() << "[Theme]"; {
return qWarning() << "[Theme]";
} }
class ThemeManager { class ThemeManager {
public: public:
ThemeManager(MainWindow* mainWindow); ThemeManager(MainWindow* mainWindow);
// maybe make private? Or put in ctor? // maybe make private? Or put in ctor?
@ -41,11 +43,10 @@ public:
void applyCurrentlySelectedTheme(); void applyCurrentlySelectedTheme();
void setApplicationTheme(const QString& name, bool initial); void setApplicationTheme(const QString& name, bool initial);
private: private:
std::map<QString, std::unique_ptr<ITheme>> m_themes; std::map<QString, std::unique_ptr<ITheme>> m_themes;
MainWindow* m_mainWindow; MainWindow* m_mainWindow;
QString AddTheme(std::unique_ptr<ITheme> theme); QString AddTheme(std::unique_ptr<ITheme> theme);
ITheme* GetTheme(QString themeId); ITheme* GetTheme(QString themeId);
}; };