#!/usr/bin/env python # -*- coding: utf-8 -*- # IconView.py by: # Cristian García # Ignacio Rodríguez # Python Joven - CeibalJAM! Uruguay import os import Globales as G from gi.repository import Gtk from gi.repository import Gdk from gi.repository import GdkPixbuf class IconView(Gtk.IconView): def __init__(self): Gtk.IconView.__init__(self) self.modelo = Gtk.ListStore(str, GdkPixbuf.Pixbuf, str) self.direccion = G.USUARIO self.set_model(self.modelo) self.set_text_column(0) self.set_pixbuf_column(1) self.__actualizar(self.direccion) self.connect('button-press-event', self.__click) self.connect('key-press-event', self.__tecla_presionada) def __actualizar(self, path): if os.path.isdir(path) or os.path.ismount(path): self.direccion = path archivos = [] carpetas = [] self.modelo.clear() tema = Gtk.IconTheme.get_default() contenido = os.listdir(path) contenido.sort() for archivo in contenido: direccion = os.path.join(path, archivo) if os.path.isfile(direccion): archivos.append(archivo) else: carpetas.append(archivo) carpetas.sort() archivos.sort() for carpeta in carpetas: direccion = os.path.join(path, carpeta) icono = tema.lookup_icon(Gtk.STOCK_DIRECTORY, 256, Gtk.IconLookupFlags.FORCE_SVG).get_filename() pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icono, 55, 55) self.modelo.insert(-1, [carpeta, pixbuf, direccion]) for archivo in archivos: direccion = os.path.join(path, archivo) icono = tema.lookup_icon(Gtk.STOCK_FILE, 256, Gtk.IconLookupFlags.FORCE_SVG).get_filename() pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icono, 55, 55) self.modelo.insert(-1, [archivo, pixbuf, direccion]) def __click(self, widget, event): """Reacciona cuando se le hace clic, obteniendo con que botón se le hizo clic y que debe hacer después""" boton = event.button posx = event.x posy = event.y tiempo = event.time try: path = widget.get_path_at_pos(int(posx), int(posy)) direccion = self.direccion if list(direccion)[-1] != '/': direccion += '/' iter = self.modelo.get_iter(path) direccion += self.modelo.get_value(iter, 0) if event.type.value_name == "GDK_2BUTTON_PRESS" and boton == 1: self.__actualizar(direccion) except TypeError: # Solo sucede cuando se le hace clic fuera de un iter, # por eso lo dejo pasar pass def __tecla_presionada(self, widget, event): tecla = event.keyval direccion = self.direccion if direccion[-1] != '/': direccion += '/' if self.get_selected_items() and self.has_focus(): path = self.get_selected_items()[0] iter = self.modelo.get_iter(path) direccion += self.modelo.get_value(iter, 0) if tecla == 65288: self.abrir_arriba() elif tecla == 65293: self.__actualizar(direccion) def abrir_arriba(self, *args): lista = self.direccion.split('/') try: self.direccion = '/' numero = len(lista) - 1 while not bool(lista[numero]): numero -= 1 for directorio in lista[:numero]: if directorio: self.direccion = self.direccion + directorio + '/' except IndexError: self.direccion = '/' self.__actualizar(self.direccion) if __name__ == "__main__": Scroll = Gtk.ScrolledWindow() Scroll.add(IconView()) Scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) Ventana = Gtk.Window() Ventana.connect("delete-event", lambda x, i: exit()) Ventana.add(Scroll) Ventana.show_all() Gtk.main()