Mercurial > lcfOS
annotate python/ide.py @ 288:a747a45dcd78
Various styling work
author | Windel Bouwman |
---|---|
date | Thu, 21 Nov 2013 14:26:13 +0100 |
parents | 1c7c1e619be8 |
children | bd2593de3ff8 |
rev | line source |
---|---|
99 | 1 #!/usr/bin/python |
2 | |
248 | 3 import sys |
4 import os | |
255 | 5 import logging |
287 | 6 import traceback |
288 | 7 import io |
64 | 8 |
9 from PyQt4.QtCore import * | |
10 from PyQt4.QtGui import * | |
1 | 11 |
12 # Compiler imports: | |
106 | 13 import ppci |
100 | 14 from astviewer import AstViewer |
162 | 15 from codeedit import CodeEdit |
281 | 16 from logview import LogView as BuildOutput |
287 | 17 from disasm import Disassembly |
131 | 18 stutil = __import__('st-util') |
165 | 19 import c3 |
249 | 20 import zcc |
21 import outstream | |
64 | 22 |
259 | 23 |
282 | 24 def handle_exception(tp, v, tb): |
25 logging.critical(str(v)) | |
26 tb = traceback.format_tb(tb) | |
27 for i in tb: | |
28 logging.critical(i.strip()) | |
29 | |
30 sys.excepthook = handle_exception | |
31 | |
167 | 32 class BuildErrors(QTreeView): |
248 | 33 sigErrorSelected = pyqtSignal(object) |
259 | 34 |
248 | 35 def __init__(self, parent=None): |
36 super(BuildErrors, self).__init__(parent) | |
37 model = QStandardItemModel() | |
38 self.setModel(model) | |
39 self.clicked.connect(self.itemSelected) | |
40 self.errorIcon = QIcon('icons/error.png') | |
41 self.model = QStandardItemModel() | |
42 self.model.setHorizontalHeaderLabels(['Message', 'Row', 'Column']) | |
281 | 43 self.header().setStretchLastSection(True) |
248 | 44 self.setModel(self.model) |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
45 |
248 | 46 def setErrorList(self, errorlist): |
259 | 47 c = self.model.rowCount() |
48 self.model.removeRows(0, c) | |
49 for e in errorlist: | |
50 item = QStandardItem(self.errorIcon, str(e.msg)) | |
51 item.setData(e) | |
277 | 52 row = str(e.loc.row) if e.loc else '' |
53 irow = QStandardItem(row) | |
259 | 54 irow.setData(e) |
277 | 55 col = str(e.loc.col) if e.loc else '' |
56 icol = QStandardItem(col) | |
259 | 57 icol.setData(e) |
58 self.model.appendRow([item, irow, icol]) | |
281 | 59 for i in range(3): |
60 self.resizeColumnToContents(i) | |
248 | 61 |
62 def itemSelected(self, index): | |
256 | 63 if not index.isValid(): |
64 return | |
65 item = self.model.itemFromIndex(index) | |
66 err = item.data() | |
67 self.sigErrorSelected.emit(err) | |
64 | 68 |
69 | |
282 | 70 |
64 | 71 class AboutDialog(QDialog): |
248 | 72 def __init__(self, parent=None): |
73 super(AboutDialog, self).__init__(parent) | |
74 self.setWindowTitle('About') | |
75 l = QVBoxLayout(self) | |
76 txt = QTextEdit(self) | |
77 txt.setReadOnly(True) | |
282 | 78 with open(os.path.join('..', 'readme.rst'), 'r') as f: |
79 aboutText = f.read() | |
248 | 80 txt.append(aboutText) |
81 l.addWidget(txt) | |
82 but = QPushButton('OK') | |
83 but.setDefault(True) | |
84 but.clicked.connect(self.close) | |
85 l.addWidget(but) | |
86 | |
64 | 87 |
88 class Ide(QMainWindow): | |
248 | 89 def __init__(self, parent=None): |
90 super(Ide, self).__init__(parent) | |
256 | 91 self.to_open_files = [] |
259 | 92 self.logger = logging.getLogger('ide') |
255 | 93 |
248 | 94 self.setWindowTitle('LCFOS IDE') |
95 icon = QIcon('icons/logo.png') | |
96 self.setWindowIcon(icon) | |
64 | 97 |
248 | 98 # Create menus: |
99 mb = self.menuBar() | |
100 self.fileMenu = mb.addMenu('File') | |
101 self.viewMenu = mb.addMenu('View') | |
102 self.helpMenu = mb.addMenu('Help') | |
64 | 103 |
248 | 104 # Create mdi area: |
105 self.mdiArea = QMdiArea() | |
106 self.mdiArea.setViewMode(QMdiArea.TabbedView) | |
249 | 107 self.mdiArea.setTabsClosable(True) |
256 | 108 self.mdiArea.setTabsMovable(True) |
248 | 109 self.setCentralWidget(self.mdiArea) |
64 | 110 |
248 | 111 # Create components: |
112 def addComponent(name, widget): | |
259 | 113 dw = QDockWidget(name) |
114 dw.setWidget(widget) | |
115 dw.setObjectName(name) | |
116 self.addDockWidget(Qt.RightDockWidgetArea, dw) | |
117 self.viewMenu.addAction(dw.toggleViewAction()) | |
118 return widget | |
64 | 119 |
248 | 120 self.buildOutput = addComponent('Build output', BuildOutput()) |
121 self.astViewer = addComponent('AST viewer', AstViewer()) | |
255 | 122 self.astViewer.sigNodeSelected.connect(lambda node: self.showLoc(node.loc)) |
248 | 123 self.builderrors = addComponent('Build errors', BuildErrors()) |
255 | 124 self.builderrors.sigErrorSelected.connect(lambda err: self.showLoc(err.loc)) |
248 | 125 self.devxplr = addComponent('Device explorer', stutil.DeviceExplorer()) |
126 self.regview = addComponent('Registers', stutil.RegisterView()) | |
127 self.memview = addComponent('Memory', stutil.MemoryView()) | |
282 | 128 self.disasm = addComponent('Disasm', Disassembly()) |
248 | 129 self.ctrlToolbar = stutil.DebugToolbar() |
130 self.addToolBar(self.ctrlToolbar) | |
131 self.ctrlToolbar.setObjectName('debugToolbar') | |
132 self.devxplr.deviceSelected.connect(self.regview.mdl.setDevice) | |
133 self.ctrlToolbar.statusChange.connect(self.memview.refresh) | |
134 self.devxplr.deviceSelected.connect(self.memview.setDevice) | |
135 self.devxplr.deviceSelected.connect(self.ctrlToolbar.setDevice) | |
136 self.ctrlToolbar.statusChange.connect(self.regview.refresh) | |
137 self.ctrlToolbar.codePosition.connect(self.pointCode) | |
64 | 138 |
248 | 139 # About dialog: |
140 self.aboutDialog = AboutDialog() | |
141 self.aboutDialog.setWindowIcon(icon) | |
259 | 142 |
248 | 143 # Create actions: |
144 def addMenuEntry(name, menu, callback, shortcut=None): | |
259 | 145 a = QAction(name, self) |
146 menu.addAction(a) | |
147 a.triggered.connect(callback) | |
148 if shortcut: | |
149 a.setShortcut(shortcut) | |
163 | 150 |
248 | 151 addMenuEntry("New", self.fileMenu, self.newFile, shortcut=QKeySequence(QKeySequence.New)) |
152 addMenuEntry("Open", self.fileMenu, self.openFile, shortcut=QKeySequence(QKeySequence.Open)) | |
153 addMenuEntry("Save", self.fileMenu, self.saveFile, shortcut=QKeySequence(QKeySequence.Save)) | |
154 addMenuEntry("Build", self.fileMenu, self.buildFile, shortcut=QKeySequence("F7")) | |
256 | 155 addMenuEntry("Build and flash", self.fileMenu, self.buildFileAndFlash, shortcut=QKeySequence("F8")) |
152 | 156 |
259 | 157 self.helpAction = QAction('Help', self) |
248 | 158 self.helpAction.setShortcut(QKeySequence('F1')) |
159 self.helpMenu.addAction(self.helpAction) | |
160 addMenuEntry('About', self.helpMenu, self.aboutDialog.open) | |
161 | |
162 addMenuEntry('Cascade windows', self.viewMenu, self.mdiArea.cascadeSubWindows) | |
163 addMenuEntry('Tile windows', self.viewMenu, self.mdiArea.tileSubWindows) | |
164 sb = self.statusBar() | |
247 | 165 |
248 | 166 # Load settings: |
167 self.settings = QSettings('windelsoft', 'lcfoside') | |
168 self.loadSettings() | |
169 self.diag = ppci.DiagnosticsManager() | |
170 self.c3front = c3.Builder(self.diag) | |
171 | |
172 # File handling: | |
173 def newFile(self): | |
174 self.newCodeEdit() | |
247 | 175 |
248 | 176 def openFile(self): |
177 filename = QFileDialog.getOpenFileName(self, "Open C3 file...", "*.c3", | |
259 | 178 "C3 source files (*.c3)") |
248 | 179 if filename: |
180 self.loadFile(filename) | |
64 | 181 |
248 | 182 def saveFile(self): |
183 ac = self.activeMdiChild() | |
184 if ac: | |
185 ac.save() | |
101 | 186 |
248 | 187 def loadFile(self, filename): |
188 ce = self.newCodeEdit() | |
189 try: | |
190 with open(filename) as f: | |
191 ce.Source = f.read() | |
192 ce.FileName = filename | |
193 except Exception as e: | |
194 print('exception opening file', e) | |
64 | 195 |
248 | 196 # MDI: |
197 def newCodeEdit(self): | |
198 ce = CodeEdit() | |
249 | 199 ce.textChanged.connect(self.parseFile) |
248 | 200 w = self.mdiArea.addSubWindow(ce) |
249 | 201 self.mdiArea.setActiveSubWindow(w) |
202 ce.showMaximized() | |
248 | 203 return ce |
204 | |
205 def activeMdiChild(self): | |
206 aw = self.mdiArea.activeSubWindow() | |
207 if aw: | |
208 return aw.widget() | |
209 | |
210 def findMdiChild(self, filename): | |
211 for wid in self.allChildren(): | |
212 if wid.filename == filename: | |
213 return wid | |
64 | 214 |
248 | 215 def allChildren(self): |
216 return [w.widget() for w in self.mdiArea.subWindowList()] | |
217 | |
218 # Settings: | |
219 def loadSettings(self): | |
220 if self.settings.contains('mainwindowstate'): | |
221 self.restoreState(self.settings.value('mainwindowstate')) | |
222 if self.settings.contains('mainwindowgeometry'): | |
223 self.restoreGeometry(self.settings.value('mainwindowgeometry')) | |
224 if self.settings.contains('lastfiles'): | |
225 lfs = self.settings.value('lastfiles') | |
277 | 226 if lfs: |
227 self.to_open_files.extend(lfs) | |
256 | 228 |
229 def showEvent(self, ev): | |
230 super().showEvent(ev) | |
231 while self.to_open_files: | |
232 fn = self.to_open_files.pop(0) | |
233 self.loadFile(fn) | |
64 | 234 |
248 | 235 def closeEvent(self, ev): |
236 self.settings.setValue('mainwindowstate', self.saveState()) | |
237 self.settings.setValue('mainwindowgeometry', self.saveGeometry()) | |
238 ac = self.activeMdiChild() | |
239 lfs = [ce.FileName for ce in self.allChildren() if ce.FileName] | |
240 self.settings.setValue('lastfiles', lfs) | |
241 ev.accept() | |
242 | |
243 # Error handling: | |
244 def nodeSelected(self, node): | |
245 self.showLoc(node.loc) | |
246 | |
247 def showLoc(self, loc): | |
248 ce = self.activeMdiChild() | |
249 if not ce: | |
250 return | |
251 if loc: | |
252 ce.setRowCol(loc.row, loc.col) | |
253 ce.setFocus() | |
64 | 254 |
248 | 255 def pointCode(self, p): |
249 | 256 # Lookup pc in debug infos: |
257 loc = None | |
282 | 258 print(p) |
259 self.disasm.showPos(p) | |
249 | 260 if hasattr(self, 'debugInfo'): |
261 for di in self.debugInfo: | |
262 if di.address > p: | |
263 loc = di.info | |
264 break | |
265 if loc: | |
266 ce = self.activeMdiChild() | |
267 if ce: | |
268 ce.ic.arrow = loc | |
269 self.showLoc(loc) | |
247 | 270 |
248 | 271 # Build recepy: |
249 | 272 def parseFile(self): |
282 | 273 self.logger.info('Parsing!') |
248 | 274 ce = self.activeMdiChild() |
275 if not ce: | |
276 return | |
277 self.diag.clear() | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
250
diff
changeset
|
278 pkg = self.c3front.parse(ce.Source) |
64 | 279 |
248 | 280 # Set errors: |
281 self.builderrors.setErrorList(self.diag.diags) | |
282 ce.setErrors(self.diag.diags) | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
250
diff
changeset
|
283 self.astViewer.setAst(pkg) |
282 | 284 if pkg: |
285 c3.AstPrinter().printAst(pkg) | |
259 | 286 self.logger.info('Done!') |
64 | 287 |
249 | 288 def buildFile(self): |
289 ce = self.activeMdiChild() | |
290 if not ce: | |
291 return | |
279 | 292 fn = ce.FileName |
293 wd = os.path.dirname(fn) | |
249 | 294 self.diag.clear() |
295 outs = outstream.TextOutputStream() | |
288 | 296 if not zcc.zcc([io.StringIO(ce.Source)], [], outs, self.diag): |
249 | 297 # Set errors: |
298 self.builderrors.setErrorList(self.diag.diags) | |
299 ce.setErrors(self.diag.diags) | |
300 return | |
301 | |
256 | 302 def buildFileAndFlash(self): |
303 ce = self.activeMdiChild() | |
304 if not ce: | |
305 return | |
279 | 306 fn = ce.FileName |
307 wd = os.path.dirname(fn) | |
256 | 308 self.diag.clear() |
309 outs = outstream.TextOutputStream() | |
288 | 310 if not zcc.zcc([io.StringIO(ce.Source)], [], outs, self.diag): |
256 | 311 # Set errors: |
312 self.builderrors.setErrorList(self.diag.diags) | |
313 ce.setErrors(self.diag.diags) | |
314 return | |
315 | |
279 | 316 outs.dump() |
249 | 317 code_s = outs.getSection('code') |
282 | 318 self.disasm.dm.setInstructions(code_s.instructions) |
249 | 319 self.debugInfo = code_s.debugInfos() |
250 | 320 if self.ctrlToolbar.device: |
255 | 321 logging.info('Flashing stm32f4 discovery') |
250 | 322 bts = code_s.to_bytes() |
323 self.ctrlToolbar.device.writeFlash(0x08000000, bts) | |
324 stl = self.ctrlToolbar.device.iface | |
325 stl.reset() | |
326 stl.halt() | |
256 | 327 stl.run() |
259 | 328 logging.info('Done!') |
329 else: | |
330 self.logger.warning('Not connected to device, skipping flash') | |
249 | 331 |
256 | 332 |
1 | 333 if __name__ == '__main__': |
258 | 334 logging.basicConfig(format=zcc.logformat, level=logging.DEBUG) |
240 | 335 app = QApplication(sys.argv) |
336 ide = Ide() | |
337 ide.show() | |
259 | 338 ide.logger.info('IDE started') |
240 | 339 app.exec_() |