-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.py
441 lines (337 loc) · 13.7 KB
/
GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
"""
Nombre: Alejandro Tejada
Curso: Diseño Compiladores
Fecha: noviembre 2021
Programa: GUI.py
Propósito: Segunda version de la interfaz
V 2.0
"""
# importing required libraries
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import os
import sys
from mainSemantic import Compilar
from mainIntermedio import CompilarIntermedio
from compiladorFinal import Compilador_Final
import pickle
# Creating main window class
class MainWindow(QMainWindow):
# constructor
def __init__(self, *args, **kwargs):
self.content_editor = ''
super(MainWindow, self).__init__(*args, **kwargs)
# self.setGeometry(1200, 800)
# setting window geometry
self.setGeometry(100, 100, 1200, 800)
# self.setAcceptDrops(True)
# setting font to the editor
fixedfont = QFontDatabase.systemFont(QFontDatabase.FixedFont)
fixedfont.setPointSize(10)
# creating a layout
mainlayout = QVBoxLayout()
# Initialize tab screen
self.tabs = QTabWidget()
self.windowEditor = QWidget()
self.windowResults = QWidget()
self.windowResultsIntermedio = QWidget()
self.windowResultsARM = QWidget()
# # AddEntryToTable tabs
self.tabs.addTab(self.windowEditor, "Editor de código")
self.tabs.addTab(self.windowResults, "Análisis semántico")
self.tabs.addTab(self.windowResultsIntermedio, "Código intermedio")
self.tabs.addTab(self.windowResultsARM, "Código ARM")
self.errorForlog2 = QPlainTextEdit()
self.errorForlog2.setFont(fixedfont)
self.windowResultsIntermedio.layout = QVBoxLayout()
self.windowResultsIntermedio.layout.addWidget(self.errorForlog2)
self.windowResultsIntermedio.setLayout(
self.windowResultsIntermedio.layout)
self.errorForlog3 = QPlainTextEdit()
self.errorForlog3.setFont(fixedfont)
self.windowResultsARM.layout = QVBoxLayout()
self.windowResultsARM.layout.addWidget(self.errorForlog3)
self.windowResultsARM.setLayout(
self.windowResultsARM.layout)
# creating a QPlainTextEdit object
self.editor = QPlainTextEdit()
self.errorForLog = QLabel()
self.errorForLog.setFont(fixedfont)
self.errorForLog.setText("")
self.windowResults.layout = QVBoxLayout()
self.windowResults.layout.addWidget(self.errorForLog)
self.windowResults.setLayout(self.windowResults.layout)
self.editor.setFont(fixedfont)
# self.path holds the path of the currently Abrir archivo.
# If none, we haven't got a file open yet (or creating new).
self.path = None
# adding editor to the layout
mainlayout.addWidget(self.tabs)
# layout.addWidget(self.editor)
self.windowEditor.layout = QVBoxLayout()
self.windowEditor.layout.addWidget(self.editor)
self.windowEditor.setLayout(self.windowEditor.layout)
# creating a QWidget layout
container = QWidget()
# setting layout to the container
container.setLayout(mainlayout)
# making container as central widget
self.setCentralWidget(container)
# creating a status bar object
self.status = QStatusBar()
# setting stats bar to the window
self.setStatusBar(self.status)
# creating a file tool bar
file_toolbar = QToolBar("File")
# adding file tool bar to the window
self.addToolBar(file_toolbar)
# creating actions to add in the file menu
# creating a Abrir archivo action
open_file_action = QAction("Abrir archivo", self)
# setting status tip
open_file_action.setStatusTip("Abrir archivo")
# adding action to the Abrir archivo
open_file_action.triggered.connect(self.file_open)
# adding this to tool bar
file_toolbar.addAction(open_file_action)
# similarly creating a save action
save_file_action = QAction("Guardar", self)
save_file_action.setStatusTip("Guardar página actual")
save_file_action.triggered.connect(self.file_save)
file_toolbar.addAction(save_file_action)
# similarly creating save action
saveas_file_action = QAction("Guardar como", self)
saveas_file_action.setStatusTip("Save current page to specified file")
saveas_file_action.triggered.connect(self.file_saveas)
file_toolbar.addAction(saveas_file_action)
compile_action = QAction("Compilar", self)
compile_action.setStatusTip("Compilar programa actual")
compile_action.triggered.connect(self.compilarFile)
file_toolbar.addAction(compile_action)
intermediate_action = QAction("CompilarIntermedio", self)
intermediate_action.setStatusTip("Compilar programa actual")
intermediate_action.triggered.connect(self.compilarcodigoIntermedio)
file_toolbar.addAction(intermediate_action)
arm_action = QAction("CompilarARM", self)
arm_action.setStatusTip("Compilar programa actual")
arm_action.triggered.connect(self.compilarCodigoARM)
file_toolbar.addAction(arm_action)
# creating another tool bar for editing text
edit_toolbar = QToolBar("Editar")
# adding this tool bar to the main window
self.addToolBar(edit_toolbar)
# adding actions to the tool bar and menu bar
# undo action
undo_action = QAction("Deshacer", self)
# adding status tip
undo_action.setStatusTip("Deshacer último cambio")
# when triggered undo the editor
undo_action.triggered.connect(self.editor.undo)
# adding this to tool and menu bar
edit_toolbar.addAction(undo_action)
# redo action
redo_action = QAction("Rehacer", self)
redo_action.setStatusTip("Rehacer ultimo cambio")
# when triggered redo the editor
redo_action.triggered.connect(self.editor.redo)
# adding this to menu and tool bar
edit_toolbar.addAction(redo_action)
# cut action
cut_action = QAction("Cortar", self)
cut_action.setStatusTip("Cortar texto seleccionado")
# when triggered cut the editor text
cut_action.triggered.connect(self.editor.cut)
# adding this to menu and tool bar
edit_toolbar.addAction(cut_action)
# copy action
copy_action = QAction("Copiar", self)
copy_action.setStatusTip("Copiar texto seleccionado")
# when triggered copy the editor text
copy_action.triggered.connect(self.editor.copy)
# adding this to menu and tool bar
edit_toolbar.addAction(copy_action)
# paste action
paste_action = QAction("Pegar", self)
paste_action.setStatusTip("Pegar de portapapeles")
# when triggered paste the copied text
paste_action.triggered.connect(self.editor.paste)
# adding this to menu and tool bar
edit_toolbar.addAction(paste_action)
# select all action
select_action = QAction("Seleccionar todo", self)
select_action.setStatusTip("Seleccionar todo el texto")
# when this triggered select the whole text
select_action.triggered.connect(self.editor.selectAll)
# adding this to menu and tool bar
edit_toolbar.addAction(select_action)
# wrap action
wrap_action = QAction("Wrap text to window", self)
wrap_action.setStatusTip("Check to wrap text to window")
# making it checkable
wrap_action.setCheckable(True)
# making it checked
wrap_action.setChecked(True)
# adding action
wrap_action.triggered.connect(self.edit_toggle_wrap)
# calling update title methpd
self.update_title()
# showing all the components
self.show()
# creating dialog critical method
# to show errors
def dialog_critical(self, s):
# creating a QMessageBox object
dlg = QMessageBox(self)
# setting text to the dlg
dlg.setText(s)
# setting icon to it
dlg.setIcon(QMessageBox.Critical)
# showing it
dlg.show()
# action called by file open action
def file_open(self):
# getting path and bool value
path, _ = QFileDialog.getOpenFileName(self, "Abrir archivo", "",
"Decaf (*.decaf)")
# if path is true
if path:
# try opening path
try:
with open(path, 'r') as f:
# read the file
text = f.read()
# if some error occured
except Exception as e:
# show error using critical method
self.dialog_critical(str(e))
# else
else:
# update path value
self.path = path
# update the text
self.editor.setPlainText(text)
# update the title
self.update_title()
# action called by file save action
def file_save(self):
# if there is no save path
if self.path is None:
# call save as method
return self.file_saveas()
# else call save to path method
self._save_to_path(self.path)
# action called by save as action
def file_saveas(self):
# opening path
path, _ = QFileDialog.getSaveFileName(self, "Guardar archivo", "",
"Text documents (*.txt);All files (*.*)")
# if dialog is cancelled i.e no path is selected
if not path:
# return this method
# i.e no action performed
return
# else call save to path method
self._save_to_path(path)
def compilarFile(self):
"""
Método para compilar
"""
if self.path is None:
return self.file_saveas()
self._save_to_path(self.path)
input = self.path
if self.editor.toPlainText() != '':
programaCompilado = Compilar(input)
if programaCompilado.HasLexicalError():
print('Se encontraron errores semánticos.\n ',
programaCompilado.errorFromAntlr.lexicalErrors)
errores = '\n'.join(
programaCompilado.errorFromAntlr.lexicalErrors)
self.errorForLog.setText(errores)
else:
if programaCompilado.printer.tipoNodo[programaCompilado.printer.root] == 'error' or len(programaCompilado.printer.errores.errores) > 0:
errores = '\n'.join(
programaCompilado.printer.errores.getAllErrors())
self.errorForLog.setText(errores)
else:
self.errorForLog.setText(
'No se encontraron errores de análisis semántico.')
self.tabs.setCurrentIndex(1)
def compilarcodigoIntermedio(self):
"""
Método para generar Codigo intermedio
"""
if self.path is None:
return self.file_saveas()
self._save_to_path(self.path)
input = self.path
if self.editor.toPlainText() != '':
programaCompilado = CompilarIntermedio(input)
# we drop the values for the piccke
infile = open("codigoIntermedioFinal", 'rb')
arrayIntermedio = pickle.load(infile)
if(arrayIntermedio != None):
acumulador = ""
for x in arrayIntermedio:
acumulador = acumulador + x + '\n'
self.errorForlog2.setPlainText(acumulador)
else:
self.errorForlog2.setPlainText(
'No se ha generado nada de codigo intermedio V2')
self.tabs.setCurrentIndex(2)
# save to path method
def compilarCodigoARM(self):
"""
Método para generar Codigo de ARM
"""
if self.path is None:
return self.file_saveas()
self._save_to_path(self.path)
input = self.path
if self.editor.toPlainText() != '':
# we drop the values for the piccke
programaCompilado = Compilador_Final()
infile = open("codigoARMDumpPicke", 'rb')
arrayARM = pickle.load(infile)
if(arrayARM != None):
acumulador = ""
""" for x in arrayARM:
acumulador = acumulador + x + '\n' """
self.errorForlog3.setPlainText(arrayARM)
else:
self.errorForlog3.setPlainText(
'No se ha generado nada de codigo ARM')
self.tabs.setCurrentIndex(3)
# save to path method
def _save_to_path(self, path):
# get the text
text = self.editor.toPlainText()
# try catch block
try:
# opening file to write
with open(path, 'w') as f:
# write text in the file
f.write(text)
# if error occurs
except Exception as e:
# show error using critical
self.dialog_critical(str(e))
# else do this
else:
# change path
self.path = path
# update the title
self.update_title()
def edit_toggle_wrap(self):
self.editor.setLineWrapMode(
1 if self.editor.lineWrapMode() == 0 else 0)
# update title method
def update_title(self):
self.setWindowTitle("%s" % (os.path.basename(self.path)
if self.path else "Untitled"))
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setApplicationName("PyQt5-Note")
window = MainWindow()
app.exec_()