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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
# -*- coding: utf-8 -*-
#
# AWL simulator - Generic source code edit widget
#
# Copyright 2014-2018 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *
from awlsim.gui.finddialog import *
from awlsim.gui.util import *
class SourceCodeEdit(QPlainTextEdit):
# Signal: Change the font size.
# If the parameter is True, increase font size.
resizeFont = Signal(bool)
# Signal: Validation request.
# A code validation should take place.
# In case of validation failure, call setErraticLine().
# The parameter is self.
validateDocument = Signal(QObject)
__findDialog = None
@classmethod
def initFindDialog(cls, parent):
cls.__findDialog = FindReplaceDialog(parent)
@classmethod
def getFindDialog(cls):
return cls.__findDialog
def __init__(self, parent=None):
QPlainTextEdit.__init__(self, parent)
self.__errLineBrushBg = QBrush(getErrorColor())
self.__errLineBrushFg = QBrush(QColor("black"))
self.__wheelSteps = 0.0
self.__undoIsAvailable = False
self.__redoIsAvailable = False
self.__copyIsAvailable = False
self.__prevLineNr = self.textCursor().blockNumber()
self.__prevColumnNr = self.textCursor().columnNumber()
self.__columnChange = False
self.enableAutoIndent()
self.enablePasteIndent()
self.__validateEn = True
self.__errLineNr = None
self.__errLineMsg = None
self.cursorPositionChanged.connect(self.__handleCursorChange)
self.textChanged.connect(self.__handleTextChange)
self.undoAvailable.connect(self.__handleUndoAvailableChange)
self.redoAvailable.connect(self.__handleRedoAvailableChange)
self.copyAvailable.connect(self.__handleCopyAvailableChange)
def __handleUndoAvailableChange(self, available):
self.__undoIsAvailable = available
def undoIsAvailable(self):
return self.__undoIsAvailable
def __handleRedoAvailableChange(self, available):
self.__redoIsAvailable = available
def redoIsAvailable(self):
return self.__redoIsAvailable
def __handleCopyAvailableChange(self, available):
self.__copyIsAvailable = available
def copyIsAvailable(self):
return self.__copyIsAvailable
def enableAutoIndent(self, enable=True):
self.__autoIndentEn = enable
def enablePasteIndent(self, enable=True):
self.__pasteIndentEn = enable
def enableValidation(self, enable=True):
self.__validateEn = enable
if not enable:
QToolTip.hideText()
self.setErraticLine(None)
self.__validate()
def __getLineIndent(self, cursor):
cursor.select(QTextCursor.LineUnderCursor)
line = cursor.selectedText()
if not line:
return ""
# Get the indent-string (that is the whitespace line prefix)
for i, c in enumerate(line):
if not c.isspace():
break
else:
i += 1
return line[:i]
def __autoIndentHandleNewline(self):
if not self.__autoIndentEn:
return
# Move cursor to previous line and get its indent string.
cursor = self.textCursor()
if not cursor.movePosition(QTextCursor.Up,
QTextCursor.MoveAnchor, 1):
return
indentStr = self.__getLineIndent(cursor)
# Insert the indent string into the current line
cursor = self.textCursor()
cursor.insertText(indentStr)
self.setTextCursor(cursor)
# Remove any old indent (righthand of the cursor)
cursor = self.textCursor()
if not cursor.movePosition(QTextCursor.EndOfLine,
QTextCursor.KeepAnchor, 1):
return
selText = cursor.selectedText()
stripCount = len(selText) - len(selText.lstrip())
if stripCount > 0:
cursor = self.textCursor()
if not cursor.movePosition(QTextCursor.Right,
QTextCursor.KeepAnchor,
stripCount):
return
cursor.deleteChar()
def __doBlockComment(self):
"""Comment or uncomment the selected lines of text.
"""
try:
if self.isReadOnly():
return
cursor = self.textCursor()
if not cursor or cursor.isNull():
return
text = str(self.toPlainText())
origStart, origEnd = cursor.selectionStart(), cursor.selectionEnd()
# Extend the cursor to whole lines
cursor.setPosition(origStart, QTextCursor.MoveAnchor)
cursor.movePosition(QTextCursor.StartOfLine, QTextCursor.MoveAnchor)
cursor.setPosition(origEnd, QTextCursor.KeepAnchor)
cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
self.setTextCursor(cursor)
adjustedStart, adjustedEnd = cursor.selectionStart(), cursor.selectionEnd()
selectedText = str(cursor.selectedText())
if selectedText:
lines = selectedText.splitlines()
if (adjustedEnd > adjustedStart and
adjustedEnd > 0 and
adjustedEnd <= len(text) and
text[adjustedEnd - 1] in "\r\n" and
(adjustedEnd == len(text) or text[adjustedEnd] in "\r\n")):
# Last empty line is not returned by selectedText()
# Explicitly add it.
lines.append("")
nrCommented = sum(1 if self._lineIsCommented(line) else 0
for line in lines)
if nrCommented >= len(lines) and lines:
# Uncomment all lines
newText = "\n".join(self._uncommentLine(line)
for line in lines)
else:
# Comment all lines
newText = "\n".join(self._commentLine(line)
for line in lines)
else:
newText = self._commentLine("")
# Replace the selected text
cursor.insertText(newText)
# Re-select the new text
cursor.setPosition(adjustedStart, QTextCursor.MoveAnchor)
cursor.setPosition(adjustedStart + len(newText), QTextCursor.KeepAnchor)
self.setTextCursor(cursor)
except NotImplementedError as e:
return
def _lineIsCommented(self, lineText):
raise NotImplementedError
def _commentLine(self, lineText):
raise NotImplementedError
def _uncommentLine(self, lineText):
raise NotImplementedError
def keyPressEvent(self, ev):
if ev.matches(QKeySequence.Find):
self.findText()
ev.accept()
return
elif ev.matches(QKeySequence.Replace):
self.findReplaceText()
ev.accept()
return
key = ev.key()
mods = ev.modifiers()
if key == Qt.Key_Slash and (mods & Qt.ControlModifier):
self.__doBlockComment()
ev.accept()
return
QPlainTextEdit.keyPressEvent(self, ev)
if key in (Qt.Key_Return, Qt.Key_Enter):
self.__autoIndentHandleNewline()
elif key == Qt.Key_Delete:
self.__validate()
def wheelEvent(self, ev):
if ev.modifiers() & Qt.ControlModifier:
# Ctrl + Scroll-wheel: Font resizing
numDegrees = ev.angleDelta().y() / 8
numSteps = numDegrees / 15
self.__wheelSteps += numSteps
if self.__wheelSteps >= 1.0:
while self.__wheelSteps >= 1.0:
self.resizeFont.emit(False)
self.__wheelSteps -= 1.0
elif self.__wheelSteps <= -1.0:
while self.__wheelSteps <= -1.0:
self.resizeFont.emit(True)
self.__wheelSteps += 1.0
ev.accept()
return
else:
self.__wheelSteps = 0.0
ev.ignore()
QPlainTextEdit.wheelEvent(self, ev)
# Add the current indent to all lines (except the first) in 'text'.
def __makeSeamlessIndent(self, text):
indentStr = self.__getLineIndent(self.textCursor())
lines = []
for i, line in enumerate(text.splitlines()):
if i == 0:
lines.append(line)
else:
lines.append(indentStr + line)
return "\n".join(lines)
def pasteText(self, text):
if self.__autoIndentEn:
text = self.__makeSeamlessIndent(text)
self.insertPlainText(text)
def insertFromMimeData(self, mimeData):
if mimeData.hasText() and self.__pasteIndentEn:
# Replace the mimeData by a re-indented mimeData.
newText = self.__makeSeamlessIndent(mimeData.text())
mimeData = QMimeData()
mimeData.setText(newText)
QPlainTextEdit.insertFromMimeData(self, mimeData)
def __handleCursorChange(self):
cursor = self.textCursor()
columnNr = cursor.columnNumber()
lineNr = cursor.blockNumber()
if columnNr != self.__prevColumnNr:
self.__columnChange = True
if lineNr == self.__errLineNr or\
(lineNr != self.__prevLineNr and\
self.__columnChange):
self.__validate()
self.__columnChange = False
self.__prevColumnNr = columnNr
self.__prevLineNr = lineNr
def __handleTextChange(self):
QToolTip.hideText()
if self.__errLineNr is not None:
self.__validate()
def setPlainText(self, text):
self.__errLineNr = None
self.__errLineMsg = None
QPlainTextEdit.setPlainText(self, text)
self.__validate()
@staticmethod
def __makeExtraSel(cursor, format):
sel = QTextEdit.ExtraSelection()
sel.cursor = cursor
sel.format = format
return sel
@staticmethod
def __makeTextCursorLineSel(cursor, lineNr):
cursor.movePosition(QTextCursor.Start,
QTextCursor.MoveAnchor, 1)
cursor.movePosition(QTextCursor.Down,
QTextCursor.MoveAnchor, lineNr)
cursor.select(QTextCursor.LineUnderCursor)
return cursor
def __validate(self):
if not self.__validateEn:
return
# Validate the current document
text = self.toPlainText()
if text.strip():
# Run the validator.
self.validateDocument.emit(self)
else:
# No text. Mark everything as ok.
self.setErraticLine(None)
def __tryShowValidateErrToolTip(self, lineNr, globalPos):
errLineNr = self.__errLineNr
errLineMsg = self.__errLineMsg
if self.__validateEn and\
not self.textCursor().hasSelection() and\
errLineNr is not None and\
errLineMsg:
errLineNr += 1
if lineNr in {errLineNr - 1, errLineNr, errLineNr + 1}:
lines = []
for line in errLineMsg.splitlines():
# Limit line length.
if len(line) > 237:
line = line[:237] + "..."
# Enforce line breaks.
while len(line) > 80:
lines.append(line[:80] + " \\")
line = line[80:]
lines.append(line)
errLineMsg = "\n".join(lines)
# Show the validation result.
QToolTip.showText(globalPos, errLineMsg,
self, QRect())
return True
return False
# Mark an erratic line
def setErraticLine(self, lineNr, errMsg=None):
if lineNr is None:
self.setExtraSelections(())
else:
cursor = self.textCursor()
fmt = self.currentCharFormat()
fmt.setBackground(self.__errLineBrushBg)
fmt.setForeground(self.__errLineBrushFg)
extraSel = self.__makeExtraSel(
self.__makeTextCursorLineSel(cursor, lineNr),
fmt)
self.setExtraSelections((extraSel,))
self.__errLineNr = lineNr
self.__errLineMsg = errMsg
if errMsg:
mouseCursorPos = QCursor.pos()
cursor = self.cursorForPosition(self.mapFromGlobal(mouseCursorPos))
self.__tryShowValidateErrToolTip(cursor.blockNumber(),
mouseCursorPos)
def __toolTipEvent(self, ev):
cursor = self.cursorForPosition(ev.pos())
if not self.__tryShowValidateErrToolTip(cursor.blockNumber(),
ev.globalPos()):
QToolTip.hideText()
def event(self, ev):
if ev.type() == QEvent.ToolTip:
self.__toolTipEvent(ev)
return super(SourceCodeEdit, self).event(ev)
def __findText(self, replace=False):
if self.isReadOnly() and replace:
return
dlg = self.getFindDialog()
if dlg:
dlg.hide()
dlg.setReplaceMode(replace)
dlg.show()
def findText(self):
self.__findText(False)
def findReplaceText(self):
self.__findText(True)
|