-
Notifications
You must be signed in to change notification settings - Fork 0
/
erdos.py
executable file
·355 lines (289 loc) · 11.5 KB
/
erdos.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import threading
import copy
import subprocess
import os
from PyQt4 import QtGui, uic
import lib
import utils
import gui
import items
MARCA_CAMBIO = "* "
class Erdos(QtGui.QMainWindow):
def __init__(self):
super(Erdos, self).__init__()
self.paginas = {}
self.nodo_copiado = None
self.ventana_ejec = gui.VentanaEjecuccion(self)
uic.loadUi(utils.path_ui("principal.ui"), self)
self.agregar_tab()
# Menu Archivo
self.actionNuevo.triggered.connect(self.agregar_tab)
self.actionAbrir.triggered.connect(self.abrir)
self.actionGuardar.triggered.connect(self.guardar)
self.actionGuardar_como.triggered.connect(self.guardar_como)
self.actionCerrar.triggered.connect(self.cerrar)
self.actionSalir.triggered.connect(self.close)
# Menu Edicion
self.actionCopiar.triggered.connect(self.copiar)
self.actionPegar.triggered.connect(self.pegar)
self.actionEliminar.triggered.connect(self.eliminar)
self.actionEditar.triggered.connect(self.editar)
self.actionDeshacer.triggered.connect(self.deshacer)
self.actionRehacer.triggered.connect(self.rehacer)
# Panel figuras
self.actionSentencia.triggered.connect(self.agregar_sentencia)
self.actionEntrada.triggered.connect(self.agregar_entrada)
self.actionSalida.triggered.connect(self.agregar_salida)
self.actionSi.triggered.connect(self.agregar_if)
self.actionPara.triggered.connect(self.agregar_for)
self.actionMientras.triggered.connect(self.agregar_while)
self.actionHasta.triggered.connect(self.agregar_until)
self.actionCaso.triggered.connect(self.agregar_case)
# Panel Debug
self.actionReload.triggered.connect(self.reload)
# Menu herramientas
self.actionEjecutar.triggered.connect(self.ejecutar)
self.actionExportar.triggered.connect(self.exportar)
self.actionMostrar_salida.triggered.connect(self.abrir_ventana_ejec)
# Menu de ayuda
self.actionManual_de_usuario.triggered.connect(self.manual)
self.actionAcerca_de_Erdos.triggered.connect(self.acerca_de)
self.actionAcerca_de_Qt.triggered.connect(self.acerca_de_qt)
self.show()
def controlador(self):
# devuelve el controlador del diagrama actual
return self.paginas.get(self.tabWidget.currentWidget())
def abrir(self):
dialogo = gui.dialogo_abrir(self)
if dialogo.exec_():
path = str(dialogo.selectedFiles()[0])
tab_index, controlador = self.agregar_tab()
nombre_archivo, alerta = controlador.abrir(path)
if alerta:
QtGui.QMessageBox.warning(
self, "Atencion", utils.ALERTA_VERSION
)
self.set_texto_tab(tab_index, nombre_archivo)
def guardar(self):
if self.controlador().archivo:
self.controlador().guardar()
self.quitar_marca_cambio()
else:
self.guardar_como()
def guardar_como(self):
dialogo = gui.dialogo_guardar(self)
if dialogo.exec_():
path = str(dialogo.selectedFiles()[0])
if not path.endswith(".erd"):
path += ".erd"
nombre_archivo = self.controlador().guardar_como(path)
tab_index = self.tabWidget.currentIndex()
self.set_texto_tab(tab_index, nombre_archivo)
def exportar(self):
dialogo = gui.dialogo_exportar(self)
if dialogo.exec_():
path = str(dialogo.selectedFiles()[0])
if not path.endswith(".png"):
path += ".png"
self.controlador().exportar(path)
def cerrar(self):
self.paginas.pop(self.tabWidget.currentWidget())
tab_index = self.tabWidget.currentIndex()
self.tabWidget.removeTab(tab_index)
if not len(self.paginas):
self.agregar_tab()
def eliminar(self):
self.controlador().eliminar_seleccionado()
def copiar(self):
figura = self.get_seleccionado()
if hasattr(figura, 'nodo'):
if figura.nodo != self.controlador().arbol.raiz:
self.nodo_copiado = copy.deepcopy(figura.nodo)
def pegar(self):
if self.nodo_copiado:
destino = self.get_seleccionado()
self.controlador().regen(self.nodo_copiado, destino)
def editar(self):
figura = self.get_seleccionado()
if type(figura) == items.ErdosSentenciaItem:
texto = figura.nodo.texto
pos, datos = utils.unpack_list(
gui.SentenciaInput(self, texto).get()
)
elif type(figura) == items.ErdosEntradaItem:
variable = figura.nodo.variable
tipo_dato = figura.nodo.tipo_dato
texto = figura.nodo.texto_dialogo
pos, datos = utils.unpack_list(
gui.EntradaInput(self, variable, texto, tipo_dato).get()
)
elif type(figura) == items.ErdosSalidaItem:
variable = figura.nodo.variable
texto = figura.nodo.texto_dialogo
prompt = figura.nodo.prompt
pos, datos = utils.unpack_list(
gui.SalidaInput(self, variable, texto, prompt).get()
)
elif type(figura) == items.ErdosIfItem:
condiciones = figura.nodo.condiciones
pos, datos = utils.unpack_list(
gui.IfInput(self, condiciones).get()
)
elif type(figura) == items.ErdosCaseItem:
n_var = figura.nodo.nom_var
r_min = figura.nodo.rango_minimo
r_max = figura.nodo.rango_maximo
pos, datos = utils.unpack_list(
gui.ForInput(self, True, n_var, r_min, r_max).get()
)
elif type(figura) == items.ErdosForItem:
n_var = figura.nodo.nom_var
r_min = figura.nodo.rango_minimo
r_max = figura.nodo.rango_maximo
pos, datos = utils.unpack_list(
gui.ForInput(self, False, n_var, r_min, r_max).get()
)
elif type(figura) == items.ErdosWhileItem:
condiciones = figura.nodo.condiciones
pos, datos = utils.unpack_list(
gui.IfInput(self, condiciones).get()
)
elif type(figura) == items.ErdosUntilItem:
condiciones = figura.nodo.condiciones
pos, datos = utils.unpack_list(
gui.IfInput(self, condiciones).get()
)
else:
pos = 0
if pos:
figura.editar(*datos)
self.controlador().guardar_historial()
self.controlador().reposicionar()
self.agregar_marca_cambio()
def deshacer(self):
self.controlador().deshacer()
self.agregar_marca_cambio()
def rehacer(self):
self.controlador().rehacer()
self.agregar_marca_cambio()
def agregar_tab(self):
vista = QtGui.QGraphicsView()
controlador = items.ControladorGUI(vista)
self.paginas[vista] = controlador
index = self.tabWidget.addTab(vista, "Nuevo " + str(len(self.paginas)))
self.tabWidget.setCurrentIndex(index)
return index, controlador
def set_texto_tab(self, index, nombre_archivo):
self.tabWidget.setTabText(index, nombre_archivo)
def reload(self):
rel = reload(lib)
reg = reload(gui)
rei = reload(items)
reu = reload(utils)
print "Modulos re-cargados: ", rel, reg, rei, reu
def get_seleccionado(self):
selected = self.controlador().selectedItems()
if selected:
return selected[0]
return self.controlador().contenedorGUI
def agregar_sentencia(self):
destino = self.get_seleccionado()
pos, sentencia = gui.SentenciaInput(self).get()
if pos:
self.controlador().agregar_sentencia(pos, destino, sentencia)
self.agregar_marca_cambio()
def agregar_entrada(self):
destino = self.get_seleccionado()
pos, var, tipodt, texto = gui.EntradaInput(self).get()
if pos:
self.controlador().agregar_entrada(pos, destino, var, tipodt, texto)
self.agregar_marca_cambio()
def agregar_salida(self):
destino = self.get_seleccionado()
pos, var, texto, prompt = gui.SalidaInput(self).get()
if pos:
self.controlador().agregar_salida(pos, destino, var, texto, prompt)
self.agregar_marca_cambio()
def agregar_if(self):
destino = self.get_seleccionado()
pos, condiciones = gui.IfInput(self).get()
if pos:
self.controlador().agregar_if(pos, destino, condiciones)
self.agregar_marca_cambio()
def agregar_for(self):
destino = self.get_seleccionado()
pos, n_var, r_max, r_min = gui.ForInput(self).get()
if pos:
self.controlador().agregar_for(pos, destino, n_var, r_max, r_min)
self.agregar_marca_cambio()
def agregar_while(self):
destino = self.get_seleccionado()
pos, condiciones = gui.IfInput(self).get()
if pos:
self.controlador().agregar_while(pos, destino, condiciones)
self.agregar_marca_cambio()
def agregar_until(self):
destino = self.get_seleccionado()
pos, condiciones = gui.IfInput(self).get()
if pos:
self.controlador().agregar_until(pos, destino, condiciones)
self.agregar_marca_cambio()
def agregar_case(self):
destino = self.get_seleccionado()
pos, n_var, r_max, r_min = gui.ForInput(self, True).get()
if pos:
self.controlador().agregar_case(pos, destino, n_var, r_min, r_max)
self.agregar_marca_cambio()
def agregar_marca_cambio(self):
index = self.tabWidget.currentIndex()
nombre = self.tabWidget.tabText(index)
if not str(nombre).startswith(MARCA_CAMBIO):
self.tabWidget.setTabText(index, MARCA_CAMBIO + nombre)
def quitar_marca_cambio(self):
index = self.tabWidget.currentIndex()
nombre = self.tabWidget.tabText(index)
if str(nombre).startswith(MARCA_CAMBIO):
nombre = nombre[2:]
self.tabWidget.setTabText(index, nombre)
def ejecutar(self):
salida, fallo = self.controlador().ejecutar()
if fallo:
self.controlador().resaltar_figura(fallo)
nombre_diag = self.tabWidget.tabText(self.tabWidget.currentIndex())
self.ventana_ejec.agregar_salida(nombre_diag, salida.getvalue())
self.abrir_ventana_ejec()
def abrir_ventana_ejec(self):
self.ventana_ejec.exec_()
def manual(self):
manual = "manual_erdos.pdf"
if sys.platform.startswith('linux'):
subprocess.call(["xdg-open", manual])
else:
os.startfile(manual)
def acerca_de(self):
texto = """
- Erdos -
Version %s
Erdos es una herramienta para crear y ejecutar diagramas Nassi-Schneiderman.
Integrantes:
* Forconi, Geronimo
* Rivera, Francisco
* Virga, Andres
Proyecto Final - UTN Facultad Regional Rosario - 2013
"""
QtGui.QMessageBox.about(self, "Erdos", texto % utils.Config.VERSION)
def acerca_de_qt(self):
QtGui.QMessageBox.aboutQt(self)
class Run(threading.Thread):
def __init__(self):
super(Run, self).__init__()
self.start()
def run(self):
app = QtGui.QApplication(sys.argv)
self.ex = Erdos()
sys.exit(app.exec_())
if __name__ == '__main__':
Run()