diff python/codeedit.py @ 249:e41e4109addd

Added current position arrow
author Windel Bouwman
date Fri, 26 Jul 2013 20:26:05 +0200
parents b10d46e5c8dd
children f5fba5b554d7
line wrap: on
line diff
--- a/python/codeedit.py	Fri Jul 26 16:46:02 2013 +0200
+++ b/python/codeedit.py	Fri Jul 26 20:26:05 2013 +0200
@@ -13,8 +13,8 @@
    return v
 
 class InnerCode(QWidget):
-   textChanged = pyqtSignal()
-   def __init__(self, scrollArea):
+    textChanged = pyqtSignal()
+    def __init__(self, scrollArea):
       super().__init__(scrollArea)
       self.scrollArea = scrollArea
       self.setFont(QFont('Courier', 12))
@@ -23,8 +23,10 @@
       self.setCursor(Qt.IBeamCursor)
       h = QFontMetrics(self.font()).height()
       self.errorPixmap = QPixmap('icons/error.png').scaled(h, h)
+      self.arrowPixmap = QPixmap('icons/arrow.png').scaled(h, h)
       self.blinkcursor = False
       self.errorlist = []
+      self.arrow = None
       # Initial values:
       self.setSource('')
       self.CursorPosition = 0
@@ -32,74 +34,86 @@
       t.timeout.connect(self.updateCursor)
       t.setInterval(500)
       t.start()
-   def updateCursor(self):
+    def updateCursor(self):
       self.blinkcursor = not self.blinkcursor
       self.update()
       #self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
-   def setSource(self, src):
+
+    def setSource(self, src):
       self.src = src
       self.adjust()
-   def getSource(self):
+
+    def getSource(self):
       return self.src
-   def setErrors(self, el):
+
+    def setErrors(self, el):
       self.errorlist = el
       self.update()
-   def setCursorPosition(self, c):
+
+    def setCursorPosition(self, c):
       self.cursorPosition = clipVal(c, 0, len(self.src))
       self.update()
-   CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition)
-   @property
-   def Rows(self):
-      # Make this nicer:
-      return self.src.split('\n')
-   @property
-   def CursorRow(self):
-      # TODO: make this nice.
-      txt = self.src[0:self.cursorPosition]
-      return len(txt.split('\n'))
-   @property
-   def CursorCol(self):
-      txt = self.src[0:self.cursorPosition]
-      curLine = txt.split('\n')[-1]
-      return len(curLine) + 1
-   @property
-   def CurrentLine(self):
-      return self.getRow(self.CursorRow)
-   def setRowCol(self, r, c):
+
+    CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition)
+
+    @property
+    def Rows(self):
+        # Make this nicer:
+        return self.src.split('\n')
+
+    @property
+    def CursorRow(self):
+        # TODO: make this nice.
+        txt = self.src[0:self.cursorPosition]
+        return len(txt.split('\n'))
+
+    @property
+    def CursorCol(self):
+        txt = self.src[0:self.cursorPosition]
+        curLine = txt.split('\n')[-1]
+        return len(curLine) + 1
+
+    @property
+    def CurrentLine(self):
+        return self.getRow(self.CursorRow)
+
+    def setRowCol(self, r, c):
       prevRows = self.Rows[:r-1]
       txt = '\n'.join(prevRows)
       c = clipVal(c, 1, len(self.getRow(r)))
       self.CursorPosition = len(txt) + c + 1
       self.showRow(self.CursorRow)
-   def getRow(self, r):
+
+    def getRow(self, r):
       rows = self.Rows
       r = r - 1
       if r < 0 or r > len(rows) - 1:
          return ''
       else:
          return rows[r]
-   def showRow(self, r):
+
+    def showRow(self, r):
       self.scrollArea.ensureVisible(self.xposTXT, r * self.charHeight, 4, self.charHeight)
-   # Annotations:
-   def addAnnotation(self, row, col, ln, msg):
+    # Annotations:
+    def addAnnotation(self, row, col, ln, msg):
       pass
-   # Text modification:
-   def getChar(self, pos):
+    # Text modification:
+    def getChar(self, pos):
       pass
-   def insertText(self, txt):
+    def insertText(self, txt):
       self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:])
       self.CursorPosition += len(txt)
       self.textChanged.emit()
-   def deleteChar(self):
+    def deleteChar(self):
       self.setSource(self.src[0:self.CursorPosition] + self.src[self.CursorPosition+1:])
       self.textChanged.emit()
-   def GotoNextChar(self):
+    def GotoNextChar(self):
       if self.src[self.CursorPosition] != '\n':
          self.CursorPosition += 1
-   def GotoPrevChar(self):
+    def GotoPrevChar(self):
       if self.src[self.CursorPosition - 1] != '\n':
          self.CursorPosition -= 1
-   def GotoNextLine(self):
+    def GotoNextLine(self):
       curLine = self.CurrentLine
       c = self.CursorCol - 1 # go to zero based
       self.CursorPosition += len(curLine) - c + 1 # line break char!
@@ -109,14 +123,14 @@
       else:
          self.CursorPosition += c
       self.showRow(self.CursorRow)
-   def GotoPrevLine(self):
+    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):
+    def paintEvent(self, event):
       # Helper variables:
       er = event.rect()
       chw, chh = self.charWidth, self.charHeight
@@ -144,6 +158,8 @@
          painter.drawText(self.xposLNA, ypos, '{0}'.format(row))
          xpos = self.xposTXT
          painter.drawText(xpos, ypos, self.getRow(row))
+         if self.arrow and self.arrow.row == row:
+                painter.drawPixmap(self.xposERR, ypos + ydt, self.arrowPixmap)
          curErrors = [e for e in self.errorlist if e.loc.row == row]
          for e in curErrors:
                painter.drawPixmap(self.xposERR, ypos + ydt, self.errorPixmap)
@@ -157,9 +173,9 @@
                #painter.drawText(x, ypos + chh, e.msg)
          #if len(curErrors) > 0:
          #   ypos += chh
+         ypos += chh
 
-         ypos += chh
-   def keyPressEvent(self, event):
+    def keyPressEvent(self, event):
       if event.matches(QKeySequence.MoveToNextChar):
          self.GotoNextChar()
       elif event.matches(QKeySequence.MoveToPreviousChar):
@@ -190,13 +206,15 @@
          if char:
             self.insertText(char)
       self.update()
-   def mousePressEvent(self, event):
+
+    def mousePressEvent(self, event):
       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) + 1
          self.setRowCol(r, c)
-   def adjust(self):
+
+    def adjust(self):
       metrics = self.fontMetrics()
       self.charHeight = metrics.height()
       self.charWidth = metrics.width('x')
@@ -223,6 +241,7 @@
         self.setRowCol = self.ic.setRowCol
         self.FileName = None
     Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v))
+
     def setErrors(self, el):
         self.ic.setErrors(el)
 
@@ -234,18 +253,18 @@
         if not fn:
             fn = 'Untitled'
         self.setWindowTitle(fn)
+
     def getFileName(self):
         return self.filename
     FileName = property(getFileName, setFileName)
 
 
 if __name__ == '__main__':
-   app = QApplication(sys.argv)
-   ce = CodeEdit()
-   ce.show()
-   src = ''.join(inspect.getsourcelines(InnerCode)[0])
-   ce.Source = src
-   print(ce.Source)
-   ce.resize(600, 800)
-   app.exec()
+    app = QApplication(sys.argv)
+    ce = CodeEdit()
+    ce.show()
+    src = ''.join(inspect.getsourcelines(InnerCode)[0])
+    ce.Source = src
+    ce.resize(600, 800)
+    app.exec()