-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathQGCodeSyntaxHighlighter.cpp
More file actions
177 lines (160 loc) · 5.44 KB
/
Copy pathQGCodeSyntaxHighlighter.cpp
File metadata and controls
177 lines (160 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @file QGCodeSyntaxHighlighter.cpp
* @brief Implementation of QGCodeSyntaxHighlighter for G-code syntax highlighting
*/
#include "QGCodeSyntaxHighlighter.h"
#include <QSyntaxHighlighter>
#include "QGCodeEditor.h"
/**
* @class QGCodeSyntaxHighlighter
* @brief Syntax highlighter for G-code (CNC machine control language)
*
* Highlights different G-code commands with distinct colors:
* - M/T words: Red
* - G words: Green
* - F words: Yellow
* - S words: Magenta
* - P/Q words: Green
* - X/Y/Z/A/B/C/U/V/W coordinates: Yellow
* - I/J/K/R arcs: Dark Gray
* - Parameters (#): Cyan
* - Comments (; and ()): White/Magenta
*/
/**
* @brief Constructor
* @param parent Parent QTextDocument object
*
* Initializes all highlighting rules for G-code syntax elements.
*/
QGCodeSyntaxHighlighter::QGCodeSyntaxHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
// The escape sequence \s should denote a space and it says so in the docs
// but if you use it you get a 'unrecognised escape char' warning so use \x20 instead
M_WordFormat.setForeground(Qt::red);
M_WordFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression("[mMtT][^\\x20]*");
#else
rule.pattern = QRegExp("[mMtT][^\\x20]*");
#endif
rule.format = M_WordFormat;
highlightingRules.append(rule);
G_WordFormat.setForeground(Qt::green);
G_WordFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression("[gG][^\\x20]*");
#else
rule.pattern = QRegExp("[gG][^\\x20]*");
#endif
rule.format = G_WordFormat;
highlightingRules.append(rule);
F_WordFormat.setForeground(Qt::yellow);
F_WordFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression("[fF][^\\x20 ]*");
#else
rule.pattern = QRegExp("[fF][^\\x20 ]*");
#endif
rule.format = F_WordFormat;
highlightingRules.append(rule);
S_WordFormat.setForeground(Qt::magenta);
S_WordFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression("[sS][^\\x20 ]*");
#else
rule.pattern = QRegExp("[sS][^\\x20 ]*");
#endif
rule.format = S_WordFormat;
highlightingRules.append(rule);
PQ_WordFormat.setForeground(Qt::green);
PQ_WordFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression("[pPqQ][^\\x20 ]*");
#else
rule.pattern = QRegExp("[pPqQ][^\\x20 ]*");
#endif
rule.format = PQ_WordFormat;
highlightingRules.append(rule);
XYZ_WordFormat.setForeground(Qt::yellow);
XYZ_WordFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression("[xXyYzZaAbBcCuUvVwW][^\\x20 ]*");
#else
rule.pattern = QRegExp("[xXyYzZaAbBcCuUvVwW][^\\x20 ]*");
#endif
rule.format = XYZ_WordFormat;
highlightingRules.append(rule);
IJKR_WordFormat.setForeground(Qt::darkGray);
IJKR_WordFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression("[iIjJkKrR][^\\x20 ]*");
#else
rule.pattern = QRegExp("[iIjJkKrR][^\\x20 ]*");
#endif
rule.format = IJKR_WordFormat;
highlightingRules.append(rule);
Param_WordFormat.setForeground(Qt::cyan);
Param_WordFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression("#[^\\x20 ]*");
#else
rule.pattern = QRegExp("#[^\\x20 ]*");
#endif
rule.format = Param_WordFormat;
highlightingRules.append(rule);
// do comments last then won't get colouring of text containing M G F S T etc
semicolonCommentFormat.setForeground(Qt::white);
semicolonCommentFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression(";[^\\n]*");
#else
rule.pattern = QRegExp(";[^\\n]*");
#endif
rule.format = semicolonCommentFormat;
highlightingRules.append(rule);
braceCommentFormat.setForeground(Qt::magenta);
braceCommentFormat.setFontWeight(QFont::Bold);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
rule.pattern = QRegularExpression("\\([^\\n]*");
#else
rule.pattern = QRegExp("\\([^\\n]*");
#endif
rule.format = braceCommentFormat;
highlightingRules.append(rule);
}
/**
* @brief Highlights a single block of text
* @param text The text block to highlight
*
* Applies all matching highlighting rules to the given text block,
* coloring G-code commands and comments appropriately.
*/
void QGCodeSyntaxHighlighter::highlightBlock(const QString &text)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
for (const HighlightingRule &rule : highlightingRules)
{
QRegularExpressionMatchIterator it = rule.pattern.globalMatch(text);
while (it.hasNext())
{
QRegularExpressionMatch match = it.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
#else
foreach (const HighlightingRule &rule, highlightingRules)
{
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0)
{
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
#endif
setCurrentBlockState(0);
}