Mercurial > lcfOS
annotate python/ide.py @ 220:3f6c30a5d234
Major change in expression parsing to enable pointers and structs
author | Windel Bouwman |
---|---|
date | Sat, 06 Jul 2013 21:32:20 +0200 |
parents | 8b2f20aae086 |
children | 6259856841a0 |
rev | line source |
---|---|
99 | 1 #!/usr/bin/python |
2 | |
69 | 3 import sys, os, base64 |
131 | 4 assert sys.version_info.major == 3, "Needs to be run in python version 3.x" |
64 | 5 |
6 from PyQt4.QtCore import * | |
7 from PyQt4.QtGui import * | |
1 | 8 |
9 # Compiler imports: | |
64 | 10 from project import Project |
106 | 11 import ppci |
100 | 12 from astviewer import AstViewer |
162 | 13 #from codeeditor import CodeEdit |
14 from codeedit import CodeEdit | |
131 | 15 stutil = __import__('st-util') |
152 | 16 import testc3 |
165 | 17 import c3 |
64 | 18 |
19 class BuildOutput(QTextEdit): | |
20 """ Build output component """ | |
21 def __init__(self, parent=None): | |
22 super(BuildOutput, self).__init__(parent) | |
23 self.setCurrentFont(QFont('Courier')) | |
24 self.setReadOnly(True) | |
25 self.append('Build output will appear here!') | |
26 | |
167 | 27 class BuildErrors(QTreeView): |
64 | 28 sigErrorSelected = pyqtSignal(object) |
29 def __init__(self, parent=None): | |
30 super(BuildErrors, self).__init__(parent) | |
31 model = QStandardItemModel() | |
32 self.setModel(model) | |
33 self.clicked.connect(self.itemSelected) | |
207 | 34 self.errorIcon = QIcon('icons/error.png') |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
35 self.model = QStandardItemModel() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
36 self.model.setHorizontalHeaderLabels(['Message', 'Row', 'Column']) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
37 self.setModel(self.model) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
38 |
64 | 39 def setErrorList(self, errorlist): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
40 c = self.model.rowCount() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
41 self.model.removeRows(0, c) |
64 | 42 for e in errorlist: |
167 | 43 item = QStandardItem(self.errorIcon, str(e.msg)) |
64 | 44 item.setData(e) |
167 | 45 irow = QStandardItem(str(e.loc.row)) |
46 irow.setData(e) | |
47 icol = QStandardItem(str(e.loc.col)) | |
48 icol.setData(e) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
49 self.model.appendRow([item, irow, icol]) |
64 | 50 def itemSelected(self, index): |
51 if not index.isValid(): | |
52 return | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
53 item = self.model.itemFromIndex(index) |
64 | 54 err = item.data() |
55 self.sigErrorSelected.emit(err) | |
56 | |
57 class ProjectView(QWidget): | |
58 sigLoadFile = pyqtSignal(str) | |
59 def __init__(self, parent=None): | |
60 super(ProjectView, self).__init__(parent) | |
61 self.treeview = QTreeView(self) | |
62 self.treeview.setContextMenuPolicy(Qt.CustomContextMenu) | |
63 l = QVBoxLayout(self) | |
64 l.addWidget(self.treeview) | |
207 | 65 self.projectIcon = QIcon('icons/logo.png') |
64 | 66 # Connect signals: |
67 self.treeview.activated.connect(self.activate) | |
68 self.treeview.customContextMenuRequested.connect(self.contextMenu) | |
69 def setProject(self, project): | |
70 self.project = project | |
71 model = QStandardItemModel() | |
72 root = model.invisibleRootItem() | |
73 pitem = QStandardItem(self.projectIcon, project.name) | |
74 pitem.setEditable(False) | |
75 pitem.setData(project) | |
76 root.appendRow(pitem) | |
67 | 77 for el in self.project.elements: |
78 fitem = QStandardItem(el) | |
64 | 79 pitem.appendRow(fitem) |
80 fitem.setEditable(False) | |
67 | 81 fitem.setData(el) |
64 | 82 self.treeview.setModel(model) |
83 self.treeview.expandAll() | |
84 def contextMenu(self, pos): | |
85 idx = self.treeview.indexAt(pos) | |
86 if not idx.isValid(): | |
87 return | |
88 item = self.treeview.model().itemFromIndex(idx) | |
89 def activate(self, index): | |
90 if not index.isValid(): | |
91 return | |
92 model = self.treeview.model() | |
93 item = model.itemFromIndex(index) | |
94 fn = item.data() | |
95 if type(fn) is str: | |
96 self.sigLoadFile.emit(fn) | |
97 | |
98 class AboutDialog(QDialog): | |
99 def __init__(self, parent=None): | |
100 super(AboutDialog, self).__init__(parent) | |
101 self.setWindowTitle('About') | |
102 l = QVBoxLayout(self) | |
103 txt = QTextEdit(self) | |
104 txt.setReadOnly(True) | |
105 aboutText = """<h1>lcfOS IDE</h1> | |
106 <p>An all-in-one IDE for OS development.</p> | |
107 <p>https://www.assembla.com/spaces/lcfOS/wiki</p> | |
108 <p>Author: Windel Bouwman</p> | |
109 """ | |
110 txt.append(aboutText) | |
111 l.addWidget(txt) | |
112 but = QPushButton('OK') | |
113 but.setDefault(True) | |
114 but.clicked.connect(self.close) | |
115 l.addWidget(but) | |
116 | |
117 class ProjectOptions(QDialog): | |
118 pass | |
119 # TODO: project options in here | |
120 | |
121 class Ide(QMainWindow): | |
122 def __init__(self, parent=None): | |
123 super(Ide, self).__init__(parent) | |
124 self.setWindowTitle('LCFOS IDE') | |
207 | 125 self.setWindowIcon(QIcon('icons/logo.png')) |
64 | 126 |
127 # Create menus: | |
68 | 128 mb = self.menuBar() |
101 | 129 self.fileMenu = mb.addMenu('File') |
68 | 130 self.projectMenu = mb.addMenu('Project') |
131 self.viewMenu = mb.addMenu('View') | |
132 self.helpMenu = mb.addMenu('Help') | |
64 | 133 |
134 # Create mdi area: | |
135 self.mdiArea = QMdiArea() | |
136 self.setCentralWidget(self.mdiArea) | |
137 | |
138 # Create components: | |
68 | 139 def addComponent(name, widget): |
140 dw = QDockWidget(name) | |
141 dw.setWidget(widget) | |
142 dw.setObjectName(name) | |
143 self.addDockWidget(Qt.RightDockWidgetArea, dw) | |
144 self.viewMenu.addAction(dw.toggleViewAction()) | |
145 return widget | |
64 | 146 |
68 | 147 self.buildOutput = addComponent('Build output', BuildOutput()) |
148 self.astViewer = addComponent('AST viewer', AstViewer()) | |
149 self.astViewer.sigNodeSelected.connect(self.nodeSelected) | |
150 self.builderrors = addComponent('Build errors', BuildErrors()) | |
64 | 151 self.builderrors.sigErrorSelected.connect(self.errorSelected) |
68 | 152 self.projectview = addComponent('Project explorer', ProjectView()) |
64 | 153 self.projectview.sigLoadFile.connect(self.loadFile) |
131 | 154 self.devxplr = addComponent('Device explorer', stutil.DeviceExplorer()) |
155 self.regview = addComponent('Registers', stutil.RegisterView()) | |
136 | 156 self.memview = addComponent('Memory', stutil.MemoryView()) |
137 | 157 self.ctrlToolbar = stutil.DebugToolbar() |
158 self.addToolBar(self.ctrlToolbar) | |
159 self.ctrlToolbar.setObjectName('debugToolbar') | |
131 | 160 self.devxplr.deviceSelected.connect(self.regview.mdl.setDevice) |
136 | 161 self.devxplr.deviceSelected.connect(self.memview.setDevice) |
137 | 162 self.devxplr.deviceSelected.connect(self.ctrlToolbar.setDevice) |
143 | 163 self.ctrlToolbar.statusChange.connect(self.regview.refresh) |
64 | 164 |
165 # About dialog: | |
166 self.aboutDialog = AboutDialog() | |
207 | 167 self.aboutDialog.setWindowIcon(QIcon('icons/logo.png')) |
64 | 168 # Create actions: |
68 | 169 def addMenuEntry(name, menu, callback, shortcut=None): |
170 a = QAction(name, self) | |
171 menu.addAction(a) | |
172 a.triggered.connect(callback) | |
173 if shortcut: | |
174 a.setShortcut(shortcut) | |
67 | 175 |
68 | 176 addMenuEntry("New", self.projectMenu, self.newProject) |
177 addMenuEntry("Open", self.projectMenu, self.openProject) | |
178 addMenuEntry("Save", self.projectMenu, self.saveProject) | |
179 addMenuEntry("Close", self.projectMenu, self.closeProject) | |
180 addMenuEntry("Build", self.projectMenu, self.buildProject, shortcut=QKeySequence('F7')) | |
67 | 181 |
101 | 182 addMenuEntry("New", self.fileMenu, self.newFile, shortcut=QKeySequence(QKeySequence.New)) |
183 addMenuEntry("Open", self.fileMenu, self.openFile, shortcut=QKeySequence(QKeySequence.Open)) | |
184 addMenuEntry("Save", self.fileMenu, self.saveFile, shortcut=QKeySequence(QKeySequence.Save)) | |
185 addMenuEntry("Build", self.fileMenu, self.buildFile, shortcut=QKeySequence("F8")) | |
186 | |
64 | 187 self.helpAction = QAction('Help', self) |
188 self.helpAction.setShortcut(QKeySequence('F1')) | |
189 self.helpMenu.addAction(self.helpAction) | |
68 | 190 addMenuEntry('About', self.helpMenu, self.aboutDialog.open) |
64 | 191 |
68 | 192 addMenuEntry('Cascade windows', self.viewMenu, self.mdiArea.cascadeSubWindows) |
193 addMenuEntry('Tile windows', self.viewMenu, self.mdiArea.tileSubWindows) | |
64 | 194 |
163 | 195 sb = self.statusBar() |
196 | |
64 | 197 # Load settings: |
198 self.settings = QSettings('windelsoft', 'lcfoside') | |
199 self.loadSettings() | |
200 | |
152 | 201 ce = self.newFile() |
162 | 202 ce.Source = testc3.testsrc |
152 | 203 |
204 self.diag = ppci.DiagnosticsManager() | |
165 | 205 self.c3front = c3.Builder(self.diag) |
152 | 206 |
64 | 207 # File handling: |
67 | 208 def newProject(self): |
209 filename = QFileDialog.getSaveFileName(self, \ | |
210 "Select new projectfile", "", "lcfos Project files (*.lcp2)") | |
211 if filename: | |
212 self.project = Project() | |
213 self.project.filename = filename | |
214 self.project.save() | |
64 | 215 |
101 | 216 def newFile(self): |
152 | 217 ce = CodeEdit() |
162 | 218 ce.textChanged.connect(self.buildProject) |
219 w = self.mdiArea.addSubWindow(ce) | |
152 | 220 ce.show() |
162 | 221 w.resize(500, 700) |
152 | 222 return ce |
101 | 223 def openFile(self): |
224 filename = QFileDialog.getOpenFileName(self, "Open K# file...", "*.ks", "K# source files (*.ks)") | |
225 if filename: | |
226 self.loadFile(filename) | |
227 def saveFile(self): | |
228 ac = self.activeMdiChild() | |
229 if ac: | |
230 ac.save() | |
231 | |
67 | 232 def saveProject(self): |
233 self.project.save() | |
64 | 234 |
67 | 235 def closeProject(self): |
64 | 236 ac = self.activeMdiChild() |
237 if ac: | |
238 self.mdiArea.removeSubWindow(ac) | |
239 | |
240 def loadFile(self, filename): | |
241 # Find existing mdi widget: | |
242 wid = self.findMdiChild(filename) | |
243 if wid: | |
244 self.mdiArea.setActiveSubWindow(wid.parent()) | |
245 return wid | |
246 | |
247 # Create a new one: | |
248 ce = CodeEdit() | |
101 | 249 |
250 #source = self.project.loadProjectFile(filename) | |
251 try: | |
252 with open(filename) as f: | |
253 source = f.read() | |
254 ce.setSource(source) | |
255 except Exception as e: | |
256 print('exception opening file', e) | |
64 | 257 self.mdiArea.addSubWindow(ce) |
101 | 258 ce.setFileName(filename) |
64 | 259 ce.show() |
260 return ce | |
261 | |
67 | 262 def loadProject(self, filename): |
263 self.project = Project(filename) | |
264 self.projectview.setProject(self.project) | |
265 | |
266 def openProject(self): | |
267 filename = QFileDialog.getOpenFileName(self, \ | |
268 "Choose project file", "", "lcfos Project files (*.lcp2)") | |
269 if filename: | |
270 self.loadProject(filename) | |
271 | |
64 | 272 # MDI: |
273 def activeMdiChild(self): | |
274 aw = self.mdiArea.activeSubWindow() | |
275 if aw: | |
276 return aw.widget() | |
277 else: | |
278 return None | |
279 | |
280 def findMdiChild(self, filename): | |
281 for window in self.mdiArea.subWindowList(): | |
282 wid = window.widget() | |
283 if wid.filename == filename: | |
284 return wid | |
285 return None | |
286 | |
287 def allChildren(self): | |
288 c = [] | |
289 for window in self.mdiArea.subWindowList(): | |
290 wid = window.widget() | |
291 c.append(wid) | |
292 return c | |
293 | |
294 # Settings: | |
295 def loadSettings(self): | |
296 if self.settings.contains('mainwindowstate'): | |
297 self.restoreState(self.settings.value('mainwindowstate')) | |
298 if self.settings.contains('mainwindowgeometry'): | |
299 self.restoreGeometry(self.settings.value('mainwindowgeometry')) | |
101 | 300 if self.settings.contains('lastfile'): |
301 self.loadFile(self.settings.value('lastfile')) | |
64 | 302 |
303 def closeEvent(self, ev): | |
304 self.settings.setValue('mainwindowstate', self.saveState()) | |
305 self.settings.setValue('mainwindowgeometry', self.saveGeometry()) | |
101 | 306 #if self.project: |
307 # self.settings.setValue('openedproject', self.project.filename) | |
308 # # TODO: ask for save of opened files | |
309 ac = self.activeMdiChild() | |
310 if ac: | |
162 | 311 if hasattr(ac, 'filename') and ac.filename: |
101 | 312 self.settings.setValue('lastfile', ac.filename) |
152 | 313 else: |
314 self.settings.remove('lastfile') | |
315 else: | |
316 self.settings.remove('lastfile') | |
64 | 317 ev.accept() |
318 | |
319 # Error handling: | |
320 def nodeSelected(self, node): | |
321 ce = self.activeMdiChild() | |
322 if not ce: | |
323 return | |
165 | 324 if node.loc: |
325 row, col = node.loc.row, node.loc.col | |
326 ce.setRowCol( row, col ) | |
168 | 327 ce.setFocus() |
64 | 328 else: |
329 ce.clearErrors() | |
330 | |
331 def errorSelected(self, err): | |
332 ce = self.activeMdiChild() | |
333 if not ce: | |
334 return | |
163 | 335 ce.setRowCol(err.loc.row, err.loc.col) |
167 | 336 ce.setFocus() |
64 | 337 |
338 # Project loading: | |
339 | |
340 # Build recepy: | |
101 | 341 def buildFile(self): |
342 ce = self.activeMdiChild() | |
343 if ce: | |
162 | 344 source = ce.Source |
101 | 345 self.buildOutput.clear() |
165 | 346 self.buildOutput.append('Starting build') |
347 ir = self.c3front.build(source) | |
348 ast = self.c3front.pkg | |
102 | 349 self.astViewer.setAst(ast) |
101 | 350 self.buildOutput.append("Done!") |
67 | 351 def buildProject(self): |
64 | 352 """ Build project """ |
353 self.buildOutput.clear() | |
152 | 354 self.diag.diags.clear() |
355 self.buildFile() | |
64 | 356 |
152 | 357 self.builderrors.setErrorList(self.diag.diags) |
358 for err in self.diag.diags: | |
359 self.buildOutput.append(str(err)) | |
154 | 360 ce = self.activeMdiChild() |
361 if ce: | |
362 ce.setErrors(self.diag.diags) | |
64 | 363 self.buildOutput.append("Done!") |
364 | |
1 | 365 if __name__ == '__main__': |
366 app = QApplication(sys.argv) | |
367 ide = Ide() | |
368 ide.show() | |
369 app.exec_() | |
370 |