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 int xmlElementIndex = m_xmlElementRegex.indexIn(text);
39 while(xmlElementIndex >= 0)
40 {
41 int matchedPos = m_xmlElementRegex.pos(1);
42 int matchedLength = m_xmlElementRegex.cap(1).length();
43 setFormat(matchedPos, matchedLength, m_xmlElementFormat);
44
45 xmlElementIndex = m_xmlElementRegex.indexIn(text, matchedPos + matchedLength);
46 }
47
48 // Highlight xml keywords *after* xml elements to fix any occasional / captured into the enclosing element
50 Iter xmlKeywordRegexesEnd = m_xmlKeywordRegexes.constEnd();
51 for(Iter it = m_xmlKeywordRegexes.constBegin(); it != xmlKeywordRegexesEnd; ++it) {
52 const QRegExp & regex = *it;
54 }
55
59}
60
61void BasicXMLSyntaxHighlighter::highlightByRegex(const QTextCharFormat & format,
62 const QRegExp & regex, const QString & text)
63{
64 int index = regex.indexIn(text);
65
66 while(index >= 0)
67 {
68 int matchedLength = regex.matchedLength();
69 setFormat(index, matchedLength, format);
70
71 index = regex.indexIn(text, index + matchedLength);
72 }
73}
74
76{
77 m_xmlElementRegex.setPattern("<[\\s]*[/]?[\\s]*([^\\n]\\w*)(?=[\\s/>])");
78 m_xmlAttributeRegex.setPattern("[\\w\\-]+(?=\\=)");
79 m_xmlValueRegex.setPattern("\"[^\\n\"]+\"(?=[\\s/>])");
80 m_xmlCommentRegex.setPattern("<!--[^\\n]*-->");
81
82 m_xmlKeywordRegexes = QList<QRegExp>() << QRegExp("<\\?") << QRegExp("/>")
83 << QRegExp(">") << QRegExp("<") << QRegExp("</")
84 << QRegExp("\\?>");
85}
86
88{
89 KConfigGroup cfg(KSharedConfig::openConfig(), "SvgTextTool");
90 QColor background = cfg.readEntry("colorEditorBackground", qApp->palette().window().color());
91
92 m_xmlKeywordFormat.setForeground(cfg.readEntry("colorKeyword", QColor(background.value() < 100 ? Qt::cyan : Qt::blue)));
93 m_xmlKeywordFormat.setFontWeight(cfg.readEntry("BoldKeyword", true) ? QFont::Bold : QFont::Normal);
94 m_xmlKeywordFormat.setFontItalic((cfg.readEntry("ItalicKeyword", false)));
95
96 m_xmlElementFormat.setForeground(cfg.readEntry("colorElement", QColor(background.value() < 100 ? Qt::magenta : Qt::darkMagenta)));
97 m_xmlElementFormat.setFontWeight(cfg.readEntry("BoldElement", true) ? QFont::Bold : QFont::Normal);
98 m_xmlElementFormat.setFontItalic((cfg.readEntry("ItalicElement", false)));
99
100 m_xmlAttributeFormat.setForeground(cfg.readEntry("colorAttribute", QColor(background.value() < 100 ? Qt::green : Qt::darkGreen)));
101 m_xmlAttributeFormat.setFontWeight(cfg.readEntry("BoldAttribute", true) ? QFont::Bold : QFont::Normal);
102 m_xmlAttributeFormat.setFontItalic((cfg.readEntry("ItalicAttribute", true)));
103
104 m_xmlValueFormat.setForeground(cfg.readEntry("colorValue", QColor(background.value() < 100 ? Qt::red: Qt::darkRed)));
105 m_xmlValueFormat.setFontWeight(cfg.readEntry("BoldValue", true) ? QFont::Bold : QFont::Normal);
106 m_xmlValueFormat.setFontItalic(cfg.readEntry("ItalicValue", false));
107
108 m_xmlCommentFormat.setForeground(cfg.readEntry("colorComment", QColor(background.value() < 100 ? Qt::lightGray : Qt::gray)));
109 m_xmlCommentFormat.setFontWeight(cfg.readEntry("BoldComment", false) ? QFont::Bold : QFont::Normal);
110 m_xmlCommentFormat.setFontItalic(cfg.readEntry("ItalicComment", false));
111
112}
113
void highlightByRegex(const QTextCharFormat &format, const QRegExp &regex, const QString &text)
void highlightBlock(const QString &text) override