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