Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/images.py
blob: 383d3176d467695c2669cf2c9b328ec2ad07710f (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# images.py
#
# Copyright 2012 S. Daniel Francis <francis@sugarlabs.org>
#
# 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)