ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/s10k/CommonLibs/BasicXMLSyntaxHighlighter/BasicXMLSyntaxHighlighter.cpp
Revision: 1096
Committed: Sat Dec 30 14:40:33 2017 UTC (7 years, 9 months ago) by s10k
Content type: text/x-c++src
File size: 3722 byte(s)
Log Message:
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 Contents

# Content
1 /*
2 *
3 The MIT License (MIT)
4
5 Copyright (c) 2015 Dmitry Ivanov
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 *
25 */
26
27 #include "BasicXMLSyntaxHighlighter.h"
28
29 BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter(QTextDocument * parent) :
30 QSyntaxHighlighter(parent)
31 {
32 setRegexes();
33 setFormats();
34 }
35
36 void 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
62 void 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
76 void 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
88 void 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