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