#!/usr/bin/env python # -*- coding: utf-8 -*- # # widgets.py # # Copyright 2012 Santiago 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. # # 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 tempfile import gobject import gtk class ImageEditor(gtk.Notebook): __gsignals__ = {"get-image-from-galery": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_PYOBJECT, (gobject.TYPE_STRING,)), "load-toolbar": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_PYOBJECT, tuple())} position = 0 def __init__(self): super(ImageEditor, self).__init__() self.set_show_border(True) self.toolbar = None drag_label = gtk.Label("Drag here an image") drag_label.drag_dest_set(gtk.DEST_DEFAULT_MOTION | gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP, [("text/plain", 0, 80)], gtk.gdk.ACTION_COPY) drag_label.connect("drag_data_received", self.image_received) drag_label.show() self.append_page(drag_label) self.scrolled_window = gtk.ScrolledWindow() self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.image_widget = gtk.Image() self.image_widget.show() self.scrolled_window.add_with_viewport(self.image_widget) self.scrolled_window.show() self.append_page(self.scrolled_window) self.set_show_tabs(False) def change_width(self, widget): if self.link_values_button.get_active(): # Cross multiplication : Rule of three # self.width - self.height # new_value - (new_height) new_value = widget.get_value() new_height = int(round(new_value *\ float(self.height) /\ float(self.width))) self.height_entry.set_value(new_height) self.rescale_image() def change_height(self, widget): if self.link_values_button.get_active(): new_value = widget.get_value() new_width = int(round(new_value *\ float(self.width) /\ float(self.height))) self.width_entry.set_value(new_width) self.rescale_image() def image_received(self, widget, context, x, y, selection, targetType, time): self.image = self.emit("get-image-from-galery", selection.data) self.toolbar = self.emit("load-toolbar") self.main_pixbuf = self.image.render_to_preview() self.pixbuf = self.image.render_to_preview() self.image_widget.set_from_pixbuf(self.pixbuf) self.set_current_page(1) self.toolbar.set_sensitive(True) #self.width_entry.set_value(self.image.width) #self.height_entry.set_value(self.image.height) #self.link_values_button.set_active(True) #self.width = self.image.width #self.height = self.image.height def link_unlink(self, widget): active = widget.get_active() widget.set_icon_name("link" if active else "unlink") if active: self.width = self.width_entry.get_value() self.height = self.height_entry.get_value() def rescale_image(self): width = self.width_entry.get_value() height = self.height_entry.get_value() self.pixbuf = self.main_pixbuf.scale_simple(int(width if self.position ==\ 0 or self.position == 180 else height), int(height if self.position ==\ 90 or self.position == 270 else width), gtk.gdk.INTERP_TILES) self.pixbuf = self.pixbuf.rotate_simple(self.position) self.image_widget.set_from_pixbuf(self.pixbuf) def rotate_image(self, widget=None): self.position += 90 self.position = 0 if self.position == 360 else self.position self.pixbuf = self.pixbuf.rotate_simple(90) self.image_widget.set_from_pixbuf(self.pixbuf) """ class ImageEditor(gtk.Notebook): def clear(self): self.filechooser.unselect_all() def set_image(self, path): self.radios[-1].set_active(True) self.saved_image_viewer.set_path(path) def get_image_path(self): current_page = self.get_current_page() if current_page == 0: return self.filechooser.get_filename() elif current_page == 1: return self.clipboardchooser.get_image_path() else: return self.saved_image_viewer.get_path() def __init__(self): gtk.Notebook.__init__(self) self.filechooser = FileChooser() self.filechooser.show() radio1 = gtk.RadioButton(None, "From file") radio1.show() self.append_page(self.filechooser, radio1) self.clipboardchooser = ClipboardChooser() self.clipboardchooser.show() radio2 = gtk.RadioButton(radio1, "From Clipboard") radio2.show() self.append_page(self.clipboardchooser, radio2) self.saved_image_viewer = SavedImageViewer() self.saved_image_viewer.show() radio3 = gtk.RadioButton(radio1, "Saved Image") radio3.show() self.append_page(self.saved_image_viewer, radio3) self.radios = [radio1, radio2, radio3] num = 0 for i in self.radios: i.connect("toggled", self.option_changed, num) num += 1 self.connect('switch-page', self.change_radio) def change_radio(self, widget, page, page_num): self.radios[page_num].set_active(True) def option_changed(self, widget, index): if widget.get_active(): self.set_current_page(index) class FileChooser(gtk.FileChooserWidget): def __init__(self): gtk.FileChooserWidget.__init__(self) self.pixbuf = None self.preview_vbox = gtk.VBox() self.preview_image = gtk.Image() self.preview_image.show() self.preview_vbox.pack_start(self.preview_image) self.preview_vbox.show() self.set_preview_widget(self.preview_vbox) self.connect("update-preview", self.update_preview_cb) self.filter = gtk.FileFilter() self.filter.add_mime_type("image/*") self.set_filter(self.filter) def update_preview_cb(self, widget): filename = self.get_preview_filename() self.pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(filename, 128, 128) self.preview_image.set_from_pixbuf(self.pixbuf) return class SavedImageViewer(gtk.ScrolledWindow): def get_path(self): return self.path def set_path(self, path): self.path = path self.pixbuf = gtk.gdk.pixbuf_new_from_file(path) self.image.set_from_pixbuf(self.pixbuf) def __init__(self): gtk.ScrolledWindow.__init__(self) self.path = None self.image = gtk.Image() self.add_with_viewport(self.image) self.image.show() self.pixbuf = None class ClipboardChooser(gtk.VBox): def get_image_path(self): if self.pixbuf != None: path = tempfile.mktemp(".png") self.pixbuf.save(path, 'png') return path def reload_image(self, widget): clipboard = gtk.Clipboard() self.pixbuf = clipboard.wait_for_image() self.image.set_from_pixbuf(self.pixbuf) def __init__(self): super(ClipboardChooser, self).__init() self.pixbuf = None toolbar = gtk.Toolbar() separator = gtk.SeparatorToolItem() separator.set_draw(False) separator.set_expand(True) separator.show() toolbar.insert(separator, -1) button = gtk.ToolButton(gtk.STOCK_REFRESH) button.set_tooltip_text("Refresh image from clipboard") button.connect('clicked', self.reload_image) button.show() toolbar.insert(button, -1) separator = gtk.SeparatorToolItem() separator.set_draw(False) separator.set_expand(True) separator.show() toolbar.insert(separator, -1) toolbar.show() self.pack_start(toolbar, False, True, 0) self.sw = gtk.ScrolledWindow() self.image = gtk.Image() self.image.show() self.sw.add_with_viewport(self.image) self.sw.show() self.pack_start(self.sw, True, True, 0) """