Hello all,

(maybe this topic should go in the developer section...)
I was in great need of a way to add Thumbnail pictures in my Product view, and 
no way to use the widget image to show it (I really wanted to have thumbnails 
with a clickable link --> open it in a browser)
So I just finish to modifie the picture.py in the client directory :
client/widget/view/form_gtk/picture.py



class wid_picture(interface.widget_interface):


   def __init__(self, window, parent, model, 
attrs={}):
      interface.widget_interface.__init__(self, window, 
parent=parent, attrs=attrs)

      self._value = ''
      self.height = int(attrs.get('img_height', 
100))
      self.width = int(attrs.get('img_width', 
100))

      self.widget = gtk.VBox(spacing=3)
      self.event = gtk.EventBox()

      self.tooltips = gtk.Tooltips()

      self.image = gtk.Image()
      self.event.add(self.image)
      self.widget.pack_start(self.event, expand=True, 
fill=True)

      self.alignment = gtk.Alignment(xalign=0.5, 
yalign=0.5)
      self.hbox = gtk.HBox(spacing=3)

####   Create the link button

      self.button = gtk.Button()
      img = gtk.Image()
      img.set_from_stock('gtk-jump-to', 
gtk.ICON_SIZE_BUTTON)
      self.button.set_image(img)
      self.button.set_relief(gtk.RELIEF_NONE)
      self.button.connect('clicked', 
self.button_clicked)
      self.button.set_alignment(0.5, 0.5)
      self.button.set_property('can-focus', False)
      self.widget.pack_start(self.button, expand=False, 
fill=False)
####

      self.alignment.add(self.hbox)
      self.widget.pack_start(self.alignment, 
expand=False, fill=False)

      self.tooltips.enable()

      self.update_img()



   def update_img(self):
      if not self._value:
         data = NOIMAGE
      else:
         data = 
urllib.urlopen(self._value).read()

      pixbuf = None
      for type in ('jpeg', 'gif', 'png', 'bmp'):
         loader = 
gtk.gdk.PixbufLoader(type)
         try:
            loader.write(data, 
len(data))
         except:
            continue
         pixbuf = loader.get_pixbuf()
         if pixbuf:
            break
      if not pixbuf:
         loader = 
gtk.gdk.PixbufLoader('png')
         loader.write(NOIMAGE, 
len(NOIMAGE))
         pixbuf = loader.get_pixbuf()

      loader.close()

      img_height = pixbuf.get_height()
      if img_height > self.height:
         height = self.height
      else:
         height = img_height

      img_width = pixbuf.get_width()
      if img_width > self.width:
         width = self.width
      else:
         width = img_width

&nbsp; &nbsp;&nbsp; &nbsp;if &#40;img_width / width&#41; < &#40;img_height / 
height&#41;&#58;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;width = float&#40;img_width&#41; / 
float&#40;img_height&#41; * float&#40;height&#41;
&nbsp; &nbsp;&nbsp; &nbsp;else&#58;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;height = float&#40;img_height&#41; / 
float&#40;img_width&#41; * float&#40;width&#41;

&nbsp; &nbsp;&nbsp; &nbsp;scaled = pixbuf.scale_simple&#40;int&#40;width&#41;, 
int&#40;height&#41;, gtk.gdk.INTERP_BILINEAR&#41;
&nbsp; &nbsp;&nbsp; &nbsp;self.image.set_from_pixbuf&#40;scaled&#41;

&nbsp; &nbsp;def display&#40;self, model, model_field&#41;&#58;
&nbsp; &nbsp;&nbsp; &nbsp;if not model_field&#58;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;return False
&nbsp; &nbsp;&nbsp; &nbsp;self._value = model_field.get&#40;model&#41;
&nbsp; &nbsp;&nbsp; &nbsp;super&#40;wid_picture, self&#41;.display&#40;model, 
model_field&#41;
&nbsp; &nbsp;&nbsp; &nbsp;self.update_img&#40;&#41;

&nbsp; &nbsp;def set_value&#40;self, model, model_field&#41;&#58;
&nbsp; &nbsp;&nbsp; &nbsp;return model_field.set_client&#40;model, self._value 
or False&#41;

&nbsp; &nbsp;def button_clicked&#40;self, widget&#41;&#58;
&nbsp; &nbsp;&nbsp; &nbsp;tools.launch_browser&#40;self._value&#41;


After having this file modified, you'll have to add in the _view.xml a field 
with widget="picture" and in the .py a fields.char (it will contain the path of 
the picture).

Hope it could be usefull...

Eric

ps:I will clean the code during this week and add other buttons (eg : 
add/save/del)[/code]





_______________________________________________
Tinyerp-users mailing list
http://tiny.be/mailman/listinfo/tinyerp-users

Reply via email to