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