annotate python/codeedit.py @ 178:c694ec551f34

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