#!/usr/bin/env python # -*- coding: utf-8 -*- # images.py # # Copyright 2012 S. Daniel Francis # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # import gtk from sugar.graphics.tray import VTray TARGET_TYPE_TEXT = 80 TARGET_TYPE_PIXMAP = 81 class Image: ready = False path = None def __init__(self): self.pixbuf = None self.width = -1 self.height = -1 def set_from_pixbuf(self, pixbuf): self.pixbuf = pixbuf self.width = self.pixbuf.get_width() self.height = self.pixbuf.get_height() self.ready = True def set_from_file(self, path): self.path = path self.pixbuf = gtk.gdk.pixbuf_new_from_file(path) self.width = self.pixbuf.get_width() self.height = self.pixbuf.get_height() self.ready = True def resize_to_galery(self): # Cross Multiplication : Rule of Three # self.width -- self.height # espected_width (150) -- new_height (unknown) new_height = 150 * self.height / self.width return self.pixbuf.scale_simple(150, new_height, gtk.gdk.INTERP_NEAREST) def render_to_preview(self, width=None, height=None): return self.pixbuf #_width = width if width else self.width #_height = height if height else self.height #return self.pixbuf.scale_simple(int(_width), # int(_height), # gtk.gdk.INTERP_BILINEAR) class ImagesGalery(VTray): def image_request(self, widget, index): return self.images[int(index)][1] def drag_send(self, widget, context, selection, targetType, eventTime): for i in self.images: if i[0].get_child() == widget: selection.set(gtk.gdk.TARGET_STRING, 8, str(self.images.index(i))) def append_image(self, image): item = gtk.ToolButton(self.main_item) child = item.get_child() child.connect("drag_data_get", self.drag_send) child.drag_source_set(gtk.gdk.BUTTON1_MASK, [("text/plain", 0, TARGET_TYPE_TEXT)], gtk.gdk.ACTION_COPY) pixbuf = image.resize_to_galery() image_widget = gtk.image_new_from_pixbuf(pixbuf) image_widget.show() item.set_icon_widget(image_widget) self.add_item(item) item.show() self.images.append((item, image)) adjustment = self._viewport.get_vadjustment() adjustment.set_value(adjustment.get_upper()) def __init__(self): super(ImagesGalery, self).__init__() self.main_item = gtk.RadioToolButton() self.images = [] self.set_size_request(170, -1)