diff 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
line wrap: on
line diff
--- a/python/codeedit.py	Sun Mar 10 11:36:55 2013 +0100
+++ b/python/codeedit.py	Mon Mar 18 20:13:57 2013 +0100
@@ -19,6 +19,8 @@
       self.scrollArea = scrollArea
       self.setFont(QFont('Courier', 16))
       self.setFocusPolicy(Qt.StrongFocus)
+      # TODO: only beam cursor in text area..
+      self.setCursor(Qt.IBeamCursor)
       h = QFontMetrics(self.font()).height()
       self.errorPixmap = QPixmap('error.png').scaled(h, h)
       self.blinkcursor = False
@@ -32,7 +34,8 @@
       t.start()
    def updateCursor(self):
       self.blinkcursor = not self.blinkcursor
-      self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
+      self.update()
+      #self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
    def setSource(self, src):
       self.src = src
       self.adjust()
@@ -43,8 +46,6 @@
       self.update()
    def setCursorPosition(self, c):
       self.cursorPosition = clipVal(c, 0, len(self.src))
-      self.cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth
-      self.cursorY = self.CursorRow * self.charHeight - self.charHeight
       self.update()
    CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition)
    @property
@@ -65,9 +66,9 @@
    def CurrentLine(self):
       return self.getRow(self.CursorRow)
    def setRowCol(self, r, c):
-      prevRows = self.Rows[:r]
+      prevRows = self.Rows[:r-1]
       txt = '\n'.join(prevRows)
-      c = clipVal(c, 1, len(self.getRow(r+1)))
+      c = clipVal(c, 1, len(self.getRow(r)))
       self.CursorPosition = len(txt) + c + 1
    def getRow(self, r):
       rows = self.Rows
@@ -76,6 +77,8 @@
          return ''
       else:
          return rows[r]
+   def showRow(self, r):
+      self.scrollArea.ensureVisible(self.xposTXT, r * self.charHeight, 4, self.charHeight)
    # Annotations:
    def addAnnotation(self, row, col, ln, msg):
       pass
@@ -104,12 +107,14 @@
          self.CursorPosition += len(curLine)
       else:
          self.CursorPosition += c
+      self.showRow(self.CursorRow)
    def GotoPrevLine(self):
       c = self.CursorCol - 1 # go to zero based
       self.CursorPosition -= c + 1 # line break char!
       curLine = self.CurrentLine
       if len(curLine) > c:
          self.CursorPosition -= len(curLine) - c
+      self.showRow(self.CursorRow)
    def paintEvent(self, event):
       # Helper variables:
       er = event.rect()
@@ -118,27 +123,39 @@
       # Background:
       painter.fillRect(er, self.palette().color(QPalette.Base))
       painter.fillRect(QRect(self.xposLNA, er.top(), 4 * chw, er.bottom() + 1), Qt.gray)
-      painter.fillRect(er.left(), (self.CursorRow - 1) * chh, er.width(), chh, Qt.yellow)
       errorPen = QPen(Qt.red, 3)
       # first and last row:
       row1 = max(int(er.top() / chh) - 1, 1)
       row2 = max(int(er.bottom() / chh) + 1, 1)
       # Draw contents:
+      ypos = row1 * chh - self.charDescent
+      curRow = self.CursorRow
+      ydt = -chh + self.charDescent
       for row in range(row1, row2 + 1):
-         ypos = row * chh - self.charDescent
+         if curRow == row:
+            painter.fillRect(self.xposTXT, ypos + ydt, er.width(), chh, Qt.yellow)
+            # cursor
+            if self.blinkcursor:
+               cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth
+               cursorY = ypos + ydt
+               painter.fillRect(cursorX, cursorY, 2, chh, Qt.black)
          painter.setPen(Qt.black)
          painter.drawText(self.xposLNA, ypos, '{0}'.format(row))
          xpos = self.xposTXT
          painter.drawText(xpos, ypos, self.getRow(row))
-         for e in self.errorlist:
-            if e.loc.row == row:
-               painter.drawPixmap(self.xposERR, ypos - chh + self.charDescent, self.errorPixmap)
+         curErrors = [e for e in self.errorlist if e.loc.row == row]
+         for e in curErrors:
+               painter.drawPixmap(self.xposERR, ypos + ydt, self.errorPixmap)
                painter.setPen(errorPen)
-               x = self.xposTXT + (e.loc.col - 1) * chw
-               painter.drawLine(x, ypos+1, x + 100, ypos+1)
-      # cursor
-      if self.blinkcursor:
-         painter.fillRect(self.cursorX, self.cursorY, 2, chh, Qt.black)
+               x = self.xposTXT + (e.loc.col - 1) * chw - 2
+               wt = e.loc.length * chw + 4
+               painter.drawRoundedRect(x, ypos + ydt, wt, chh, 7, 7)
+               # print error balloon
+               painter.drawText(x, ypos + chh, e.msg)
+         if len(curErrors) > 0:
+            ypos += chh
+
+         ypos += chh
    def keyPressEvent(self, event):
       if event.matches(QKeySequence.MoveToNextChar):
          self.GotoNextChar()
@@ -174,7 +191,7 @@
       pos = event.pos()
       if pos.x() > self.xposTXT and pos.x():
          c = round((pos.x() - self.xposTXT) / self.charWidth)
-         r = int(pos.y() / self.charHeight)
+         r = int(pos.y() / self.charHeight) + 1
          self.setRowCol(r, c)
    def adjust(self):
       metrics = self.fontMetrics()
@@ -199,6 +216,8 @@
       self.setWidgetResizable(True)
       self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
       self.setFocusPolicy(Qt.NoFocus)
+      self.showRow = self.ic.showRow
+      self.setRowCol = self.ic.setRowCol
    Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v))
    def setErrors(self, el):
       self.ic.setErrors(el)