diff --git a/src/UI/ScriptViews/ScriptHighlighter.cc b/src/UI/ScriptViews/ScriptHighlighter.cc index 2baf555a1ac8e625e4f3e6b4c87546ef4b9b6265..6c50b405e8d066fed53d1f8a9118b9ceb3bc37ce 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 492259dad9adc6795952277f02b958396fccc425..cf636bdb3fff70692d69f778314a76087ec09b15 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 2c7512057e86c27a96fdacc89733bcce41da7c55..ef144f86e833f69cfd721255eaaf42e5c5cb1e96 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 3b3542d9adb4a60a4b820ec20e9edee3743c479f..887b87f98a943f7a40137177301eb3c8e8ad0d5b 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 b716bf4556ef221d20b01d91a16bf8563786a1c9..d0a25b8bc48e85138ede3015397f00ec43de1977 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;