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 QRegExp &regex, const QString &text)
 
void setRegexes ()
 

Private Attributes

QTextCharFormat m_xmlAttributeFormat
 
QRegExp m_xmlAttributeRegex
 
QTextCharFormat m_xmlCommentFormat
 
QRegExp m_xmlCommentRegex
 
QTextCharFormat m_xmlElementFormat
 
QRegExp m_xmlElementRegex
 
QTextCharFormat m_xmlKeywordFormat
 
QList< QRegExp > m_xmlKeywordRegexes
 
QTextCharFormat m_xmlValueFormat
 
QRegExp 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 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}
void highlightByRegex(const QTextCharFormat &format, const QRegExp &regex, const QString &text)

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 QRegExp & regex,
const QString & text )
private

Definition at line 61 of file BasicXMLSyntaxHighlighter.cpp.

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}

◆ setFormats()

void BasicXMLSyntaxHighlighter::setFormats ( )

Definition at line 87 of file BasicXMLSyntaxHighlighter.cpp.

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}

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

◆ setRegexes()

void BasicXMLSyntaxHighlighter::setRegexes ( )
private

Definition at line 75 of file BasicXMLSyntaxHighlighter.cpp.

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}

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

QRegExp 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

QRegExp 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

QRegExp 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<QRegExp> 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

QRegExp BasicXMLSyntaxHighlighter::m_xmlValueRegex
private

Definition at line 54 of file BasicXMLSyntaxHighlighter.h.


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