Mercurial > lcfOS
annotate python/ide.py @ 289:bd2593de3ff8
Semifix burn2
author | Windel Bouwman |
---|---|
date | Thu, 21 Nov 2013 15:46:50 +0100 |
parents | a747a45dcd78 |
children |
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 |
249 | 293 self.diag.clear() |
294 outs = outstream.TextOutputStream() | |
288 | 295 if not zcc.zcc([io.StringIO(ce.Source)], [], outs, self.diag): |
249 | 296 # Set errors: |
297 self.builderrors.setErrorList(self.diag.diags) | |
298 ce.setErrors(self.diag.diags) | |
299 return | |
300 | |
256 | 301 def buildFileAndFlash(self): |
302 ce = self.activeMdiChild() | |
303 if not ce: | |
304 return | |
279 | 305 fn = ce.FileName |
256 | 306 self.diag.clear() |
307 outs = outstream.TextOutputStream() | |
289 | 308 imps = [open(ce.FileName, 'r') for ce in self.allChildren() if ce.FileName and ce.FileName != fn] |
309 if not zcc.zcc([open(fn, 'r')], imps, outs, self.diag): | |
256 | 310 # Set errors: |
311 self.builderrors.setErrorList(self.diag.diags) | |
312 ce.setErrors(self.diag.diags) | |
313 return | |
314 | |
279 | 315 outs.dump() |
249 | 316 code_s = outs.getSection('code') |
282 | 317 self.disasm.dm.setInstructions(code_s.instructions) |
249 | 318 self.debugInfo = code_s.debugInfos() |
250 | 319 if self.ctrlToolbar.device: |
255 | 320 logging.info('Flashing stm32f4 discovery') |
250 | 321 bts = code_s.to_bytes() |
322 self.ctrlToolbar.device.writeFlash(0x08000000, bts) | |
323 stl = self.ctrlToolbar.device.iface | |
324 stl.reset() | |
325 stl.halt() | |
256 | 326 stl.run() |
259 | 327 logging.info('Done!') |
328 else: | |
329 self.logger.warning('Not connected to device, skipping flash') | |
249 | 330 |
256 | 331 |
1 | 332 if __name__ == '__main__': |
258 | 333 logging.basicConfig(format=zcc.logformat, level=logging.DEBUG) |
240 | 334 app = QApplication(sys.argv) |
335 ide = Ide() | |
336 ide.show() | |
259 | 337 ide.logger.info('IDE started') |
240 | 338 app.exec_() |