99
|
1 #!/usr/bin/python
|
|
2
|
69
|
3 import sys, os, base64
|
131
|
4 assert sys.version_info.major == 3, "Needs to be run in python version 3.x"
|
64
|
5
|
|
6 from PyQt4.QtCore import *
|
|
7 from PyQt4.QtGui import *
|
1
|
8
|
|
9 # Compiler imports:
|
64
|
10 from project import Project
|
106
|
11 import ppci
|
100
|
12 from astviewer import AstViewer
|
162
|
13 #from codeeditor import CodeEdit
|
|
14 from codeedit import CodeEdit
|
131
|
15 stutil = __import__('st-util')
|
152
|
16 import testc3
|
165
|
17 import c3
|
64
|
18
|
|
19 lcfospng = base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A\n/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9sJEhMKBk7B678AAAA/SURBVFjD\n7dbBCQAgDATBi9h/y7EFA4Kf2QLCwH1S6XQu6sqoujublc8BAAAAAAAAAAB8B+zXT6YJAAAAAKYd\nWSgFQNUyijIAAAAASUVORK5CYII=\n')
|
|
20
|
|
21 class BuildOutput(QTextEdit):
|
|
22 """ Build output component """
|
|
23 def __init__(self, parent=None):
|
|
24 super(BuildOutput, self).__init__(parent)
|
|
25 self.setCurrentFont(QFont('Courier'))
|
|
26 self.setReadOnly(True)
|
|
27 self.append('Build output will appear here!')
|
|
28
|
|
29 class BuildErrors(QListView):
|
|
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)
|
154
|
36 self.errorIcon = QIcon('error.png')
|
64
|
37 def setErrorList(self, errorlist):
|
|
38 model = QStandardItemModel()
|
|
39 for e in errorlist:
|
154
|
40 item = QStandardItem(self.errorIcon, str(e))
|
64
|
41 item.setData(e)
|
|
42 model.appendRow(item)
|
|
43 self.setModel(model)
|
|
44 def itemSelected(self, index):
|
|
45 if not index.isValid():
|
|
46 return
|
|
47 model = self.model()
|
|
48 item = model.itemFromIndex(index)
|
|
49 err = item.data()
|
|
50 self.sigErrorSelected.emit(err)
|
|
51
|
|
52 class ProjectView(QWidget):
|
|
53 sigLoadFile = pyqtSignal(str)
|
|
54 def __init__(self, parent=None):
|
|
55 super(ProjectView, self).__init__(parent)
|
|
56 self.treeview = QTreeView(self)
|
|
57 self.treeview.setContextMenuPolicy(Qt.CustomContextMenu)
|
|
58 l = QVBoxLayout(self)
|
|
59 l.addWidget(self.treeview)
|
|
60 pm = QPixmap()
|
|
61 pm.loadFromData(lcfospng)
|
|
62 self.projectIcon = QIcon(pm)
|
|
63 # Connect signals:
|
|
64 self.treeview.activated.connect(self.activate)
|
|
65 self.treeview.customContextMenuRequested.connect(self.contextMenu)
|
|
66 def setProject(self, project):
|
|
67 self.project = project
|
|
68 model = QStandardItemModel()
|
|
69 root = model.invisibleRootItem()
|
|
70 pitem = QStandardItem(self.projectIcon, project.name)
|
|
71 pitem.setEditable(False)
|
|
72 pitem.setData(project)
|
|
73 root.appendRow(pitem)
|
67
|
74 for el in self.project.elements:
|
|
75 fitem = QStandardItem(el)
|
64
|
76 pitem.appendRow(fitem)
|
|
77 fitem.setEditable(False)
|
67
|
78 fitem.setData(el)
|
64
|
79 self.treeview.setModel(model)
|
|
80 self.treeview.expandAll()
|
|
81 def contextMenu(self, pos):
|
|
82 idx = self.treeview.indexAt(pos)
|
|
83 if not idx.isValid():
|
|
84 return
|
|
85 item = self.treeview.model().itemFromIndex(idx)
|
|
86 def activate(self, index):
|
|
87 if not index.isValid():
|
|
88 return
|
|
89 model = self.treeview.model()
|
|
90 item = model.itemFromIndex(index)
|
|
91 fn = item.data()
|
|
92 if type(fn) is str:
|
|
93 self.sigLoadFile.emit(fn)
|
|
94
|
|
95 class AboutDialog(QDialog):
|
|
96 def __init__(self, parent=None):
|
|
97 super(AboutDialog, self).__init__(parent)
|
|
98 self.setWindowTitle('About')
|
|
99 l = QVBoxLayout(self)
|
|
100 txt = QTextEdit(self)
|
|
101 txt.setReadOnly(True)
|
|
102 aboutText = """<h1>lcfOS IDE</h1>
|
|
103 <p>An all-in-one IDE for OS development.</p>
|
|
104 <p>https://www.assembla.com/spaces/lcfOS/wiki</p>
|
|
105 <p>Author: Windel Bouwman</p>
|
|
106 """
|
|
107 txt.append(aboutText)
|
|
108 l.addWidget(txt)
|
|
109 but = QPushButton('OK')
|
|
110 but.setDefault(True)
|
|
111 but.clicked.connect(self.close)
|
|
112 l.addWidget(but)
|
|
113
|
|
114 class ProjectOptions(QDialog):
|
|
115 pass
|
|
116 # TODO: project options in here
|
|
117
|
|
118 class Ide(QMainWindow):
|
|
119 def __init__(self, parent=None):
|
|
120 super(Ide, self).__init__(parent)
|
|
121 self.setWindowTitle('LCFOS IDE')
|
|
122 icon = QPixmap()
|
|
123 icon.loadFromData(lcfospng)
|
|
124 self.setWindowIcon(QIcon(icon))
|
|
125
|
|
126 # Create menus:
|
68
|
127 mb = self.menuBar()
|
101
|
128 self.fileMenu = mb.addMenu('File')
|
68
|
129 self.projectMenu = mb.addMenu('Project')
|
|
130 self.viewMenu = mb.addMenu('View')
|
|
131 self.helpMenu = mb.addMenu('Help')
|
64
|
132
|
|
133 # Create mdi area:
|
|
134 self.mdiArea = QMdiArea()
|
|
135 self.setCentralWidget(self.mdiArea)
|
|
136
|
|
137 # Create components:
|
68
|
138 def addComponent(name, widget):
|
|
139 dw = QDockWidget(name)
|
|
140 dw.setWidget(widget)
|
|
141 dw.setObjectName(name)
|
|
142 self.addDockWidget(Qt.RightDockWidgetArea, dw)
|
|
143 self.viewMenu.addAction(dw.toggleViewAction())
|
|
144 return widget
|
64
|
145
|
68
|
146 self.buildOutput = addComponent('Build output', BuildOutput())
|
|
147 self.astViewer = addComponent('AST viewer', AstViewer())
|
|
148 self.astViewer.sigNodeSelected.connect(self.nodeSelected)
|
|
149 self.builderrors = addComponent('Build errors', BuildErrors())
|
64
|
150 self.builderrors.sigErrorSelected.connect(self.errorSelected)
|
68
|
151 self.projectview = addComponent('Project explorer', ProjectView())
|
64
|
152 self.projectview.sigLoadFile.connect(self.loadFile)
|
131
|
153 self.devxplr = addComponent('Device explorer', stutil.DeviceExplorer())
|
|
154 self.regview = addComponent('Registers', stutil.RegisterView())
|
136
|
155 self.memview = addComponent('Memory', stutil.MemoryView())
|
137
|
156 self.ctrlToolbar = stutil.DebugToolbar()
|
|
157 self.addToolBar(self.ctrlToolbar)
|
|
158 self.ctrlToolbar.setObjectName('debugToolbar')
|
131
|
159 self.devxplr.deviceSelected.connect(self.regview.mdl.setDevice)
|
136
|
160 self.devxplr.deviceSelected.connect(self.memview.setDevice)
|
137
|
161 self.devxplr.deviceSelected.connect(self.ctrlToolbar.setDevice)
|
143
|
162 self.ctrlToolbar.statusChange.connect(self.regview.refresh)
|
64
|
163
|
|
164 # About dialog:
|
|
165 self.aboutDialog = AboutDialog()
|
|
166 self.aboutDialog.setWindowIcon(QIcon(icon))
|
|
167 # Create actions:
|
68
|
168 def addMenuEntry(name, menu, callback, shortcut=None):
|
|
169 a = QAction(name, self)
|
|
170 menu.addAction(a)
|
|
171 a.triggered.connect(callback)
|
|
172 if shortcut:
|
|
173 a.setShortcut(shortcut)
|
67
|
174
|
68
|
175 addMenuEntry("New", self.projectMenu, self.newProject)
|
|
176 addMenuEntry("Open", self.projectMenu, self.openProject)
|
|
177 addMenuEntry("Save", self.projectMenu, self.saveProject)
|
|
178 addMenuEntry("Close", self.projectMenu, self.closeProject)
|
|
179 addMenuEntry("Build", self.projectMenu, self.buildProject, shortcut=QKeySequence('F7'))
|
67
|
180
|
101
|
181 addMenuEntry("New", self.fileMenu, self.newFile, shortcut=QKeySequence(QKeySequence.New))
|
|
182 addMenuEntry("Open", self.fileMenu, self.openFile, shortcut=QKeySequence(QKeySequence.Open))
|
|
183 addMenuEntry("Save", self.fileMenu, self.saveFile, shortcut=QKeySequence(QKeySequence.Save))
|
|
184 addMenuEntry("Build", self.fileMenu, self.buildFile, shortcut=QKeySequence("F8"))
|
|
185
|
64
|
186 self.helpAction = QAction('Help', self)
|
|
187 self.helpAction.setShortcut(QKeySequence('F1'))
|
|
188 self.helpMenu.addAction(self.helpAction)
|
68
|
189 addMenuEntry('About', self.helpMenu, self.aboutDialog.open)
|
64
|
190
|
68
|
191 addMenuEntry('Cascade windows', self.viewMenu, self.mdiArea.cascadeSubWindows)
|
|
192 addMenuEntry('Tile windows', self.viewMenu, self.mdiArea.tileSubWindows)
|
64
|
193
|
163
|
194 sb = self.statusBar()
|
|
195
|
64
|
196 # Load settings:
|
|
197 self.settings = QSettings('windelsoft', 'lcfoside')
|
|
198 self.loadSettings()
|
|
199
|
152
|
200 ce = self.newFile()
|
162
|
201 ce.Source = testc3.testsrc
|
152
|
202
|
|
203 self.diag = ppci.DiagnosticsManager()
|
165
|
204 self.c3front = c3.Builder(self.diag)
|
152
|
205
|
64
|
206 # File handling:
|
67
|
207 def newProject(self):
|
|
208 filename = QFileDialog.getSaveFileName(self, \
|
|
209 "Select new projectfile", "", "lcfos Project files (*.lcp2)")
|
|
210 if filename:
|
|
211 self.project = Project()
|
|
212 self.project.filename = filename
|
|
213 self.project.save()
|
64
|
214
|
101
|
215 def newFile(self):
|
152
|
216 ce = CodeEdit()
|
162
|
217 ce.textChanged.connect(self.buildProject)
|
|
218 w = self.mdiArea.addSubWindow(ce)
|
152
|
219 ce.show()
|
162
|
220 w.resize(500, 700)
|
152
|
221 return ce
|
101
|
222 def openFile(self):
|
|
223 filename = QFileDialog.getOpenFileName(self, "Open K# file...", "*.ks", "K# source files (*.ks)")
|
|
224 if filename:
|
|
225 self.loadFile(filename)
|
|
226 def saveFile(self):
|
|
227 ac = self.activeMdiChild()
|
|
228 if ac:
|
|
229 ac.save()
|
|
230
|
67
|
231 def saveProject(self):
|
|
232 self.project.save()
|
64
|
233
|
67
|
234 def closeProject(self):
|
64
|
235 ac = self.activeMdiChild()
|
|
236 if ac:
|
|
237 self.mdiArea.removeSubWindow(ac)
|
|
238
|
|
239 def loadFile(self, filename):
|
|
240 # Find existing mdi widget:
|
|
241 wid = self.findMdiChild(filename)
|
|
242 if wid:
|
|
243 self.mdiArea.setActiveSubWindow(wid.parent())
|
|
244 return wid
|
|
245
|
|
246 # Create a new one:
|
|
247 ce = CodeEdit()
|
101
|
248
|
|
249 #source = self.project.loadProjectFile(filename)
|
|
250 try:
|
|
251 with open(filename) as f:
|
|
252 source = f.read()
|
|
253 ce.setSource(source)
|
|
254 except Exception as e:
|
|
255 print('exception opening file', e)
|
64
|
256 self.mdiArea.addSubWindow(ce)
|
101
|
257 ce.setFileName(filename)
|
64
|
258 ce.show()
|
|
259 return ce
|
|
260
|
67
|
261 def loadProject(self, filename):
|
|
262 self.project = Project(filename)
|
|
263 self.projectview.setProject(self.project)
|
|
264
|
|
265 def openProject(self):
|
|
266 filename = QFileDialog.getOpenFileName(self, \
|
|
267 "Choose project file", "", "lcfos Project files (*.lcp2)")
|
|
268 if filename:
|
|
269 self.loadProject(filename)
|
|
270
|
64
|
271 # MDI:
|
|
272 def activeMdiChild(self):
|
|
273 aw = self.mdiArea.activeSubWindow()
|
|
274 if aw:
|
|
275 return aw.widget()
|
|
276 else:
|
|
277 return None
|
|
278
|
|
279 def findMdiChild(self, filename):
|
|
280 for window in self.mdiArea.subWindowList():
|
|
281 wid = window.widget()
|
|
282 if wid.filename == filename:
|
|
283 return wid
|
|
284 return None
|
|
285
|
|
286 def allChildren(self):
|
|
287 c = []
|
|
288 for window in self.mdiArea.subWindowList():
|
|
289 wid = window.widget()
|
|
290 c.append(wid)
|
|
291 return c
|
|
292
|
|
293 # Settings:
|
|
294 def loadSettings(self):
|
|
295 if self.settings.contains('mainwindowstate'):
|
|
296 self.restoreState(self.settings.value('mainwindowstate'))
|
|
297 if self.settings.contains('mainwindowgeometry'):
|
|
298 self.restoreGeometry(self.settings.value('mainwindowgeometry'))
|
101
|
299 if self.settings.contains('lastfile'):
|
|
300 self.loadFile(self.settings.value('lastfile'))
|
64
|
301
|
|
302 def closeEvent(self, ev):
|
|
303 self.settings.setValue('mainwindowstate', self.saveState())
|
|
304 self.settings.setValue('mainwindowgeometry', self.saveGeometry())
|
101
|
305 #if self.project:
|
|
306 # self.settings.setValue('openedproject', self.project.filename)
|
|
307 # # TODO: ask for save of opened files
|
|
308 ac = self.activeMdiChild()
|
|
309 if ac:
|
162
|
310 if hasattr(ac, 'filename') and ac.filename:
|
101
|
311 self.settings.setValue('lastfile', ac.filename)
|
152
|
312 else:
|
|
313 self.settings.remove('lastfile')
|
|
314 else:
|
|
315 self.settings.remove('lastfile')
|
64
|
316 ev.accept()
|
|
317
|
|
318 # Error handling:
|
|
319 def nodeSelected(self, node):
|
|
320 ce = self.activeMdiChild()
|
|
321 if not ce:
|
|
322 return
|
165
|
323 if node.loc:
|
|
324 row, col = node.loc.row, node.loc.col
|
|
325 ce.setRowCol( row, col )
|
64
|
326 else:
|
|
327 ce.clearErrors()
|
|
328
|
|
329 def errorSelected(self, err):
|
|
330 ce = self.activeMdiChild()
|
|
331 if not ce:
|
|
332 return
|
163
|
333 ce.setRowCol(err.loc.row, err.loc.col)
|
64
|
334
|
|
335 # Project loading:
|
|
336
|
|
337 # Build recepy:
|
101
|
338 def buildFile(self):
|
|
339 ce = self.activeMdiChild()
|
152
|
340 print('BUILD file')
|
101
|
341 if ce:
|
162
|
342 source = ce.Source
|
101
|
343 self.buildOutput.clear()
|
165
|
344 self.buildOutput.append('Starting build')
|
|
345 ir = self.c3front.build(source)
|
|
346 ast = self.c3front.pkg
|
164
|
347 print('setting ast', ast)
|
102
|
348 self.astViewer.setAst(ast)
|
101
|
349 self.buildOutput.append("Done!")
|
67
|
350 def buildProject(self):
|
64
|
351 """ Build project """
|
152
|
352 print('BUILD project')
|
64
|
353 self.buildOutput.clear()
|
152
|
354 self.diag.diags.clear()
|
|
355 self.buildFile()
|
64
|
356
|
152
|
357 self.builderrors.setErrorList(self.diag.diags)
|
|
358 #self.astViewer.setAst(mods[0])
|
|
359 for err in self.diag.diags:
|
|
360 self.buildOutput.append(str(err))
|
154
|
361 ce = self.activeMdiChild()
|
|
362 if ce:
|
|
363 print('setting errors')
|
|
364 ce.setErrors(self.diag.diags)
|
64
|
365 self.buildOutput.append("Done!")
|
|
366
|
1
|
367 if __name__ == '__main__':
|
|
368 app = QApplication(sys.argv)
|
|
369 ide = Ide()
|
|
370 ide.show()
|
|
371 app.exec_()
|
|
372
|