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