~openerp-dev/openobject-client/duplication_of_ir.filters

« back to all changes in this revision

Viewing changes to bin/widget/view/gallery_gtk/gallery.py

  • Committer: RGA(OpenERP)
  • Date: 2010-10-12 11:11:19 UTC
  • mfrom: (1564.1.45 trunk)
  • Revision ID: rga@tinyerp.com-20101012111119-jsuwv2erjdxq44us
Merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution   
5
 
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6
 
#    $Id$
7
 
#
8
 
#    This program is free software: you can redistribute it and/or modify
9
 
#    it under the terms of the GNU General Public License as published by
10
 
#    the Free Software Foundation, either version 3 of the License, or
11
 
#    (at your option) any later version.
12
 
#
13
 
#    This program is distributed in the hope that it will be useful,
14
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#    GNU General Public License for more details.
17
 
#
18
 
#    You should have received a copy of the GNU General Public License
19
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
#
21
 
##############################################################################
22
 
 
23
 
 
24
 
import gtk
25
 
from gtk import glade
26
 
import gobject
27
 
 
28
 
import time
29
 
import locale
30
 
 
31
 
import rpc
32
 
import tools
33
 
from cStringIO import StringIO
34
 
from base64 import encodestring, decodestring
35
 
 
36
 
from common import terp_path_pixmaps
37
 
from widget.view import interface
38
 
 
39
 
class GalleryStore(gtk.ListStore):
40
 
    def __init__(self, model_group, *args):
41
 
        r = super(GalleryStore, self).__init__(*args)
42
 
        self.model_group = model_group
43
 
        return r
44
 
 
45
 
class ViewGallery(object):
46
 
    def __init__(self, model, fields, attrs, gallery_fields):
47
 
        self.widget = gtk.IconView()
48
 
 
49
 
        self.store = None
50
 
        self.field_text = gallery_fields['text']
51
 
        self.field_picture = gallery_fields['image']
52
 
        self.height = attrs.get('height', 75)
53
 
        self.width = attrs.get('width', 75)
54
 
        self.col = attrs.get('col', -1)
55
 
 
56
 
        # set some property of gtk.IconView
57
 
        self.widget.set_pixbuf_column(1)
58
 
        self.widget.set_text_column(2)
59
 
        self.widget.set_selection_mode(gtk.SELECTION_MULTIPLE)
60
 
        if self.col != -1 and self.col > 0:
61
 
            self.widget.set_columns(self.col)
62
 
        self.widget.set_item_width(self.width + 20)
63
 
 
64
 
        # Create a blank pictures
65
 
        self.blank_picture = gtk.gdk.pixbuf_new_from_file(terp_path_pixmaps('noimage.trans.png')).scale_simple(int(self.width), int(self.height), gtk.gdk.INTERP_BILINEAR)
66
 
        self.default_picture = self.create_composite_pixbuf(self.widget.render_icon('terp-eog-image-collection', gtk.ICON_SIZE_DIALOG))
67
 
        # Refresh
68
 
 
69
 
    def create_composite_pixbuf(self, pb):
70
 
        """Return composited pixbuf
71
 
        """
72
 
        # scale image if needed
73
 
        imgh = float(pb.get_height())
74
 
        h = float(imgh > self.height and self.height or imgh)
75
 
        imgw = float(pb.get_width())
76
 
        w = float(imgw > self.width and self.width or imgw)
77
 
 
78
 
        if (imgw / w) < (imgh / h):
79
 
            w = imgw / imgh * h
80
 
        else:
81
 
            h = imgh / imgw * w
82
 
        pb_scaled = pb.scale_simple(int(w), int(h), gtk.gdk.INTERP_BILINEAR)
83
 
 
84
 
        # create a composite image on top of blank pixbuf to make the picture
85
 
        # centered horizontally and verically
86
 
        pb_new = self.blank_picture.copy()
87
 
        pb_new_x = int((pb_new.get_width() - w) / 2.0)
88
 
        pb_new_y = int((pb_new.get_height() - h) / 2.0)
89
 
 
90
 
        pb_scaled.composite(pb_new,
91
 
                pb_new_x, pb_new_y,
92
 
                int(w), int(h),
93
 
                pb_new_x, pb_new_y,
94
 
                1.0, 1.0,
95
 
                gtk.gdk.INTERP_BILINEAR,
96
 
                255)
97
 
 
98
 
        return pb_new
99
 
 
100
 
    def create_store_model(self, mo):
101
 
        # try loading picture data
102
 
        pict_field = mo.mgroup.mfields.get(self.field_picture, False)
103
 
        pict_data = pict_field.get(mo)
104
 
        pict_buffer = None
105
 
        if pict_data:
106
 
            pict_data = decodestring(pict_data)
107
 
            pict_data_len = len(pict_data)
108
 
            for type in ('jpeg','gif','png','bmp','tiff'):
109
 
                loader = gtk.gdk.PixbufLoader(type)
110
 
                try:
111
 
                    loader.write(pict_data, pict_data_len)
112
 
                except:
113
 
                    continue
114
 
                pict_buffer = loader.get_pixbuf()
115
 
                if pict_buffer:
116
 
                    loader.close()
117
 
                    break
118
 
 
119
 
        # or use default picture (code from form_gtk/image.py)
120
 
        if not pict_buffer:
121
 
            pict_pixbuf = self.default_picture.copy()
122
 
        else:
123
 
            pict_pixbuf = self.create_composite_pixbuf(pict_buffer)
124
 
 
125
 
        return (mo, pict_pixbuf, mo.value[self.field_text])
126
 
 
127
 
    def create_store(self, model_group):
128
 
        return GalleryStore(model_group,
129
 
                            gobject.TYPE_PYOBJECT,
130
 
                            gtk.gdk.Pixbuf,
131
 
                            gobject.TYPE_STRING)
132
 
 
133
 
    def display(self, screen, reload=False):
134
 
        models = screen.models
135
 
        active_path = None
136
 
        active_screen_id = (screen.current_model and screen.current_model.id or None)
137
 
 
138
 
        if reload:
139
 
            # Create a store for our iconview and fill it with stock icons
140
 
            new_store = self.create_store(models)
141
 
            new_store.model_group = models
142
 
 
143
 
            for mo in (models and models.models or []):
144
 
                # add new line to store
145
 
                stor_item_data = self.create_store_model(mo)
146
 
                item_iter = new_store.append(stor_item_data)
147
 
 
148
 
            if self.store:
149
 
                self.widget.set_model(None)
150
 
                del(self.store)
151
 
            self.store = new_store
152
 
            self.widget.set_model(self.store)
153
 
 
154
 
        # update active item
155
 
        self.set_cursor(screen)
156
 
 
157
 
    def set_cursor(self, screen):
158
 
        current_model = screen.current_model
159
 
        if current_model:
160
 
            try:
161
 
                cursor = screen.models.models.index(current_model)
162
 
            except ValueError:
163
 
                # item was not found
164
 
                return
165
 
            self.select_path(cursor)
166
 
            self.widget.set_cursor(cursor)
167
 
 
168
 
    def select_path(self, path):
169
 
        self.widget.unselect_all()
170
 
        self.widget.select_path(path)
171
 
#        self.widget.scroll_to_path(path, False, 0.5, 0.5)
172
 
 
173
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: