Mercurial > lcfOS
annotate python/codeedit.py @ 254:bd26dc13f270
Added logger
author | Windel Bouwman |
---|---|
date | Wed, 31 Jul 2013 21:20:58 +0200 |
parents | f5fba5b554d7 |
children | 225f444019b1 |
rev | line source |
---|---|
160 | 1 #!/usr/bin/python |
2 | |
3 import sys | |
4 from PyQt4.QtCore import * | |
5 from PyQt4.QtGui import * | |
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 | |
15 class InnerCode(QWidget): | |
249 | 16 textChanged = pyqtSignal() |
17 def __init__(self, scrollArea): | |
160 | 18 super().__init__(scrollArea) |
19 self.scrollArea = scrollArea | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
167
diff
changeset
|
20 self.setFont(QFont('Courier', 12)) |
160 | 21 self.setFocusPolicy(Qt.StrongFocus) |
163 | 22 # TODO: only beam cursor in text area.. |
23 self.setCursor(Qt.IBeamCursor) | |
162 | 24 h = QFontMetrics(self.font()).height() |
247 | 25 self.errorPixmap = QPixmap('icons/error.png').scaled(h, h) |
249 | 26 self.arrowPixmap = QPixmap('icons/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 | |
250 | 33 self.t = QTimer(self) |
34 self.t.timeout.connect(self.updateCursor) | |
35 self.t.setInterval(500) | |
36 self.t.start() | |
37 | |
249 | 38 def updateCursor(self): |
160 | 39 self.blinkcursor = not self.blinkcursor |
163 | 40 self.update() |
41 #self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight) | |
249 | 42 |
43 def setSource(self, src): | |
160 | 44 self.src = src |
45 self.adjust() | |
249 | 46 |
47 def getSource(self): | |
162 | 48 return self.src |
249 | 49 |
50 def setErrors(self, el): | |
162 | 51 self.errorlist = el |
52 self.update() | |
249 | 53 |
54 def setCursorPosition(self, c): | |
160 | 55 self.cursorPosition = clipVal(c, 0, len(self.src)) |
161 | 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) |
249 | 98 # Annotations: |
99 def addAnnotation(self, row, col, ln, msg): | |
162 | 100 pass |
249 | 101 # Text modification: |
102 def getChar(self, pos): | |
160 | 103 pass |
249 | 104 def insertText(self, txt): |
160 | 105 self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:]) |
106 self.CursorPosition += len(txt) | |
162 | 107 self.textChanged.emit() |
249 | 108 def deleteChar(self): |
161 | 109 self.setSource(self.src[0:self.CursorPosition] + self.src[self.CursorPosition+1:]) |
162 | 110 self.textChanged.emit() |
249 | 111 def GotoNextChar(self): |
160 | 112 if self.src[self.CursorPosition] != '\n': |
113 self.CursorPosition += 1 | |
249 | 114 def GotoPrevChar(self): |
160 | 115 if self.src[self.CursorPosition - 1] != '\n': |
116 self.CursorPosition -= 1 | |
249 | 117 def GotoNextLine(self): |
160 | 118 curLine = self.CurrentLine |
162 | 119 c = self.CursorCol - 1 # go to zero based |
161 | 120 self.CursorPosition += len(curLine) - c + 1 # line break char! |
160 | 121 curLine = self.CurrentLine |
161 | 122 if len(curLine) < c: |
123 self.CursorPosition += len(curLine) | |
124 else: | |
125 self.CursorPosition += c | |
163 | 126 self.showRow(self.CursorRow) |
249 | 127 def GotoPrevLine(self): |
162 | 128 c = self.CursorCol - 1 # go to zero based |
161 | 129 self.CursorPosition -= c + 1 # line break char! |
130 curLine = self.CurrentLine | |
131 if len(curLine) > c: | |
132 self.CursorPosition -= len(curLine) - c | |
163 | 133 self.showRow(self.CursorRow) |
249 | 134 def paintEvent(self, event): |
160 | 135 # Helper variables: |
136 er = event.rect() | |
137 chw, chh = self.charWidth, self.charHeight | |
138 painter = QPainter(self) | |
139 # Background: | |
140 painter.fillRect(er, self.palette().color(QPalette.Base)) | |
162 | 141 painter.fillRect(QRect(self.xposLNA, er.top(), 4 * chw, er.bottom() + 1), Qt.gray) |
142 errorPen = QPen(Qt.red, 3) | |
160 | 143 # first and last row: |
161 | 144 row1 = max(int(er.top() / chh) - 1, 1) |
145 row2 = max(int(er.bottom() / chh) + 1, 1) | |
160 | 146 # Draw contents: |
163 | 147 ypos = row1 * chh - self.charDescent |
148 curRow = self.CursorRow | |
149 ydt = -chh + self.charDescent | |
160 | 150 for row in range(row1, row2 + 1): |
250 | 151 if curRow == row and self.hasFocus(): |
163 | 152 painter.fillRect(self.xposTXT, ypos + ydt, er.width(), chh, Qt.yellow) |
153 # cursor | |
154 if self.blinkcursor: | |
155 cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth | |
156 cursorY = ypos + ydt | |
157 painter.fillRect(cursorX, cursorY, 2, chh, Qt.black) | |
160 | 158 painter.setPen(Qt.black) |
162 | 159 painter.drawText(self.xposLNA, ypos, '{0}'.format(row)) |
160 | 160 xpos = self.xposTXT |
161 | 161 painter.drawText(xpos, ypos, self.getRow(row)) |
249 | 162 if self.arrow and self.arrow.row == row: |
163 painter.drawPixmap(self.xposERR, ypos + ydt, self.arrowPixmap) | |
163 | 164 curErrors = [e for e in self.errorlist if e.loc.row == row] |
165 for e in curErrors: | |
166 painter.drawPixmap(self.xposERR, ypos + ydt, self.errorPixmap) | |
162 | 167 painter.setPen(errorPen) |
163 | 168 x = self.xposTXT + (e.loc.col - 1) * chw - 2 |
169 wt = e.loc.length * chw + 4 | |
167 | 170 dy = self.charDescent |
171 painter.drawLine(x, ypos + dy, x + wt, ypos + dy) | |
172 #painter.drawRoundedRect(x, ypos + ydt, wt, chh, 7, 7) | |
163 | 173 # print error balloon |
167 | 174 #painter.drawText(x, ypos + chh, e.msg) |
175 #if len(curErrors) > 0: | |
176 # ypos += chh | |
249 | 177 ypos += chh |
163 | 178 |
249 | 179 def keyPressEvent(self, event): |
160 | 180 if event.matches(QKeySequence.MoveToNextChar): |
181 self.GotoNextChar() | |
161 | 182 elif event.matches(QKeySequence.MoveToPreviousChar): |
160 | 183 self.GotoPrevChar() |
161 | 184 elif event.matches(QKeySequence.MoveToNextLine): |
160 | 185 self.GotoNextLine() |
161 | 186 elif event.matches(QKeySequence.MoveToPreviousLine): |
160 | 187 self.GotoPrevLine() |
161 | 188 elif event.matches(QKeySequence.MoveToNextPage): |
160 | 189 for i in range(5): |
190 self.GotoNextLine() | |
161 | 191 elif event.matches(QKeySequence.MoveToPreviousPage): |
160 | 192 for i in range(5): |
193 self.GotoPrevLine() | |
161 | 194 elif event.matches(QKeySequence.MoveToEndOfLine): |
160 | 195 self.CursorPosition += len(self.CurrentLine) - self.CursorCol + 1 |
161 | 196 elif event.matches(QKeySequence.MoveToStartOfLine): |
160 | 197 self.CursorPosition -= self.CursorCol - 1 |
161 | 198 elif event.matches(QKeySequence.Delete): |
199 self.deleteChar() | |
200 elif event.matches(QKeySequence.InsertParagraphSeparator): | |
201 self.insertText('\n') | |
202 elif event.key() == Qt.Key_Backspace: | |
203 self.CursorPosition -= 1 | |
204 self.deleteChar() | |
205 else: | |
206 char = event.text() | |
207 if char: | |
208 self.insertText(char) | |
160 | 209 self.update() |
249 | 210 |
211 def mousePressEvent(self, event): | |
161 | 212 pos = event.pos() |
213 if pos.x() > self.xposTXT and pos.x(): | |
214 c = round((pos.x() - self.xposTXT) / self.charWidth) | |
163 | 215 r = int(pos.y() / self.charHeight) + 1 |
161 | 216 self.setRowCol(r, c) |
249 | 217 |
218 def adjust(self): | |
161 | 219 metrics = self.fontMetrics() |
220 self.charHeight = metrics.height() | |
221 self.charWidth = metrics.width('x') | |
222 self.charDescent = metrics.descent() | |
162 | 223 self.xposERR = GAP |
224 self.xposLNA = self.xposERR + GAP + self.errorPixmap.width() | |
225 self.xposTXT = self.xposLNA + 4 * self.charWidth + GAP | |
160 | 226 self.xposEnd = self.xposTXT + self.charWidth * 80 |
227 self.setMinimumWidth(self.xposEnd) | |
228 txt = self.src.split('\n') | |
229 self.setMinimumHeight(self.charHeight * len(txt)) | |
230 self.update() | |
231 | |
232 class CodeEdit(QScrollArea): | |
248 | 233 def __init__(self): |
234 super().__init__() | |
235 self.ic = InnerCode(self) | |
236 self.textChanged = self.ic.textChanged | |
237 self.setWidget(self.ic) | |
238 self.setWidgetResizable(True) | |
239 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) | |
240 self.setFocusPolicy(Qt.NoFocus) | |
241 self.showRow = self.ic.showRow | |
242 self.setRowCol = self.ic.setRowCol | |
243 self.FileName = None | |
244 Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v)) | |
249 | 245 |
248 | 246 def setErrors(self, el): |
247 self.ic.setErrors(el) | |
248 | |
249 def setFocus(self): | |
250 self.ic.setFocus() | |
251 | |
252 def setFileName(self, fn): | |
253 self.filename = fn | |
254 if not fn: | |
255 fn = 'Untitled' | |
256 self.setWindowTitle(fn) | |
249 | 257 |
248 | 258 def getFileName(self): |
259 return self.filename | |
260 FileName = property(getFileName, setFileName) | |
261 | |
160 | 262 |
263 if __name__ == '__main__': | |
249 | 264 app = QApplication(sys.argv) |
265 ce = CodeEdit() | |
266 ce.show() | |
267 src = ''.join(inspect.getsourcelines(InnerCode)[0]) | |
268 ce.Source = src | |
269 ce.resize(600, 800) | |
270 app.exec() | |
160 | 271 |