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