aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/gui/sourcecodeedit.py
blob: 5606eb5122b460b1f53bfc14ea922eb2a07a3f33 (plain)
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
# -*- coding: utf-8 -*-
#
# AWL simulator - Generic source code edit widget
#
# Copyright 2014 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.compat 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)

	def __init__(self, parent=None):
		QPlainTextEdit.__init__(self, parent)

		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 keyPressEvent(self, ev):
		QPlainTextEdit.keyPressEvent(self, ev)

		if ev.key() in (Qt.Key_Return, Qt.Key_Enter):
			self.__autoIndentHandleNewline()
		elif ev.key() == Qt.Key_Delete:
			self.__validate()

	def wheelEvent(self, ev):
		if ev.modifiers() & Qt.ControlModifier:
			# Ctrl + Scroll-wheel: Font resizing
			if isQt4:
				numDegrees = ev.delta() / 8
			else:
				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
			return
		else:
			self.__wheelSteps = 0.0

		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

	__errLineBrush = QBrush(getErrorColor())

	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 errLineNr is not None and errLineMsg:
			errLineNr += 1
			if lineNr in {errLineNr - 1, errLineNr, errLineNr + 1}:
				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.__errLineBrush)
			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)
bues.ch cgit interface