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