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