Krita Source Code Documentation
Loading...
Searching...
No Matches
BasicXMLSyntaxHighlighter.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Dmitry Ivanov
3 *
4 * SPDX-License-Identifier: MIT
5 */
7
8#include <QApplication>
9#include <QPalette>
10
11#include <ksharedconfig.h>
12#include <kconfiggroup.h>
13
15 QSyntaxHighlighter(parent)
16{
17 setRegexes();
18 setFormats();
19}
20
22 QSyntaxHighlighter(parent)
23{
24 setRegexes();
25 setFormats();
26}
27
29 QSyntaxHighlighter(parent)
30{
31 setRegexes();
32 setFormats();
33}
34
36{
37 // Special treatment for xml element regex as we use captured text to emulate lookbehind
38 QRegularExpressionMatchIterator i = m_xmlElementRegex.globalMatch(text);
39 while (i.hasNext()) {
40 QRegularExpressionMatch match = i.next();
41 setFormat(match.capturedStart(0), match.capturedLength(0), m_xmlElementFormat);
42 }
43
44 // Highlight xml keywords *after* xml elements to fix any occasional / captured into the enclosing element
46 Iter xmlKeywordRegexesEnd = m_xmlKeywordRegexes.constEnd();
47 for(Iter it = m_xmlKeywordRegexes.constBegin(); it != xmlKeywordRegexesEnd; ++it) {
48 const QRegularExpression & regex = *it;
50 }
51
55}
56
57void BasicXMLSyntaxHighlighter::highlightByRegex(const QTextCharFormat & format,
58 const QRegularExpression & regex, const QString & text)
59{
60 QRegularExpressionMatchIterator i = regex.globalMatch(text);
61 while (i.hasNext()) {
62 QRegularExpressionMatch match = i.next();
63 setFormat(match.capturedStart(0), match.capturedLength(0), format);
64 }
65}
66
68{
69 m_xmlElementRegex.setPattern("<[\\s]*[/]?[\\s]*([^\\n]\\w*)(?=[\\s/>])");
70 m_xmlAttributeRegex.setPattern("[\\w\\-]+(?=\\=)");
71 m_xmlValueRegex.setPattern("\"[^\\n\"]+\"(?=[\\s/>])");
72 m_xmlCommentRegex.setPattern("<!--[^\\n]*-->");
73
75 << QRegularExpression("<\\?") << QRegularExpression("/>")
76 << QRegularExpression(">") << QRegularExpression("<") << QRegularExpression("</")
77 << QRegularExpression("\\?>");
78}
79
81{
82 KConfigGroup cfg(KSharedConfig::openConfig(), "SvgTextTool");
83 QColor background = cfg.readEntry("colorEditorBackground", qApp->palette().window().color());
84
85 m_xmlKeywordFormat.setForeground(cfg.readEntry("colorKeyword", QColor(background.value() < 100 ? Qt::cyan : Qt::blue)));
86 m_xmlKeywordFormat.setFontWeight(cfg.readEntry("BoldKeyword", true) ? QFont::Bold : QFont::Normal);
87 m_xmlKeywordFormat.setFontItalic((cfg.readEntry("ItalicKeyword", false)));
88
89 m_xmlElementFormat.setForeground(cfg.readEntry("colorElement", QColor(background.value() < 100 ? Qt::magenta : Qt::darkMagenta)));
90 m_xmlElementFormat.setFontWeight(cfg.readEntry("BoldElement", true) ? QFont::Bold : QFont::Normal);
91 m_xmlElementFormat.setFontItalic((cfg.readEntry("ItalicElement", false)));
92
93 m_xmlAttributeFormat.setForeground(cfg.readEntry("colorAttribute", QColor(background.value() < 100 ? Qt::green : Qt::darkGreen)));
94 m_xmlAttributeFormat.setFontWeight(cfg.readEntry("BoldAttribute", true) ? QFont::Bold : QFont::Normal);
95 m_xmlAttributeFormat.setFontItalic((cfg.readEntry("ItalicAttribute", true)));
96
97 m_xmlValueFormat.setForeground(cfg.readEntry("colorValue", QColor(background.value() < 100 ? Qt::red: Qt::darkRed)));
98 m_xmlValueFormat.setFontWeight(cfg.readEntry("BoldValue", true) ? QFont::Bold : QFont::Normal);
99 m_xmlValueFormat.setFontItalic(cfg.readEntry("ItalicValue", false));
100
101 m_xmlCommentFormat.setForeground(cfg.readEntry("colorComment", QColor(background.value() < 100 ? Qt::lightGray : Qt::gray)));
102 m_xmlCommentFormat.setFontWeight(cfg.readEntry("BoldComment", false) ? QFont::Bold : QFont::Normal);
103 m_xmlCommentFormat.setFontItalic(cfg.readEntry("ItalicComment", false));
104
105}
106
void highlightBlock(const QString &text) override
void highlightByRegex(const QTextCharFormat &format, const QRegularExpression &regex, const QString &text)
QList< QRegularExpression > m_xmlKeywordRegexes