changeset 68:654c5ac4f2c5

Refactor of menus
author windel
date Sat, 13 Oct 2012 16:13:05 +0200
parents 99bf4f7d47f4
children 60cc36ef5a50
files python/apps/build.py python/apps/ide.py python/libs/project.py
diffstat 3 files changed, 50 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/python/apps/build.py	Fri Oct 12 16:11:31 2012 +0200
+++ b/python/apps/build.py	Sat Oct 13 16:13:05 2012 +0200
@@ -1,4 +1,4 @@
-import sys, os
+import sys, os, argparse
 sys.path.insert(0, os.path.join('..','libs'))
 
 # Compiler imports:
@@ -6,12 +6,15 @@
 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)
+   parser = argparse.ArgumentParser(description='Build tool to build projects')
+   parser.add_argument('project', type=str, help='the project to be build')
+   args = parser.parse_args()
+
+   try:
+      project = Project(args.project)
+   except IOError:
+      print('Failed to load {0}'.format(args.project))
+      sys.exit(3)
    pc = Compiler()
    pc.compileProject(project)
 
--- a/python/apps/ide.py	Fri Oct 12 16:11:31 2012 +0200
+++ b/python/apps/ide.py	Sat Oct 13 16:13:05 2012 +0200
@@ -116,84 +116,67 @@
   def __init__(self, parent=None):
     super(Ide, self).__init__(parent)
     self.setWindowTitle('LCFOS IDE')
+    self.compiler = Compiler()
     icon = QPixmap()
     icon.loadFromData(lcfospng)
     self.setWindowIcon(QIcon(icon))
 
     # Create menus:
-    self.projectMenu = self.menuBar().addMenu('Project')
-    self.viewMenu = self.menuBar().addMenu('View')
-    self.helpMenu = self.menuBar().addMenu('Help')
+    mb = self.menuBar()
+    self.projectMenu = mb.addMenu('Project')
+    self.viewMenu = mb.addMenu('View')
+    self.helpMenu = mb.addMenu('Help')
 
     # Create mdi area:
     self.mdiArea = QMdiArea()
     self.setCentralWidget(self.mdiArea)
 
     # Create components:
-    self.buildOutput = BuildOutput()
-    self.addComponent('Build output', self.buildOutput)
-
-    self.astViewer = AstViewer()
-    self.addComponent('AST viewer', self.astViewer)
-    self.astViewer.sigNodeSelected.connect(self.nodeSelected)
+    def addComponent(name, widget):
+       dw = QDockWidget(name)
+       dw.setWidget(widget)
+       dw.setObjectName(name)
+       self.addDockWidget(Qt.RightDockWidgetArea, dw)
+       self.viewMenu.addAction(dw.toggleViewAction())
+       return widget
 
-    self.builderrors = BuildErrors()
-    self.addComponent('Build errors', self.builderrors)
+    self.buildOutput = addComponent('Build output', BuildOutput())
+    self.astViewer = addComponent('AST viewer', AstViewer())
+    self.astViewer.sigNodeSelected.connect(self.nodeSelected)
+    self.builderrors = addComponent('Build errors', BuildErrors())
     self.builderrors.sigErrorSelected.connect(self.errorSelected)
-
-    self.projectview = ProjectView()
-    self.addComponent('Project explorer', self.projectview)
+    self.projectview = addComponent('Project explorer', ProjectView())
     self.projectview.sigLoadFile.connect(self.loadFile)
 
     # About dialog:
     self.aboutDialog = AboutDialog()
     self.aboutDialog.setWindowIcon(QIcon(icon))
     # Create actions:
-    self.newFileAction = QAction("New", self)
-    self.projectMenu.addAction(self.newFileAction)
-    self.newFileAction.triggered.connect(self.newProject)
-
-    self.openAction = QAction("Open", self)
-    self.openAction.triggered.connect(self.openProject)
-    self.projectMenu.addAction(self.openAction)
+    def addMenuEntry(name, menu, callback, shortcut=None):
+      a = QAction(name, self)
+      menu.addAction(a)
+      a.triggered.connect(callback)
+      if shortcut:
+         a.setShortcut(shortcut)
 
-    self.saveAction = QAction("Save", self)
-    self.projectMenu.addAction(self.saveAction)
-    self.saveAction.triggered.connect(self.saveProject)
-    self.closeAction = QAction("Close", self)
-    self.projectMenu.addAction(self.closeAction)
-    self.closeAction.triggered.connect(self.closeProject)
-    self.buildAction = QAction('Build', self)
-    self.buildAction.setShortcut(QKeySequence('F7'))
-    self.projectMenu.addAction(self.buildAction)
-    self.buildAction.triggered.connect(self.buildProject)
+    addMenuEntry("New", self.projectMenu, self.newProject)
+    addMenuEntry("Open", self.projectMenu, self.openProject)
+    addMenuEntry("Save", self.projectMenu, self.saveProject)
+    addMenuEntry("Close", self.projectMenu, self.closeProject)
+    addMenuEntry("Build", self.projectMenu, self.buildProject, shortcut=QKeySequence('F7'))
 
     self.helpAction  = QAction('Help', self)
     self.helpAction.setShortcut(QKeySequence('F1'))
     self.helpMenu.addAction(self.helpAction)
-    self.aboutAction = QAction('About', self)
-    self.helpMenu.addAction(self.aboutAction)
-    self.aboutAction.triggered.connect(self.aboutDialog.open)
-
+    addMenuEntry('About', self.helpMenu, self.aboutDialog.open)
 
-    cascadeAction = QAction("Cascade windows", self)
-    cascadeAction.triggered.connect(self.mdiArea.cascadeSubWindows)
-    self.viewMenu.addAction(cascadeAction)
-    tileAction = QAction('Tile windows', self)
-    tileAction.triggered.connect(self.mdiArea.tileSubWindows)
-    self.viewMenu.addAction(tileAction)
+    addMenuEntry('Cascade windows', self.viewMenu, self.mdiArea.cascadeSubWindows)
+    addMenuEntry('Tile windows', self.viewMenu, self.mdiArea.tileSubWindows)
 
     # Load settings:
     self.settings = QSettings('windelsoft', 'lcfoside')
     self.loadSettings()
 
-  def addComponent(self, name, widget):
-     dw = QDockWidget(name)
-     dw.setWidget(widget)
-     dw.setObjectName(name)
-     self.addDockWidget(Qt.RightDockWidgetArea, dw)
-     self.viewMenu.addAction(dw.toggleViewAction())
-
   # File handling:
   def newProject(self):
      filename = QFileDialog.getSaveFileName(self, \
@@ -299,7 +282,6 @@
   # Build recepy:
   def buildProject(self):
      """ Build project """
-     self.saveAll()
      self.buildOutput.clear()
      self.buildOutput.append(str(self.compiler))
      mods = self.compiler.compileProject(self.project)
@@ -310,11 +292,9 @@
         self.buildOutput.append(str(err))
      self.buildOutput.append("Done!")
 
-
 if __name__ == '__main__':
    app = QApplication(sys.argv)
    ide = Ide()
-   ide.compiler = Compiler()
    ide.show()
    app.exec_()
 
--- a/python/libs/project.py	Fri Oct 12 16:11:31 2012 +0200
+++ b/python/libs/project.py	Sat Oct 13 16:13:05 2012 +0200
@@ -4,7 +4,6 @@
  - modules
    - primitives like functions, types and variables
    - other modules
-
 """
 
 import json
@@ -15,14 +14,17 @@
       self.modules = []
       self.elements = []
       self.settings = {}
+      if filename:
+         self.load(filename)
+
+   def load(self, filename):
+      """ Load the project from file """
       self.filename = filename
 
-      if self.filename:
-         """ Load the project from the XML file """
-         with open(self.filename, 'r') as f:
-            d = json.load(f)
-         self.elements = d['elements']
-
+      with open(self.filename, 'r') as f:
+         d = json.load(f)
+      self.elements = d['elements']
+      
    def save(self):
       if self.filename:
          d = {'elements': self.elements}