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
|
64
|
17
|
|
18 lcfospng = base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A\n/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9sJEhMKBk7B678AAAA/SURBVFjD\n7dbBCQAgDATBi9h/y7EFA4Kf2QLCwH1S6XQu6sqoujublc8BAAAAAAAAAAB8B+zXT6YJAAAAAKYd\nWSgFQNUyijIAAAAASUVORK5CYII=\n')
|
|
19
|
|
20 class BuildOutput(QTextEdit):
|
|
21 """ Build output component """
|
|
22 def __init__(self, parent=None):
|
|
23 super(BuildOutput, self).__init__(parent)
|
|
24 self.setCurrentFont(QFont('Courier'))
|
|
25 self.setReadOnly(True)
|
|
26 self.append('Build output will appear here!')
|
|
27
|
|
28 class BuildErrors(QListView):
|
|
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)
|
154
|
35 self.errorIcon = QIcon('error.png')
|
64
|
36 def setErrorList(self, errorlist):
|
|
37 model = QStandardItemModel()
|
|
38 for e in errorlist:
|
154
|
39 item = QStandardItem(self.errorIcon, str(e))
|
64
|
40 item.setData(e)
|
|
41 model.appendRow(item)
|
|
42 self.setModel(model)
|
|
43 def itemSelected(self, index):
|
|
44 if not index.isValid():
|
|
45 return
|
|
46 model = self.model()
|
|
47 item = model.itemFromIndex(index)
|
|
48 err = item.data()
|
|
49 self.sigErrorSelected.emit(err)
|
|
50
|
|
51 class ProjectView(QWidget):
|
|
52 sigLoadFile = pyqtSignal(str)
|
|
53 def __init__(self, parent=None):
|
|
54 super(ProjectView, self).__init__(parent)
|
|
55 self.treeview = QTreeView(self)
|
|
56 self.treeview.setContextMenuPolicy(Qt.CustomContextMenu)
|
|
57 l = QVBoxLayout(self)
|
|
58 l.addWidget(self.treeview)
|
|
59 pm = QPixmap()
|
|
60 pm.loadFromData(lcfospng)
|
|
61 self.projectIcon = QIcon(pm)
|
|
62 # Connect signals:
|
|
63 self.treeview.activated.connect(self.activate)
|
|
64 self.treeview.customContextMenuRequested.connect(self.contextMenu)
|
|
65 def setProject(self, project):
|
|
66 self.project = project
|
|
67 model = QStandardItemModel()
|
|
68 root = model.invisibleRootItem()
|
|
69 pitem = QStandardItem(self.projectIcon, project.name)
|
|
70 pitem.setEditable(False)
|
|
71 pitem.setData(project)
|
|
72 root.appendRow(pitem)
|
67
|
73 for el in self.project.elements:
|
|
74 fitem = QStandardItem(el)
|
64
|
75 pitem.appendRow(fitem)
|
|
76 fitem.setEditable(False)
|
67
|
77 fitem.setData(el)
|
64
|
78 self.treeview.setModel(model)
|
|
79 self.treeview.expandAll()
|
|
80 def contextMenu(self, pos):
|
|
81 idx = self.treeview.indexAt(pos)
|
|
82 if not idx.isValid():
|
|
83 return
|
|
84 item = self.treeview.model().itemFromIndex(idx)
|
|
85 def activate(self, index):
|
|
86 if not index.isValid():
|
|
87 return
|
|
88 model = self.treeview.model()
|
|
89 item = model.itemFromIndex(index)
|
|
90 fn = item.data()
|
|
91 if type(fn) is str:
|
|
92 self.sigLoadFile.emit(fn)
|
|
93
|
|
94 class AboutDialog(QDialog):
|
|
95 def __init__(self, parent=None):
|
|
96 super(AboutDialog, self).__init__(parent)
|
|
97 self.setWindowTitle('About')
|
|
98 l = QVBoxLayout(self)
|
|
99 txt = QTextEdit(self)
|
|
100 txt.setReadOnly(True)
|
|
101 aboutText = """<h1>lcfOS IDE</h1>
|
|
102 <p>An all-in-one IDE for OS development.</p>
|
|
103 <p>https://www.assembla.com/spaces/lcfOS/wiki</p>
|
|
104 <p>Author: Windel Bouwman</p>
|
|
105 """
|
|
106 txt.append(aboutText)
|
|
107 l.addWidget(txt)
|
|
108 but = QPushButton('OK')
|
|
109 but.setDefault(True)
|
|
110 but.clicked.connect(self.close)
|
|
111 l.addWidget(but)
|
|
112
|
|
113 class ProjectOptions(QDialog):
|
|
114 pass
|
|
115 # TODO: project options in here
|
|
116
|
|
117 class Ide(QMainWindow):
|
|
118 def __init__(self, parent=None):
|
|
119 super(Ide, self).__init__(parent)
|
|
120 self.setWindowTitle('LCFOS IDE')
|
130
|
121 self.compiler = None # TODO
|
64
|
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()
|
|
204
|
64
|
205 # File handling:
|
67
|
206 def newProject(self):
|
|
207 filename = QFileDialog.getSaveFileName(self, \
|
|
208 "Select new projectfile", "", "lcfos Project files (*.lcp2)")
|
|
209 if filename:
|
|
210 self.project = Project()
|
|
211 self.project.filename = filename
|
|
212 self.project.save()
|
64
|
213
|
101
|
214 def newFile(self):
|
152
|
215 ce = CodeEdit()
|
162
|
216 ce.textChanged.connect(self.buildProject)
|
|
217 w = self.mdiArea.addSubWindow(ce)
|
152
|
218 ce.show()
|
162
|
219 w.resize(500, 700)
|
152
|
220 return ce
|
101
|
221 def openFile(self):
|
|
222 filename = QFileDialog.getOpenFileName(self, "Open K# file...", "*.ks", "K# source files (*.ks)")
|
|
223 if filename:
|
|
224 self.loadFile(filename)
|
|
225 def saveFile(self):
|
|
226 ac = self.activeMdiChild()
|
|
227 if ac:
|
|
228 ac.save()
|
|
229
|
67
|
230 def saveProject(self):
|
|
231 self.project.save()
|
64
|
232
|
67
|
233 def closeProject(self):
|
64
|
234 ac = self.activeMdiChild()
|
|
235 if ac:
|
|
236 self.mdiArea.removeSubWindow(ac)
|
|
237
|
|
238 def loadFile(self, filename):
|
|
239 # Find existing mdi widget:
|
|
240 wid = self.findMdiChild(filename)
|
|
241 if wid:
|
|
242 self.mdiArea.setActiveSubWindow(wid.parent())
|
|
243 return wid
|
|
244
|
|
245 # Create a new one:
|
|
246 ce = CodeEdit()
|
101
|
247
|
|
248 #source = self.project.loadProjectFile(filename)
|
|
249 try:
|
|
250 with open(filename) as f:
|
|
251 source = f.read()
|
|
252 ce.setSource(source)
|
|
253 except Exception as e:
|
|
254 print('exception opening file', e)
|
64
|
255 self.mdiArea.addSubWindow(ce)
|
101
|
256 ce.setFileName(filename)
|
64
|
257 ce.show()
|
|
258 return ce
|
|
259
|
67
|
260 def loadProject(self, filename):
|
|
261 self.project = Project(filename)
|
|
262 self.projectview.setProject(self.project)
|
|
263
|
|
264 def openProject(self):
|
|
265 filename = QFileDialog.getOpenFileName(self, \
|
|
266 "Choose project file", "", "lcfos Project files (*.lcp2)")
|
|
267 if filename:
|
|
268 self.loadProject(filename)
|
|
269
|
64
|
270 # MDI:
|
|
271 def activeMdiChild(self):
|
|
272 aw = self.mdiArea.activeSubWindow()
|
|
273 if aw:
|
|
274 return aw.widget()
|
|
275 else:
|
|
276 return None
|
|
277
|
|
278 def findMdiChild(self, filename):
|
|
279 for window in self.mdiArea.subWindowList():
|
|
280 wid = window.widget()
|
|
281 if wid.filename == filename:
|
|
282 return wid
|
|
283 return None
|
|
284
|
|
285 def allChildren(self):
|
|
286 c = []
|
|
287 for window in self.mdiArea.subWindowList():
|
|
288 wid = window.widget()
|
|
289 c.append(wid)
|
|
290 return c
|
|
291
|
|
292 # Settings:
|
|
293 def loadSettings(self):
|
|
294 if self.settings.contains('mainwindowstate'):
|
|
295 self.restoreState(self.settings.value('mainwindowstate'))
|
|
296 if self.settings.contains('mainwindowgeometry'):
|
|
297 self.restoreGeometry(self.settings.value('mainwindowgeometry'))
|
101
|
298 if self.settings.contains('lastfile'):
|
|
299 self.loadFile(self.settings.value('lastfile'))
|
64
|
300
|
|
301 def closeEvent(self, ev):
|
|
302 self.settings.setValue('mainwindowstate', self.saveState())
|
|
303 self.settings.setValue('mainwindowgeometry', self.saveGeometry())
|
101
|
304 #if self.project:
|
|
305 # self.settings.setValue('openedproject', self.project.filename)
|
|
306 # # TODO: ask for save of opened files
|
|
307 ac = self.activeMdiChild()
|
|
308 if ac:
|
162
|
309 if hasattr(ac, 'filename') and ac.filename:
|
101
|
310 self.settings.setValue('lastfile', ac.filename)
|
152
|
311 else:
|
|
312 self.settings.remove('lastfile')
|
|
313 else:
|
|
314 self.settings.remove('lastfile')
|
64
|
315 ev.accept()
|
|
316
|
|
317 # Error handling:
|
|
318 def nodeSelected(self, node):
|
|
319 ce = self.activeMdiChild()
|
|
320 if not ce:
|
|
321 return
|
|
322 if node.location:
|
|
323 row, col = node.location
|
|
324 ce.highlightErrorLocation( row, col )
|
|
325 else:
|
|
326 ce.clearErrors()
|
|
327
|
|
328 def errorSelected(self, err):
|
|
329 ce = self.activeMdiChild()
|
|
330 if not ce:
|
|
331 return
|
163
|
332 ce.setRowCol(err.loc.row, err.loc.col)
|
64
|
333
|
|
334 # Project loading:
|
|
335
|
|
336 # Build recepy:
|
101
|
337 def buildFile(self):
|
|
338 ce = self.activeMdiChild()
|
152
|
339 print('BUILD file')
|
101
|
340 if ce:
|
162
|
341 source = ce.Source
|
101
|
342 self.buildOutput.clear()
|
|
343 self.buildOutput.append(str(self.compiler))
|
152
|
344 ast = testc3.c3compile(source, self.diag)
|
|
345 #ast = self.compiler.compilesource(source)
|
102
|
346 self.astViewer.setAst(ast)
|
101
|
347 self.buildOutput.append("Done!")
|
67
|
348 def buildProject(self):
|
64
|
349 """ Build project """
|
152
|
350 print('BUILD project')
|
64
|
351 self.buildOutput.clear()
|
152
|
352 self.diag.diags.clear()
|
64
|
353 self.buildOutput.append(str(self.compiler))
|
152
|
354 self.buildFile()
|
|
355 #mods = self.compiler.compileProject(self.project)
|
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
|