comparison python/codeedit.py @ 256:225f444019b1

Added build and flash menu option
author Windel Bouwman
date Sun, 04 Aug 2013 18:32:04 +0200
parents f5fba5b554d7
children 046017431c6a
comparison
equal deleted inserted replaced
255:7416c923a02a 256:225f444019b1
34 self.t.timeout.connect(self.updateCursor) 34 self.t.timeout.connect(self.updateCursor)
35 self.t.setInterval(500) 35 self.t.setInterval(500)
36 self.t.start() 36 self.t.start()
37 37
38 def updateCursor(self): 38 def updateCursor(self):
39 self.blinkcursor = not self.blinkcursor 39 self.blinkcursor = not self.blinkcursor
40 self.update() 40 self.update()
41 #self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight) 41 #self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
42 42
43 def setSource(self, src): 43 def setSource(self, src):
44 self.src = src 44 self.src = src
45 self.adjust() 45 self.adjust()
46 46
47 def getSource(self): 47 def getSource(self):
48 return self.src 48 return self.src
49 49
50 def setErrors(self, el): 50 def setErrors(self, el):
51 self.errorlist = el 51 self.errorlist = el
52 self.update() 52 self.update()
53 53
54 def setCursorPosition(self, c): 54 def setCursorPosition(self, c):
55 self.cursorPosition = clipVal(c, 0, len(self.src)) 55 self.cursorPosition = clipVal(c, 0, len(self.src))
56 self.update() 56 self.update()
57 57
58 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition) 58 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition)
59 59
60 @property 60 @property
61 def Rows(self): 61 def Rows(self):
93 else: 93 else:
94 return rows[r] 94 return rows[r]
95 95
96 def showRow(self, r): 96 def showRow(self, r):
97 self.scrollArea.ensureVisible(self.xposTXT, r * self.charHeight, 4, self.charHeight) 97 self.scrollArea.ensureVisible(self.xposTXT, r * self.charHeight, 4, self.charHeight)
98
98 # Annotations: 99 # Annotations:
99 def addAnnotation(self, row, col, ln, msg): 100 def addAnnotation(self, row, col, ln, msg):
100 pass 101 pass
102
101 # Text modification: 103 # Text modification:
102 def getChar(self, pos): 104 def getChar(self, pos):
103 pass 105 pass
106
104 def insertText(self, txt): 107 def insertText(self, txt):
105 self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:]) 108 self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:])
106 self.CursorPosition += len(txt) 109 self.CursorPosition += len(txt)
107 self.textChanged.emit() 110 self.textChanged.emit()
111
108 def deleteChar(self): 112 def deleteChar(self):
109 self.setSource(self.src[0:self.CursorPosition] + self.src[self.CursorPosition+1:]) 113 self.setSource(self.src[0:self.CursorPosition] + self.src[self.CursorPosition+1:])
110 self.textChanged.emit() 114 self.textChanged.emit()
115
111 def GotoNextChar(self): 116 def GotoNextChar(self):
112 if self.src[self.CursorPosition] != '\n': 117 if self.src[self.CursorPosition] != '\n':
113 self.CursorPosition += 1 118 self.CursorPosition += 1
119
114 def GotoPrevChar(self): 120 def GotoPrevChar(self):
115 if self.src[self.CursorPosition - 1] != '\n': 121 if self.src[self.CursorPosition - 1] != '\n':
116 self.CursorPosition -= 1 122 self.CursorPosition -= 1
123
117 def GotoNextLine(self): 124 def GotoNextLine(self):
118 curLine = self.CurrentLine 125 curLine = self.CurrentLine
119 c = self.CursorCol - 1 # go to zero based 126 c = self.CursorCol - 1 # go to zero based
120 self.CursorPosition += len(curLine) - c + 1 # line break char! 127 self.CursorPosition += len(curLine) - c + 1 # line break char!
121 curLine = self.CurrentLine 128 curLine = self.CurrentLine
122 if len(curLine) < c: 129 if len(curLine) < c:
123 self.CursorPosition += len(curLine) 130 self.CursorPosition += len(curLine)
124 else: 131 else:
125 self.CursorPosition += c 132 self.CursorPosition += c
126 self.showRow(self.CursorRow) 133 self.showRow(self.CursorRow)
134
127 def GotoPrevLine(self): 135 def GotoPrevLine(self):
128 c = self.CursorCol - 1 # go to zero based 136 c = self.CursorCol - 1 # go to zero based
129 self.CursorPosition -= c + 1 # line break char! 137 self.CursorPosition -= c + 1 # line break char!
130 curLine = self.CurrentLine 138 curLine = self.CurrentLine
131 if len(curLine) > c: 139 if len(curLine) > c:
132 self.CursorPosition -= len(curLine) - c 140 self.CursorPosition -= len(curLine) - c
133 self.showRow(self.CursorRow) 141 self.showRow(self.CursorRow)
142
134 def paintEvent(self, event): 143 def paintEvent(self, event):
135 # Helper variables: 144 # Helper variables:
136 er = event.rect() 145 er = event.rect()
137 chw, chh = self.charWidth, self.charHeight 146 chw, chh = self.charWidth, self.charHeight
138 painter = QPainter(self) 147 painter = QPainter(self)
207 if char: 216 if char:
208 self.insertText(char) 217 self.insertText(char)
209 self.update() 218 self.update()
210 219
211 def mousePressEvent(self, event): 220 def mousePressEvent(self, event):
212 pos = event.pos() 221 pos = event.pos()
213 if pos.x() > self.xposTXT and pos.x(): 222 if pos.x() > self.xposTXT and pos.x():
214 c = round((pos.x() - self.xposTXT) / self.charWidth) 223 c = round((pos.x() - self.xposTXT) / self.charWidth)
215 r = int(pos.y() / self.charHeight) + 1 224 r = int(pos.y() / self.charHeight) + 1
216 self.setRowCol(r, c) 225 self.setRowCol(r, c)
226 super().mousePressEvent(event)
217 227
218 def adjust(self): 228 def adjust(self):
219 metrics = self.fontMetrics() 229 metrics = self.fontMetrics()
220 self.charHeight = metrics.height() 230 self.charHeight = metrics.height()
221 self.charWidth = metrics.width('x') 231 self.charWidth = metrics.width('x')
222 self.charDescent = metrics.descent() 232 self.charDescent = metrics.descent()
223 self.xposERR = GAP 233 self.xposERR = GAP
224 self.xposLNA = self.xposERR + GAP + self.errorPixmap.width() 234 self.xposLNA = self.xposERR + GAP + self.errorPixmap.width()
225 self.xposTXT = self.xposLNA + 4 * self.charWidth + GAP 235 self.xposTXT = self.xposLNA + 4 * self.charWidth + GAP
226 self.xposEnd = self.xposTXT + self.charWidth * 80 236 self.xposEnd = self.xposTXT + self.charWidth * 80
227 self.setMinimumWidth(self.xposEnd) 237 self.setMinimumWidth(self.xposEnd)
228 txt = self.src.split('\n') 238 txt = self.src.split('\n')
229 self.setMinimumHeight(self.charHeight * len(txt)) 239 self.setMinimumHeight(self.charHeight * len(txt))
230 self.update() 240 self.update()
231 241
232 class CodeEdit(QScrollArea): 242 class CodeEdit(QScrollArea):
233 def __init__(self): 243 def __init__(self):
234 super().__init__() 244 super().__init__()
235 self.ic = InnerCode(self) 245 self.ic = InnerCode(self)
246 def setErrors(self, el): 256 def setErrors(self, el):
247 self.ic.setErrors(el) 257 self.ic.setErrors(el)
248 258
249 def setFocus(self): 259 def setFocus(self):
250 self.ic.setFocus() 260 self.ic.setFocus()
261 super().setFocus()
251 262
252 def setFileName(self, fn): 263 def setFileName(self, fn):
253 self.filename = fn 264 self.filename = fn
254 if not fn: 265 if not fn:
255 fn = 'Untitled' 266 fn = 'Untitled'