File indexing completed on 2024-06-09 04:28:39
0001 /* 0002 * SPDX-FileCopyrightText: 2015 Dmitry Ivanov 0003 * 0004 * SPDX-License-Identifier: MIT 0005 */ 0006 #include "BasicXMLSyntaxHighlighter.h" 0007 0008 #include <QApplication> 0009 #include <QPalette> 0010 0011 #include <ksharedconfig.h> 0012 #include <kconfiggroup.h> 0013 0014 BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter(QObject * parent) : 0015 QSyntaxHighlighter(parent) 0016 { 0017 setRegexes(); 0018 setFormats(); 0019 } 0020 0021 BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter(QTextDocument * parent) : 0022 QSyntaxHighlighter(parent) 0023 { 0024 setRegexes(); 0025 setFormats(); 0026 } 0027 0028 BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter(QTextEdit * parent) : 0029 QSyntaxHighlighter(parent) 0030 { 0031 setRegexes(); 0032 setFormats(); 0033 } 0034 0035 void BasicXMLSyntaxHighlighter::highlightBlock(const QString & text) 0036 { 0037 // Special treatment for xml element regex as we use captured text to emulate lookbehind 0038 int xmlElementIndex = m_xmlElementRegex.indexIn(text); 0039 while(xmlElementIndex >= 0) 0040 { 0041 int matchedPos = m_xmlElementRegex.pos(1); 0042 int matchedLength = m_xmlElementRegex.cap(1).length(); 0043 setFormat(matchedPos, matchedLength, m_xmlElementFormat); 0044 0045 xmlElementIndex = m_xmlElementRegex.indexIn(text, matchedPos + matchedLength); 0046 } 0047 0048 // Highlight xml keywords *after* xml elements to fix any occasional / captured into the enclosing element 0049 typedef QList<QRegExp>::const_iterator Iter; 0050 Iter xmlKeywordRegexesEnd = m_xmlKeywordRegexes.constEnd(); 0051 for(Iter it = m_xmlKeywordRegexes.constBegin(); it != xmlKeywordRegexesEnd; ++it) { 0052 const QRegExp & regex = *it; 0053 highlightByRegex(m_xmlKeywordFormat, regex, text); 0054 } 0055 0056 highlightByRegex(m_xmlAttributeFormat, m_xmlAttributeRegex, text); 0057 highlightByRegex(m_xmlCommentFormat, m_xmlCommentRegex, text); 0058 highlightByRegex(m_xmlValueFormat, m_xmlValueRegex, text); 0059 } 0060 0061 void BasicXMLSyntaxHighlighter::highlightByRegex(const QTextCharFormat & format, 0062 const QRegExp & regex, const QString & text) 0063 { 0064 int index = regex.indexIn(text); 0065 0066 while(index >= 0) 0067 { 0068 int matchedLength = regex.matchedLength(); 0069 setFormat(index, matchedLength, format); 0070 0071 index = regex.indexIn(text, index + matchedLength); 0072 } 0073 } 0074 0075 void BasicXMLSyntaxHighlighter::setRegexes() 0076 { 0077 m_xmlElementRegex.setPattern("<[\\s]*[/]?[\\s]*([^\\n]\\w*)(?=[\\s/>])"); 0078 m_xmlAttributeRegex.setPattern("[\\w\\-]+(?=\\=)"); 0079 m_xmlValueRegex.setPattern("\"[^\\n\"]+\"(?=[\\s/>])"); 0080 m_xmlCommentRegex.setPattern("<!--[^\\n]*-->"); 0081 0082 m_xmlKeywordRegexes = QList<QRegExp>() << QRegExp("<\\?") << QRegExp("/>") 0083 << QRegExp(">") << QRegExp("<") << QRegExp("</") 0084 << QRegExp("\\?>"); 0085 } 0086 0087 void BasicXMLSyntaxHighlighter::setFormats() 0088 { 0089 KConfigGroup cfg(KSharedConfig::openConfig(), "SvgTextTool"); 0090 QColor background = cfg.readEntry("colorEditorBackground", qApp->palette().window().color()); 0091 0092 m_xmlKeywordFormat.setForeground(cfg.readEntry("colorKeyword", QColor(background.value() < 100 ? Qt::cyan : Qt::blue))); 0093 m_xmlKeywordFormat.setFontWeight(cfg.readEntry("BoldKeyword", true) ? QFont::Bold : QFont::Normal); 0094 m_xmlKeywordFormat.setFontItalic((cfg.readEntry("ItalicKeyword", false))); 0095 0096 m_xmlElementFormat.setForeground(cfg.readEntry("colorElement", QColor(background.value() < 100 ? Qt::magenta : Qt::darkMagenta))); 0097 m_xmlElementFormat.setFontWeight(cfg.readEntry("BoldElement", true) ? QFont::Bold : QFont::Normal); 0098 m_xmlElementFormat.setFontItalic((cfg.readEntry("ItalicElement", false))); 0099 0100 m_xmlAttributeFormat.setForeground(cfg.readEntry("colorAttribute", QColor(background.value() < 100 ? Qt::green : Qt::darkGreen))); 0101 m_xmlAttributeFormat.setFontWeight(cfg.readEntry("BoldAttribute", true) ? QFont::Bold : QFont::Normal); 0102 m_xmlAttributeFormat.setFontItalic((cfg.readEntry("ItalicAttribute", true))); 0103 0104 m_xmlValueFormat.setForeground(cfg.readEntry("colorValue", QColor(background.value() < 100 ? Qt::red: Qt::darkRed))); 0105 m_xmlValueFormat.setFontWeight(cfg.readEntry("BoldValue", true) ? QFont::Bold : QFont::Normal); 0106 m_xmlValueFormat.setFontItalic(cfg.readEntry("ItalicValue", false)); 0107 0108 m_xmlCommentFormat.setForeground(cfg.readEntry("colorComment", QColor(background.value() < 100 ? Qt::lightGray : Qt::gray))); 0109 m_xmlCommentFormat.setFontWeight(cfg.readEntry("BoldComment", false) ? QFont::Bold : QFont::Normal); 0110 m_xmlCommentFormat.setFontItalic(cfg.readEntry("ItalicComment", false)); 0111 0112 } 0113