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