Fixed issue-1477 (Macro IDE: changing the colors does not have an effect)

This commit is contained in:
Matthias Koefferlein 2023-09-11 01:06:49 +02:00
parent 5c0f810006
commit d31a084748
5 changed files with 37 additions and 13 deletions

View File

@ -148,18 +148,18 @@ MacroEditorHighlighters::MacroEditorHighlighters (QObject *parent)
for (std::vector<std::pair<std::string, GenericSyntaxHighlighterAttributes> >::iterator a = m_attributes.begin (); a != m_attributes.end (); ++a) { for (std::vector<std::pair<std::string, GenericSyntaxHighlighterAttributes> >::iterator a = m_attributes.begin (); a != m_attributes.end (); ++a) {
// Note: this loads and initializes the attributes // 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 * 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); std::string scheme = scheme_for (lang, dsl_name);
for (std::vector<std::pair<std::string, GenericSyntaxHighlighterAttributes> >::iterator a = m_attributes.begin (); a != m_attributes.end (); ++a) { for (std::vector<std::pair<std::string, GenericSyntaxHighlighterAttributes> >::iterator a = m_attributes.begin (); a != m_attributes.end (); ++a) {
if (a->first == scheme) { 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 * 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 ()) { if (! scheme.empty ()) {
@ -186,7 +186,7 @@ MacroEditorHighlighters::highlighter_for_scheme (QObject *parent, const std::str
QBuffer input (&data); QBuffer input (&data);
input.open (QIODevice::ReadOnly); input.open (QIODevice::ReadOnly);
lay::GenericSyntaxHighlighter *hl = new GenericSyntaxHighlighter (parent, input, attributes); lay::GenericSyntaxHighlighter *hl = new GenericSyntaxHighlighter (parent, input, attributes, initialize);
input.close (); input.close ();
return hl; return hl;
@ -1096,7 +1096,7 @@ void MacroEditorPage::connect_macro (lym::Macro *macro)
mp_text->setPlainText (tl::to_qstring (mp_macro->text ())); mp_text->setPlainText (tl::to_qstring (mp_macro->text ()));
mp_text->setReadOnly (macro->is_readonly ()); mp_text->setReadOnly (macro->is_readonly ());
mp_readonly_label->setVisible (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) { if (mp_highlighter) {
mp_highlighter->setDocument (mp_text->document ()); mp_highlighter->setDocument (mp_text->document ());
} }

View File

@ -63,7 +63,7 @@ class MacroEditorHighlighters
public: public:
MacroEditorHighlighters (QObject *parent); 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 *attributes_for (lym::Macro::Interpreter lang, const std::string &dsl_name);
GenericSyntaxHighlighterAttributes *basic_attributes (); GenericSyntaxHighlighterAttributes *basic_attributes ();
@ -98,7 +98,7 @@ private:
std::vector<std::pair<std::string, GenericSyntaxHighlighterAttributes> > m_attributes; std::vector<std::pair<std::string, GenericSyntaxHighlighterAttributes> > m_attributes;
GenericSyntaxHighlighterAttributes m_basic_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); std::string scheme_for (lym::Macro::Interpreter lang, const std::string &dsl_name);
}; };

View File

@ -1095,7 +1095,7 @@ UserPropertiesForm::UserPropertiesForm (QWidget *parent)
input.open (QIODevice::ReadOnly); input.open (QIODevice::ReadOnly);
mp_hl_basic_attributes.reset (new GenericSyntaxHighlighterAttributes ()); mp_hl_basic_attributes.reset (new GenericSyntaxHighlighterAttributes ());
mp_hl_attributes.reset (new GenericSyntaxHighlighterAttributes (mp_hl_basic_attributes.get ())); 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 (); input.close ();
hl->setDocument (mp_ui->text_edit->document ()); hl->setDocument (mp_ui->text_edit->document ());

View File

@ -975,6 +975,12 @@ GenericSyntaxHighlighterAttributes::assign (const GenericSyntaxHighlighterAttrib
m_ids = other.m_ids; m_ids = other.m_ids;
} }
bool
GenericSyntaxHighlighterAttributes::has_attribute (const QString &name) const
{
return m_ids.find (name) != m_ids.end ();
}
int int
GenericSyntaxHighlighterAttributes::id (const QString &name) GenericSyntaxHighlighterAttributes::id (const QString &name)
{ {
@ -1467,9 +1473,15 @@ parse_context (QDomElement e, const std::map<QString, QDomElement> &contexts_by_
} }
static void 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 (); 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); int attribute_id = attributes.id (name);
def_style ds = dsNormal; def_style ds = dsNormal;
@ -1532,7 +1544,7 @@ parse_item_data (QDomElement e, GenericSyntaxHighlighterAttributes &attributes)
attributes.set_styles (attribute_id, ds, format); 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) : QSyntaxHighlighter (parent), mp_attributes (attributes), m_generation_id (0)
{ {
QDomDocument d; QDomDocument d;
@ -1593,7 +1605,7 @@ GenericSyntaxHighlighter::GenericSyntaxHighlighter (QObject *parent, QIODevice &
if (nn.isElement()) { if (nn.isElement()) {
QDomElement ee = nn.toElement (); QDomElement ee = nn.toElement ();
if (ee.tagName () == QString::fromUtf8 ("itemData")) { if (ee.tagName () == QString::fromUtf8 ("itemData")) {
parse_item_data (ee, *mp_attributes); parse_item_data (ee, *mp_attributes, initialize_attributes);
} }
} }
} }

View File

@ -607,6 +607,11 @@ public:
return m_ids.end (); 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 * @brief Get the attribute ID for a given name
* *
@ -716,7 +721,14 @@ class LAYUI_PUBLIC GenericSyntaxHighlighter
: public QSyntaxHighlighter : public QSyntaxHighlighter
{ {
public: 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 * @brief Implementation of the highlighter