-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcodeline.cpp
More file actions
117 lines (96 loc) · 2.53 KB
/
gcodeline.cpp
File metadata and controls
117 lines (96 loc) · 2.53 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
#include "gcodeline.h"
const QRegExp commentsSplitter(";");
const QRegExp fieldsSplitter("\\s");
GCodeLine::GCodeLine()
: mLine(QString()),
mLineType(Empty),
mCommand(QString()),
mComment(QString()),
mSelected(false)
{
}
GCodeLine::GCodeLine(const QString &line)
: mLine(line),
mLineType(Empty),
mCommand(QString()),
mComment(QString()),
mSelected(false)
{
if (line.length() > 0) {
int pos = line.indexOf(commentsSplitter);
if (pos < 0) {
mCommand = line.trimmed();
mLineType = Command;
} else if (pos == 0) {
mComment = line.mid(1).trimmed();
mLineType = Comment;
} else {
mCommand = line.left(pos).trimmed();
mComment = line.mid(pos + 1).trimmed();
mLineType = Command;
}
if (mCommand.startsWith("M117", Qt::CaseInsensitive)) {
mCommand = "M117" + mCommand.mid(4);
} else {
mCommand = mCommand.toUpper();
}
if (mLineType == Command) {
mFields = mCommand.split(fieldsSplitter, QString::SkipEmptyParts);
}
QString c = code();
if (!(c.isEmpty() || c == "M117")) {
for (int i = 1; i < mFields.size(); i++) {
QString f = mFields[i];
char p = f.at(0).toUpper().toLatin1();
mKeys.append(p);
mParameters.insert(p, f.mid(1));
}
}
}
}
QString GCodeLine::code() const
{
if (mFields.isEmpty()) {
return QString();
}
return mFields.at(0);
}
double GCodeLine::parameter(const char &p, bool *ok) const
{
QString par = parameterStr(p, ok);
return par.isEmpty() ? 0.0 :par.toDouble(ok);
}
float GCodeLine::parameterFloat(const char &p, bool *ok) const
{
QString par = parameterStr(p, ok);
return par.isEmpty() ? 0.0f :par.toFloat(ok);
}
int GCodeLine::parameterInt(const char &p, bool *ok) const
{
QString par = parameterStr(p, ok);
return par.isEmpty() ? 0 :par.toInt(ok);
}
QString GCodeLine::parameterStr(const char &p, bool *ok) const
{
bool yes = mParameters.contains(p);
if (ok) {
*ok = yes;
}
if (yes) {
return mParameters.value(p);
}
return QString();
}
void GCodeLine::select()
{
mSelected = true;
}
void GCodeLine::deselect()
{
mSelected = false;
}
bool GCodeLine::toggleSelection()
{
mSelected = !mSelected;
return mSelected;
}