-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgcode_editor.py
More file actions
132 lines (117 loc) · 5 KB
/
gcode_editor.py
File metadata and controls
132 lines (117 loc) · 5 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
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import *
import gcode
class QsciGcodeLexer(QsciLexerCPP):
def keywords(self, index):
keywords = QsciLexerCPP.keywords(self, index) or ''
# primary keywords
if index == 1:
return 'G' + 'M' + "F"
# secondary keywords
if index == 2:
return "X"+"Y"+"Z"+"I"+"J"
# doc comment keywords
if index == 3:
return keywords
# global classes
if index == 4:
return keywords
return keywords
class GcodeEditorWidget(QWidget):
def __init__(self, object_viewer=None):
QWidget.__init__(self)
self.lexers={"ngc":QsciGcodeLexer()}
self.suffixToLexer={"ngc":"ngc"}
self.object_viewer = object_viewer
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.label = QLabel()
self.editor = QsciScintilla()
self.configureEditor(self.editor)
self.layout.addWidget(self.label)
self.layout.addWidget(self.editor)
self.editor.selectionChanged.connect(self.onSelectionChanged)
self.editor.textChanged.connect(self.onTextChanged)
self.pathTool = None
self.editingFlag = False
def setObjectViewer(self, object_viewer):
self.object_viewer = object_viewer
def setPathTool(self, path):
self.pathTool = path
def onSelectionChanged(self):
selection = self.editor.getSelection()
if self.object_viewer is not None:
self.object_viewer.setSelection(selection[0], selection[2])
def onTextChanged(self):
self.editingFlag=True
if self.pathTool is not None:
print ("..")
self.pathTool.updatePath( gcode.parse_gcode(self.getText()))
self.editingFlag=False
def configureEditor(self, editor):
self.__lexer = self.lexers["ngc"]
editor.setLexer(self.__lexer)
editor.setMarginType(1, QsciScintilla.TextMargin)
editor.setMarginType(0, QsciScintilla.SymbolMargin)
editor.setMarginMarkerMask(1, 0b1111)
editor.setMarginMarkerMask(0, 0b1111)
editor.setMarginsForegroundColor(QColor("#ffFF8888"))
editor.setUtf8(True) # Set encoding to UTF-8
#editor.indicatorDefine(QsciScintilla.FullBoxIndicator, 0)
editor.indicatorDefine(QsciScintilla.BoxIndicator, 0)
editor.setAnnotationDisplay(QsciScintilla.AnnotationStandard)
def highlightLine(self, line_number, refresh = False):
if self.editingFlag: # don't update text if a path tool is set, and we're in editing mode
return
marginTextStyle = QsciStyle()
marginTextStyle.setPaper(QColor("#ffFF8888"))
self.editor.blockSignals(True)
self.editor.setCursorPosition(line_number, 0)
self.editor.setSelection(line_number, 0, line_number+1, 0)
self.editor.blockSignals(False)
if refresh and self.object_viewer is not None:
self.object_viewer.setSelection(0, line_number)
def getText(self):
return [l for l in self.editor.text().splitlines()]
def updateText(self, text, label="", fileSuffix="ngc"):
print("updating text")
if self.editingFlag: # don't update text if user is currently editing, to avoid propagation loops
return
# turn off signals to prevent event loops
self.editor.blockSignals(True)
if fileSuffix in self.suffixToLexer.keys():
self.__lexer = self.lexers[self.suffixToLexer[fileSuffix]]
self.editor.setLexer(self.__lexer)
#label+=" ("+self.suffixToLexer[fileSuffix]+")"
marginTextStyle= QsciStyle()
marginTextStyle.setPaper(QColor("#ffFF8888"))
self.editor.setText("")
self.label.setText(label)
skipped_lines = 0
annotation=None
self.editor.setText(text)
# for linenumber, l in enumerate(text):
# idx=linenumber-skipped_lines
# if l is None:
# #editor.append("\n")
# if annotation is None:
# annotation="<"
# else:
# annotation+="\n<"
# skipped_lines+=1
# self.editor.setMarginText(idx, "~", marginTextStyle)
# else:
# if annotation is not None:
# self.editor.annotate(idx-1, annotation, 0)
# annotation=None
# if '\0' in l or '\1' in l:
# self.editor.append(l.replace('\0+', '').replace('\0-', '').replace('\m', '').replace('\0^', '').replace('\1', ''))
# self.editor.markerAdd(idx, QsciScintilla.Circle)
# self.editor.setMarginText(idx, l[1], marginTextStyle)
# self.editor.fillIndicatorRange(idx, l.find('\0'), idx, l.rfind("\1"), 0)
# else:
# self.editor.append(l)
print("finished updating")
self.editor.blockSignals(False)