Mercurial > lcfOS
comparison python/codeedit.py @ 160:10330be89bc2
Started from scratch with code edit
author | Windel Bouwman |
---|---|
date | Sat, 09 Mar 2013 11:56:48 +0100 |
parents | |
children | 956f8e5ee48a |
comparison
equal
deleted
inserted
replaced
159:5e1dd04cb61c | 160:10330be89bc2 |
---|---|
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 print(self.cursorPosition, self.CursorRow, self.CursorCol) | |
38 self.cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth | |
39 self.cursorY = self.CursorRow * self.charHeight - self.charHeight | |
40 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition) | |
41 @property | |
42 def CursorRow(self): | |
43 # TODO: make this nice. | |
44 txt = self.src[0:self.cursorPosition] | |
45 return len(txt.split('\n')) | |
46 @property | |
47 def CursorCol(self): | |
48 txt = self.src[0:self.cursorPosition] | |
49 curLine = txt.split('\n')[-1] | |
50 return len(curLine) + 1 | |
51 @property | |
52 def CurrentLine(self): | |
53 return self.getRow(self.CursorRow) | |
54 def getRow(self, r): | |
55 rows = self.src.split('\n') | |
56 r = r - 1 | |
57 if r < 0 or r > len(rows) - 1: | |
58 return '' | |
59 else: | |
60 return rows[r] | |
61 def getChar(self, pos): | |
62 pass | |
63 def insertText(self, txt): | |
64 self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:]) | |
65 self.CursorPosition += len(txt) | |
66 def GotoNextChar(self): | |
67 if self.src[self.CursorPosition] != '\n': | |
68 self.CursorPosition += 1 | |
69 def GotoPrevChar(self): | |
70 if self.src[self.CursorPosition - 1] != '\n': | |
71 self.CursorPosition -= 1 | |
72 def GotoNextLine(self): | |
73 curLine = self.CurrentLine | |
74 c = self.CursorCol | |
75 self.CursorPosition += len(curLine) - c + c + 1 # line break char! | |
76 def GotoPrevLine(self): | |
77 curLine = self.CurrentLine | |
78 c = self.CursorCol | |
79 self.CursorPosition -= len(curLine) + c + 1 # line break char! | |
80 def paintEvent(self, event): | |
81 # Helper variables: | |
82 er = event.rect() | |
83 chw, chh = self.charWidth, self.charHeight | |
84 painter = QPainter(self) | |
85 # Background: | |
86 painter.fillRect(er, self.palette().color(QPalette.Base)) | |
87 painter.fillRect(QRect(self.xposLNA, er.top(), 8 * chw, er.bottom() + 1), Qt.gray) | |
88 painter.fillRect(self.xposTXT, (self.CursorRow - 1) * chh, er.width(), chh, Qt.yellow) | |
89 painter.setPen(Qt.gray) | |
90 # first and last row: | |
91 row1 = max(int(er.top() / chh) - 1, 0) | |
92 row2 = max(int(er.bottom() / chh) + 1, 0) | |
93 # Draw contents: | |
94 txt = self.src.split('\n') | |
95 print('update', row1, row2) | |
96 for row in range(row1, row2 + 1): | |
97 ypos = row * chh | |
98 painter.setPen(Qt.black) | |
99 painter.drawText(self.xposLNA, ypos, 'R ={0}'.format(row)) | |
100 xpos = self.xposTXT | |
101 if row - 1 < len(txt): | |
102 painter.drawText(xpos, ypos, txt[row - 1]) | |
103 # cursor | |
104 if self.blinkcursor: | |
105 painter.fillRect(self.cursorX, self.cursorY, 2, chh, Qt.black) | |
106 def keyPressEvent(self, event): | |
107 if event.matches(QKeySequence.MoveToNextChar): | |
108 self.GotoNextChar() | |
109 if event.matches(QKeySequence.MoveToPreviousChar): | |
110 self.GotoPrevChar() | |
111 if event.matches(QKeySequence.MoveToNextLine): | |
112 self.GotoNextLine() | |
113 if event.matches(QKeySequence.MoveToPreviousLine): | |
114 self.GotoPrevLine() | |
115 if event.matches(QKeySequence.MoveToNextPage): | |
116 for i in range(5): | |
117 self.GotoNextLine() | |
118 if event.matches(QKeySequence.MoveToPreviousPage): | |
119 for i in range(5): | |
120 self.GotoPrevLine() | |
121 if event.matches(QKeySequence.MoveToEndOfLine): | |
122 self.CursorPosition += len(self.CurrentLine) - self.CursorCol + 1 | |
123 if event.matches(QKeySequence.MoveToStartOfLine): | |
124 self.CursorPosition -= self.CursorCol - 1 | |
125 char = event.text().lower() | |
126 if char: | |
127 print('ins', char) | |
128 self.insertText(char) | |
129 self.update() | |
130 def adjust(self): | |
131 self.charHeight = self.fontMetrics().height() | |
132 self.charWidth = self.fontMetrics().width('x') | |
133 self.xposLNA = GAP | |
134 self.xposTXT = self.xposLNA + 8 * self.charWidth + GAP | |
135 self.xposEnd = self.xposTXT + self.charWidth * 80 | |
136 self.setMinimumWidth(self.xposEnd) | |
137 txt = self.src.split('\n') | |
138 self.setMinimumHeight(self.charHeight * len(txt)) | |
139 self.update() | |
140 | |
141 class CodeEdit(QScrollArea): | |
142 def __init__(self): | |
143 super().__init__() | |
144 self.ic = InnerCode(self) | |
145 self.setWidget(self.ic) | |
146 self.setWidgetResizable(True) | |
147 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) | |
148 self.setFocusPolicy(Qt.NoFocus) | |
149 Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v)) | |
150 | |
151 if __name__ == '__main__': | |
152 app = QApplication(sys.argv) | |
153 ce = CodeEdit() | |
154 ce.show() | |
155 src = ''.join(inspect.getsourcelines(InnerCode)[0]) | |
156 ce.Source = src | |
157 ce.resize(600, 500) | |
158 app.exec() | |
159 |