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