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