annotate python/ide/codeedit.py @ 297:a6f61e9a9d5c

Added docs requirements
author Windel Bouwman
date Sun, 01 Dec 2013 17:35:54 +0100
parents 7b38782ed496
children dcae6574c974
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
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
4 import os
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
5 from PyQt4.QtCore import *
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
6 from PyQt4.QtGui import *
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
7 import inspect
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
8
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
9 GAP = 5
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
10
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
11 def clipVal(v, mn, mx):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
12 if v < mn: return mn
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
13 if v > mx: return mx
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
14 return v
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
15
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
16 class InnerCode(QWidget):
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
17 textChanged = pyqtSignal()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
18 def __init__(self, scrollArea):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
19 super().__init__(scrollArea)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
20 self.scrollArea = scrollArea
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 167
diff changeset
21 self.setFont(QFont('Courier', 12))
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
22 self.setFocusPolicy(Qt.StrongFocus)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
23 # TODO: only beam cursor in text area..
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
24 self.setCursor(Qt.IBeamCursor)
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 161
diff changeset
25 h = QFontMetrics(self.font()).height()
247
dd8bbb963458 project remove
Windel Bouwman
parents: 169
diff changeset
26 self.errorPixmap = QPixmap('icons/error.png').scaled(h, h)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
27 self.arrowPixmap = QPixmap('icons/arrow.png').scaled(h, h)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
28 self.blinkcursor = False
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 161
diff changeset
29 self.errorlist = []
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
30 self.arrow = None
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
31 # Initial values:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
32 self.setSource('')
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
33 self.CursorPosition = 0
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
34 self.t = QTimer(self)
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
35 self.t.timeout.connect(self.updateCursor)
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
36 self.t.setInterval(500)
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
37 self.t.start()
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
38
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
39 def updateCursor(self):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
40 self.blinkcursor = not self.blinkcursor
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
41 self.update()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
42 #self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
43
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
44 def setSource(self, src):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
45 self.src = src
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
46 self.adjust()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
47
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
48 def getSource(self):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
49 return self.src
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
50
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
51 def setErrors(self, el):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
52 self.errorlist = el
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
53 self.update()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
54
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
55 def setCursorPosition(self, c):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
56 self.cursorPosition = clipVal(c, 0, len(self.src))
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
57 self.update()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
58
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
59 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
60
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
61 @property
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
62 def Rows(self):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
63 # Make this nicer:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
64 return self.src.split('\n')
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
65
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
66 @property
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
67 def CursorRow(self):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
68 # TODO: make this nice.
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
69 txt = self.src[0:self.cursorPosition]
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
70 return len(txt.split('\n'))
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
71
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
72 @property
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
73 def CursorCol(self):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
74 txt = self.src[0:self.cursorPosition]
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
75 curLine = txt.split('\n')[-1]
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
76 return len(curLine) + 1
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
77
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
78 @property
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
79 def CurrentLine(self):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
80 return self.getRow(self.CursorRow)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
81
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
82 def setRowCol(self, r, c):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
83 prevRows = self.Rows[:r-1]
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
84 txt = '\n'.join(prevRows)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
85 c = clipVal(c, 1, len(self.getRow(r)))
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 161
diff changeset
86 self.CursorPosition = len(txt) + c + 1
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 163
diff changeset
87 self.showRow(self.CursorRow)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
88
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
89 def getRow(self, r):
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
90 rows = self.Rows
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
91 r = r - 1
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
92 if r < 0 or r > len(rows) - 1:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
93 return ''
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
94 else:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
95 return rows[r]
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
96
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
97 def showRow(self, r):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
98 self.scrollArea.ensureVisible(self.xposTXT, r * self.charHeight, 4, self.charHeight)
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
99
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
100 # Annotations:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
101 def addAnnotation(self, row, col, ln, msg):
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 161
diff changeset
102 pass
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
103
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
104 # Text modification:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
105 def getChar(self, pos):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
106 pass
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
107
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
108 def insertText(self, txt):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
109 self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:])
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
110 self.CursorPosition += len(txt)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
111 self.textChanged.emit()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
112
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
113 def deleteChar(self):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
114 self.setSource(self.src[0:self.CursorPosition] + self.src[self.CursorPosition+1:])
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
115 self.textChanged.emit()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
116
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
117 def GotoNextChar(self):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
118 if self.src[self.CursorPosition] != '\n':
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
119 self.CursorPosition += 1
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
120
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
121 def GotoPrevChar(self):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
122 if self.src[self.CursorPosition - 1] != '\n':
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
123 self.CursorPosition -= 1
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
124
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
125 def GotoNextLine(self):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
126 curLine = self.CurrentLine
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
127 c = self.CursorCol - 1 # go to zero based
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
128 self.CursorPosition += len(curLine) - c + 1 # line break char!
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
129 curLine = self.CurrentLine
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
130 if len(curLine) < c:
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
131 self.CursorPosition += len(curLine)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
132 else:
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
133 self.CursorPosition += c
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
134 self.showRow(self.CursorRow)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
135
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
136 def GotoPrevLine(self):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
137 c = self.CursorCol - 1 # go to zero based
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
138 self.CursorPosition -= c + 1 # line break char!
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
139 curLine = self.CurrentLine
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
140 if len(curLine) > c:
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
141 self.CursorPosition -= len(curLine) - c
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
142 self.showRow(self.CursorRow)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
143
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
144 def paintEvent(self, event):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
145 # Helper variables:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
146 er = event.rect()
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
147 chw, chh = self.charWidth, self.charHeight
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
148 painter = QPainter(self)
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
149 # Background:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
150 painter.fillRect(er, self.palette().color(QPalette.Base))
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 161
diff changeset
151 painter.fillRect(QRect(self.xposLNA, er.top(), 4 * chw, er.bottom() + 1), Qt.gray)
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 161
diff changeset
152 errorPen = QPen(Qt.red, 3)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
153 # first and last row:
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
154 row1 = max(int(er.top() / chh) - 1, 1)
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
155 row2 = max(int(er.bottom() / chh) + 1, 1)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
156 # Draw contents:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
157 ypos = row1 * chh - self.charDescent
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
158 curRow = self.CursorRow
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
159 ydt = -chh + self.charDescent
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
160 for row in range(row1, row2 + 1):
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
161 if curRow == row and self.hasFocus():
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
162 painter.fillRect(self.xposTXT, ypos + ydt, er.width(), chh, Qt.yellow)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
163 # cursor
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
164 if self.blinkcursor:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
165 cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
166 cursorY = ypos + ydt
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
167 painter.fillRect(cursorX, cursorY, 2, chh, Qt.black)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
168 painter.setPen(Qt.black)
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 161
diff changeset
169 painter.drawText(self.xposLNA, ypos, '{0}'.format(row))
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
170 xpos = self.xposTXT
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
171 painter.drawText(xpos, ypos, self.getRow(row))
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
172 if self.arrow and self.arrow.row == row:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
173 painter.drawPixmap(self.xposERR, ypos + ydt, self.arrowPixmap)
277
046017431c6a Started register allocator
Windel Bouwman
parents: 256
diff changeset
174 curErrors = [e for e in self.errorlist if e.loc and e.loc.row == row]
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
175 for e in curErrors:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
176 painter.drawPixmap(self.xposERR, ypos + ydt, self.errorPixmap)
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 161
diff changeset
177 painter.setPen(errorPen)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
178 x = self.xposTXT + (e.loc.col - 1) * chw - 2
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
179 wt = e.loc.length * chw + 4
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 163
diff changeset
180 dy = self.charDescent
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 163
diff changeset
181 painter.drawLine(x, ypos + dy, x + wt, ypos + dy)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 163
diff changeset
182 #painter.drawRoundedRect(x, ypos + ydt, wt, chh, 7, 7)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
183 # print error balloon
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 163
diff changeset
184 #painter.drawText(x, ypos + chh, e.msg)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 163
diff changeset
185 #if len(curErrors) > 0:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 163
diff changeset
186 # ypos += chh
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
187 ypos += chh
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
188
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
189 def keyPressEvent(self, event):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
190 if event.matches(QKeySequence.MoveToNextChar):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
191 self.GotoNextChar()
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
192 elif event.matches(QKeySequence.MoveToPreviousChar):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
193 self.GotoPrevChar()
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
194 elif event.matches(QKeySequence.MoveToNextLine):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
195 self.GotoNextLine()
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
196 elif event.matches(QKeySequence.MoveToPreviousLine):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
197 self.GotoPrevLine()
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
198 elif event.matches(QKeySequence.MoveToNextPage):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
199 for i in range(5):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
200 self.GotoNextLine()
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
201 elif event.matches(QKeySequence.MoveToPreviousPage):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
202 for i in range(5):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
203 self.GotoPrevLine()
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
204 elif event.matches(QKeySequence.MoveToEndOfLine):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
205 self.CursorPosition += len(self.CurrentLine) - self.CursorCol + 1
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
206 elif event.matches(QKeySequence.MoveToStartOfLine):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
207 self.CursorPosition -= self.CursorCol - 1
161
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
208 elif event.matches(QKeySequence.Delete):
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
209 self.deleteChar()
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
210 elif event.matches(QKeySequence.InsertParagraphSeparator):
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
211 self.insertText('\n')
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
212 elif event.key() == Qt.Key_Backspace:
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
213 self.CursorPosition -= 1
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
214 self.deleteChar()
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
215 else:
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
216 char = event.text()
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
217 if char:
956f8e5ee48a Improvements to code edit
Windel Bouwman
parents: 160
diff changeset
218 self.insertText(char)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
219 self.update()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
220
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
221 def mousePressEvent(self, event):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
222 pos = event.pos()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
223 if pos.x() > self.xposTXT and pos.x():
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
224 c = round((pos.x() - self.xposTXT) / self.charWidth)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
225 r = int(pos.y() / self.charHeight) + 1
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
226 self.setRowCol(r, c)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
227 super().mousePressEvent(event)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
228
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
229 def adjust(self):
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
230 metrics = self.fontMetrics()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
231 self.charHeight = metrics.height()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
232 self.charWidth = metrics.width('x')
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
233 self.charDescent = metrics.descent()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
234 self.xposERR = GAP
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
235 self.xposLNA = self.xposERR + GAP + self.errorPixmap.width()
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
236 self.xposTXT = self.xposLNA + 4 * self.charWidth + GAP
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
237 self.xposEnd = self.xposTXT + self.charWidth * 80
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
238 self.setMinimumWidth(self.xposEnd)
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
239 txt = self.src.split('\n')
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
240 self.setMinimumHeight(self.charHeight * len(txt))
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
241 self.update()
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
242
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
243 class CodeEdit(QScrollArea):
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
244 def __init__(self):
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
245 super().__init__()
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
246 self.ic = InnerCode(self)
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
247 self.textChanged = self.ic.textChanged
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
248 self.setWidget(self.ic)
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
249 self.setWidgetResizable(True)
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
250 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
251 self.setFocusPolicy(Qt.NoFocus)
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
252 self.showRow = self.ic.showRow
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
253 self.setRowCol = self.ic.setRowCol
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
254 self.FileName = None
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
255 Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v))
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
256
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
257 def setErrors(self, el):
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
258 self.ic.setErrors(el)
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
259
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
260 def setFocus(self):
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
261 self.ic.setFocus()
256
225f444019b1 Added build and flash menu option
Windel Bouwman
parents: 250
diff changeset
262 super().setFocus()
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
263
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
264 def setFileName(self, fn):
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
265 self.filename = fn
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
266 if fn:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
267 fn = os.path.basename(fn)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
268 else:
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
269 fn = 'Untitled'
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
270 self.setWindowTitle(fn)
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
271
248
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
272 def getFileName(self):
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
273 return self.filename
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
274 FileName = property(getFileName, setFileName)
b10d46e5c8dd ide refactor
Windel Bouwman
parents: 247
diff changeset
275
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
276 def save(self):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
277 if self.FileName:
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
278 s = self.Source
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
279 with open(self.FileName, 'w') as f:
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
280 f.write(s)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
281
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
282
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
283 if __name__ == '__main__':
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
284 app = QApplication(sys.argv)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
285 ce = CodeEdit()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
286 ce.show()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
287 src = ''.join(inspect.getsourcelines(InnerCode)[0])
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
288 ce.Source = src
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
289 ce.resize(600, 800)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 248
diff changeset
290 app.exec()
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents:
diff changeset
291