#!/usr/bin/env python # -*- coding: utf-8 -*- # # animation.py # # Copyright 2012 S. Daniel Francis , # Agustin Zubiaga # # 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 import gobject import logging _logger = logging.getLogger('animate-animation') _logger.setLevel(logging.DEBUG) logging.basicConfig() class Animation(gtk.Image): def __init__(self, width, height, sleep_time=1): gtk.Image.__init__(self) self._current_image = 0 self._timeout = None self._return = True self._running = False self._images = [] self._size = width, height self._sleep_time = sleep_time * 1000 def set_return(self, _return=True): """Defines if when get to the last image, should return to the first""" self._return = _return def add_image(self, image_path): """Appends an image to the list""" self._images += [image_path] def run(self): """Runs the animation""" if not self._running: try: pixbuf = gtk.gdk.pixbuf_new_from_file_at_size( self._images[self._current_image], self._size[0], self._size[1]) self.set_from_pixbuf(pixbuf) self._running = True self._timeout = gobject.timeout_add(self._sleep_time, self.next) _logger.info(" Running!") except IndexError: _logger.error(" There aren't images") else: _logger.error(" The animation is runing already") def set_pos(self, pos): self._current_image = pos pixbuf = gtk.gdk.pixbuf_new_from_file_at_size( self._images[self._current_image], self._size[0], self._size[1]) self.set_from_pixbuf(pixbuf) def stop(self): """Stops the animation""" gobject.source_remove(self._timeout) self._running = False def next(self): """Shows the next frame""" if self._current_image != len(self._images) - 1: self._current_image += 1 elif not self._current_image != len(self._images) - 1 and self._return: self._current_image = 0 pixbuf = gtk.gdk.pixbuf_new_from_file_at_size( self._images[self._current_image], self._size[0], self._size[1]) self.set_from_pixbuf(pixbuf) return self._running def back(self): """Shows the previous frame""" if self._current_image != 0: self._current_image -= 1 elif self._current_image == 0 and self._return: self._current_image = -1 pixbuf = gtk.gdk.pixbuf_new_from_file_at_size( self._images[self._current_image], self._size[0], self._size[1]) self.set_from_pixbuf(pixbuf)