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