From a261ca937ccf7b2de26904d70e910db2b7813f47 Mon Sep 17 00:00:00 2001 From: Cristian Garcia Date: Fri, 26 Jul 2013 21:37:30 +0000 Subject: 'Mejorando' el dialogo reemplazar, basandose en el que hice para BatovIde --- diff --git a/CristianEdit/CristianEdit.py b/CristianEdit/CristianEdit.py index 06114e2..ee0cae1 100644 --- a/CristianEdit/CristianEdit.py +++ b/CristianEdit/CristianEdit.py @@ -95,8 +95,8 @@ class CristianEdit(Gtk.Plug): hbox_toolbar.add(self.toolbar) self.vbox.pack_start(self.notebook, True, True, 0) - self.notebook.set_show_tabs(False) self.menu.actualizar_recientes(self.configuraciones['recientes']) + self.notebook.set_show_tabs(False) self.menu.connect('abrir', self.abrir) self.menu.connect('accion', self.set_accion) @@ -107,10 +107,10 @@ class CristianEdit(Gtk.Plug): self.notebook.connect('boton-cerrar-clicked', self.cerrar_desde_boton) self.notebook.connect('boton-nuevo-clicked', self.pagina_nueva) self.notebook.connect('switch-page', self.pre_actualizar_widgets) - self.dialogo.connect('solicitar-buffer', self.setear_variable_buffer) ventana.add_accel_group(self.menu.grupo) self.pagina_nueva() + self.dialogo.set_view(self.get_view()) for direccion in direcciones: if os.path.exists(direccion) and G.get_mime_type(direccion): @@ -130,6 +130,7 @@ class CristianEdit(Gtk.Plug): view = self.get_view() self.actualizar_widgets(view=view) + self.dialogo.set_view(view) def actualizar_widgets(self, view=None): """Sensitiva los widgets, según si @@ -525,6 +526,7 @@ class CristianEdit(Gtk.Plug): self.lugares.append('Sin dirección') self.etiquetas.append(label) self.barras_de_abajo.append(barra_inferior) + self.dialogo.set_view(view) view.configurar(self.configuraciones) @@ -623,8 +625,8 @@ class CristianEdit(Gtk.Plug): def mostrar_dialogo_reemplazar(self, widget=None): """Muestra un diálogo para reemplzar texto""" - if not self.dialogo.mostrado: - self.dialogo.mostrado = True + if not self.dialogo.visible: + self.dialogo.visible = True texto_actual = '' @@ -637,12 +639,9 @@ class CristianEdit(Gtk.Plug): if len(texto_actual.splitlines()) > 1: texto_actual = '' - self.dialogo.set_text(texto_actual) + self.dialogo.buscar_entry.set_text(texto_actual) self.dialogo.show_all() - else: - self.dialogo.set_focus(True) - def mostrar_dialogo_configuraciones(self, widget=None): """Abre un díalogo con configuraciones.""" diff --git a/CristianEdit/objetos.py b/CristianEdit/objetos.py index 0745fdf..59b6e10 100644 --- a/CristianEdit/objetos.py +++ b/CristianEdit/objetos.py @@ -694,75 +694,191 @@ class ComboLenguajes(Gtk.ComboBoxText): class DialogoReemplazarTexto(Gtk.Dialog): + """Clase basada en la del diálogo reemplazar del IDE de Batovi""" - __gsignals__ = { - 'solicitar-buffer': (GObject.SIGNAL_RUN_FIRST, - None, []) - } + def __init__(self, ventana, parent_window=None, + title = "Reemplazar Texto", texto=None): - def __init__(self, padre): + Gtk.Dialog.__init__(self, + title=title, + parent=parent_window) + + self.set_border_width(15) + + self.visible = False + self.seleccion = False - Gtk.Dialog.__init__(self) + self.buscar_entry = Gtk.Entry() + self.reemplazar_entry = Gtk.Entry() + + hbox = Gtk.HBox() + hbox.pack_start(Gtk.Label("Buscar:"), True, True, 3) + hbox.pack_start(self.buscar_entry, False, False, 0) + hbox.show_all() + + self.vbox.pack_start(hbox, False, False, 3) + + hbox = Gtk.HBox() + hbox.pack_start(Gtk.Label("Reemplazar:"), True, True, 3) + hbox.pack_start(self.reemplazar_entry, False, False, 0) + hbox.show_all() + + self.vbox.pack_start(hbox, False, False, 10) - self.set_title('Reemplazar') - self.set_border_width(10) - self.set_transient_for(padre) + cerrar = Gtk.Button("Cerrar") + self.boton_reemplazar = Gtk.Button("Reemplazar") + self.boton_buscar = Gtk.Button("Saltear") + + hbox = Gtk.HBox() + hbox.pack_start(self.boton_reemplazar, True, True, 3) + hbox.pack_start(self.boton_buscar, True, True, 3) + hbox.pack_start(cerrar, True, True, 0) + hbox.show_all() + + self.vbox.pack_start(hbox, False, False, 0) + + self.boton_reemplazar.set_sensitive(False) + self.boton_buscar.set_sensitive(False) - self.buffer = False - self.mostrado = False + cerrar.connect("clicked", self.cerrar) + self.boton_buscar.connect('clicked', self.buscar, 'Adelante') + self.boton_reemplazar.connect("clicked", self.reemplazar) + + if texto: + self.buscar_entry.set_text(texto) + seleccion = self.view.get_buffer().get_selection_bounds() + self.seleccion = True + GObject.idle_add(self.update, texto, seleccion) - self.entrada_buscar = Gtk.Entry() - self.entrada_reemplazar = Gtk.Entry() + GObject.idle_add(self.changed) + + def set_view(self, view): - tabla = Gtk.Table(2, 2, False) + self.view = view - tabla.attach(Gtk.Label('Texto a buscar:'), 0, 1, 0, 1) - tabla.attach(self.entrada_buscar, 1, 2, 0, 1) - tabla.attach(Gtk.Label('Texto con el que se reemplazará:'), 0, 1, 1, 2) - tabla.attach(self.entrada_reemplazar, 1, 2, 1, 2) + def update(self, texto, selection): + buffer = self.view.get_buffer() + + start, end = buffer.get_bounds() + _texto = buffer.get_text(start, end, 0) + numero = len(_texto) - hbox = Gtk.HBox() + if end.get_offset() == numero and not selection: + inicio = buffer.get_start_iter() + self.seleccionar_texto(texto, inicio, 'Adelante') - boton_ok = Gtk.Button('Reemplazar') - boton_cerrar = Gtk.Button(None, Gtk.STOCK_CLOSE) + else: + inicio, fin = selection + buffer.select_range(inicio, fin) - boton_ok.connect('clicked', self.reemplazar_todo) - boton_cerrar.connect('clicked', self.cerrar) + def changed(self): + """ + Habilita y deshabilita los botones de busqueda y reemplazo. + """ + + self.boton_buscar.set_sensitive(bool(self.buscar_entry.get_text())) + buffer = self.view.get_buffer() + select = buffer.get_selection_bounds() + + if len(select) == 2: + select = True + else: + select = False - hbox.pack_start(boton_ok, False, False, 2) - hbox.pack_start(boton_cerrar, False, False, 2) + self.boton_reemplazar.set_sensitive(select and \ + bool(self.buscar_entry.get_text()) and \ + bool(self.reemplazar_entry.get_text())) - self.vbox.add(tabla) - self.vbox.pack_end(hbox, False, False, 10) + return True - self.vbox.remove(self.get_children()[0].get_children()[-1]) + def buscar(self, widget, direccion): + """ + Busca el texto en el buffer. + """ - def reemplazar_todo(self, widget): - """Reemplaza el texto.""" + texto = self.buscar_entry.get_text() + buffer = self.view.get_buffer() + inicio, fin = buffer.get_bounds() + + texto_actual = buffer.get_text(inicio, fin, 0) - self.emit('solicitar-buffer') + posicion = buffer.get_iter_at_mark(buffer.get_insert()) - inicio, fin = self.buffer.get_bounds() + if texto: + if texto in texto_actual: + inicio = posicion - texto_buscado = self.entrada_buscar.get_text() - texto_solicitado = self.entrada_reemplazar.get_text() - texto_existente = self.buffer.get_text(inicio, fin, 0) + if direccion == 'Adelante': + if inicio.get_offset() == buffer.get_char_count(): + inicio = buffer.get_start_iter() - if texto_buscado and texto_buscado in texto_existente: - texto = texto_existente.replace(texto_buscado, texto_solicitado) + elif direccion == 'Atras': + if buffer.get_selection_bounds(): + + start, end = buffer.get_selection_bounds() + _texto = buffer.get_text(start, end, 0) + numero = len(_texto) - self.buffer.set_text(texto) + if end.get_offset() == numero: + inicio = buffer.get_end_iter() - def set_text(self, texto): - """Setea el texto de la entrada de texto a buscar.""" + else: + inicio = buffer.get_selection_bounds()[0] - self.entrada_buscar.set_text(texto) - - def cerrar(self, widget): - """Oculta el widget del que ereda la clase.""" + self.seleccionar_texto(texto, inicio, direccion) + + else: + buffer.select_range(posicion, posicion) + if self.seleccion: + self.seleccion = False + self.boton_buscar.clicked() + + def cerrar(self, widget, event=None): + self.hide() - self.mostrado = False + self.visible = False + + def reemplazar(self, widget): + + buffer = self.view.get_buffer() + inicio_s, fin_s = buffer.get_selection_bounds() + texto_reemplazo = self.reemplazar_entry.get_text() + + buffer.delete(inicio_s, fin_s) + buffer.insert_at_cursor(texto_reemplazo) + + self.seleccion = False + + self.boton_buscar.clicked() + + def seleccionar_texto(self, texto, inicio, direccion): + """ + Selecciona el texto solicitado, + y mueve el scrolled sí es necesario. + """ + + buffer = self.view.get_buffer() + + if direccion == 'Adelante': + match = inicio.forward_search(texto, 0, None) + + elif direccion == 'Atras': + match = inicio.backward_search(texto, 0, None) + + if match: + match_start, match_end = match + buffer.select_range(match_end, match_start) + self.view.scroll_to_iter(match_end, 0.1, 1, 1, 1) + + else: + if direccion == 'Adelante': + inicio = buffer.get_start_iter() + + elif direccion == 'Atras': + inicio = buffer.get_end_iter() + + self.seleccionar_texto(texto, inicio, direccion) class DialogoReemplazar(Gtk.MessageDialog): -- cgit v0.9.1