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