/
BasePanel.py
583 lines (463 loc) · 19.8 KB
/
BasePanel.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# BasePanel.py por:
# Cristian García <cristian99garcia@gmail.com>
# Ignacio Rodriguez <nachoel01@gmail.com>
# Flavio Danesse <fdanesse@gmail.com>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
from gi.repository import Gtk
from gi.repository import GObject
from gi.repository import GLib
from InfoNotebook import InfoNotebook
from WorkPanel import WorkPanel
from Toolbars import ToolbarProyecto
from Toolbars import ToolbarArchivo
from Toolbars import ToolbarBusquedas
home = os.environ["HOME"]
BatovideWorkSpace = os.path.join(home, 'BatovideWorkSpace')
class BasePanel(Gtk.Paned):
"""
Gtk.HPaned:
Gtk.VBox: Estructura de Proyecto e Introspección sobre el mismo.
ToolbarProyecto
InfoNotebook
ToolbarBusquedas
Gtk.VBox: Archivos y terminales.
ToolbarArchivo
WorkPanel
"""
__gtype_name__ = 'JAMediaEditorBasePanel'
__gsignals__ = {
'update': (GObject.SIGNAL_RUN_LAST,
GObject.TYPE_NONE, (GObject.TYPE_PYOBJECT,)),
'proyecto_abierto': (GObject.SIGNAL_RUN_LAST,
GObject.TYPE_NONE, (GObject.TYPE_BOOLEAN,)),
'ejecucion': (GObject.SIGNAL_RUN_LAST,
GObject.TYPE_NONE, (GObject.TYPE_STRING,
GObject.TYPE_BOOLEAN))}
def __init__(self):
Gtk.Paned.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
self.set_border_width(5)
self.proyecto = {}
self.workpanel = WorkPanel()
self.infonotebook = InfoNotebook()
self.seleccionado_actual = 0
self.toolbarproyecto = ToolbarProyecto()
self.toolbararchivo = ToolbarArchivo()
toolbarbusquedas = ToolbarBusquedas()
self.infonotebook_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.infonotebook_box.pack_start(self.toolbarproyecto, False, False, 0)
self.infonotebook_box.pack_start(self.infonotebook, True, True, 0)
self.infonotebook_box.pack_end(toolbarbusquedas, False, False, 0)
workpanel_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
workpanel_box.pack_start(self.toolbararchivo, False, False, 0)
workpanel_box.pack_end(self.workpanel, True, True, 0)
self.pack1(self.infonotebook_box, resize=False, shrink=False)
self.pack2(workpanel_box, resize=True, shrink=True)
self.show_all()
self.infonotebook_box.set_size_request(300, -1)
self.workpanel.connect('new_select', self.__set_introspeccion)
self.workpanel.connect('ejecucion', self.__re_emit_ejecucion)
self.workpanel.connect('update', self.__re_emit_update)
self.toolbararchivo.connect('accion', self.set_accion_archivo)
self.toolbarproyecto.connect('accion', self.set_accion_proyecto)
toolbarbusquedas.connect("buscar", self.__buscar)
toolbarbusquedas.connect("accion", self.__buscar_mas)
self.infonotebook.connect('new_select', self.__set_linea)
self.infonotebook.connect('open', self.__abrir_archivo)
self.infonotebook.connect('search_on_grep', self.__search_grep)
self.infonotebook.connect('remove_proyect', self.__remove_proyect)
def __re_emit_update(self, widget, dict):
self.emit("update", dict)
def __search_grep(self, widget, datos, parent):
"""
Cuando se hace una busqueda grep en la estructura del proyecto y luego
se selecciona una linea en los resultados de dicha búsqueda.
Se abre el archivo
Se selecciona y
Se abre el dialogo buscar.
"""
print "FIXME:", self.__search_grep
'''
self.__abrir_archivo(None, datos[0])
paginas = self.workpanel.notebook_sourceview.get_n_pages()
for indice in range(paginas):
sourceview = self.workpanel.notebook_sourceview.get_nth_page(
indice).get_child()
visible = sourceview.get_show_line_numbers()
sourceview.set_show_line_numbers(True)
if sourceview.archivo == datos[0]:
self.workpanel.notebook_sourceview.set_current_page(indice)
break
from Widgets import DialogoBuscar
dialogo = DialogoBuscar(sourceview, parent_window=parent,
title="Buscar Texto", texto=datos[2])
dialogo.run()
dialogo.destroy()
sourceview.set_show_line_numbers(visible)
'''
def __buscar(self, widget, texto):
"""
Recibe el texto a buscar.
"""
self.seleccionado_actual = 0
self.infonotebook.buscar(texto)
def __buscar_mas(self, widget, accion, texto):
"""
Cuando se hace click en anterior o siguiente en toolbar de busquedas.
"""
self.infonotebook.buscar(texto)
if self.infonotebook.get_current_page() == 0:
tree = self.infonotebook.introspeccion
seleccion = self.infonotebook.introspeccion.get_selection()
posibles = self.infonotebook.introspeccion.posibles
else:
tree = self.infonotebook.estructura_proyecto
seleccion = self.infonotebook.estructura_proyecto.get_selection()
posibles = self.infonotebook.estructura_proyecto.posibles
if accion == "Siguiente":
self.seleccionado_actual += 1
else:
self.seleccionado_actual -= 1
if self.seleccionado_actual > len(posibles) - 1:
self.seleccionado_actual = 0
elif self.seleccionado_actual < 0:
self.seleccionado_actual = len(posibles) - 1
if posibles != []:
seleccion.select_iter(posibles[self.seleccionado_actual])
new_path = tree.get_model().get_path(
posibles[self.seleccionado_actual])
tree.scroll_to_cell(new_path)
def __set_linea(self, widget, index, texto):
"""
Recibe la linea seleccionada en instrospeccion y
y la pasa a workpanel para ser seleccionada en el código.
"""
self.workpanel.set_linea(index, texto)
def __re_emit_ejecucion(self, widget, tipo, valor):
"""
Cuando se ejecutan o detienen archivos en terminales.
"""
self.emit("ejecucion", tipo, valor)
def __set_introspeccion(self, widget, view, tipo):
"""
Cuando se selecciona una lengüeta en el Notebook:
Recibe nombre y contenido de archivo para realizar
introspeccion sobre él.
"""
nombre = "Introspección"
text = ''
if view:
_buffer = view.get_buffer()
archivo = view.archivo
if archivo:
nombre = os.path.basename(archivo)
inicio, fin = _buffer.get_bounds()
text = _buffer.get_text(inicio, fin, 0)
# Setear Introspeción.
self.infonotebook.set_introspeccion(nombre, text, view, tipo)
GLib.idle_add(self.__set_estructura, view, tipo)
def __set_estructura(self, view, tipo):
# FIXME: Verifica si hay archivos de proyecto y si los hay, actualiza
# la vista de estructura. Otro punto de vista sería actualizar según
# que archivo se ha seleccionado, en este caso, la vista de proyecto
# solo sería activa si el archivo seleccionado está en el proyecto.
path = self.proyecto.get("path", False)
if path:
codeviews = self.workpanel.get_archivos_de_proyecto(path)
if codeviews:
self.infonotebook.set_path_estructura(path)
self.emit("proyecto_abierto", True)
else:
self.proyecto = {}
self.emit("proyecto_abierto", False)
self.infonotebook.set_path_estructura(False)
else:
self.proyecto = {}
self.emit("proyecto_abierto", False)
self.infonotebook.set_path_estructura(False)
def __cargar_proyecto(self, proyecto):
"""
Carga los datos del proyecto en la interfaz de la aplicación.
"""
self.proyecto = proyecto
path = proyecto.get("path", False)
main = proyecto.get("main", False)
self.__abrir_archivo(False, os.path.join(path, main))
def __abrir_archivo(self, widget, archivo):
if archivo:
import commands
datos = commands.getoutput(
'file -ik %s%s%s' % ("\"", archivo, "\""))
if "text" in datos or "x-python" in datos or \
"x-empty" in datos or "svg+xml" in datos or \
"application/xml" in datos:
self.workpanel.abrir_archivo(archivo)
else:
self.workpanel.abrir_archivo(False)
def __abrir_proyecto(self, widget, archivo):
extension = os.path.splitext(os.path.split(archivo)[1])[1]
if not extension == ".ide":
return
import json
import codecs
pro = codecs.open(archivo, "r", "utf-8")
proyecto = json.JSONDecoder("utf-8").decode(pro.read())
proyecto["path"] = os.path.dirname(archivo)
# Validación de datos del proyecto.
if not proyecto.get("nombre", False):
return
if not proyecto.get("main", False):
return
if not os.path.exists(os.path.join(
proyecto["path"], proyecto["main"])):
return
anterior_cerrado = self.cerrar_proyecto()
# El proyecto se carga sólo si se cerró el anterior.
if anterior_cerrado:
self.__cargar_proyecto(proyecto)
def __guardar_archivos_de_proyecto(self):
"""
Guarda todos los archivos del proyecto abierto.
"""
if not self.proyecto:
return
codeviews = self.workpanel.get_archivos_de_proyecto(
self.proyecto["path"])
for view in codeviews:
view.guardar()
def __guardar_proyecto(self, proyecto):
"""
Guarda el proyecto actual.
"""
# Todo Proyecto Requiere un Nombre.
if not proyecto.get("nombre", False):
return
# Seteo automático del path del proyecto.
path = False
if proyecto.get("path", False):
path = proyecto["path"]
else:
path = os.path.join(BatovideWorkSpace, proyecto["nombre"])
if not os.path.exists(path):
os.mkdir(path)
proyecto["path"] = path
# Seteo automático del main del proyecto.
if not proyecto.get("main", False):
proyecto["main"] = "Main.py"
main_path = os.path.join(proyecto["path"], proyecto["main"])
if not os.path.exists(main_path):
arch = open(main_path, "w")
arch.write("#!/usr/bin/env python\n# -*- coding: utf-8 -*-")
arch.close()
# Seteo automático de licencia
licencia_path = os.path.join(proyecto["path"], "COPYING")
import Licencias as Lic
arch = open(licencia_path, "w")
arch.write(Lic.dict[proyecto["licencia"]])
arch.close()
# Seteo automático de autores.
autores_path = os.path.join(proyecto["path"], "AUTHORS")
arch = open(autores_path, "w")
try:
for autor in proyecto["autores"]:
arch.write(u"%s %s\n" % (autor[0], autor[1]))
except:
print "FIXME: UnicodeEncodeError: 'ascii' codec can't encode"
print "character u'\xed' in position 13: ordinal not in range(128)"
arch.close()
# Guardar el Proyecto.
proyecto_file = os.path.join(path, "proyecto.ide")
import json
archivo = open(proyecto_file, "w")
archivo.write(json.dumps(proyecto, indent=4,
separators=(", ", ":"), sort_keys=True))
archivo.close()
def __remove_proyect(self, widget):
"""
Cuando se elimina el proyecto desde la vista de estructura.
"""
self.workpanel.remove_proyect(self.proyecto["path"])
self.infonotebook.set_path_estructura(False)
self.proyecto = {}
return True
def cerrar_proyecto(self):
if not self.proyecto:
self.proyecto = {}
self.infonotebook.set_path_estructura(False)
return True
codeviews = self.workpanel.get_archivos_de_proyecto(
self.proyecto["path"])
if codeviews:
# Cerrar Archivos. Esto pedirá guardar si hay cambios en él.
for view in codeviews:
view.set_accion("Cerrar Archivo")
else:
self.proyecto = {}
self.infonotebook.set_path_estructura(False)
return True
# Si algún archivo ha debido guardarse, no se ha cerrado.
if not self.proyecto:
self.proyecto = {}
self.infonotebook.set_path_estructura(False)
return True
codeviews = self.workpanel.get_archivos_de_proyecto(
self.proyecto["path"])
if codeviews:
# Cerrar Archivos.
for view in codeviews:
view.set_accion("Cerrar Archivo")
else:
self.proyecto = {}
self.infonotebook.set_path_estructura(False)
return True
# Si todavía hay archivos abiertos, el usuario no desea cerrarlos.
if not self.proyecto:
self.proyecto = {}
self.infonotebook.set_path_estructura(False)
return True
codeviews = self.workpanel.get_archivos_de_proyecto(
self.proyecto["path"])
if codeviews:
return False
else:
self.proyecto = {}
self.infonotebook.set_path_estructura(False)
return True
def set_accion_proyecto(self, widget, accion):
"""
Cuando se hace click en la toolbar de proyecto o
se manda ejecutar una acción desde el menú.
"""
if accion == "Nuevo Proyecto":
from DialogoProyecto import DialogoProyecto
dialog = DialogoProyecto(parent_window=self.get_toplevel(),
title="Crear Nuevo Proyecto")
response = dialog.run()
nuevoproyecto = False
if Gtk.ResponseType(response) == Gtk.ResponseType.ACCEPT:
nuevoproyecto = dialog.get_proyecto()
dialog.destroy()
# El Proyecto se crea solo cuando se ha cerrado el anterior.
if nuevoproyecto:
if nuevoproyecto["nombre"] in os.listdir(BatovideWorkSpace):
print "FIXME: Ya existe un Proyecto con este Nombre"
return
anterior_cerrado = self.cerrar_proyecto()
if anterior_cerrado:
self.__guardar_proyecto(nuevoproyecto)
self.__abrir_proyecto(None,
os.path.join(nuevoproyecto["path"], "proyecto.ide"))
elif accion == "Editar Proyecto":
if self.proyecto:
from DialogoProyecto import DialogoProyecto
dialog = DialogoProyecto(parent_window=self.get_toplevel(),
title="Editar Proyecto", accion="editar")
dialog.set_proyecto(self.proyecto)
response = dialog.run()
if Gtk.ResponseType(response) == Gtk.ResponseType.ACCEPT:
nuevoproyecto = dialog.get_proyecto()
self.__guardar_proyecto(nuevoproyecto)
self.__abrir_proyecto(None,
os.path.join(nuevoproyecto["path"], "proyecto.ide"))
dialog.destroy()
elif accion == "Abrir Proyecto":
from Widgets import My_FileChooser
filechooser = My_FileChooser(parent_window=self.get_toplevel(),
action_type=Gtk.FileChooserAction.OPEN, filter_type=["*.ide"],
title="Abrir proyecto", path=BatovideWorkSpace)
filechooser.connect('load', self.__abrir_proyecto)
elif accion == "Guardar Proyecto":
self.__guardar_archivos_de_proyecto()
self.__guardar_proyecto(self.proyecto)
elif accion == "Cerrar Proyecto":
if self.cerrar_proyecto():
self.proyecto = {}
elif accion == "Ejecutar Proyecto":
if self.proyecto:
main = os.path.join(self.proyecto["path"],
self.proyecto["main"])
self.workpanel.ejecutar(archivo=main)
elif accion == "Detener Ejecución":
self.workpanel.detener_ejecucion()
elif accion == "Construir":
self.get_toplevel().set_sensitive(False)
from Widget_Setup import DialogoSetup
dialog = DialogoSetup(parent_window=self.get_toplevel(),
proyecto=self.proyecto)
dialog.run()
dialog.destroy()
self.get_toplevel().set_sensitive(True)
else:
print "Acccion sin asignar en BasePanel", accion
def set_accion_codigo(self, widget, accion):
"""
Cuando se hace click en una opción del menú Código.
"""
self.workpanel.set_accion_codigo(accion)
def set_accion_ver(self, widget, accion, valor):
"""
Cuando se hace click en una opción del menú ver.
"""
if accion == "Panel lateral":
if not valor:
self.infonotebook_box.hide()
else:
self.infonotebook_box.show()
elif accion == "Numeracion" or accion == "Panel inferior":
self.workpanel.set_accion_ver(accion, valor)
def set_accion_archivo(self, widget, accion):
"""
Cuando se hace click en la toolbar de archivos o
se manda ejecutar una acción desde el menú.
"""
if accion == "Nuevo Archivo":
self.__abrir_archivo(None, None)
elif accion == "Abrir Archivo":
path = self.workpanel.get_default_path()
if not path:
path = BatovideWorkSpace
if self.proyecto:
path = self.proyecto["path"]
from Widgets import Multiple_FileChooser
filechooser = Multiple_FileChooser(
parent_window=self.get_toplevel(), title="Abrir Archivo",
path=path, mime_type=["text/*", "image/svg+xml"])
filechooser.connect('load', self.__abrir_archivo)
elif accion == "Guardar Archivo":
self.workpanel.guardar_archivo()
elif accion == "Guardar Como":
self.workpanel.guardar_archivo_como()
elif accion in ["Deshacer", "Rehacer", "Copiar",
"Cortar", "Pegar", "Seleccionar Todo", "Cerrar Archivo",
"Buscar Texto", "Remplazar Texto"]:
self.workpanel.set_accion_archivos(accion)
elif accion == "Ejecutar Archivo":
self.workpanel.ejecutar()
elif accion == "Detener Ejecución":
self.workpanel.detener_ejecucion()
else:
print "Accion sin asignar en BasePanel", accion
def external_open_proyect(self, ide_file):
"""
Cuando se abre el editor con archivo como parámetro.
"""
self.__abrir_proyecto(None, ide_file)
def external_open_file(self, archivo):
"""
Cuando se abre el editor con archivo como parámetro.
"""
self.__abrir_archivo(None, archivo)