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