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