65
|
1 import sys, os
|
66
|
2 if sys.version_info.major != 3:
|
|
3 print("Needs to be run in python version 3.x")
|
|
4 sys.exit(1)
|
64
|
5
|
|
6 from PyQt4.QtCore import *
|
|
7 from PyQt4.QtGui import *
|
|
8 import base64
|
1
|
9
|
|
10 # Compiler imports:
|
66
|
11 sys.path.insert(0, os.path.join('..','libs'))
|
64
|
12 from project import Project
|
65
|
13 from compiler import Compiler
|
|
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)
|
|
71 for f in self.project.files:
|
|
72 fitem = QStandardItem(f)
|
|
73 pitem.appendRow(fitem)
|
|
74 fitem.setEditable(False)
|
|
75 fitem.setData(f)
|
|
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')
|
|
119 icon = QPixmap()
|
|
120 icon.loadFromData(lcfospng)
|
|
121 self.setWindowIcon(QIcon(icon))
|
|
122
|
|
123 # Create menus:
|
|
124 self.fileMenu = self.menuBar().addMenu('File')
|
|
125 self.viewMenu = self.menuBar().addMenu('View')
|
|
126 self.projectMenu = self.menuBar().addMenu('Project')
|
|
127 self.helpMenu = self.menuBar().addMenu('Help')
|
|
128
|
|
129 # Create mdi area:
|
|
130 self.mdiArea = QMdiArea()
|
|
131 self.setCentralWidget(self.mdiArea)
|
|
132
|
|
133 # Create components:
|
|
134 self.buildOutput = BuildOutput()
|
|
135 self.addComponent('Build output', self.buildOutput)
|
|
136
|
|
137 self.astViewer = AstViewer()
|
|
138 self.addComponent('AST viewer', self.astViewer)
|
|
139 self.astViewer.sigNodeSelected.connect(self.nodeSelected)
|
|
140
|
|
141 self.builderrors = BuildErrors()
|
|
142 self.addComponent('Build errors', self.builderrors)
|
|
143 self.builderrors.sigErrorSelected.connect(self.errorSelected)
|
|
144
|
|
145 self.projectview = ProjectView()
|
|
146 self.addComponent('Project', self.projectview)
|
|
147 self.projectview.sigLoadFile.connect(self.loadFile)
|
|
148
|
|
149 # About dialog:
|
|
150 self.aboutDialog = AboutDialog()
|
|
151 self.aboutDialog.setWindowIcon(QIcon(icon))
|
|
152 # Create actions:
|
|
153 self.buildAction = QAction('Build!', self)
|
|
154 self.buildAction.setShortcut(QKeySequence('F7'))
|
|
155 self.projectMenu.addAction(self.buildAction)
|
|
156 self.buildAction.triggered.connect(self.buildFile)
|
|
157 self.openProjectAction = QAction("Open project", self)
|
|
158 self.openProjectAction.triggered.connect(self.openProject)
|
|
159 self.projectMenu.addAction(self.openProjectAction)
|
|
160 self.helpAction = QAction('Help', self)
|
|
161 self.helpAction.setShortcut(QKeySequence('F1'))
|
|
162 self.helpMenu.addAction(self.helpAction)
|
|
163 self.aboutAction = QAction('About', self)
|
|
164 self.helpMenu.addAction(self.aboutAction)
|
|
165 self.aboutAction.triggered.connect(self.aboutDialog.open)
|
|
166
|
|
167 self.newFileAction = QAction("New File", self)
|
|
168 self.fileMenu.addAction(self.newFileAction)
|
|
169 self.newFileAction.triggered.connect(self.newFile)
|
|
170 self.saveFileAction = QAction("Save File", self)
|
|
171 self.fileMenu.addAction(self.saveFileAction)
|
|
172 self.saveFileAction.triggered.connect(self.saveFile)
|
|
173 self.closeFileAction = QAction("Close File", self)
|
|
174 self.fileMenu.addAction(self.closeFileAction)
|
|
175 self.closeFileAction.triggered.connect(self.closeFile)
|
|
176
|
|
177 cascadeAction = QAction("Cascade windows", self)
|
|
178 cascadeAction.triggered.connect(self.mdiArea.cascadeSubWindows)
|
|
179 self.viewMenu.addAction(cascadeAction)
|
|
180 tileAction = QAction('Tile windows', self)
|
|
181 tileAction.triggered.connect(self.mdiArea.tileSubWindows)
|
|
182 self.viewMenu.addAction(tileAction)
|
|
183
|
|
184 # Load settings:
|
|
185 self.settings = QSettings('windelsoft', 'lcfoside')
|
|
186 self.loadSettings()
|
|
187
|
|
188 def addComponent(self, name, widget):
|
|
189 dw = QDockWidget(name)
|
|
190 dw.setWidget(widget)
|
|
191 dw.setObjectName(name)
|
|
192 self.addDockWidget(Qt.RightDockWidgetArea, dw)
|
|
193 self.viewMenu.addAction(dw.toggleViewAction())
|
|
194
|
|
195 # File handling:
|
|
196 def newFile(self):
|
|
197 ce = CodeEdit()
|
|
198 w = self.mdiArea.addSubWindow(ce)
|
|
199 ce.show()
|
|
200
|
|
201 def saveFile(self):
|
|
202 ac = self.activeMdiChild()
|
|
203 if ac:
|
|
204 ac.saveFile()
|
|
205
|
|
206 def saveAll(self):
|
|
207 pass
|
|
208
|
|
209 def openFile(self):
|
|
210 # TODO
|
|
211 pass
|
|
212
|
|
213 def closeFile(self):
|
|
214 ac = self.activeMdiChild()
|
|
215 if ac:
|
|
216 self.mdiArea.removeSubWindow(ac)
|
|
217
|
|
218 def loadFile(self, filename):
|
|
219 # Find existing mdi widget:
|
|
220 wid = self.findMdiChild(filename)
|
|
221 if wid:
|
|
222 self.mdiArea.setActiveSubWindow(wid.parent())
|
|
223 return wid
|
|
224
|
|
225 # Create a new one:
|
|
226 ce = CodeEdit()
|
|
227 source = self.project.loadProjectFile(filename)
|
|
228 ce.setSource(source)
|
|
229 self.mdiArea.addSubWindow(ce)
|
|
230 ce.show()
|
|
231 return ce
|
|
232
|
|
233 # MDI:
|
|
234 def activeMdiChild(self):
|
|
235 aw = self.mdiArea.activeSubWindow()
|
|
236 if aw:
|
|
237 return aw.widget()
|
|
238 else:
|
|
239 return None
|
|
240
|
|
241 def findMdiChild(self, filename):
|
|
242 for window in self.mdiArea.subWindowList():
|
|
243 wid = window.widget()
|
|
244 if wid.filename == filename:
|
|
245 return wid
|
|
246 return None
|
|
247
|
|
248 def allChildren(self):
|
|
249 c = []
|
|
250 for window in self.mdiArea.subWindowList():
|
|
251 wid = window.widget()
|
|
252 c.append(wid)
|
|
253 return c
|
|
254
|
|
255 # Settings:
|
|
256 def loadSettings(self):
|
|
257 if self.settings.contains('mainwindowstate'):
|
|
258 self.restoreState(self.settings.value('mainwindowstate'))
|
|
259 if self.settings.contains('mainwindowgeometry'):
|
|
260 self.restoreGeometry(self.settings.value('mainwindowgeometry'))
|
|
261 if self.settings.contains('openedproject'):
|
|
262 projectfile = self.settings.value('openedproject')
|
|
263 self.loadProject(projectfile)
|
|
264
|
|
265 def closeEvent(self, ev):
|
|
266 self.settings.setValue('mainwindowstate', self.saveState())
|
|
267 self.settings.setValue('mainwindowgeometry', self.saveGeometry())
|
|
268 if self.project:
|
|
269 self.settings.setValue('openedproject', self.project.filename)
|
|
270 # TODO: ask for save of opened files
|
|
271 ev.accept()
|
|
272
|
|
273 # Error handling:
|
|
274 def nodeSelected(self, node):
|
|
275 ce = self.activeMdiChild()
|
|
276 if not ce:
|
|
277 return
|
|
278 if node.location:
|
|
279 row, col = node.location
|
|
280 ce.highlightErrorLocation( row, col )
|
|
281 else:
|
|
282 ce.clearErrors()
|
|
283
|
|
284 def errorSelected(self, err):
|
|
285 row, col, msg = err
|
|
286 ce = self.activeMdiChild()
|
|
287 if not ce:
|
|
288 return
|
|
289 ce.highlightErrorLocation(row, col)
|
|
290
|
|
291 # Project loading:
|
|
292 def loadProject(self, filename):
|
|
293 self.project = Project(filename)
|
|
294 self.projectview.setProject(self.project)
|
|
295
|
|
296 def openProject(self):
|
|
297 filename = QFileDialog.getOpenFileName(self, \
|
|
298 "Choose project file", "", "lcfos Project files (*.lcp)")
|
|
299 if filename:
|
|
300 self.loadProject(filename)
|
|
301
|
|
302 # Build recepy:
|
|
303 def buildFile(self):
|
|
304 """ Build project """
|
|
305 self.saveAll()
|
|
306 self.buildOutput.clear()
|
|
307 self.buildOutput.append(str(self.compiler))
|
|
308 mods = self.compiler.compileProject(self.project)
|
|
309
|
|
310 self.builderrors.setErrorList(self.compiler.errorlist)
|
|
311 self.astViewer.setAst(mods[0])
|
|
312 for err in self.compiler.errorlist:
|
|
313 self.buildOutput.append(str(err))
|
|
314 self.buildOutput.append("Done!")
|
|
315
|
1
|
316
|
|
317 if __name__ == '__main__':
|
|
318 app = QApplication(sys.argv)
|
|
319 ide = Ide()
|
|
320 ide.compiler = Compiler()
|
|
321 ide.show()
|
|
322 app.exec_()
|
|
323
|