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