comparison python/codeedit.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents d8c735dc31f9
children 0b5b2ee6b435
comparison
equal deleted inserted replaced
162:d8c735dc31f9 163:8104fc8b5e90
17 def __init__(self, scrollArea): 17 def __init__(self, scrollArea):
18 super().__init__(scrollArea) 18 super().__init__(scrollArea)
19 self.scrollArea = scrollArea 19 self.scrollArea = scrollArea
20 self.setFont(QFont('Courier', 16)) 20 self.setFont(QFont('Courier', 16))
21 self.setFocusPolicy(Qt.StrongFocus) 21 self.setFocusPolicy(Qt.StrongFocus)
22 # TODO: only beam cursor in text area..
23 self.setCursor(Qt.IBeamCursor)
22 h = QFontMetrics(self.font()).height() 24 h = QFontMetrics(self.font()).height()
23 self.errorPixmap = QPixmap('error.png').scaled(h, h) 25 self.errorPixmap = QPixmap('error.png').scaled(h, h)
24 self.blinkcursor = False 26 self.blinkcursor = False
25 self.errorlist = [] 27 self.errorlist = []
26 # Initial values: 28 # Initial values:
30 t.timeout.connect(self.updateCursor) 32 t.timeout.connect(self.updateCursor)
31 t.setInterval(500) 33 t.setInterval(500)
32 t.start() 34 t.start()
33 def updateCursor(self): 35 def updateCursor(self):
34 self.blinkcursor = not self.blinkcursor 36 self.blinkcursor = not self.blinkcursor
35 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight) 37 self.update()
38 #self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
36 def setSource(self, src): 39 def setSource(self, src):
37 self.src = src 40 self.src = src
38 self.adjust() 41 self.adjust()
39 def getSource(self): 42 def getSource(self):
40 return self.src 43 return self.src
41 def setErrors(self, el): 44 def setErrors(self, el):
42 self.errorlist = el 45 self.errorlist = el
43 self.update() 46 self.update()
44 def setCursorPosition(self, c): 47 def setCursorPosition(self, c):
45 self.cursorPosition = clipVal(c, 0, len(self.src)) 48 self.cursorPosition = clipVal(c, 0, len(self.src))
46 self.cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth
47 self.cursorY = self.CursorRow * self.charHeight - self.charHeight
48 self.update() 49 self.update()
49 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition) 50 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition)
50 @property 51 @property
51 def Rows(self): 52 def Rows(self):
52 # Make this nicer: 53 # Make this nicer:
63 return len(curLine) + 1 64 return len(curLine) + 1
64 @property 65 @property
65 def CurrentLine(self): 66 def CurrentLine(self):
66 return self.getRow(self.CursorRow) 67 return self.getRow(self.CursorRow)
67 def setRowCol(self, r, c): 68 def setRowCol(self, r, c):
68 prevRows = self.Rows[:r] 69 prevRows = self.Rows[:r-1]
69 txt = '\n'.join(prevRows) 70 txt = '\n'.join(prevRows)
70 c = clipVal(c, 1, len(self.getRow(r+1))) 71 c = clipVal(c, 1, len(self.getRow(r)))
71 self.CursorPosition = len(txt) + c + 1 72 self.CursorPosition = len(txt) + c + 1
72 def getRow(self, r): 73 def getRow(self, r):
73 rows = self.Rows 74 rows = self.Rows
74 r = r - 1 75 r = r - 1
75 if r < 0 or r > len(rows) - 1: 76 if r < 0 or r > len(rows) - 1:
76 return '' 77 return ''
77 else: 78 else:
78 return rows[r] 79 return rows[r]
80 def showRow(self, r):
81 self.scrollArea.ensureVisible(self.xposTXT, r * self.charHeight, 4, self.charHeight)
79 # Annotations: 82 # Annotations:
80 def addAnnotation(self, row, col, ln, msg): 83 def addAnnotation(self, row, col, ln, msg):
81 pass 84 pass
82 # Text modification: 85 # Text modification:
83 def getChar(self, pos): 86 def getChar(self, pos):
102 curLine = self.CurrentLine 105 curLine = self.CurrentLine
103 if len(curLine) < c: 106 if len(curLine) < c:
104 self.CursorPosition += len(curLine) 107 self.CursorPosition += len(curLine)
105 else: 108 else:
106 self.CursorPosition += c 109 self.CursorPosition += c
110 self.showRow(self.CursorRow)
107 def GotoPrevLine(self): 111 def GotoPrevLine(self):
108 c = self.CursorCol - 1 # go to zero based 112 c = self.CursorCol - 1 # go to zero based
109 self.CursorPosition -= c + 1 # line break char! 113 self.CursorPosition -= c + 1 # line break char!
110 curLine = self.CurrentLine 114 curLine = self.CurrentLine
111 if len(curLine) > c: 115 if len(curLine) > c:
112 self.CursorPosition -= len(curLine) - c 116 self.CursorPosition -= len(curLine) - c
117 self.showRow(self.CursorRow)
113 def paintEvent(self, event): 118 def paintEvent(self, event):
114 # Helper variables: 119 # Helper variables:
115 er = event.rect() 120 er = event.rect()
116 chw, chh = self.charWidth, self.charHeight 121 chw, chh = self.charWidth, self.charHeight
117 painter = QPainter(self) 122 painter = QPainter(self)
118 # Background: 123 # Background:
119 painter.fillRect(er, self.palette().color(QPalette.Base)) 124 painter.fillRect(er, self.palette().color(QPalette.Base))
120 painter.fillRect(QRect(self.xposLNA, er.top(), 4 * chw, er.bottom() + 1), Qt.gray) 125 painter.fillRect(QRect(self.xposLNA, er.top(), 4 * chw, er.bottom() + 1), Qt.gray)
121 painter.fillRect(er.left(), (self.CursorRow - 1) * chh, er.width(), chh, Qt.yellow)
122 errorPen = QPen(Qt.red, 3) 126 errorPen = QPen(Qt.red, 3)
123 # first and last row: 127 # first and last row:
124 row1 = max(int(er.top() / chh) - 1, 1) 128 row1 = max(int(er.top() / chh) - 1, 1)
125 row2 = max(int(er.bottom() / chh) + 1, 1) 129 row2 = max(int(er.bottom() / chh) + 1, 1)
126 # Draw contents: 130 # Draw contents:
131 ypos = row1 * chh - self.charDescent
132 curRow = self.CursorRow
133 ydt = -chh + self.charDescent
127 for row in range(row1, row2 + 1): 134 for row in range(row1, row2 + 1):
128 ypos = row * chh - self.charDescent 135 if curRow == row:
136 painter.fillRect(self.xposTXT, ypos + ydt, er.width(), chh, Qt.yellow)
137 # cursor
138 if self.blinkcursor:
139 cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth
140 cursorY = ypos + ydt
141 painter.fillRect(cursorX, cursorY, 2, chh, Qt.black)
129 painter.setPen(Qt.black) 142 painter.setPen(Qt.black)
130 painter.drawText(self.xposLNA, ypos, '{0}'.format(row)) 143 painter.drawText(self.xposLNA, ypos, '{0}'.format(row))
131 xpos = self.xposTXT 144 xpos = self.xposTXT
132 painter.drawText(xpos, ypos, self.getRow(row)) 145 painter.drawText(xpos, ypos, self.getRow(row))
133 for e in self.errorlist: 146 curErrors = [e for e in self.errorlist if e.loc.row == row]
134 if e.loc.row == row: 147 for e in curErrors:
135 painter.drawPixmap(self.xposERR, ypos - chh + self.charDescent, self.errorPixmap) 148 painter.drawPixmap(self.xposERR, ypos + ydt, self.errorPixmap)
136 painter.setPen(errorPen) 149 painter.setPen(errorPen)
137 x = self.xposTXT + (e.loc.col - 1) * chw 150 x = self.xposTXT + (e.loc.col - 1) * chw - 2
138 painter.drawLine(x, ypos+1, x + 100, ypos+1) 151 wt = e.loc.length * chw + 4
139 # cursor 152 painter.drawRoundedRect(x, ypos + ydt, wt, chh, 7, 7)
140 if self.blinkcursor: 153 # print error balloon
141 painter.fillRect(self.cursorX, self.cursorY, 2, chh, Qt.black) 154 painter.drawText(x, ypos + chh, e.msg)
155 if len(curErrors) > 0:
156 ypos += chh
157
158 ypos += chh
142 def keyPressEvent(self, event): 159 def keyPressEvent(self, event):
143 if event.matches(QKeySequence.MoveToNextChar): 160 if event.matches(QKeySequence.MoveToNextChar):
144 self.GotoNextChar() 161 self.GotoNextChar()
145 elif event.matches(QKeySequence.MoveToPreviousChar): 162 elif event.matches(QKeySequence.MoveToPreviousChar):
146 self.GotoPrevChar() 163 self.GotoPrevChar()
172 self.update() 189 self.update()
173 def mousePressEvent(self, event): 190 def mousePressEvent(self, event):
174 pos = event.pos() 191 pos = event.pos()
175 if pos.x() > self.xposTXT and pos.x(): 192 if pos.x() > self.xposTXT and pos.x():
176 c = round((pos.x() - self.xposTXT) / self.charWidth) 193 c = round((pos.x() - self.xposTXT) / self.charWidth)
177 r = int(pos.y() / self.charHeight) 194 r = int(pos.y() / self.charHeight) + 1
178 self.setRowCol(r, c) 195 self.setRowCol(r, c)
179 def adjust(self): 196 def adjust(self):
180 metrics = self.fontMetrics() 197 metrics = self.fontMetrics()
181 self.charHeight = metrics.height() 198 self.charHeight = metrics.height()
182 self.charWidth = metrics.width('x') 199 self.charWidth = metrics.width('x')
197 self.textChanged = self.ic.textChanged 214 self.textChanged = self.ic.textChanged
198 self.setWidget(self.ic) 215 self.setWidget(self.ic)
199 self.setWidgetResizable(True) 216 self.setWidgetResizable(True)
200 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) 217 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
201 self.setFocusPolicy(Qt.NoFocus) 218 self.setFocusPolicy(Qt.NoFocus)
219 self.showRow = self.ic.showRow
220 self.setRowCol = self.ic.setRowCol
202 Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v)) 221 Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v))
203 def setErrors(self, el): 222 def setErrors(self, el):
204 self.ic.setErrors(el) 223 self.ic.setErrors(el)
205 224
206 if __name__ == '__main__': 225 if __name__ == '__main__':