Mercurial > lcfOS
annotate python/ide/codeedit.py @ 337:b00219172a42
Added cool lm3s811 qemu project
author | Windel Bouwman |
---|---|
date | Thu, 20 Feb 2014 20:04:52 +0100 |
parents | dcae6574c974 |
children |
rev | line source |
---|---|
160 | 1 #!/usr/bin/python |
2 | |
3 import sys | |
279 | 4 import os |
333 | 5 from qtwrapper import QtGui, QtCore, QtWidgets, pyqtSignal, Qt, get_icon |
160 | 6 import inspect |
7 | |
8 GAP = 5 | |
9 | |
10 def clipVal(v, mn, mx): | |
11 if v < mn: return mn | |
12 if v > mx: return mx | |
13 return v | |
14 | |
333 | 15 class InnerCode(QtWidgets.QWidget): |
249 | 16 textChanged = pyqtSignal() |
17 def __init__(self, scrollArea): | |
160 | 18 super().__init__(scrollArea) |
19 self.scrollArea = scrollArea | |
333 | 20 self.setFont(QtGui.QFont('Courier', 12)) |
160 | 21 self.setFocusPolicy(Qt.StrongFocus) |
163 | 22 # TODO: only beam cursor in text area.. |
23 self.setCursor(Qt.IBeamCursor) | |
333 | 24 h = QtGui.QFontMetrics(self.font()).height() |
25 self.errorPixmap = get_icon('error.png').scaled(h, h) | |
26 self.arrowPixmap = get_icon('arrow.png').scaled(h, h) | |
160 | 27 self.blinkcursor = False |
162 | 28 self.errorlist = [] |
249 | 29 self.arrow = None |
160 | 30 # Initial values: |
31 self.setSource('') | |
32 self.CursorPosition = 0 | |
333 | 33 self.t = QtCore.QTimer(self) |
250 | 34 self.t.timeout.connect(self.updateCursor) |
35 self.t.setInterval(500) | |
36 self.t.start() | |
37 | |
249 | 38 def updateCursor(self): |
256 | 39 self.blinkcursor = not self.blinkcursor |
40 self.update() | |
41 #self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight) | |
249 | 42 |
43 def setSource(self, src): | |
256 | 44 self.src = src |
45 self.adjust() | |
249 | 46 |
47 def getSource(self): | |
256 | 48 return self.src |
249 | 49 |
50 def setErrors(self, el): | |
256 | 51 self.errorlist = el |
52 self.update() | |
249 | 53 |
54 def setCursorPosition(self, c): | |
256 | 55 self.cursorPosition = clipVal(c, 0, len(self.src)) |
56 self.update() | |
249 | 57 |
58 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition) | |
59 | |
60 @property | |
61 def Rows(self): | |
62 # Make this nicer: | |
63 return self.src.split('\n') | |
64 | |
65 @property | |
66 def CursorRow(self): | |
67 # TODO: make this nice. | |
68 txt = self.src[0:self.cursorPosition] | |
69 return len(txt.split('\n')) | |
70 | |
71 @property | |
72 def CursorCol(self): | |
73 txt = self.src[0:self.cursorPosition] | |
74 curLine = txt.split('\n')[-1] | |
75 return len(curLine) + 1 | |
76 | |
77 @property | |
78 def CurrentLine(self): | |
79 return self.getRow(self.CursorRow) | |
80 | |
81 def setRowCol(self, r, c): | |
163 | 82 prevRows = self.Rows[:r-1] |
161 | 83 txt = '\n'.join(prevRows) |
163 | 84 c = clipVal(c, 1, len(self.getRow(r))) |
162 | 85 self.CursorPosition = len(txt) + c + 1 |
167 | 86 self.showRow(self.CursorRow) |
249 | 87 |
88 def getRow(self, r): | |
161 | 89 rows = self.Rows |
160 | 90 r = r - 1 |
91 if r < 0 or r > len(rows) - 1: | |
92 return '' | |
93 else: | |
94 return rows[r] | |
249 | 95 |
96 def showRow(self, r): | |
163 | 97 self.scrollArea.ensureVisible(self.xposTXT, r * self.charHeight, 4, self.charHeight) |
256 | 98 |
249 | 99 # Annotations: |
100 def addAnnotation(self, row, col, ln, msg): | |
162 | 101 pass |
256 | 102 |
249 | 103 # Text modification: |
104 def getChar(self, pos): | |
160 | 105 pass |
256 | 106 |
249 | 107 def insertText(self, txt): |
256 | 108 self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:]) |
109 self.CursorPosition += len(txt) | |
110 self.textChanged.emit() | |
111 | |
249 | 112 def deleteChar(self): |
256 | 113 self.setSource(self.src[0:self.CursorPosition] + self.src[self.CursorPosition+1:]) |
114 self.textChanged.emit() | |
115 | |
249 | 116 def GotoNextChar(self): |
256 | 117 if self.src[self.CursorPosition] != '\n': |
118 self.CursorPosition += 1 | |
119 | |
249 | 120 def GotoPrevChar(self): |
256 | 121 if self.src[self.CursorPosition - 1] != '\n': |
122 self.CursorPosition -= 1 | |
123 | |
249 | 124 def GotoNextLine(self): |
256 | 125 curLine = self.CurrentLine |
126 c = self.CursorCol - 1 # go to zero based | |
127 self.CursorPosition += len(curLine) - c + 1 # line break char! | |
128 curLine = self.CurrentLine | |
129 if len(curLine) < c: | |
130 self.CursorPosition += len(curLine) | |
131 else: | |
132 self.CursorPosition += c | |
133 self.showRow(self.CursorRow) | |
134 | |
249 | 135 def GotoPrevLine(self): |
256 | 136 c = self.CursorCol - 1 # go to zero based |
137 self.CursorPosition -= c + 1 # line break char! | |
138 curLine = self.CurrentLine | |
139 if len(curLine) > c: | |
140 self.CursorPosition -= len(curLine) - c | |
141 self.showRow(self.CursorRow) | |
142 | |
249 | 143 def paintEvent(self, event): |
160 | 144 # Helper variables: |
145 er = event.rect() | |
146 chw, chh = self.charWidth, self.charHeight | |
333 | 147 painter = QtGui.QPainter(self) |
160 | 148 # Background: |
333 | 149 painter.fillRect(er, self.palette().color(QtGui.QPalette.Base)) |
150 painter.fillRect(QtCore.QRect(self.xposLNA, er.top(), 4 * chw, er.bottom() + 1), Qt.gray) | |
151 errorPen = QtGui.QPen(Qt.red, 3) | |
160 | 152 # first and last row: |
161 | 153 row1 = max(int(er.top() / chh) - 1, 1) |
154 row2 = max(int(er.bottom() / chh) + 1, 1) | |
160 | 155 # Draw contents: |
163 | 156 ypos = row1 * chh - self.charDescent |
157 curRow = self.CursorRow | |
158 ydt = -chh + self.charDescent | |
160 | 159 for row in range(row1, row2 + 1): |
250 | 160 if curRow == row and self.hasFocus(): |
163 | 161 painter.fillRect(self.xposTXT, ypos + ydt, er.width(), chh, Qt.yellow) |
162 # cursor | |
163 if self.blinkcursor: | |
164 cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth | |
165 cursorY = ypos + ydt | |
166 painter.fillRect(cursorX, cursorY, 2, chh, Qt.black) | |
160 | 167 painter.setPen(Qt.black) |
162 | 168 painter.drawText(self.xposLNA, ypos, '{0}'.format(row)) |
160 | 169 xpos = self.xposTXT |
161 | 170 painter.drawText(xpos, ypos, self.getRow(row)) |
249 | 171 if self.arrow and self.arrow.row == row: |
172 painter.drawPixmap(self.xposERR, ypos + ydt, self.arrowPixmap) | |
277 | 173 curErrors = [e for e in self.errorlist if e.loc and e.loc.row == row] |
163 | 174 for e in curErrors: |
175 painter.drawPixmap(self.xposERR, ypos + ydt, self.errorPixmap) | |
162 | 176 painter.setPen(errorPen) |
163 | 177 x = self.xposTXT + (e.loc.col - 1) * chw - 2 |
178 wt = e.loc.length * chw + 4 | |
167 | 179 dy = self.charDescent |
180 painter.drawLine(x, ypos + dy, x + wt, ypos + dy) | |
181 #painter.drawRoundedRect(x, ypos + ydt, wt, chh, 7, 7) | |
163 | 182 # print error balloon |
167 | 183 #painter.drawText(x, ypos + chh, e.msg) |
184 #if len(curErrors) > 0: | |
185 # ypos += chh | |
249 | 186 ypos += chh |
163 | 187 |
249 | 188 def keyPressEvent(self, event): |
333 | 189 if event.matches(QtGui.QKeySequence.MoveToNextChar): |
160 | 190 self.GotoNextChar() |
333 | 191 elif event.matches(QtGui.QKeySequence.MoveToPreviousChar): |
160 | 192 self.GotoPrevChar() |
333 | 193 elif event.matches(QtGui.QKeySequence.MoveToNextLine): |
160 | 194 self.GotoNextLine() |
333 | 195 elif event.matches(QtGui.QKeySequence.MoveToPreviousLine): |
160 | 196 self.GotoPrevLine() |
333 | 197 elif event.matches(QtGui.QKeySequence.MoveToNextPage): |
160 | 198 for i in range(5): |
199 self.GotoNextLine() | |
333 | 200 elif event.matches(QtGui.QKeySequence.MoveToPreviousPage): |
160 | 201 for i in range(5): |
202 self.GotoPrevLine() | |
333 | 203 elif event.matches(QtGui.QKeySequence.MoveToEndOfLine): |
160 | 204 self.CursorPosition += len(self.CurrentLine) - self.CursorCol + 1 |
333 | 205 elif event.matches(QtGui.QKeySequence.MoveToStartOfLine): |
160 | 206 self.CursorPosition -= self.CursorCol - 1 |
333 | 207 elif event.matches(QtGui.QKeySequence.Delete): |
161 | 208 self.deleteChar() |
333 | 209 elif event.matches(QtGui.QKeySequence.InsertParagraphSeparator): |
161 | 210 self.insertText('\n') |
211 elif event.key() == Qt.Key_Backspace: | |
212 self.CursorPosition -= 1 | |
213 self.deleteChar() | |
214 else: | |
215 char = event.text() | |
216 if char: | |
217 self.insertText(char) | |
160 | 218 self.update() |
249 | 219 |
220 def mousePressEvent(self, event): | |
256 | 221 pos = event.pos() |
222 if pos.x() > self.xposTXT and pos.x(): | |
223 c = round((pos.x() - self.xposTXT) / self.charWidth) | |
224 r = int(pos.y() / self.charHeight) + 1 | |
225 self.setRowCol(r, c) | |
226 super().mousePressEvent(event) | |
249 | 227 |
228 def adjust(self): | |
256 | 229 metrics = self.fontMetrics() |
230 self.charHeight = metrics.height() | |
231 self.charWidth = metrics.width('x') | |
232 self.charDescent = metrics.descent() | |
233 self.xposERR = GAP | |
234 self.xposLNA = self.xposERR + GAP + self.errorPixmap.width() | |
235 self.xposTXT = self.xposLNA + 4 * self.charWidth + GAP | |
236 self.xposEnd = self.xposTXT + self.charWidth * 80 | |
237 self.setMinimumWidth(self.xposEnd) | |
238 txt = self.src.split('\n') | |
239 self.setMinimumHeight(self.charHeight * len(txt)) | |
240 self.update() | |
160 | 241 |
333 | 242 class CodeEdit(QtWidgets.QScrollArea): |
248 | 243 def __init__(self): |
244 super().__init__() | |
245 self.ic = InnerCode(self) | |
246 self.textChanged = self.ic.textChanged | |
247 self.setWidget(self.ic) | |
248 self.setWidgetResizable(True) | |
249 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) | |
250 self.setFocusPolicy(Qt.NoFocus) | |
251 self.showRow = self.ic.showRow | |
252 self.setRowCol = self.ic.setRowCol | |
253 self.FileName = None | |
254 Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v)) | |
249 | 255 |
248 | 256 def setErrors(self, el): |
257 self.ic.setErrors(el) | |
258 | |
259 def setFocus(self): | |
260 self.ic.setFocus() | |
256 | 261 super().setFocus() |
248 | 262 |
263 def setFileName(self, fn): | |
264 self.filename = fn | |
279 | 265 if fn: |
266 fn = os.path.basename(fn) | |
267 else: | |
248 | 268 fn = 'Untitled' |
269 self.setWindowTitle(fn) | |
249 | 270 |
248 | 271 def getFileName(self): |
272 return self.filename | |
273 FileName = property(getFileName, setFileName) | |
274 | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
275 def save(self): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
276 if self.FileName: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
277 s = self.Source |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
278 with open(self.FileName, 'w') as f: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
279 f.write(s) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
280 |
160 | 281 |
282 if __name__ == '__main__': | |
249 | 283 app = QApplication(sys.argv) |
284 ce = CodeEdit() | |
285 ce.show() | |
286 src = ''.join(inspect.getsourcelines(InnerCode)[0]) | |
287 ce.Source = src | |
288 ce.resize(600, 800) | |
289 app.exec() |