Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/IconView.py
blob: ccd02f71ce283b6209e352a43d608810458298ac (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# IconView.py by:
# Cristian García <cristian99garcia@gmail.com>
# Ignacio Rodríguez <nachoel01@gmail.com>
# 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()