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