From d31a0847482a145d9afe5c7eeaca8dffa472c0eb Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Mon, 11 Sep 2023 01:06:49 +0200 Subject: [PATCH] Fixed issue-1477 (Macro IDE: changing the colors does not have an effect) --- src/lay/lay/layMacroEditorPage.cc | 12 ++++++------ src/lay/lay/layMacroEditorPage.h | 4 ++-- src/layui/layui/layDialogs.cc | 2 +- src/layui/layui/layGenericSyntaxHighlighter.cc | 18 +++++++++++++++--- src/layui/layui/layGenericSyntaxHighlighter.h | 14 +++++++++++++- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/lay/lay/layMacroEditorPage.cc b/src/lay/lay/layMacroEditorPage.cc index b0980c315..2cb13c178 100644 --- a/src/lay/lay/layMacroEditorPage.cc +++ b/src/lay/lay/layMacroEditorPage.cc @@ -148,18 +148,18 @@ MacroEditorHighlighters::MacroEditorHighlighters (QObject *parent) for (std::vector >::iterator a = m_attributes.begin (); a != m_attributes.end (); ++a) { // Note: this loads and initializes the attributes - delete highlighter_for_scheme (parent, a->first, &a->second); + delete highlighter_for_scheme (parent, a->first, &a->second, true); } } QSyntaxHighlighter * -MacroEditorHighlighters::highlighter_for (QObject *parent, lym::Macro::Interpreter lang, const std::string &dsl_name) +MacroEditorHighlighters::highlighter_for (QObject *parent, lym::Macro::Interpreter lang, const std::string &dsl_name, bool initialize) { std::string scheme = scheme_for (lang, dsl_name); for (std::vector >::iterator a = m_attributes.begin (); a != m_attributes.end (); ++a) { if (a->first == scheme) { - return highlighter_for_scheme (parent, a->first, &a->second); + return highlighter_for_scheme (parent, a->first, &a->second, initialize); } } @@ -167,7 +167,7 @@ MacroEditorHighlighters::highlighter_for (QObject *parent, lym::Macro::Interpret } lay::GenericSyntaxHighlighter * -MacroEditorHighlighters::highlighter_for_scheme (QObject *parent, const std::string &scheme, GenericSyntaxHighlighterAttributes *attributes) +MacroEditorHighlighters::highlighter_for_scheme (QObject *parent, const std::string &scheme, GenericSyntaxHighlighterAttributes *attributes, bool initialize) { if (! scheme.empty ()) { @@ -186,7 +186,7 @@ MacroEditorHighlighters::highlighter_for_scheme (QObject *parent, const std::str QBuffer input (&data); input.open (QIODevice::ReadOnly); - lay::GenericSyntaxHighlighter *hl = new GenericSyntaxHighlighter (parent, input, attributes); + lay::GenericSyntaxHighlighter *hl = new GenericSyntaxHighlighter (parent, input, attributes, initialize); input.close (); return hl; @@ -1096,7 +1096,7 @@ void MacroEditorPage::connect_macro (lym::Macro *macro) mp_text->setPlainText (tl::to_qstring (mp_macro->text ())); mp_text->setReadOnly (macro->is_readonly ()); mp_readonly_label->setVisible (macro->is_readonly ()); - mp_highlighter = mp_highlighters->highlighter_for (mp_text, mp_macro->interpreter (), mp_macro->dsl_interpreter ()); + mp_highlighter = mp_highlighters->highlighter_for (mp_text, mp_macro->interpreter (), mp_macro->dsl_interpreter (), false); if (mp_highlighter) { mp_highlighter->setDocument (mp_text->document ()); } diff --git a/src/lay/lay/layMacroEditorPage.h b/src/lay/lay/layMacroEditorPage.h index 3e4e1fbf6..3305d159d 100644 --- a/src/lay/lay/layMacroEditorPage.h +++ b/src/lay/lay/layMacroEditorPage.h @@ -63,7 +63,7 @@ class MacroEditorHighlighters public: MacroEditorHighlighters (QObject *parent); - QSyntaxHighlighter *highlighter_for (QObject *parent, lym::Macro::Interpreter lang, const std::string &dsl_name); + QSyntaxHighlighter *highlighter_for (QObject *parent, lym::Macro::Interpreter lang, const std::string &dsl_name, bool initialize); GenericSyntaxHighlighterAttributes *attributes_for (lym::Macro::Interpreter lang, const std::string &dsl_name); GenericSyntaxHighlighterAttributes *basic_attributes (); @@ -98,7 +98,7 @@ private: std::vector > m_attributes; GenericSyntaxHighlighterAttributes m_basic_attributes; - lay::GenericSyntaxHighlighter *highlighter_for_scheme (QObject *parent, const std::string &scheme, GenericSyntaxHighlighterAttributes *attributes); + lay::GenericSyntaxHighlighter *highlighter_for_scheme (QObject *parent, const std::string &scheme, GenericSyntaxHighlighterAttributes *attributes, bool initialize); std::string scheme_for (lym::Macro::Interpreter lang, const std::string &dsl_name); }; diff --git a/src/layui/layui/layDialogs.cc b/src/layui/layui/layDialogs.cc index dae96cc00..b35d5bc11 100644 --- a/src/layui/layui/layDialogs.cc +++ b/src/layui/layui/layDialogs.cc @@ -1095,7 +1095,7 @@ UserPropertiesForm::UserPropertiesForm (QWidget *parent) input.open (QIODevice::ReadOnly); mp_hl_basic_attributes.reset (new GenericSyntaxHighlighterAttributes ()); mp_hl_attributes.reset (new GenericSyntaxHighlighterAttributes (mp_hl_basic_attributes.get ())); - lay::GenericSyntaxHighlighter *hl = new GenericSyntaxHighlighter (mp_ui->text_edit, input, mp_hl_attributes.get ()); + lay::GenericSyntaxHighlighter *hl = new GenericSyntaxHighlighter (mp_ui->text_edit, input, mp_hl_attributes.get (), true); input.close (); hl->setDocument (mp_ui->text_edit->document ()); diff --git a/src/layui/layui/layGenericSyntaxHighlighter.cc b/src/layui/layui/layGenericSyntaxHighlighter.cc index 8a8d9ff2d..3a3cd7e75 100644 --- a/src/layui/layui/layGenericSyntaxHighlighter.cc +++ b/src/layui/layui/layGenericSyntaxHighlighter.cc @@ -975,6 +975,12 @@ GenericSyntaxHighlighterAttributes::assign (const GenericSyntaxHighlighterAttrib m_ids = other.m_ids; } +bool +GenericSyntaxHighlighterAttributes::has_attribute (const QString &name) const +{ + return m_ids.find (name) != m_ids.end (); +} + int GenericSyntaxHighlighterAttributes::id (const QString &name) { @@ -1467,9 +1473,15 @@ parse_context (QDomElement e, const std::map &contexts_by_ } static void -parse_item_data (QDomElement e, GenericSyntaxHighlighterAttributes &attributes) +parse_item_data (QDomElement e, GenericSyntaxHighlighterAttributes &attributes, bool initialize) { QString name = e.attributeNode (QString::fromUtf8 ("name")).value (); + + // skip attribute if already present so we don't overwrite specific settings + if (! initialize && attributes.has_attribute (name)) { + return; + } + int attribute_id = attributes.id (name); def_style ds = dsNormal; @@ -1532,7 +1544,7 @@ parse_item_data (QDomElement e, GenericSyntaxHighlighterAttributes &attributes) attributes.set_styles (attribute_id, ds, format); } -GenericSyntaxHighlighter::GenericSyntaxHighlighter (QObject *parent, QIODevice &input, GenericSyntaxHighlighterAttributes *attributes) +GenericSyntaxHighlighter::GenericSyntaxHighlighter (QObject *parent, QIODevice &input, GenericSyntaxHighlighterAttributes *attributes, bool initialize_attributes) : QSyntaxHighlighter (parent), mp_attributes (attributes), m_generation_id (0) { QDomDocument d; @@ -1593,7 +1605,7 @@ GenericSyntaxHighlighter::GenericSyntaxHighlighter (QObject *parent, QIODevice & if (nn.isElement()) { QDomElement ee = nn.toElement (); if (ee.tagName () == QString::fromUtf8 ("itemData")) { - parse_item_data (ee, *mp_attributes); + parse_item_data (ee, *mp_attributes, initialize_attributes); } } } diff --git a/src/layui/layui/layGenericSyntaxHighlighter.h b/src/layui/layui/layGenericSyntaxHighlighter.h index 037536df3..35fef725b 100644 --- a/src/layui/layui/layGenericSyntaxHighlighter.h +++ b/src/layui/layui/layGenericSyntaxHighlighter.h @@ -607,6 +607,11 @@ public: return m_ids.end (); } + /** + * @brief Gets a value indicating whether the given name is present already + */ + bool has_attribute (const QString &name) const; + /** * @brief Get the attribute ID for a given name * @@ -716,7 +721,14 @@ class LAYUI_PUBLIC GenericSyntaxHighlighter : public QSyntaxHighlighter { public: - GenericSyntaxHighlighter (QObject *parent, QIODevice &input, GenericSyntaxHighlighterAttributes *attributes); + /** + * @brief Creates a GenericSyntaxHighlighter + * @param parent The owner of the highlighter + * @param input The stream from which to pull + * @param attributes The attributes + * @param initialize_attributes If true, the attributes are initialized from the itemData lines + */ + GenericSyntaxHighlighter (QObject *parent, QIODevice &input, GenericSyntaxHighlighterAttributes *attributes, bool initialize_attributes); /** * @brief Implementation of the highlighter