Subversion Repositories basico

Rev

Rev 4 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: menus.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Icon manager service
7
 
8
import os
9
 
10
import pkg_resources
11
 
12
from gi.repository import Gtk
13
from gi.repository import Gio
14
from gi.repository import Pango
15
from gi.repository.GdkPixbuf import Pixbuf
16
 
17
from .service import Service
18
 
19
 
20
def resource_filename(file_name):
21
    paths = map(
22
        lambda path: os.path.join(path, file_name),
23
        (
24
            '/opt/extras.ubuntu.com/',
25
            '/usr/local/',
26
            '/usr/',
27
        ),
28
    )
29
    for path in paths:
30
        if os.path.isfile(path):
31
            return path
32
    return pkg_resources.resource_filename(
33
        pkg_resources.Requirement.parse("basico"), file_name)
34
 
74 t00mlabs 35
 
4 t00mlabs 36
class IconManager(Service):
37
    def initialize(self):
38
        APP_DIR_ICONS = self.app.get_var('ICONS')
39
        self.icondict = {}
40
        self.theme = Gtk.IconTheme.get_default()
41
        self.theme.prepend_search_path (APP_DIR_ICONS)
42
 
43
    def get_themed_icon(self, icon_name):
44
        APP_DIR_ICONS = self.app.get_var('ICONS')
45
        ICON = APP_DIR_ICONS + icon_name + '.png'
46
        icon = Gio.ThemedIcon.new(ICON)
47
 
48
        return icon
49
 
50
 
51
 
52
    def get_icon(self, name, width=24, height=24):
53
        key = "%s-%d-%d" % (name, width, height)
54
 
55
        # Get icon from cache if exists or add a new one
56
        try:
57
            icon = self.icondict[key]
74 t00mlabs 58
            #~ print ("ICON TRY: %s: %s" % (type(icon), icon))
4 t00mlabs 59
        except:
60
            #~ OLD WAY: icon = Pixbuf.new_from_file_at_scale(icon_name, width, height, True)
61
            iconinfo = self.theme.lookup_icon(name, width, Gtk.IconLookupFlags.GENERIC_FALLBACK)
62
            icon = iconinfo.load_icon()
74 t00mlabs 63
            #~ print ("ICON EXCEPT: %s: %s" % (type(icon), icon))
4 t00mlabs 64
            self.icondict[key] = icon
74 t00mlabs 65
            #~ icon = None
4 t00mlabs 66
        return icon
67
 
68
 
69
    def get_pixbuf_icon(self, name, width=36, height=36):
70
        key = "%s-%d-%d" % (name, width, height)
71
 
72
        # Get icon from cache if exists or add a new one
73
        try:
74
            icon = self.icondict[key]
75
        except:
74 t00mlabs 76
            icon = None
4 t00mlabs 77
            #~ OLD WAY: icon = Pixbuf.new_from_file_at_scale(icon_name, width, height, True)
74 t00mlabs 78
            #~ fout = open('icons.txt', 'w')
79
            #~ for icon in self.theme.list_icons():
80
                #~ fout.write('%s\n' % icon)
81
            #~ fout.close()
82
            if name in self.theme.list_icons():
83
                return self.theme.load_icon(name, width, Gtk.IconLookupFlags.GENERIC_FALLBACK)
84
            #~ if self.theme.has_icon(name):
85
 
86
            #~ iconinfo = self.theme.lookup_icon(name, width, Gtk.IconLookupFlags.GENERIC_FALLBACK)
87
            #~ icon = iconinfo.load_icon()
88
            #~ self.icondict[key] = icon
4 t00mlabs 89
        return icon
90
 
91
 
92
    def get_image_icon(self, name, width=36, height=36):
93
        pixbuf = self.get_pixbuf_icon(name, width, height)
94
        return Gtk.Image.new_from_pixbuf(pixbuf)