1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21
##############################################################################
33
from cStringIO import StringIO
34
from base64 import encodestring, decodestring
36
from common import terp_path_pixmaps
37
from widget.view import interface
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
45
class ViewGallery(object):
46
def __init__(self, model, fields, attrs, gallery_fields):
47
self.widget = gtk.IconView()
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)
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)
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))
69
def create_composite_pixbuf(self, pb):
70
"""Return composited pixbuf
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)
78
if (imgw / w) < (imgh / h):
82
pb_scaled = pb.scale_simple(int(w), int(h), gtk.gdk.INTERP_BILINEAR)
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)
90
pb_scaled.composite(pb_new,
95
gtk.gdk.INTERP_BILINEAR,
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)
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)
111
loader.write(pict_data, pict_data_len)
114
pict_buffer = loader.get_pixbuf()
119
# or use default picture (code from form_gtk/image.py)
121
pict_pixbuf = self.default_picture.copy()
123
pict_pixbuf = self.create_composite_pixbuf(pict_buffer)
125
return (mo, pict_pixbuf, mo.value[self.field_text])
127
def create_store(self, model_group):
128
return GalleryStore(model_group,
129
gobject.TYPE_PYOBJECT,
133
def display(self, screen, reload=False):
134
models = screen.models
136
active_screen_id = (screen.current_model and screen.current_model.id or None)
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
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)
149
self.widget.set_model(None)
151
self.store = new_store
152
self.widget.set_model(self.store)
155
self.set_cursor(screen)
157
def set_cursor(self, screen):
158
current_model = screen.current_model
161
cursor = screen.models.models.index(current_model)
165
self.select_path(cursor)
166
self.widget.set_cursor(cursor)
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)
173
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: