view ide/ide/ide.py @ 1:92df07bc2081

Initial import of compiler
author windel
date Sun, 18 Sep 2011 19:00:29 +0200
parents
children 77202b0e0f40
line wrap: on
line source

from PyQt4.QtCore import *
from PyQt4.QtGui import *
# ide components:
from .codeeditor import CodeEdit
from .astviewer import AstViewer

source = """
module x;
var 
 a,b,c : integer;
begin
  a := 12;
  b := a * 12 + 33;
  c := a div b + a * b * 99;
end x.
"""

class BuildOutput(QTextEdit):
   """ Build output component """
   def __init__(self, parent=None):
      super(BuildOutput, self).__init__(parent)
      self.setCurrentFont(QFont('Courier'))
      self.setReadOnly(True)
      self.append('Build output will appear here!')

class Ide(QMainWindow):
  def __init__(self, parent=None):
    super(Ide, self).__init__(parent)
    self.setWindowTitle('LCFOS IDE')

    # Create menus:
    self.viewMenu = self.menuBar().addMenu('View')
    self.projectMenu = self.menuBar().addMenu('Project')

    # Create components:
    self.codeedit = CodeEdit()
    self.setCentralWidget(self.codeedit)

    self.buildOutput = BuildOutput()
    self.addComponent('Build output', self.buildOutput)

    self.astViewer = AstViewer()
    self.addComponent('AST viewer', self.astViewer)

    # Create actions:
    self.buildAction = QAction('Build!', self)
    self.buildAction.setShortcut(QKeySequence('F7'))
    self.projectMenu.addAction(self.buildAction)
    self.buildAction.triggered.connect(self.buildFile)

    # END of UI construction

    # Load settings:
    self.settings = QSettings('windelsoft', 'lcfoside')
    self.loadSettings()

    # Load example source:
    self.codeedit.setPlainText(source)

  def addComponent(self, name, widget):
     dw = QDockWidget(name)
     dw.setWidget(widget)
     dw.setObjectName(name)
     self.addDockWidget(Qt.RightDockWidgetArea, dw)
     self.viewMenu.addAction(dw.toggleViewAction())
  def loadSettings(self):
     if self.settings.contains('mainwindowstate'):
        self.restoreState(self.settings.value('mainwindowstate'))
     if self.settings.contains('mainwindowgeometry'):
        self.restoreGeometry(self.settings.value('mainwindowgeometry'))
  def closeEvent(self, ev):
     self.settings.setValue('mainwindowstate', self.saveState())
     self.settings.setValue('mainwindowgeometry', self.saveGeometry())
     self.codeedit.saveFile()
     ev.accept()
  def buildFile(self):
     self.buildOutput.clear()
     self.codeedit.clearErrors()
     source = str(self.codeedit.toPlainText())
     self.buildOutput.append(str(self.compiler))
     self.astViewer.setAst(None)
     try:
        output = self.compiler.compilesource(source)
        self.astViewer.setAst(output)
        self.buildOutput.append("Done!")
     except compiler.errors.CompilerException as e:
        self.buildOutput.append(e.msg)
        self.codeedit.highlightErrorLocation( e.row, e.col )