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