source: s10k/CommonLibs/BasicXMLSyntaxHighlighter/BasicXMLSyntaxHighlighter.cpp@ 1109

Last change on this file since 1109 was 1096, checked in by s10k, 7 years ago

Added zlib, quazip, basicxmlsyntaxhighlighter, conditionalsemaphore and linenumberdisplay libraries. zlib and quazip are pre-compiled, but you can compile them yourself, just delete the dll files (or equivalent binary files to your OS)

File size: 3.6 KB
RevLine 
[1096]1/*
2 *
3The MIT License (MIT)
4
5Copyright (c) 2015 Dmitry Ivanov
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23THE SOFTWARE.
24*
25*/
26
27#include "BasicXMLSyntaxHighlighter.h"
28
29BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter(QTextDocument * parent) :
30 QSyntaxHighlighter(parent)
31{
32 setRegexes();
33 setFormats();
34}
35
36void BasicXMLSyntaxHighlighter::highlightBlock(const QString & text)
37{
38 // Special treatment for xml element regex as we use captured text to emulate lookbehind
39 int xmlElementIndex = m_xmlElementRegex.indexIn(text);
40 while(xmlElementIndex >= 0)
41 {
42 int matchedPos = m_xmlElementRegex.pos(1);
43 int matchedLength = m_xmlElementRegex.cap(1).length();
44 setFormat(matchedPos, matchedLength, m_xmlElementFormat);
45
46 xmlElementIndex = m_xmlElementRegex.indexIn(text, matchedPos + matchedLength);
47 }
48
49 // Highlight xml keywords *after* xml elements to fix any occasional / captured into the enclosing element
50 typedef QList<QRegExp>::const_iterator Iter;
51 Iter xmlKeywordRegexesEnd = m_xmlKeywordRegexes.end();
52 for(Iter it = m_xmlKeywordRegexes.begin(); it != xmlKeywordRegexesEnd; ++it) {
53 const QRegExp & regex = *it;
54 highlightByRegex(m_xmlKeywordFormat, regex, text);
55 }
56
57 highlightByRegex(m_xmlAttributeFormat, m_xmlAttributeRegex, text);
58 highlightByRegex(m_xmlCommentFormat, m_xmlCommentRegex, text);
59 highlightByRegex(m_xmlValueFormat, m_xmlValueRegex, text);
60}
61
62void BasicXMLSyntaxHighlighter::highlightByRegex(const QTextCharFormat & format,
63 const QRegExp & regex, const QString & text)
64{
65 int index = regex.indexIn(text);
66
67 while(index >= 0)
68 {
69 int matchedLength = regex.matchedLength();
70 setFormat(index, matchedLength, format);
71
72 index = regex.indexIn(text, index + matchedLength);
73 }
74}
75
76void BasicXMLSyntaxHighlighter::setRegexes()
77{
78 m_xmlElementRegex.setPattern("<[\\s]*[/]?[\\s]*([^\\n]\\w*)(?=[\\s/>])");
79 m_xmlAttributeRegex.setPattern("\\w+(?=\\=)");
80 m_xmlValueRegex.setPattern("\"[^\\n\"]+\"(?=[\\s/>])");
81 m_xmlCommentRegex.setPattern("<!--[^\\n]*-->");
82
83 m_xmlKeywordRegexes = QList<QRegExp>() << QRegExp("<\\?") << QRegExp("/>")
84 << QRegExp(">") << QRegExp("<") << QRegExp("</")
85 << QRegExp("\\?>");
86}
87
88void BasicXMLSyntaxHighlighter::setFormats()
89{
90 m_xmlKeywordFormat.setForeground(Qt::blue);
91
92 m_xmlElementFormat.setForeground(Qt::blue);
93
94 m_xmlAttributeFormat.setForeground(Qt::red);
95
96 m_xmlValueFormat.setForeground(QColor(0x8055FF));
97 m_xmlValueFormat.setFontWeight(QFont::Bold);
98
99 m_xmlCommentFormat.setForeground(Qt::darkGreen);
100}
101
Note: See TracBrowser for help on using the repository browser.