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