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