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