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()
|
|
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)
|
|
151
|
|
152 # About dialog:
|
|
153 self.aboutDialog = AboutDialog()
|
|
154 self.aboutDialog.setWindowIcon(QIcon(icon))
|
|
155 # Create actions:
|
68
|
156 def addMenuEntry(name, menu, callback, shortcut=None):
|
|
157 a = QAction(name, self)
|
|
158 menu.addAction(a)
|
|
159 a.triggered.connect(callback)
|
|
160 if shortcut:
|
|
161 a.setShortcut(shortcut)
|
67
|
162
|
68
|
163 addMenuEntry("New", self.projectMenu, self.newProject)
|
|
164 addMenuEntry("Open", self.projectMenu, self.openProject)
|
|
165 addMenuEntry("Save", self.projectMenu, self.saveProject)
|
|
166 addMenuEntry("Close", self.projectMenu, self.closeProject)
|
|
167 addMenuEntry("Build", self.projectMenu, self.buildProject, shortcut=QKeySequence('F7'))
|
67
|
168
|
64
|
169 self.helpAction = QAction('Help', self)
|
|
170 self.helpAction.setShortcut(QKeySequence('F1'))
|
|
171 self.helpMenu.addAction(self.helpAction)
|
68
|
172 addMenuEntry('About', self.helpMenu, self.aboutDialog.open)
|
64
|
173
|
68
|
174 addMenuEntry('Cascade windows', self.viewMenu, self.mdiArea.cascadeSubWindows)
|
|
175 addMenuEntry('Tile windows', self.viewMenu, self.mdiArea.tileSubWindows)
|
64
|
176
|
|
177 # Load settings:
|
|
178 self.settings = QSettings('windelsoft', 'lcfoside')
|
|
179 self.loadSettings()
|
|
180
|
|
181 # File handling:
|
67
|
182 def newProject(self):
|
|
183 filename = QFileDialog.getSaveFileName(self, \
|
|
184 "Select new projectfile", "", "lcfos Project files (*.lcp2)")
|
|
185 if filename:
|
|
186 self.project = Project()
|
|
187 self.project.filename = filename
|
|
188 self.project.save()
|
64
|
189
|
67
|
190 def saveProject(self):
|
|
191 self.project.save()
|
64
|
192
|
67
|
193 def closeProject(self):
|
64
|
194 ac = self.activeMdiChild()
|
|
195 if ac:
|
|
196 self.mdiArea.removeSubWindow(ac)
|
|
197
|
|
198 def loadFile(self, filename):
|
|
199 # Find existing mdi widget:
|
|
200 wid = self.findMdiChild(filename)
|
|
201 if wid:
|
|
202 self.mdiArea.setActiveSubWindow(wid.parent())
|
|
203 return wid
|
|
204
|
|
205 # Create a new one:
|
|
206 ce = CodeEdit()
|
|
207 source = self.project.loadProjectFile(filename)
|
|
208 ce.setSource(source)
|
|
209 self.mdiArea.addSubWindow(ce)
|
|
210 ce.show()
|
|
211 return ce
|
|
212
|
67
|
213 def loadProject(self, filename):
|
|
214 self.project = Project(filename)
|
|
215 self.projectview.setProject(self.project)
|
|
216
|
|
217 def openProject(self):
|
|
218 filename = QFileDialog.getOpenFileName(self, \
|
|
219 "Choose project file", "", "lcfos Project files (*.lcp2)")
|
|
220 if filename:
|
|
221 self.loadProject(filename)
|
|
222
|
64
|
223 # MDI:
|
|
224 def activeMdiChild(self):
|
|
225 aw = self.mdiArea.activeSubWindow()
|
|
226 if aw:
|
|
227 return aw.widget()
|
|
228 else:
|
|
229 return None
|
|
230
|
|
231 def findMdiChild(self, filename):
|
|
232 for window in self.mdiArea.subWindowList():
|
|
233 wid = window.widget()
|
|
234 if wid.filename == filename:
|
|
235 return wid
|
|
236 return None
|
|
237
|
|
238 def allChildren(self):
|
|
239 c = []
|
|
240 for window in self.mdiArea.subWindowList():
|
|
241 wid = window.widget()
|
|
242 c.append(wid)
|
|
243 return c
|
|
244
|
|
245 # Settings:
|
|
246 def loadSettings(self):
|
|
247 if self.settings.contains('mainwindowstate'):
|
|
248 self.restoreState(self.settings.value('mainwindowstate'))
|
|
249 if self.settings.contains('mainwindowgeometry'):
|
|
250 self.restoreGeometry(self.settings.value('mainwindowgeometry'))
|
|
251 if self.settings.contains('openedproject'):
|
|
252 projectfile = self.settings.value('openedproject')
|
67
|
253 #self.loadProject(projectfile)
|
64
|
254
|
|
255 def closeEvent(self, ev):
|
|
256 self.settings.setValue('mainwindowstate', self.saveState())
|
|
257 self.settings.setValue('mainwindowgeometry', self.saveGeometry())
|
|
258 if self.project:
|
|
259 self.settings.setValue('openedproject', self.project.filename)
|
|
260 # TODO: ask for save of opened files
|
|
261 ev.accept()
|
|
262
|
|
263 # Error handling:
|
|
264 def nodeSelected(self, node):
|
|
265 ce = self.activeMdiChild()
|
|
266 if not ce:
|
|
267 return
|
|
268 if node.location:
|
|
269 row, col = node.location
|
|
270 ce.highlightErrorLocation( row, col )
|
|
271 else:
|
|
272 ce.clearErrors()
|
|
273
|
|
274 def errorSelected(self, err):
|
|
275 row, col, msg = err
|
|
276 ce = self.activeMdiChild()
|
|
277 if not ce:
|
|
278 return
|
|
279 ce.highlightErrorLocation(row, col)
|
|
280
|
|
281 # Project loading:
|
|
282
|
|
283 # Build recepy:
|
67
|
284 def buildProject(self):
|
64
|
285 """ Build project """
|
|
286 self.buildOutput.clear()
|
|
287 self.buildOutput.append(str(self.compiler))
|
|
288 mods = self.compiler.compileProject(self.project)
|
|
289
|
|
290 self.builderrors.setErrorList(self.compiler.errorlist)
|
|
291 self.astViewer.setAst(mods[0])
|
|
292 for err in self.compiler.errorlist:
|
|
293 self.buildOutput.append(str(err))
|
|
294 self.buildOutput.append("Done!")
|
|
295
|
1
|
296 if __name__ == '__main__':
|
|
297 app = QApplication(sys.argv)
|
|
298 ide = Ide()
|
|
299 ide.show()
|
|
300 app.exec_()
|
|
301
|