Skip to content

Commit e4724e7

Browse files
author
Michal
committed
Added syntax highlighting - WIP, added TODO
1 parent c59ef7b commit e4724e7

File tree

8 files changed

+305
-65
lines changed

8 files changed

+305
-65
lines changed

src/QtSerialMonitor.pro

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ CONFIG += c++11
3232
SOURCES += \
3333
codeeditor.cpp \
3434
filereader.cpp \
35+
highlighter.cpp \
3536
infodialog.cpp \
3637
logger.cpp \
3738
main.cpp \
@@ -46,6 +47,7 @@ HEADERS += \
4647
codeeditor.h \
4748
config.h \
4849
filereader.h \
50+
highlighter.h \
4951
infodialog.h \
5052
logger.h \
5153
mainwindow.h \
@@ -66,7 +68,8 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
6668
!isEmpty(target.path): INSTALLS += target
6769

6870
DISTFILES += \
69-
QtSM.ico
71+
QtSM.ico \
72+
TODO
7073

7174
RESOURCES += \
7275
3dres.qrc

src/TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- QRegExp -> new QRegularExpressions from Qt 5.0

src/codeeditor.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
1010
connect(this, &CodeEditor::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine);
1111

1212
updateLineNumberAreaWidth(0);
13-
// highlightCurrentLine();
1413
}
1514

1615
int CodeEditor::lineNumberAreaWidth()
@@ -24,7 +23,7 @@ int CodeEditor::lineNumberAreaWidth()
2423
++digits;
2524
}
2625

27-
int space = 3 + fontMetrics().horizontalAdvance(" ") * digits;
26+
int space = 10 + fontMetrics().horizontalAdvance(" ") * digits;
2827

2928
return space;
3029
}
@@ -56,21 +55,19 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
5655
void CodeEditor::highlightCurrentLine()
5756
{
5857
QList<QTextEdit::ExtraSelection> extraSelections;
58+
QTextEdit::ExtraSelection selection;
59+
QColor lineColor = QColor(Qt::yellow).lighter(160);
5960

60-
if (!isReadOnly())
61-
{
62-
QTextEdit::ExtraSelection selection;
61+
selection.format.setBackground(lineColor);
62+
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
63+
selection.cursor = textCursor();
64+
selection.cursor.clearSelection();
65+
extraSelections.append(selection);
6366

64-
QColor lineColor = QColor(Qt::yellow).lighter(160);
67+
setExtraSelections(extraSelections);
6568

66-
selection.format.setBackground(lineColor);
67-
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
68-
selection.cursor = textCursor();
69-
selection.cursor.clearSelection();
70-
extraSelections.append(selection);
71-
}
69+
selection.cursor.clearSelection();
7270

73-
setExtraSelections(extraSelections);
7471
}
7572

7673
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)

src/highlighter.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2016 The Qt Company Ltd.
4+
** Contact: https://www.qt.io/licensing/
5+
**
6+
** This file is part of the examples of the Qt Toolkit.
7+
**
8+
** $QT_BEGIN_LICENSE:BSD$
9+
** Commercial License Usage
10+
** Licensees holding valid commercial Qt licenses may use this file in
11+
** accordance with the commercial license agreement provided with the
12+
** Software or, alternatively, in accordance with the terms contained in
13+
** a written agreement between you and The Qt Company. For licensing terms
14+
** and conditions see https://www.qt.io/terms-conditions. For further
15+
** information use the contact form at https://www.qt.io/contact-us.
16+
**
17+
** BSD License Usage
18+
** Alternatively, you may use this file under the terms of the BSD license
19+
** as follows:
20+
**
21+
** "Redistribution and use in source and binary forms, with or without
22+
** modification, are permitted provided that the following conditions are
23+
** met:
24+
** * Redistributions of source code must retain the above copyright
25+
** notice, this list of conditions and the following disclaimer.
26+
** * Redistributions in binary form must reproduce the above copyright
27+
** notice, this list of conditions and the following disclaimer in
28+
** the documentation and/or other materials provided with the
29+
** distribution.
30+
** * Neither the name of The Qt Company Ltd nor the names of its
31+
** contributors may be used to endorse or promote products derived
32+
** from this software without specific prior written permission.
33+
**
34+
**
35+
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36+
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37+
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38+
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39+
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40+
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41+
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42+
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43+
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44+
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45+
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46+
**
47+
** $QT_END_LICENSE$
48+
**
49+
****************************************************************************/
50+
51+
#include "highlighter.h"
52+
53+
Highlighter::Highlighter(QTextDocument *parent)
54+
: QSyntaxHighlighter(parent)
55+
{
56+
HighlightingRule rule;
57+
58+
keywordFormat.setForeground(Qt::darkBlue);
59+
keywordFormat.setFontWeight(QFont::Bold);
60+
// const QString keywordPatterns[] = {
61+
// QStringLiteral("\\bchar\\b"), QStringLiteral("\\bclass\\b"), QStringLiteral("\\bconst\\b"),
62+
// QStringLiteral("\\bdouble\\b"), QStringLiteral("\\benum\\b"), QStringLiteral("\\bexplicit\\b"),
63+
// QStringLiteral("\\bfriend\\b"), QStringLiteral("\\binline\\b"), QStringLiteral("\\bint\\b"),
64+
// QStringLiteral("\\blong\\b"), QStringLiteral("\\bnamespace\\b"), QStringLiteral("\\boperator\\b"),
65+
// QStringLiteral("\\bprivate\\b"), QStringLiteral("\\bprotected\\b"), QStringLiteral("\\bpublic\\b"),
66+
// QStringLiteral("\\bshort\\b"), QStringLiteral("\\bsignals\\b"), QStringLiteral("\\bsigned\\b"),
67+
// QStringLiteral("\\bslots\\b"), QStringLiteral("\\bstatic\\b"), QStringLiteral("\\bstruct\\b"),
68+
// QStringLiteral("\\btemplate\\b"), QStringLiteral("\\btypedef\\b"), QStringLiteral("\\btypename\\b"),
69+
// QStringLiteral("\\bunion\\b"), QStringLiteral("\\bunsigned\\b"), QStringLiteral("\\bvirtual\\b"),
70+
// QStringLiteral("\\bvoid\\b"), QStringLiteral("\\bvolatile\\b"), QStringLiteral("\\bbool\\b")
71+
// };
72+
// for (const QString &pattern : keywordPatterns) {
73+
// rule.pattern = QRegularExpression(pattern);
74+
// rule.format = keywordFormat;
75+
// highlightingRules.append(rule);
76+
// }
77+
78+
classFormat.setFontWeight(QFont::Bold);
79+
classFormat.setForeground(Qt::darkMagenta);
80+
rule.pattern = QRegularExpression(QStringLiteral("\\bQ[A-Za-z]+\\b"));
81+
rule.format = classFormat;
82+
highlightingRules.append(rule);
83+
84+
singleLineCommentFormat.setForeground(Qt::red);
85+
rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*"));
86+
rule.format = singleLineCommentFormat;
87+
highlightingRules.append(rule);
88+
89+
multiLineCommentFormat.setForeground(Qt::red);
90+
91+
quotationFormat.setForeground(Qt::darkGreen);
92+
rule.pattern = QRegularExpression(QStringLiteral("\".*\""));
93+
rule.format = quotationFormat;
94+
highlightingRules.append(rule);
95+
96+
functionFormat.setFontItalic(true);
97+
functionFormat.setForeground(Qt::blue);
98+
rule.pattern = QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()"));
99+
rule.format = functionFormat;
100+
highlightingRules.append(rule);
101+
102+
commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
103+
commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
104+
}
105+
106+
void Highlighter::highlightBlock(const QString &text)
107+
{
108+
for (const HighlightingRule &rule : qAsConst(highlightingRules))
109+
{
110+
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text,0,
111+
QRegularExpression::MatchType::PartialPreferCompleteMatch,
112+
QRegularExpression::MatchOption::NoMatchOption);
113+
if (matchIterator.hasNext())
114+
{
115+
QRegularExpressionMatch match = matchIterator.next();
116+
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
117+
}
118+
}
119+
setCurrentBlockState(0);
120+
121+
int startIndex = 0;
122+
if (previousBlockState() != 1)
123+
startIndex = text.indexOf(commentStartExpression);
124+
125+
while (startIndex >= 0)
126+
{
127+
QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
128+
int endIndex = match.capturedStart();
129+
int commentLength = 0;
130+
if (endIndex == -1)
131+
{
132+
setCurrentBlockState(1);
133+
commentLength = text.length() - startIndex;
134+
}
135+
else
136+
{
137+
commentLength = endIndex - startIndex + match.capturedLength();
138+
}
139+
setFormat(startIndex, commentLength, multiLineCommentFormat);
140+
startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
141+
}
142+
}

src/highlighter.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2016 The Qt Company Ltd.
4+
** Contact: https://www.qt.io/licensing/
5+
**
6+
** This file is part of the examples of the Qt Toolkit.
7+
**
8+
** $QT_BEGIN_LICENSE:BSD$
9+
** Commercial License Usage
10+
** Licensees holding valid commercial Qt licenses may use this file in
11+
** accordance with the commercial license agreement provided with the
12+
** Software or, alternatively, in accordance with the terms contained in
13+
** a written agreement between you and The Qt Company. For licensing terms
14+
** and conditions see https://www.qt.io/terms-conditions. For further
15+
** information use the contact form at https://www.qt.io/contact-us.
16+
**
17+
** BSD License Usage
18+
** Alternatively, you may use this file under the terms of the BSD license
19+
** as follows:
20+
**
21+
** "Redistribution and use in source and binary forms, with or without
22+
** modification, are permitted provided that the following conditions are
23+
** met:
24+
** * Redistributions of source code must retain the above copyright
25+
** notice, this list of conditions and the following disclaimer.
26+
** * Redistributions in binary form must reproduce the above copyright
27+
** notice, this list of conditions and the following disclaimer in
28+
** the documentation and/or other materials provided with the
29+
** distribution.
30+
** * Neither the name of The Qt Company Ltd nor the names of its
31+
** contributors may be used to endorse or promote products derived
32+
** from this software without specific prior written permission.
33+
**
34+
**
35+
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36+
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37+
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38+
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39+
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40+
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41+
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42+
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43+
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44+
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45+
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46+
**
47+
** $QT_END_LICENSE$
48+
**
49+
****************************************************************************/
50+
51+
#ifndef HIGHLIGHTER_H
52+
#define HIGHLIGHTER_H
53+
54+
#include <QSyntaxHighlighter>
55+
#include <QTextCharFormat>
56+
#include <QRegularExpression>
57+
58+
QT_BEGIN_NAMESPACE
59+
class QTextDocument;
60+
QT_END_NAMESPACE
61+
62+
//! [0]
63+
class Highlighter : public QSyntaxHighlighter
64+
{
65+
Q_OBJECT
66+
67+
public:
68+
Highlighter(QTextDocument *parent = nullptr);
69+
70+
protected:
71+
void highlightBlock(const QString &text) override;
72+
73+
private:
74+
struct HighlightingRule
75+
{
76+
QRegularExpression pattern;
77+
QTextCharFormat format;
78+
};
79+
QVector<HighlightingRule> highlightingRules;
80+
81+
QRegularExpression commentStartExpression;
82+
QRegularExpression commentEndExpression;
83+
84+
QTextCharFormat keywordFormat;
85+
QTextCharFormat classFormat;
86+
QTextCharFormat singleLineCommentFormat;
87+
QTextCharFormat multiLineCommentFormat;
88+
QTextCharFormat quotationFormat;
89+
QTextCharFormat functionFormat;
90+
};
91+
//! [0]
92+
93+
#endif // HIGHLIGHTER_H

0 commit comments

Comments
 (0)