From 429ab12a4427cef1c1f9d3fbc71f0935bee166ec Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Wed, 8 Sep 2021 13:38:56 +0200 Subject: [PATCH] UI: The highlighter theme is now contained inside the global Theme structure --- src/UI/ScriptViews/ScriptHighlighter.cc | 23 ++++++------- src/UI/ScriptViews/ScriptHighlighter.hh | 38 +-------------------- src/UI/Theme/Theme.cc | 6 ++++ src/UI/Theme/Theme.hh | 44 ++++++++++++++++++++++--- src/VivyApplication.hh | 3 +- 5 files changed, 59 insertions(+), 55 deletions(-) diff --git a/src/UI/ScriptViews/ScriptHighlighter.cc b/src/UI/ScriptViews/ScriptHighlighter.cc index 2baf555a..6c50b405 100644 --- a/src/UI/ScriptViews/ScriptHighlighter.cc +++ b/src/UI/ScriptViews/ScriptHighlighter.cc @@ -1,4 +1,6 @@ #include "ScriptHighlighter.hh" +#include "../Theme/Theme.hh" +#include "../../VivyApplication.hh" #include "../../Lib/Utils.hh" using namespace Vivy; @@ -13,16 +15,17 @@ void ScriptHighlighter::resetHighlightingRule() noexcept { highlightingRules.clear(); + const Theme::HighlightingTheme &theme = vivyApp->getTheme()->getHighlightingTheme(); // function calls - functionFormat.setForeground(fromTheme().functionForeground); + functionFormat.setForeground(theme.functionForeground); highlightingRules.emplace_back(QRegExpLiteral("\\b[A-Za-z0-9_]+[ ]*(?=\\()"), functionFormat); highlightingRules.emplace_back(QRegExpLiteral("\\b[A-Za-z0-9_]+[ ]*(?=\\{)"), functionFormat); // Member and enum members - enumFormat.setForeground(fromTheme().enumForeground); + enumFormat.setForeground(theme.enumForeground); enumFormat.setFontWeight(QFont::Bold); - memberFormat.setForeground(fromTheme().memberForeground); + memberFormat.setForeground(theme.memberForeground); memberFormat.setFontWeight(QFont::StyleItalic); highlightingRules.emplace_back(QRegExpLiteral("\\.[a-z][\\dA-Za-z]*\\b"), memberFormat); highlightingRules.emplace_back(QRegExpLiteral("\\b[A-Z][\\dA-Z_]*\\b"), enumFormat); @@ -43,7 +46,7 @@ ScriptHighlighter::resetHighlightingRule() noexcept QStringLiteral("\\b\\*\\=\\b"), QStringLiteral("\\b\\/\\=\\b"), }; - keywordFormat.setForeground(fromTheme().keywordForeground); + keywordFormat.setForeground(theme.keywordForeground); keywordFormat.setFontWeight(QFont::Bold); for (QString const &pattern : keywordPatterns) @@ -60,21 +63,21 @@ ScriptHighlighter::resetHighlightingRule() noexcept QStringLiteral("\\b\\[\\dA-Fa-F]+\\b"), }; - valueFormat.setForeground(fromTheme().valueForeground); + valueFormat.setForeground(theme.valueForeground); valueFormat.setFontWeight(QFont::Normal); for (QString const &pattern : valuePatterns) highlightingRules.emplace_back(QRegExp(pattern), valueFormat); // strings - quotationFormat.setForeground(fromTheme().valueForeground); + quotationFormat.setForeground(theme.valueForeground); highlightingRules.emplace_back(QRegExpLiteral("\"[^\"]*\""), quotationFormat); highlightingRules.emplace_back(QRegExpLiteral("\'[^\']*\'"), quotationFormat); quoteStartExpression = QRegExpLiteral("\\[\\["); quoteEndExpression = QRegExpLiteral("\\]\\]"); // comments - singleLineCommentFormat.setForeground(fromTheme().commentForeground); + singleLineCommentFormat.setForeground(theme.commentForeground); highlightingRules.emplace_back(QRegExpLiteral("--[^\n]*"), singleLineCommentFormat); commentStartExpression = QRegExpLiteral("--\\[\\["); commentEndExpression = QRegExpLiteral("\\]\\]"); @@ -166,9 +169,3 @@ ScriptHighlighter::highlightCommentBlock(const QString &text, highlightBlock(text, previousState, BlockState::Comment, commentStartExpression, commentEndExpression, singleLineCommentFormat); } - -const ScriptHighlighter::HighlightingTheme -ScriptHighlighter::fromTheme() const noexcept -{ - return highlightingTheme[theme]; -} diff --git a/src/UI/ScriptViews/ScriptHighlighter.hh b/src/UI/ScriptViews/ScriptHighlighter.hh index 492259da..cf636bdb 100644 --- a/src/UI/ScriptViews/ScriptHighlighter.hh +++ b/src/UI/ScriptViews/ScriptHighlighter.hh @@ -4,7 +4,7 @@ #error "This is a C++ header" #endif -class QTextDocument; +#include "../Theme/Theme.hh" namespace Vivy { @@ -18,16 +18,6 @@ class ScriptHighlighter final : public QSyntaxHighlighter { QTextCharFormat format; }; - struct HighlightingTheme { - const QBrush functionForeground; - const QBrush keywordForeground; - const QBrush valueForeground; - const QBrush commentForeground; - const QBrush memberForeground; - const QBrush enumForeground; - }; - - enum Theme { DarkTheme = 0, LightTheme = 1, ThemeCount }; enum class BlockState { None = -1, Quote = 1, Comment = 2 }; public: @@ -47,34 +37,8 @@ private: const BlockState toHighlight, const QRegExp &start, const QRegExp &end, const QTextCharFormat &format) noexcept; - const HighlightingTheme fromTheme() const noexcept; void resetHighlightingRule() noexcept; - const HighlightingTheme darkHighlightingTheme = { - .functionForeground = Qt::darkCyan, - .keywordForeground = Qt::darkYellow, - .valueForeground = QColor(Qt::cyan).darker(120), - .commentForeground = QColor(Qt::darkGray).darker(120), - .memberForeground = QColor(Qt::darkGray).lighter(160), - .enumForeground = QColor(Qt::magenta).darker(170), - }; - - const HighlightingTheme lightHighlightingTheme = { - .functionForeground = Qt::blue, - .keywordForeground = Qt::darkBlue, - .valueForeground = Qt::red, - .commentForeground = QColor(Qt::darkGray).darker(120), - .memberForeground = QColor(Qt::darkGray).lighter(120), - .enumForeground = Qt::darkMagenta, - }; - - const HighlightingTheme highlightingTheme[ThemeCount] = { - darkHighlightingTheme, - lightHighlightingTheme, - }; - - Theme theme{ DarkTheme }; - std::vector<HighlightingRule> highlightingRules; QRegExp commentStartExpression; diff --git a/src/UI/Theme/Theme.cc b/src/UI/Theme/Theme.cc index 2c751205..ef144f86 100644 --- a/src/UI/Theme/Theme.cc +++ b/src/UI/Theme/Theme.cc @@ -152,6 +152,12 @@ Theme::fromVivyTheme(const QString &id, const VivyTheme which) noexcept : QStringLiteral(":qdarkstyle/light/style.qss")); } +const Theme::HighlightingTheme & +Theme::getHighlightingTheme() const noexcept +{ + return highlightingTheme[Utils::toUnderlying(d->preferedColor)]; +} + void Theme::readSettings(QSettings *const settings) noexcept { diff --git a/src/UI/Theme/Theme.hh b/src/UI/Theme/Theme.hh index 3b3542d9..887b87f9 100644 --- a/src/UI/Theme/Theme.hh +++ b/src/UI/Theme/Theme.hh @@ -1,5 +1,7 @@ #pragma once +#include "../../Lib/Utils.hh" + // #include "../../Lib/Log.hh" namespace Vivy @@ -12,13 +14,23 @@ class Theme final : public QObject { public: enum class Type { QtCreator, System, QssFile }; - enum class VivyTheme { Dark, Light }; + enum class VivyTheme { Dark, Light, ___COUNT }; + + struct HighlightingTheme final { + const QBrush functionForeground; + const QBrush keywordForeground; + const QBrush valueForeground; + const QBrush commentForeground; + const QBrush memberForeground; + const QBrush enumForeground; + }; private: struct ThemePrivate final { ThemePrivate() noexcept; Theme::Type classType{ Theme::Type::System }; + Theme::VivyTheme preferedColor{ Theme::VivyTheme::Dark }; QString id; QString fileName; @@ -39,11 +51,12 @@ private: public: ~Theme() noexcept override; - [[nodiscard]] static Theme *fromQssFile(const QString &, const QString &) noexcept; - [[nodiscard]] static Theme *fromSettings(const QString &, QSettings *const) noexcept; - [[nodiscard]] static Theme *fromVivyTheme(const QString &, const VivyTheme) noexcept; + [[nodiscard("free")]] static Theme *fromQssFile(const QString &, const QString &) noexcept; + [[nodiscard("free")]] static Theme *fromSettings(const QString &, QSettings *const) noexcept; + [[nodiscard("free")]] static Theme *fromVivyTheme(const QString &, const VivyTheme) noexcept; void applyToApplication() const noexcept; + [[nodiscard]] const HighlightingTheme &getHighlightingTheme() const noexcept; enum Color { BackgroundColorAlternate, @@ -483,5 +496,28 @@ public: private: QPair<QColor, QString> readNamedColor(const QString &color) const; ThemePrivate *d; + + const Theme::HighlightingTheme darkHighlightingTheme = { + .functionForeground = Qt::darkCyan, + .keywordForeground = Qt::darkYellow, + .valueForeground = QColor(Qt::cyan).darker(120), + .commentForeground = QColor(Qt::darkGray).darker(120), + .memberForeground = QColor(Qt::darkGray).lighter(160), + .enumForeground = QColor(Qt::magenta).darker(170), + }; + + const Theme::HighlightingTheme lightHighlightingTheme = { + .functionForeground = Qt::blue, + .keywordForeground = Qt::darkBlue, + .valueForeground = Qt::red, + .commentForeground = QColor(Qt::darkGray).darker(120), + .memberForeground = QColor(Qt::darkGray).lighter(120), + .enumForeground = Qt::darkMagenta, + }; + + const Theme::HighlightingTheme highlightingTheme[Utils::toUnderlying(VivyTheme::___COUNT)] = { + darkHighlightingTheme, + lightHighlightingTheme, + }; }; } diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh index b716bf45..d0a25b8b 100644 --- a/src/VivyApplication.hh +++ b/src/VivyApplication.hh @@ -103,7 +103,8 @@ public: [[nodiscard]] MainWindow *getMainWindow() const; [[nodiscard]] AbstractDocument *getCurrentDocument() const; [[nodiscard]] bool getUseFakeVimEditor() const noexcept; - std::shared_ptr<LogSink> getLogSink() noexcept { return logSink; } + [[nodiscard]] const Theme *getTheme() const noexcept { return qtCreatorThemePtr.get(); } + [[nodiscard]] std::shared_ptr<LogSink> getLogSink() noexcept { return logSink; } void setUseFakeVimEditor(bool) noexcept; void setTheme(Theme::Type, Theme::VivyTheme) noexcept; -- GitLab