changeset 15:de004f808e56

Fixup in multidocument area stuff
author windel
date Tue, 15 Nov 2011 07:35:00 +0100
parents 607898120eb1
children ddefe6d97cd7
files ide/compiler/compiler.py ide/compiler/errors.py ide/compiler/parser.py ide/ide/codeeditor.py ide/ide/ide.py ide/project.py ide/runbuild.py ide/runtests.py
diffstat 8 files changed, 97 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/ide/compiler/compiler.py	Sun Nov 13 23:24:10 2011 +0100
+++ b/ide/compiler/compiler.py	Tue Nov 15 07:35:00 2011 +0100
@@ -39,6 +39,15 @@
             sym = ExportedSymbol(proc.name, proc.typ)
             sym.imageoffset = proc.entrypoint
             ast.exports.append(sym)
-
       return ast
 
+   def compileProject(self, project):
+      mods = []
+      for fname in project.files:
+         print('Compiling {0}...'.format(fname))
+         source = project.loadProjectFile(fname)
+         mod = self.compilesource(source)
+         mods.append(mod)
+      return mods
+
+
--- a/ide/compiler/errors.py	Sun Nov 13 23:24:10 2011 +0100
+++ b/ide/compiler/errors.py	Tue Nov 15 07:35:00 2011 +0100
@@ -1,10 +1,11 @@
 """ Error handling routines """
 
 class CompilerException(Exception):
-  def __init__(self, msg, row=0, col=0):
+  def __init__(self, msg, row=0, col=0, filename=None):
     self.msg = msg
     self.row = row
     self.col = col
+    self.filename = filename
   def __repr__(self):
      return self.msg
   def __str__(self):
--- a/ide/compiler/parser.py	Sun Nov 13 23:24:10 2011 +0100
+++ b/ide/compiler/parser.py	Tue Nov 15 07:35:00 2011 +0100
@@ -37,6 +37,7 @@
      
    def NextToken(self):
      self.token = self.tokens.__next__()
+     # TODO: store filename in location?
      self.location = (self.token.row, self.token.col)
 
    # Helpers to find location of the error in the code:
--- a/ide/ide/codeeditor.py	Sun Nov 13 23:24:10 2011 +0100
+++ b/ide/ide/codeeditor.py	Tue Nov 15 07:35:00 2011 +0100
@@ -64,13 +64,13 @@
       # Syntax highlighter:
       self.highlighter = MySyntaxHighlighter(self.document())
 
-   def loadFile(self, filename):
+   def setFileName(self, filename):
       self.filename = filename
       self.isUntitled = False
-      with open(filename, 'r') as f:
-         source = f.read()
+      self.setWindowTitle(filename)
+   def setSource(self, source):
       self.setPlainText(source)
-      self.setWindowTitle(filename)
+
    def save(self):
       pass
    def saveAs(self):
--- a/ide/ide/ide.py	Sun Nov 13 23:24:10 2011 +0100
+++ b/ide/ide/ide.py	Tue Nov 15 07:35:00 2011 +0100
@@ -74,7 +74,6 @@
       if not idx.isValid():
          return
       item = self.treeview.model().itemFromIndex(idx)
-      print(item)
    def activate(self, index):
       if not index.isValid():
          return
@@ -126,9 +125,6 @@
     self.setCentralWidget(self.mdiArea)
 
     # Create components:
-    self.codeedit = CodeEdit()
-    self.mdiArea.addSubWindow(self.codeedit)
-
     self.buildOutput = BuildOutput()
     self.addComponent('Build output', self.buildOutput)
 
@@ -199,9 +195,11 @@
   def saveFile(self):
      ac = self.activeMdiChild()
      if ac:
-        print(ac)
         ac.saveFile()
 
+  def saveAll(self):
+     pass
+
   def openFile(self):
      # TODO
      pass
@@ -212,19 +210,16 @@
         self.mdiArea.removeSubWindow(ac)
   
   def loadFile(self, filename):
-     # Extend filename:
-     basedir = os.path.dirname(self.project.filename)
-     filename = os.path.join(basedir, filename)
-
      # Find existing mdi widget:
      wid = self.findMdiChild(filename)
      if wid:
         self.mdiArea.setActiveSubWindow(wid.parent())
-        return
+        return wid
 
      # Create a new one:
      ce = CodeEdit()
-     ce.loadFile(filename)
+     source = self.project.loadProjectFile(filename)
+     ce.setSource(source)
      self.mdiArea.addSubWindow(ce)
      ce.show()
      return ce
@@ -243,7 +238,14 @@
         if wid.filename == filename:
            return wid
      return None
-        
+  
+  def allChildren(self):
+     c = []
+     for window in self.mdiArea.subWindowList():
+        wid = window.widget()
+        c.append(wid)
+     return c
+
   # Settings:
   def loadSettings(self):
      if self.settings.contains('mainwindowstate'):
@@ -251,52 +253,57 @@
      if self.settings.contains('mainwindowgeometry'):
         self.restoreGeometry(self.settings.value('mainwindowgeometry'))
      if self.settings.contains('openedproject'):
-        self.projectfile = self.settings.value('openedproject')
-        self.loadProject()
-     else:
-        self.projectfile = ""
+        projectfile = self.settings.value('openedproject')
+        self.loadProject(projectfile)
   
   def closeEvent(self, ev):
      self.settings.setValue('mainwindowstate', self.saveState())
      self.settings.setValue('mainwindowgeometry', self.saveGeometry())
-     self.settings.setValue('openedproject', self.projectfile)
-     self.codeedit.saveFile()
+     if self.project:
+        self.settings.setValue('openedproject', self.project.filename)
+        # TODO: ask for save of opened files
      ev.accept()
 
   # Error handling:
   def nodeSelected(self, node):
+      ce = self.activeMdiChild()
+      if not ce:
+         return
       if node.location:
          row, col = node.location
-         self.codeedit.highlightErrorLocation( row, col )
+         ce.highlightErrorLocation( row, col )
       else:
-         self.codeedit.clearErrors()
+         ce.clearErrors()
 
   def errorSelected(self, err):
      row, col, msg = err
-     self.codeedit.highlightErrorLocation(row, col)
+     ce = self.activeMdiChild()
+     if not ce:
+        return
+     ce.highlightErrorLocation(row, col)
 
   # Project loading:
-  def loadProject(self):
-     if self.projectfile:
-        self.project = Project()
-        self.project.load(self.projectfile)
-        self.projectview.setProject(self.project)
+  def loadProject(self, filename):
+     self.project = Project(filename)
+     self.projectview.setProject(self.project)
 
   def openProject(self):
-     self.projectfile = QFileDialog.getOpenFileName(self, "Choose project file", "", "lcfos Project files (*.lcp)")
-     self.loadProject()
+     filename = QFileDialog.getOpenFileName(self, \
+       "Choose project file", "", "lcfos Project files (*.lcp)")
+     if filename:
+        self.loadProject(filename)
 
   # Build recepy:
   def buildFile(self):
+     """ Build project """
+     self.saveAll()
      self.buildOutput.clear()
-     self.codeedit.clearErrors()
-     source = str(self.codeedit.toPlainText())
      self.buildOutput.append(str(self.compiler))
-     output = self.compiler.compilesource(source)
+     mods = self.compiler.compileProject(self.project)
+
      self.builderrors.setErrorList(self.compiler.errorlist)
-     self.astViewer.setAst(output)
-     if self.compiler.errorlist:
-        self.buildOutput.append("error occurred")
-     else:
-        self.buildOutput.append("Done!")
+     self.astViewer.setAst(mods[0])
+     for err in self.compiler.errorlist:
+        self.buildOutput.append(str(err))
+     self.buildOutput.append("Done!")
 
--- a/ide/project.py	Sun Nov 13 23:24:10 2011 +0100
+++ b/ide/project.py	Tue Nov 15 07:35:00 2011 +0100
@@ -21,12 +21,23 @@
       if name == 'Files':
          self.inFiles = False
 
+class ProjectFile:
+   def __init__(self, filename):
+      self.filename = filename
+
 class Project:
-   def __init__(self):
+   def __init__(self, filename, isnew=False):
       self.name = ""
       self.files = []
       self.settings = {}
-      self.filename = ""
+      self.filename = filename
+
+      if not isnew:
+         """ Load the project from the XML file """
+         parser = make_parser()
+         handler = ProjectContentHandler(self)
+         parser.setContentHandler(handler)
+         parser.parse(filename)
 
    def save(self, filename):
       """ Save the project in XML format """
@@ -50,14 +61,10 @@
       with open(filename, 'w') as f:
          f.write(xml)
 
-   def load(self, filename):
-      """ Load the project from the XML file """
-      if not os.path.exists(filename):
-         return
-      self.filename = filename
-      print(self.filename)
-      parser = make_parser()
-      handler = ProjectContentHandler(self)
-      parser.setContentHandler(handler)
-      parser.parse(filename)
+   def loadProjectFile(self, filename):
+      basedir = os.path.dirname(self.filename)
+      filename = os.path.join(basedir, filename)
+      with open(filename, 'r') as f:
+         src = f.read()
+      return src
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ide/runbuild.py	Tue Nov 15 07:35:00 2011 +0100
@@ -0,0 +1,16 @@
+import sys
+
+# Compiler imports:
+from compiler.compiler import Compiler
+from project import Project
+
+if __name__ == '__main__':
+   if len(sys.argv) < 2:
+      print('Use {0} projectfile'.format(sys.argv[0]))
+      sys.exit(-1)
+   filename = sys.argv[1]
+   project = Project()
+   project.load(filename)
+   pc = Compiler()
+   pc.compileProject(project)
+
--- a/ide/runtests.py	Sun Nov 13 23:24:10 2011 +0100
+++ b/ide/runtests.py	Tue Nov 15 07:35:00 2011 +0100
@@ -254,14 +254,13 @@
       # This also works: 4D0FAFFE (another variant?? )
       assert(assembler.imulreg64('r15', 'r14') == [0x4d, 0x0f, 0xaf, 0xfe])
    def testProject(self):
-      p = Project()
+      p = Project('test.xml', isnew=True)
       p.name = "Test project"
       p.files.append('main.mod')
       p.files.append('test.mod')
       p.save('test.xml')
 
-      q = Project()
-      q.load('test.xml')
+      q = Project('test.xml')
 
       assert(p.name == q.name)
       assert(p.files == q.files)