comparison python/ide.py @ 95:4a37d6992bd3

movage
author windel
date Mon, 24 Dec 2012 13:24:59 +0100
parents python/apps/ide.py@60cc36ef5a50
children 3f772feb12ef
comparison
equal deleted inserted replaced
94:1be00bcfaabb 95:4a37d6992bd3
1 import sys, os, base64
2 if sys.version_info.major != 3:
3 print("Needs to be run in python version 3.x")
4 sys.exit(1)
5
6 from PyQt4.QtCore import *
7 from PyQt4.QtGui import *
8
9 # Compiler imports:
10 sys.path.insert(0, os.path.join('..','libs'))
11 from project import Project
12 from compiler import Compiler
13 from widgets import CodeEdit, AstViewer
14
15 lcfospng = base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A\n/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9sJEhMKBk7B678AAAA/SURBVFjD\n7dbBCQAgDATBi9h/y7EFA4Kf2QLCwH1S6XQu6sqoujublc8BAAAAAAAAAAB8B+zXT6YJAAAAAKYd\nWSgFQNUyijIAAAAASUVORK5CYII=\n')
16
17 class BuildOutput(QTextEdit):
18 """ Build output component """
19 def __init__(self, parent=None):
20 super(BuildOutput, self).__init__(parent)
21 self.setCurrentFont(QFont('Courier'))
22 self.setReadOnly(True)
23 self.append('Build output will appear here!')
24
25 class BuildErrors(QListView):
26 sigErrorSelected = pyqtSignal(object)
27 def __init__(self, parent=None):
28 super(BuildErrors, self).__init__(parent)
29 model = QStandardItemModel()
30 self.setModel(model)
31 self.clicked.connect(self.itemSelected)
32 def setErrorList(self, errorlist):
33 model = QStandardItemModel()
34 for e in errorlist:
35 row, col, msg = e
36 item = QStandardItem(str(msg))
37 item.setData(e)
38 model.appendRow(item)
39 self.setModel(model)
40 def itemSelected(self, index):
41 if not index.isValid():
42 return
43 model = self.model()
44 item = model.itemFromIndex(index)
45 err = item.data()
46 self.sigErrorSelected.emit(err)
47
48 class ProjectView(QWidget):
49 sigLoadFile = pyqtSignal(str)
50 def __init__(self, parent=None):
51 super(ProjectView, self).__init__(parent)
52 self.treeview = QTreeView(self)
53 self.treeview.setContextMenuPolicy(Qt.CustomContextMenu)
54 l = QVBoxLayout(self)
55 l.addWidget(self.treeview)
56 pm = QPixmap()
57 pm.loadFromData(lcfospng)
58 self.projectIcon = QIcon(pm)
59 # Connect signals:
60 self.treeview.activated.connect(self.activate)
61 self.treeview.customContextMenuRequested.connect(self.contextMenu)
62 def setProject(self, project):
63 self.project = project
64 model = QStandardItemModel()
65 root = model.invisibleRootItem()
66 pitem = QStandardItem(self.projectIcon, project.name)
67 pitem.setEditable(False)
68 pitem.setData(project)
69 root.appendRow(pitem)
70 for el in self.project.elements:
71 fitem = QStandardItem(el)
72 pitem.appendRow(fitem)
73 fitem.setEditable(False)
74 fitem.setData(el)
75 self.treeview.setModel(model)
76 self.treeview.expandAll()
77 def contextMenu(self, pos):
78 idx = self.treeview.indexAt(pos)
79 if not idx.isValid():
80 return
81 item = self.treeview.model().itemFromIndex(idx)
82 def activate(self, index):
83 if not index.isValid():
84 return
85 model = self.treeview.model()
86 item = model.itemFromIndex(index)
87 fn = item.data()
88 if type(fn) is str:
89 self.sigLoadFile.emit(fn)
90
91 class AboutDialog(QDialog):
92 def __init__(self, parent=None):
93 super(AboutDialog, self).__init__(parent)
94 self.setWindowTitle('About')
95 l = QVBoxLayout(self)
96 txt = QTextEdit(self)
97 txt.setReadOnly(True)
98 aboutText = """<h1>lcfOS IDE</h1>
99 <p>An all-in-one IDE for OS development.</p>
100 <p>https://www.assembla.com/spaces/lcfOS/wiki</p>
101 <p>Author: Windel Bouwman</p>
102 """
103 txt.append(aboutText)
104 l.addWidget(txt)
105 but = QPushButton('OK')
106 but.setDefault(True)
107 but.clicked.connect(self.close)
108 l.addWidget(but)
109
110 class ProjectOptions(QDialog):
111 pass
112 # TODO: project options in here
113
114 class Ide(QMainWindow):
115 def __init__(self, parent=None):
116 super(Ide, self).__init__(parent)
117 self.setWindowTitle('LCFOS IDE')
118 self.compiler = Compiler()
119 icon = QPixmap()
120 icon.loadFromData(lcfospng)
121 self.setWindowIcon(QIcon(icon))
122
123 # Create menus:
124 mb = self.menuBar()
125 self.projectMenu = mb.addMenu('Project')
126 self.viewMenu = mb.addMenu('View')
127 self.helpMenu = mb.addMenu('Help')
128
129 # Create mdi area:
130 self.mdiArea = QMdiArea()
131 self.setCentralWidget(self.mdiArea)
132
133 # Create components:
134 def addComponent(name, widget):
135 dw = QDockWidget(name)
136 dw.setWidget(widget)
137 dw.setObjectName(name)
138 self.addDockWidget(Qt.RightDockWidgetArea, dw)
139 self.viewMenu.addAction(dw.toggleViewAction())
140 return widget
141
142 self.buildOutput = addComponent('Build output', BuildOutput())
143 self.astViewer = addComponent('AST viewer', AstViewer())
144 self.astViewer.sigNodeSelected.connect(self.nodeSelected)
145 self.builderrors = addComponent('Build errors', BuildErrors())
146 self.builderrors.sigErrorSelected.connect(self.errorSelected)
147 self.projectview = addComponent('Project explorer', ProjectView())
148 self.projectview.sigLoadFile.connect(self.loadFile)
149
150 # About dialog:
151 self.aboutDialog = AboutDialog()
152 self.aboutDialog.setWindowIcon(QIcon(icon))
153 # Create actions:
154 def addMenuEntry(name, menu, callback, shortcut=None):
155 a = QAction(name, self)
156 menu.addAction(a)
157 a.triggered.connect(callback)
158 if shortcut:
159 a.setShortcut(shortcut)
160
161 addMenuEntry("New", self.projectMenu, self.newProject)
162 addMenuEntry("Open", self.projectMenu, self.openProject)
163 addMenuEntry("Save", self.projectMenu, self.saveProject)
164 addMenuEntry("Close", self.projectMenu, self.closeProject)
165 addMenuEntry("Build", self.projectMenu, self.buildProject, shortcut=QKeySequence('F7'))
166
167 self.helpAction = QAction('Help', self)
168 self.helpAction.setShortcut(QKeySequence('F1'))
169 self.helpMenu.addAction(self.helpAction)
170 addMenuEntry('About', self.helpMenu, self.aboutDialog.open)
171
172 addMenuEntry('Cascade windows', self.viewMenu, self.mdiArea.cascadeSubWindows)
173 addMenuEntry('Tile windows', self.viewMenu, self.mdiArea.tileSubWindows)
174
175 # Load settings:
176 self.settings = QSettings('windelsoft', 'lcfoside')
177 self.loadSettings()
178
179 # File handling:
180 def newProject(self):
181 filename = QFileDialog.getSaveFileName(self, \
182 "Select new projectfile", "", "lcfos Project files (*.lcp2)")
183 if filename:
184 self.project = Project()
185 self.project.filename = filename
186 self.project.save()
187
188 def saveProject(self):
189 self.project.save()
190
191 def closeProject(self):
192 ac = self.activeMdiChild()
193 if ac:
194 self.mdiArea.removeSubWindow(ac)
195
196 def loadFile(self, filename):
197 # Find existing mdi widget:
198 wid = self.findMdiChild(filename)
199 if wid:
200 self.mdiArea.setActiveSubWindow(wid.parent())
201 return wid
202
203 # Create a new one:
204 ce = CodeEdit()
205 source = self.project.loadProjectFile(filename)
206 ce.setSource(source)
207 self.mdiArea.addSubWindow(ce)
208 ce.show()
209 return ce
210
211 def loadProject(self, filename):
212 self.project = Project(filename)
213 self.projectview.setProject(self.project)
214
215 def openProject(self):
216 filename = QFileDialog.getOpenFileName(self, \
217 "Choose project file", "", "lcfos Project files (*.lcp2)")
218 if filename:
219 self.loadProject(filename)
220
221 # MDI:
222 def activeMdiChild(self):
223 aw = self.mdiArea.activeSubWindow()
224 if aw:
225 return aw.widget()
226 else:
227 return None
228
229 def findMdiChild(self, filename):
230 for window in self.mdiArea.subWindowList():
231 wid = window.widget()
232 if wid.filename == filename:
233 return wid
234 return None
235
236 def allChildren(self):
237 c = []
238 for window in self.mdiArea.subWindowList():
239 wid = window.widget()
240 c.append(wid)
241 return c
242
243 # Settings:
244 def loadSettings(self):
245 if self.settings.contains('mainwindowstate'):
246 self.restoreState(self.settings.value('mainwindowstate'))
247 if self.settings.contains('mainwindowgeometry'):
248 self.restoreGeometry(self.settings.value('mainwindowgeometry'))
249 if self.settings.contains('openedproject'):
250 projectfile = self.settings.value('openedproject')
251 #self.loadProject(projectfile)
252
253 def closeEvent(self, ev):
254 self.settings.setValue('mainwindowstate', self.saveState())
255 self.settings.setValue('mainwindowgeometry', self.saveGeometry())
256 if self.project:
257 self.settings.setValue('openedproject', self.project.filename)
258 # TODO: ask for save of opened files
259 ev.accept()
260
261 # Error handling:
262 def nodeSelected(self, node):
263 ce = self.activeMdiChild()
264 if not ce:
265 return
266 if node.location:
267 row, col = node.location
268 ce.highlightErrorLocation( row, col )
269 else:
270 ce.clearErrors()
271
272 def errorSelected(self, err):
273 row, col, msg = err
274 ce = self.activeMdiChild()
275 if not ce:
276 return
277 ce.highlightErrorLocation(row, col)
278
279 # Project loading:
280
281 # Build recepy:
282 def buildProject(self):
283 """ Build project """
284 self.buildOutput.clear()
285 self.buildOutput.append(str(self.compiler))
286 mods = self.compiler.compileProject(self.project)
287
288 self.builderrors.setErrorList(self.compiler.errorlist)
289 self.astViewer.setAst(mods[0])
290 for err in self.compiler.errorlist:
291 self.buildOutput.append(str(err))
292 self.buildOutput.append("Done!")
293
294 if __name__ == '__main__':
295 app = QApplication(sys.argv)
296 ide = Ide()
297 ide.show()
298 app.exec_()
299