~pabluk/ubatar/trunk

« back to all changes in this revision

Viewing changes to ubatar/UbatarWindow.py

  • Committer: Pablo SEMINARIO
  • Date: 2012-07-05 22:52:29 UTC
  • mfrom: (34.1.3 ubatar-ribbons)
  • Revision ID: pabluk@gmail.com-20120705225229-xiaaiwq2oo2zksbw
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
logger = logging.getLogger('ubatar')
25
25
 
26
26
from ubatar_lib import Window
27
 
from ubatar_lib import helpers
 
27
from ubatar_lib.utils import pixbuf_normalize, get_ribbons_files
28
28
from ubatar_lib.keyring import SecretServiceKeyring
 
29
from ubatar_lib.ubatarconfig import get_data_file
29
30
from ubatar.plugins import get_installed_services
30
31
from ubatar.AboutUbatarDialog import AboutUbatarDialog
31
32
from ubatar.PreferencesUbatarDialog import PreferencesUbatarDialog
51
52
            Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
52
53
            Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
53
54
 
 
55
        self.img_default_size = 600
 
56
        self.img_thumb_size = 128
 
57
        self.ribbons = get_ribbons_files()
 
58
 
 
59
        self.pixbuf_selected = None
54
60
        last_image_path = self.get_last_image_path()
55
61
        if os.path.exists(last_image_path):
56
 
            self.update_image_widget(last_image_path)
 
62
            self.update_image_from_file(last_image_path)
57
63
 
58
64
        self.services = get_installed_services()
59
65
        self.show_icon_for_services()
61
67
    def on_toolbtn_add_clicked(self, filechooser):
62
68
        response = filechooser.run()
63
69
        if response == Gtk.ResponseType.OK:
64
 
            image_path = filechooser.get_filename()
65
 
            self.update_image_widget(image_path)
 
70
            self.image_selected = filechooser.get_filename()
 
71
            self.update_image_from_file(self.image_selected)
66
72
        filechooser.hide()
67
73
 
68
74
    def on_toolbtn_sync_clicked(self, image):
82
88
                instance.set_image(pixbuf)
83
89
        self.show_notification()
84
90
 
85
 
    def update_image_widget(self, image_path):
 
91
    def update_image_from_file(self, image_path):
86
92
        """Update main image widget using image_path."""
87
 
        max_width = 600
88
 
        pixbuf = GdkPixbuf.Pixbuf.new_from_file(image_path)
89
 
        width = pixbuf.get_width()
90
 
        heigth = pixbuf.get_height()
91
 
        ratio = width / float(heigth)
92
 
        if width >= max_width:
93
 
            new_width = max_width
94
 
            new_heigth = new_width / ratio
95
 
            pixbuf = pixbuf.scale_simple(new_width, new_heigth, GdkPixbuf.InterpType.BILINEAR)
 
93
        self.pixbuf_selected = GdkPixbuf.Pixbuf.new_from_file(image_path)
 
94
        self.pixbuf_selected = pixbuf_normalize(self.pixbuf_selected, self.img_default_size)
 
95
        self.update_image_from_pixbuf(self.pixbuf_selected)
 
96
        self.load_ribbons(image_path)
 
97
 
 
98
    def update_image_from_pixbuf(self, pixbuf):
 
99
        """Update main image widget using a GdkPixbuf object."""
96
100
        self.ui.image1.set_from_pixbuf(pixbuf)
97
 
        width = pixbuf.get_width()
98
 
        heigth = pixbuf.get_height()
99
 
        self.resize(width, heigth)
 
101
        self.resize(self.img_default_size, self.img_default_size)
100
102
 
101
103
    def on_preferences_changed(self, settings, key, data=None):
102
104
        enabled_services = ['%s-enable' % s.name for s in self.services]
111
113
        """Set icon for the enabled services."""
112
114
        for service in self.services:
113
115
            enabled = self.settings.get_boolean('%s-enable' % service.name)
114
 
            #icon = helpers.get_media_file(service.icon)
115
 
            icon = 'data/media/%s' % service.icon
 
116
            icon = get_data_file('media', service.icon)
116
117
            img = Gtk.Image.new_from_file(icon)
117
118
            img.set_name('img_%s' % service.name)
118
119
            img.set_tooltip_text(service.title)
140
141
            _('Your new profile picture was synchronized with all the enabled services.'),
141
142
            self.get_last_image_path())
142
143
        notification.show()
 
144
 
 
145
    def load_ribbons(self, image_path):
 
146
        self.ui.iconview.set_pixbuf_column(0)
 
147
        self.ui.liststore.clear()
 
148
        for img in self.ribbons.values():
 
149
            current_preview = GdkPixbuf.Pixbuf.new_from_file(image_path)
 
150
            current_preview = pixbuf_normalize(current_preview, 128)
 
151
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(img, 128, 128)
 
152
            pixbuf = GdkPixbuf.Pixbuf.add_alpha(pixbuf, False, 0, 0, 0)
 
153
            pixbuf.composite(current_preview, 0, 0, 128, 128, 0, 0, 1, 1, GdkPixbuf.InterpType.BILINEAR, 255)
 
154
            self.ui.liststore.append([current_preview])
 
155
        self.ui.iconview.connect('selection-changed', self.on_ribbon_selection_changed)
 
156
        
 
157
    def on_ribbon_selection_changed(self, iconview):
 
158
        items = self.ui.iconview.get_selected_items()
 
159
        if items:
 
160
            item = int(items[0].to_string())
 
161
            ribbon_path = self.ribbons[item]
 
162
            self.apply_ribbon_to_img(ribbon_path)
 
163
 
 
164
    def apply_ribbon_to_img(self, img_path):
 
165
        current = self.pixbuf_selected.copy()
 
166
        pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(img_path, self.img_default_size, self.img_default_size)
 
167
        pixbuf = GdkPixbuf.Pixbuf.add_alpha(pixbuf, False, 0, 0, 0)
 
168
        pixbuf.composite(current, 0, 0, self.img_default_size, self.img_default_size, 0, 0, 1, 1, GdkPixbuf.InterpType.BILINEAR, 255)
 
169
        self.update_image_from_pixbuf(current)
 
170
 
 
171
    def on_toolbtn_ribbons_toggled(self, widget, data=None):
 
172
        if widget.get_active():
 
173
            self.ui.iconview.show()
 
174
        else:
 
175
            self.ui.iconview.hide()
 
176
            self.resize(self.img_default_size, self.img_default_size)