changeset 162:d8c735dc31f9

Used new editor in ide
author Windel Bouwman
date Sun, 10 Mar 2013 11:36:55 +0100
parents 956f8e5ee48a
children 8104fc8b5e90
files python/codeedit.py python/codeeditor.py python/ide.py python/testc3.py
diffstat 4 files changed, 48 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/python/codeedit.py	Sat Mar 09 15:52:55 2013 +0100
+++ b/python/codeedit.py	Sun Mar 10 11:36:55 2013 +0100
@@ -13,12 +13,16 @@
    return v
 
 class InnerCode(QWidget):
+   textChanged = pyqtSignal()
    def __init__(self, scrollArea):
       super().__init__(scrollArea)
       self.scrollArea = scrollArea
       self.setFont(QFont('Courier', 16))
       self.setFocusPolicy(Qt.StrongFocus)
+      h = QFontMetrics(self.font()).height()
+      self.errorPixmap = QPixmap('error.png').scaled(h, h)
       self.blinkcursor = False
+      self.errorlist = []
       # Initial values:
       self.setSource('')
       self.CursorPosition = 0
@@ -32,6 +36,11 @@
    def setSource(self, src):
       self.src = src
       self.adjust()
+   def getSource(self):
+      return self.src
+   def setErrors(self, el):
+      self.errorlist = el
+      self.update()
    def setCursorPosition(self, c):
       self.cursorPosition = clipVal(c, 0, len(self.src))
       self.cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth
@@ -58,7 +67,8 @@
    def setRowCol(self, r, c):
       prevRows = self.Rows[:r]
       txt = '\n'.join(prevRows)
-      self.CursorPosition = len(txt) + c
+      c = clipVal(c, 1, len(self.getRow(r+1)))
+      self.CursorPosition = len(txt) + c + 1
    def getRow(self, r):
       rows = self.Rows
       r = r - 1
@@ -66,13 +76,19 @@
          return ''
       else:
          return rows[r]
+   # Annotations:
+   def addAnnotation(self, row, col, ln, msg):
+      pass
+   # Text modification:
    def getChar(self, pos):
       pass
    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):
       self.setSource(self.src[0:self.CursorPosition] + self.src[self.CursorPosition+1:])
+      self.textChanged.emit()
    def GotoNextChar(self):
       if self.src[self.CursorPosition] != '\n':
          self.CursorPosition += 1
@@ -81,7 +97,7 @@
          self.CursorPosition -= 1
    def GotoNextLine(self):
       curLine = self.CurrentLine
-      c = self.CursorCol
+      c = self.CursorCol - 1 # go to zero based
       self.CursorPosition += len(curLine) - c + 1 # line break char!
       curLine = self.CurrentLine
       if len(curLine) < c:
@@ -89,7 +105,7 @@
       else:
          self.CursorPosition += c
    def GotoPrevLine(self):
-      c = self.CursorCol
+      c = self.CursorCol - 1 # go to zero based
       self.CursorPosition -= c + 1 # line break char!
       curLine = self.CurrentLine
       if len(curLine) > c:
@@ -101,9 +117,9 @@
       painter = QPainter(self)
       # Background:
       painter.fillRect(er, self.palette().color(QPalette.Base))
-      painter.fillRect(QRect(self.xposLNA, er.top(), 8 * chw, er.bottom() + 1), Qt.gray)
+      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)
-      painter.setPen(Qt.gray)
+      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)
@@ -111,9 +127,15 @@
       for row in range(row1, row2 + 1):
          ypos = row * chh - self.charDescent
          painter.setPen(Qt.black)
-         painter.drawText(self.xposLNA, ypos, 'R ={0}'.format(row))
+         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)
+               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)
@@ -159,8 +181,9 @@
       self.charHeight = metrics.height()
       self.charWidth = metrics.width('x')
       self.charDescent = metrics.descent()
-      self.xposLNA = GAP
-      self.xposTXT = self.xposLNA + 8 * self.charWidth + GAP
+      self.xposERR = GAP
+      self.xposLNA = self.xposERR + GAP + self.errorPixmap.width()
+      self.xposTXT = self.xposLNA + 4 * self.charWidth + GAP
       self.xposEnd = self.xposTXT + self.charWidth * 80
       self.setMinimumWidth(self.xposEnd)
       txt = self.src.split('\n')
@@ -171,11 +194,14 @@
    def __init__(self):
       super().__init__()
       self.ic = InnerCode(self)
+      self.textChanged = self.ic.textChanged
       self.setWidget(self.ic)
       self.setWidgetResizable(True)
       self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
       self.setFocusPolicy(Qt.NoFocus)
    Source = property(lambda s: s.ic.getSource(), lambda s, v: s.ic.setSource(v))
+   def setErrors(self, el):
+      self.ic.setErrors(el)
 
 if __name__ == '__main__':
    app = QApplication(sys.argv)
@@ -183,6 +209,7 @@
    ce.show()
    src = ''.join(inspect.getsourcelines(InnerCode)[0])
    ce.Source = src
+   print(ce.Source)
    ce.resize(600, 800)
    app.exec()
 
--- a/python/codeeditor.py	Sat Mar 09 15:52:55 2013 +0100
+++ b/python/codeeditor.py	Sun Mar 10 11:36:55 2013 +0100
@@ -77,7 +77,7 @@
       self.setPlainText(source)
    def getSource(self):
       return self.toPlainText()
-   source = property(getSource, setSource)
+   Source = property(getSource, setSource)
 
    def save(self):
       self.saveFile()
--- a/python/ide.py	Sat Mar 09 15:52:55 2013 +0100
+++ b/python/ide.py	Sun Mar 10 11:36:55 2013 +0100
@@ -10,7 +10,8 @@
 from project import Project
 import ppci
 from astviewer import AstViewer
-from codeeditor import CodeEdit
+#from codeeditor import CodeEdit
+from codeedit import CodeEdit
 stutil = __import__('st-util')
 import testc3
 
@@ -195,7 +196,7 @@
     self.loadSettings()
 
     ce = self.newFile()
-    ce.setSource(testc3.testsrc)
+    ce.Source = testc3.testsrc
 
     self.diag = ppci.DiagnosticsManager()
 
@@ -210,8 +211,10 @@
 
   def newFile(self):
      ce = CodeEdit()
-     self.mdiArea.addSubWindow(ce)
+     ce.textChanged.connect(self.buildProject)
+     w = self.mdiArea.addSubWindow(ce)
      ce.show()
+     w.resize(500, 700)
      return ce
   def openFile(self):
      filename = QFileDialog.getOpenFileName(self, "Open K# file...", "*.ks", "K# source files (*.ks)")
@@ -301,7 +304,7 @@
      #   # TODO: ask for save of opened files
      ac = self.activeMdiChild()
      if ac:
-        if ac.filename:
+        if hasattr(ac, 'filename') and ac.filename:
            self.settings.setValue('lastfile', ac.filename)
         else:
            self.settings.remove('lastfile')
@@ -333,7 +336,7 @@
      ce = self.activeMdiChild()
      print('BUILD file')
      if ce:
-        source = ce.source
+        source = ce.Source
         self.buildOutput.clear()
         self.buildOutput.append(str(self.compiler))
         ast = testc3.c3compile(source, self.diag)
--- a/python/testc3.py	Sat Mar 09 15:52:55 2013 +0100
+++ b/python/testc3.py	Sun Mar 10 11:36:55 2013 +0100
@@ -1,8 +1,7 @@
 import c3
 import time, ppci, x86, ir
 
-testsrc = """
-package test;
+testsrc = """package test;
 
 var u32 c, d;
 
@@ -64,6 +63,9 @@
    cg = c3.CodeGenerator()
    x86gen = x86.X86CodeGen(diag)
    p.parseSource(src)
+   ok = len(diag.diags) == 0
+   if not ok:
+      return
    tc.checkPackage(sema.mod)
    print('{0} errors'.format(len(diag.diags)))