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