-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcodeedit.py
More file actions
227 lines (186 loc) · 6.65 KB
/
gcodeedit.py
File metadata and controls
227 lines (186 loc) · 6.65 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
(GE_EMPTY, GE_LOADING, GE_STOPPED, GE_RUNNING, GE_SINGLESTEP) = range(5)
(GE_COL_STATUS, GE_COL_GCODE) = range(2)
class gcodeEdit(QTableWidget):
'''A class that adds a few methods to QTextEdit to
support gcode editing and control of gcode processing.
'''
def __init__(self, parent=None):
super(gcodeEdit, self).__init__(parent)
self.activeLine = -1
self.oldCursorLine = -1
self.oldCursorFormat = ''
# self.connect(self, SIGNAL("cursorPositionChanged()"), self.highlightCursorLine)
self.state = GE_EMPTY
self.setSortingEnabled(False)
font = QFont("Monaco", 12)
font.setFixedPitch(True)
self.setFont(font)
self.setShowGrid(False)
self.verticalHeader().hide()
self.horizontalHeader().hide()
def resizeEvent(self, event):
self.setColumnWidth(1, event.size().width()-16)
def loadNewFile(self, filename):
file = open(filename)
a=file.readlines()
file.close()
print len(a)
self.clear()
self.setSortingEnabled(False)
self.setRowCount(len(a))
self.setColumnCount(2)
self.setColumnWidth(0,16)
print 'Width', self.width()
self.setColumnWidth(1, self.width()-16)
# self.tableWidget.setHorizontalHeaderLabels(headers)
self.clear()
for i, line in enumerate(a):
self.setItem(i,GE_COL_GCODE, QTableWidgetItem(line.strip('\n')))
self.setRowHeight(i,16)
self.setState(GE_STOPPED)
self.setCurrentCell(0,GE_COL_GCODE)
# self.resizeColumnsToContents()
#
def cursorToLine(self, lineNumber):
self.setCurrentCell(lineNumber, GE_COL_GCODE)
# block = self.document().findBlockByNumber(lineNumber-1)
# cursor = QTextCursor(block)
# self.setTextCursor(cursor)
#
def getLine(self, lineNumber):
# block = self.document().findBlockByNumber(lineNumber-1)
return str(self.item(lineNumber-1,GE_COL_GCODE).text())
#
def setState(self, state):
self.state = state
def state(self):
return self.state
def highlightCursorLine(self):
pass
# if (self.state == GE_EMPTY) or (self.state == GE_RUNNING):
# return
#
# cursor = self.textCursor()
# cursorLine = cursor.block().blockNumber()+1
#
# if self.oldCursorLine == cursorLine:
# return
#
# if self.oldCursorLine>=0:
# #print 'Restoring old cursor'
# block = self.document().findBlockByNumber(self.oldCursorLine-1)
# old_cursor = QTextCursor(block)
# old_cursor.setBlockFormat(self.oldCursorFormat)
#
## cursor = self.textCursor()
# self.oldCursorFormat = cursor.blockFormat()
# self.oldCursorLine = cursorLine
# cursorLineFormat = QTextBlockFormat()
# cursorLineFormat.setBackground(QColor(Qt.green))
# #print 'Drawing green line'
# cursor.setBlockFormat(cursorLineFormat)
#
def getActiveLine(self):
return self.currentRow()+1
# cursor = self.textCursor()
# return cursor.block().blockNumber()+1
#
def setActiveLine(self, lineNumber):
self.setCurrentCell(lineNumber-1,GE_COL_GCODE)
def markSent(self, lineNumber):
'''Mark this line as having been sent to grbl.
'''
self.item(lineNumber-1,GE_COL_GCODE).setTextColor(QColor(Qt.darkGreen))
def markDone(self, lineNumber):
'''Mark as having come back from grbl okay.
'''
self.item(lineNumber-1,GE_COL_GCODE).setTextColor(QColor(Qt.darkGray))
def markError(self, lineNumber):
self.item(lineNumber-1, GE_COL_GCODE).setTextColor(QColor(Qt.black))
self.item(lineNumber-1, GE_COL_GCODE).setBackgroundColor(QColor(Qt.red))
'''Mark line has having an error
'''
pass
# def markLineActive(self, lineNumber):
def markBreakpoint(self, lineNumber):
'''Place a breakpoint at this line
'''
pass
# '''Highlight the line in the text editor that has
# the line number currently being executed. The line
# number sent to grbl starts with 1, but corresponds
# to line 0 in the text editor, hence the -1 in the
# calls that set the highlight.
#
# In QTextEdit, a block is the same as a \n delimited
# line, as long as there are no images or tables in
# the document.
# '''
#
# #print 'setActiveLine: ', lineNumber
#
# #unhighlight old block, if it wasn't zero:
# if lineNumber == self.activeLine:
# return
#
# keywordFormat = QTextBlockFormat()
# if self.activeLine>0:
# block = self.document().findBlockByNumber(self.activeLine-1)
#
# format = block.blockFormat()
# format.clearBackground()
#
# cursor = QTextCursor(block)
# cursor.setBlockFormat(format)
#
# #highlight the new block, if it isn't zero:
# self.activeLine = lineNumber
# if self.activeLine>0:
# block = self.document().findBlockByNumber(self.activeLine-1)
# cursor = QTextCursor(block)
# keywordFormat.setBackground(Qt.yellow)
# cursor.setBlockFormat(keywordFormat)
# self.setTextCursor(cursor)
#
#
def lines(self, index=1):
index=index-1
while index<self.rowCount():
block = self.item(index,GE_COL_GCODE)
yield index+1, str(block.text())
index+=1
class Form(QDialog):
'''A test form for gcodeEdit.
'''
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.editor = gcodeEdit()
self.editor.setMinimumWidth(500)
self.editor.setMinimumHeight(400)
self.closeButton = QPushButton("Close")
layout = QVBoxLayout()
layout.addWidget(self.editor)
hl = QHBoxLayout()
hl.addWidget(self.closeButton)
hl.setAlignment(self.closeButton, Qt.AlignRight)
layout.addLayout(hl)
self.setLayout(layout)
# self.connect(self.jog, SIGNAL("jogEvent(char, int)"), self.test_jog)
self.connect(self.closeButton, SIGNAL("clicked()"), self.close)
self.editor.loadNewFile('gcode files/cnc_circles.txt')
self.editor.markDone(1)
self.editor.markDone(2)
self.editor.markError(3)
self.editor.setActiveLine(4)
self.editor.markSent(5)
self.editor.markSent(6)
self.editor.markBreakpoint(7)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Form()
form.show()
form.raise_()
app.exec_()