Krita Source Code Documentation
Loading...
Searching...
No Matches
BasicXMLSyntaxHighlighter Class Reference

#include <BasicXMLSyntaxHighlighter.h>

+ Inheritance diagram for BasicXMLSyntaxHighlighter:

Public Member Functions

 BasicXMLSyntaxHighlighter (QObject *parent)
 
 BasicXMLSyntaxHighlighter (QTextDocument *parent)
 
 BasicXMLSyntaxHighlighter (QTextEdit *parent)
 
void setFormats ()
 

Protected Member Functions

void highlightBlock (const QString &text) override
 

Private Member Functions

void highlightByRegex (const QTextCharFormat &format, const QRegularExpression &regex, const QString &text)
 
void setRegexes ()
 

Private Attributes

QTextCharFormat m_xmlAttributeFormat
 
QRegularExpression m_xmlAttributeRegex
 
QTextCharFormat m_xmlCommentFormat
 
QRegularExpression m_xmlCommentRegex
 
QTextCharFormat m_xmlElementFormat
 
QRegularExpression m_xmlElementRegex
 
QTextCharFormat m_xmlKeywordFormat
 
QList< QRegularExpression > m_xmlKeywordRegexes
 
QTextCharFormat m_xmlValueFormat
 
QRegularExpression m_xmlValueRegex
 

Detailed Description

A Basic XML syntax highlighter in C++/Qt (subclass of QSyntaxHighlighter). Uses simple regexes to highlight not very complicated XML content.

  • The primary limitations are:
  • No specific handling for nested quotes within attributes
  • No handling for multi-line comments
See also
https://github.com/d1vanov/basic-xml-syntax-highlighter

Definition at line 24 of file BasicXMLSyntaxHighlighter.h.

Constructor & Destructor Documentation

◆ BasicXMLSyntaxHighlighter() [1/3]

BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter ( QObject * parent)

Definition at line 14 of file BasicXMLSyntaxHighlighter.cpp.

14 :
15 QSyntaxHighlighter(parent)
16{
17 setRegexes();
18 setFormats();
19}

References setFormats(), and setRegexes().

◆ BasicXMLSyntaxHighlighter() [2/3]

BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter ( QTextDocument * parent)

Definition at line 21 of file BasicXMLSyntaxHighlighter.cpp.

21 :
22 QSyntaxHighlighter(parent)
23{
24 setRegexes();
25 setFormats();
26}

References setFormats(), and setRegexes().

◆ BasicXMLSyntaxHighlighter() [3/3]

BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter ( QTextEdit * parent)

Definition at line 28 of file BasicXMLSyntaxHighlighter.cpp.

28 :
29 QSyntaxHighlighter(parent)
30{
31 setRegexes();
32 setFormats();
33}

References setFormats(), and setRegexes().

Member Function Documentation

◆ highlightBlock()

void BasicXMLSyntaxHighlighter::highlightBlock ( const QString & text)
overrideprotected

Definition at line 35 of file BasicXMLSyntaxHighlighter.cpp.

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}
void highlightByRegex(const QTextCharFormat &format, const QRegularExpression &regex, const QString &text)
QList< QRegularExpression > m_xmlKeywordRegexes

References highlightByRegex(), m_xmlAttributeFormat, m_xmlAttributeRegex, m_xmlCommentFormat, m_xmlCommentRegex, m_xmlElementFormat, m_xmlElementRegex, m_xmlKeywordFormat, m_xmlKeywordRegexes, m_xmlValueFormat, and m_xmlValueRegex.

◆ highlightByRegex()

void BasicXMLSyntaxHighlighter::highlightByRegex ( const QTextCharFormat & format,
const QRegularExpression & regex,
const QString & text )
private

Definition at line 57 of file BasicXMLSyntaxHighlighter.cpp.

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}

◆ setFormats()

void BasicXMLSyntaxHighlighter::setFormats ( )

Definition at line 80 of file BasicXMLSyntaxHighlighter.cpp.

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}

References m_xmlAttributeFormat, m_xmlCommentFormat, m_xmlElementFormat, m_xmlKeywordFormat, and m_xmlValueFormat.

◆ setRegexes()

void BasicXMLSyntaxHighlighter::setRegexes ( )
private

Definition at line 67 of file BasicXMLSyntaxHighlighter.cpp.

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}

References m_xmlAttributeRegex, m_xmlCommentRegex, m_xmlElementRegex, m_xmlKeywordRegexes, and m_xmlValueRegex.

Member Data Documentation

◆ m_xmlAttributeFormat

QTextCharFormat BasicXMLSyntaxHighlighter::m_xmlAttributeFormat
private

Definition at line 47 of file BasicXMLSyntaxHighlighter.h.

◆ m_xmlAttributeRegex

QRegularExpression BasicXMLSyntaxHighlighter::m_xmlAttributeRegex
private

Definition at line 53 of file BasicXMLSyntaxHighlighter.h.

◆ m_xmlCommentFormat

QTextCharFormat BasicXMLSyntaxHighlighter::m_xmlCommentFormat
private

Definition at line 49 of file BasicXMLSyntaxHighlighter.h.

◆ m_xmlCommentRegex

QRegularExpression BasicXMLSyntaxHighlighter::m_xmlCommentRegex
private

Definition at line 55 of file BasicXMLSyntaxHighlighter.h.

◆ m_xmlElementFormat

QTextCharFormat BasicXMLSyntaxHighlighter::m_xmlElementFormat
private

Definition at line 46 of file BasicXMLSyntaxHighlighter.h.

◆ m_xmlElementRegex

QRegularExpression BasicXMLSyntaxHighlighter::m_xmlElementRegex
private

Definition at line 52 of file BasicXMLSyntaxHighlighter.h.

◆ m_xmlKeywordFormat

QTextCharFormat BasicXMLSyntaxHighlighter::m_xmlKeywordFormat
private

Definition at line 45 of file BasicXMLSyntaxHighlighter.h.

◆ m_xmlKeywordRegexes

QList<QRegularExpression> BasicXMLSyntaxHighlighter::m_xmlKeywordRegexes
private

Definition at line 51 of file BasicXMLSyntaxHighlighter.h.

◆ m_xmlValueFormat

QTextCharFormat BasicXMLSyntaxHighlighter::m_xmlValueFormat
private

Definition at line 48 of file BasicXMLSyntaxHighlighter.h.

◆ m_xmlValueRegex

QRegularExpression BasicXMLSyntaxHighlighter::m_xmlValueRegex
private

Definition at line 54 of file BasicXMLSyntaxHighlighter.h.


The documentation for this class was generated from the following files: