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())
|
136
|
153 self.memview = addComponent('Memory', stutil.MemoryView())
|
137
|
154 self.ctrlToolbar = stutil.DebugToolbar()
|
|
155 self.addToolBar(self.ctrlToolbar)
|
|
156 self.ctrlToolbar.setObjectName('debugToolbar')
|
131
|
157 self.devxplr.deviceSelected.connect(self.regview.mdl.setDevice)
|
136
|
158 self.devxplr.deviceSelected.connect(self.memview.setDevice)
|
137
|
159 self.devxplr.deviceSelected.connect(self.ctrlToolbar.setDevice)
|
64
|
160
|
|
161 # About dialog:
|
|
162 self.aboutDialog = AboutDialog()
|
|
163 self.aboutDialog.setWindowIcon(QIcon(icon))
|
|
164 # Create actions:
|
68
|
165 def addMenuEntry(name, menu, callback, shortcut=None):
|
|
166 a = QAction(name, self)
|
|
167 menu.addAction(a)
|
|
168 a.triggered.connect(callback)
|
|
169 if shortcut:
|
|
170 a.setShortcut(shortcut)
|
67
|
171
|
68
|
172 addMenuEntry("New", self.projectMenu, self.newProject)
|
|
173 addMenuEntry("Open", self.projectMenu, self.openProject)
|
|
174 addMenuEntry("Save", self.projectMenu, self.saveProject)
|
|
175 addMenuEntry("Close", self.projectMenu, self.closeProject)
|
|
176 addMenuEntry("Build", self.projectMenu, self.buildProject, shortcut=QKeySequence('F7'))
|
67
|
177
|
101
|
178 addMenuEntry("New", self.fileMenu, self.newFile, shortcut=QKeySequence(QKeySequence.New))
|
|
179 addMenuEntry("Open", self.fileMenu, self.openFile, shortcut=QKeySequence(QKeySequence.Open))
|
|
180 addMenuEntry("Save", self.fileMenu, self.saveFile, shortcut=QKeySequence(QKeySequence.Save))
|
|
181 addMenuEntry("Build", self.fileMenu, self.buildFile, shortcut=QKeySequence("F8"))
|
|
182
|
64
|
183 self.helpAction = QAction('Help', self)
|
|
184 self.helpAction.setShortcut(QKeySequence('F1'))
|
|
185 self.helpMenu.addAction(self.helpAction)
|
68
|
186 addMenuEntry('About', self.helpMenu, self.aboutDialog.open)
|
64
|
187
|
68
|
188 addMenuEntry('Cascade windows', self.viewMenu, self.mdiArea.cascadeSubWindows)
|
|
189 addMenuEntry('Tile windows', self.viewMenu, self.mdiArea.tileSubWindows)
|
64
|
190
|
|
191 # Load settings:
|
|
192 self.settings = QSettings('windelsoft', 'lcfoside')
|
|
193 self.loadSettings()
|
|
194
|
|
195 # File handling:
|
67
|
196 def newProject(self):
|
|
197 filename = QFileDialog.getSaveFileName(self, \
|
|
198 "Select new projectfile", "", "lcfos Project files (*.lcp2)")
|
|
199 if filename:
|
|
200 self.project = Project()
|
|
201 self.project.filename = filename
|
|
202 self.project.save()
|
64
|
203
|
101
|
204 def newFile(self):
|
|
205 self.loadFile('main.ks')
|
|
206 def openFile(self):
|
|
207 filename = QFileDialog.getOpenFileName(self, "Open K# file...", "*.ks", "K# source files (*.ks)")
|
|
208 if filename:
|
|
209 self.loadFile(filename)
|
|
210 def saveFile(self):
|
|
211 ac = self.activeMdiChild()
|
|
212 if ac:
|
|
213 ac.save()
|
|
214
|
67
|
215 def saveProject(self):
|
|
216 self.project.save()
|
64
|
217
|
67
|
218 def closeProject(self):
|
64
|
219 ac = self.activeMdiChild()
|
|
220 if ac:
|
|
221 self.mdiArea.removeSubWindow(ac)
|
|
222
|
|
223 def loadFile(self, filename):
|
|
224 # Find existing mdi widget:
|
|
225 wid = self.findMdiChild(filename)
|
|
226 if wid:
|
|
227 self.mdiArea.setActiveSubWindow(wid.parent())
|
|
228 return wid
|
|
229
|
|
230 # Create a new one:
|
|
231 ce = CodeEdit()
|
101
|
232
|
|
233 #source = self.project.loadProjectFile(filename)
|
|
234 try:
|
|
235 with open(filename) as f:
|
|
236 source = f.read()
|
|
237 ce.setSource(source)
|
|
238 except Exception as e:
|
|
239 print('exception opening file', e)
|
64
|
240 self.mdiArea.addSubWindow(ce)
|
101
|
241 ce.setFileName(filename)
|
64
|
242 ce.show()
|
|
243 return ce
|
|
244
|
67
|
245 def loadProject(self, filename):
|
|
246 self.project = Project(filename)
|
|
247 self.projectview.setProject(self.project)
|
|
248
|
|
249 def openProject(self):
|
|
250 filename = QFileDialog.getOpenFileName(self, \
|
|
251 "Choose project file", "", "lcfos Project files (*.lcp2)")
|
|
252 if filename:
|
|
253 self.loadProject(filename)
|
|
254
|
64
|
255 # MDI:
|
|
256 def activeMdiChild(self):
|
|
257 aw = self.mdiArea.activeSubWindow()
|
|
258 if aw:
|
|
259 return aw.widget()
|
|
260 else:
|
|
261 return None
|
|
262
|
|
263 def findMdiChild(self, filename):
|
|
264 for window in self.mdiArea.subWindowList():
|
|
265 wid = window.widget()
|
|
266 if wid.filename == filename:
|
|
267 return wid
|
|
268 return None
|
|
269
|
|
270 def allChildren(self):
|
|
271 c = []
|
|
272 for window in self.mdiArea.subWindowList():
|
|
273 wid = window.widget()
|
|
274 c.append(wid)
|
|
275 return c
|
|
276
|
|
277 # Settings:
|
|
278 def loadSettings(self):
|
|
279 if self.settings.contains('mainwindowstate'):
|
|
280 self.restoreState(self.settings.value('mainwindowstate'))
|
|
281 if self.settings.contains('mainwindowgeometry'):
|
|
282 self.restoreGeometry(self.settings.value('mainwindowgeometry'))
|
|
283 if self.settings.contains('openedproject'):
|
|
284 projectfile = self.settings.value('openedproject')
|
67
|
285 #self.loadProject(projectfile)
|
101
|
286 if self.settings.contains('lastfile'):
|
|
287 self.loadFile(self.settings.value('lastfile'))
|
64
|
288
|
|
289 def closeEvent(self, ev):
|
|
290 self.settings.setValue('mainwindowstate', self.saveState())
|
|
291 self.settings.setValue('mainwindowgeometry', self.saveGeometry())
|
101
|
292 #if self.project:
|
|
293 # self.settings.setValue('openedproject', self.project.filename)
|
|
294 # # TODO: ask for save of opened files
|
|
295 ac = self.activeMdiChild()
|
|
296 if ac:
|
|
297 if ac.filename:
|
|
298 self.settings.setValue('lastfile', ac.filename)
|
64
|
299 ev.accept()
|
|
300
|
|
301 # Error handling:
|
|
302 def nodeSelected(self, node):
|
|
303 ce = self.activeMdiChild()
|
|
304 if not ce:
|
|
305 return
|
|
306 if node.location:
|
|
307 row, col = node.location
|
|
308 ce.highlightErrorLocation( row, col )
|
|
309 else:
|
|
310 ce.clearErrors()
|
|
311
|
|
312 def errorSelected(self, err):
|
|
313 row, col, msg = err
|
|
314 ce = self.activeMdiChild()
|
|
315 if not ce:
|
|
316 return
|
|
317 ce.highlightErrorLocation(row, col)
|
|
318
|
|
319 # Project loading:
|
|
320
|
|
321 # Build recepy:
|
101
|
322 def buildFile(self):
|
|
323 ce = self.activeMdiChild()
|
|
324 if ce:
|
|
325 source = ce.source
|
|
326 self.buildOutput.clear()
|
|
327 self.buildOutput.append(str(self.compiler))
|
102
|
328 ast = self.compiler.compilesource(source)
|
|
329 self.astViewer.setAst(ast)
|
101
|
330 self.buildOutput.append("Done!")
|
67
|
331 def buildProject(self):
|
64
|
332 """ Build project """
|
|
333 self.buildOutput.clear()
|
|
334 self.buildOutput.append(str(self.compiler))
|
|
335 mods = self.compiler.compileProject(self.project)
|
|
336
|
101
|
337 #self.builderrors.setErrorList(self.compiler.errorlist)
|
64
|
338 self.astViewer.setAst(mods[0])
|
101
|
339 #for err in self.compiler.errorlist:
|
|
340 # self.buildOutput.append(str(err))
|
64
|
341 self.buildOutput.append("Done!")
|
|
342
|
1
|
343 if __name__ == '__main__':
|
|
344 app = QApplication(sys.argv)
|
|
345 ide = Ide()
|
|
346 ide.show()
|
|
347 app.exec_()
|
|
348
|