AnimatedGifResizer.py :  » Network » emesene » emesene-1.6.2 » pygif » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » emesene 
emesene » emesene 1.6.2 » pygif » AnimatedGifResizer.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

### Copyright (C) 2009 Riccardo (C10uD) <c10ud.dev@gmail.com>
###
### This library is free software; you can redistribute it and/or
### modify it under the terms of the GNU Lesser General Public
### License as published by the Free Software Foundation; either
### version 2.1 of the License, or (at your option) any later version.
###
### This library 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
### Lesser General Public License for more details.
###
### You should have received a copy of the GNU Lesser General Public
### License along with this library; if not, write to the Free Software
### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

import pygtk
import gtk
import gobject

class AnimatedResizableImage(gtk.Image):
    def __init__(self, maxh=None, maxw=None):
        gtk.Image.__init__(self)    
        
        self.maxh = maxh
        self.maxw = maxw
        self.edited_dimensions = False
        self.mustKill = False
        self.connect('destroy', self._must_destroy)
        
    def _must_destroy(self, *args):
        self.mustKill = True
        
    def set_tooltip(self, tooltiptext):
        self.set_property('has-tooltip', True)
        self.set_property('tooltip-text', tooltiptext)

    def set_from_filename(self, filename):
        animation = gtk.gdk.PixbufAnimation(filename)
        if animation.is_static_image():
            self._place_static(animation)
            return
        self._start_animation(animation)
        
    def set_from_custom_animation(self, animation):
        if animation.is_static_image():
            self._place_static(animation)
            return
        self._start_animation(animation)
          
    def _place_static(self, animation):
        self.h = animation.get_height()
        self.w = animation.get_width()
        ratio = float(self.w) / self.h
        while self.h > self.maxh or self.w > self.maxw:
            self.edited_dimensions = True
            if self.h < self.maxh:
                self.h -= 10
            else:
                self.h = self.maxh
            self.w = self.h * ratio
            
        if self.edited_dimensions:
            self.set_from_pixbuf(animation.get_static_image().scale_simple(\
                    int(self.w), self.h, gtk.gdk.INTERP_BILINEAR))    
        else:
            self.set_from_pixbuf(animation.get_static_image())    
            
    def _start_animation(self, animation):
        iteran = animation.get_iter()
        
        self.h = animation.get_height()
        self.w = animation.get_width()
        ratio = float(self.w) / self.h
        while self.h > self.maxh or self.w > self.maxw:
            self.edited_dimensions = True
            if self.h <= self.maxh:
                self.h -= 10
            else:
                self.h = self.maxh
            self.w = self.h * ratio  
        
        if not self.edited_dimensions:
            self.set_from_animation(animation) # fallback to default handler
            return
         
        gobject.timeout_add(iteran.get_delay_time(), self._advance, iteran)
    
    def _advance(self, iteran):
        iteran.advance()
        if not self.mustKill:
            self.set_from_pixbuf(iteran.get_pixbuf().scale_simple(int(self.w), \
                    self.h, gtk.gdk.INTERP_NEAREST))
        else:
            return False # end the update game.
        
        gobject.timeout_add(iteran.get_delay_time(), self._advance, iteran)
        return False

    

def close_application(widget, event, data=None):
    gtk.main_quit()
    return False
    
if __name__ == "__main__":
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.connect("delete_event", close_application)
    window.set_border_width(10)
    window.show()
    hbox = gtk.HBox()
    
    ari = AnimatedResizableImage(800, 800)
    ari.set_tooltip('hy')
    ari.set_from_filename('toggi.gif')
        
    hbox.pack_start(ari)
    window.add(hbox)
    window.show_all()
    gtk.main()

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.