annotate 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
rev   line source
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
2
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
3 import sys
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
4 from PyQt4.QtCore import *
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
5 from PyQt4.QtGui import *
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
6 import inspect
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
7
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
8 GAP = 5
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
9
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
10 def clipVal(v, mn, mx):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
11 if v < mn: return mn
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
12 if v > mx: return mx
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
13 return v
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
14
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
15 class InnerCode(QWidget):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
16 def __init__(self, scrollArea):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
17 super().__init__(scrollArea)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
18 self.scrollArea = scrollArea
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
19 self.setFont(QFont('Courier', 16))
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
20 self.setFocusPolicy(Qt.StrongFocus)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
21 self.blinkcursor = False
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
22 # Initial values:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
23 self.setSource('')
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
24 self.CursorPosition = 0
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
25 t = QTimer(self)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
26 t.timeout.connect(self.updateCursor)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
27 t.setInterval(500)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
28 t.start()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
29 def updateCursor(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
30 self.blinkcursor = not self.blinkcursor
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
31 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
32 def setSource(self, src):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
33 self.src = src
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
34 self.adjust()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
35 def setCursorPosition(self, c):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
36 self.cursorPosition = clipVal(c, 0, len(self.src))
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
37 print(self.cursorPosition, self.CursorRow, self.CursorCol)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
38 self.cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
39 self.cursorY = self.CursorRow * self.charHeight - self.charHeight
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
40 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
41 @property
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
42 def CursorRow(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
43 # TODO: make this nice.
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
44 txt = self.src[0:self.cursorPosition]
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
45 return len(txt.split('\n'))
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
46 @property
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
47 def CursorCol(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
48 txt = self.src[0:self.cursorPosition]
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
49 curLine = txt.split('\n')[-1]
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
50 return len(curLine) + 1
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
51 @property
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
52 def CurrentLine(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
53 return self.getRow(self.CursorRow)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
54 def getRow(self, r):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
55 rows = self.src.split('\n')
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
56 r = r - 1
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
57 if r < 0 or r > len(rows) - 1:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
58 return ''
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
59 else:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
60 return rows[r]
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
61 def getChar(self, pos):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
62 pass
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
63 def insertText(self, txt):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
64 self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:])
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
65 self.CursorPosition += len(txt)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
66 def GotoNextChar(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
67 if self.src[self.CursorPosition] != '\n':
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
68 self.CursorPosition += 1
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
69 def GotoPrevChar(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
70 if self.src[self.CursorPosition - 1] != '\n':
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
71 self.CursorPosition -= 1
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
72 def GotoNextLine(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
73 curLine = self.CurrentLine
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
74 c = self.CursorCol
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
75 self.CursorPosition += len(curLine) - c + c + 1 # line break char!
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
76 def GotoPrevLine(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
77 curLine = self.CurrentLine
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
78 c = self.CursorCol
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
79 self.CursorPosition -= len(curLine) + c + 1 # line break char!
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
80 def paintEvent(self, event):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
81 # Helper variables:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
82 er = event.rect()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
83 chw, chh = self.charWidth, self.charHeight
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
84 painter = QPainter(self)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
85 # Background:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
86 painter.fillRect(er, self.palette().color(QPalette.Base))
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
87 painter.fillRect(QRect(self.xposLNA, er.top(), 8 * chw, er.bottom() + 1), Qt.gray)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
88 painter.fillRect(self.xposTXT, (self.CursorRow - 1) * chh, er.width(), chh, Qt.yellow)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
89 painter.setPen(Qt.gray)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
90 # first and last row:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
91 row1 = max(int(er.top() / chh) - 1, 0)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
92 row2 = max(int(er.bottom() / chh) + 1, 0)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
93 # Draw contents:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
94 txt = self.src.split('\n')
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
95 print('update', row1, row2)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
96 for row in range(row1, row2 + 1):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
97 ypos = row * chh
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
98 painter.setPen(Qt.black)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
99 painter.drawText(self.xposLNA, ypos, 'R ={0}'.format(row))
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
100 xpos = self.xposTXT
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
101 if row - 1 < len(txt):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
102 painter.drawText(xpos, ypos, txt[row - 1])
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
103 # cursor
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
104 if self.blinkcursor:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
105 painter.fillRect(self.cursorX, self.cursorY, 2, chh, Qt.black)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
106 def keyPressEvent(self, event):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
107 if event.matches(QKeySequence.MoveToNextChar):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
108 self.GotoNextChar()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
109 if event.matches(QKeySequence.MoveToPreviousChar):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
110 self.GotoPrevChar()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
111 if event.matches(QKeySequence.MoveToNextLine):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
112 self.GotoNextLine()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
113 if event.matches(QKeySequence.MoveToPreviousLine):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
114 self.GotoPrevLine()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
115 if event.matches(QKeySequence.MoveToNextPage):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
116 for i in range(5):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
117 self.GotoNextLine()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
118 if event.matches(QKeySequence.MoveToPreviousPage):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
119 for i in range(5):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
120 self.GotoPrevLine()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
121 if event.matches(QKeySequence.MoveToEndOfLine):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
122 self.CursorPosition += len(self.CurrentLine) - self.CursorCol + 1
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
123 if event.matches(QKeySequence.MoveToStartOfLine):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
124 self.CursorPosition -= self.CursorCol - 1
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
125 char = event.text().lower()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
126 if char:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
127 print('ins', char)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
128 self.insertText(char)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
129 self.update()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
130 def adjust(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
131 self.charHeight = self.fontMetrics().height()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
132 self.charWidth = self.fontMetrics().width('x')
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
133 self.xposLNA = GAP
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
134 self.xposTXT = self.xposLNA + 8 * self.charWidth + GAP
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
135 self.xposEnd = self.xposTXT + self.charWidth * 80
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
136 self.setMinimumWidth(self.xposEnd)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
137 txt = self.src.split('\n')
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
138 self.setMinimumHeight(self.charHeight * len(txt))
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
139 self.update()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
140
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
141 class CodeEdit(QScrollArea):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
142 def __init__(self):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
143 super().__init__()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
144 self.ic = InnerCode(self)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
145 self.setWidget(self.ic)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
146 self.setWidgetResizable(True)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
147 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
148 self.setFocusPolicy(Qt.NoFocus)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
149 Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v))
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
150
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
151 if __name__ == '__main__':
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
152 app = QApplication(sys.argv)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
153 ce = CodeEdit()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
154 ce.show()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
155 src = ''.join(inspect.getsourcelines(InnerCode)[0])
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
156 ce.Source = src
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
157 ce.resize(600, 500)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
158 app.exec()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
159