Back to index

system-config-printer  1.3.9+20120706
gtkspinner.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## system-config-printer
00004 
00005 ## Copyright (C) 2009, 2010 Red Hat, Inc
00006 ## Author: Tim Waugh <twaugh@redhat.com>
00007 
00008 ## This program is free software; you can redistribute it and/or modify
00009 ## it under the terms of the GNU General Public License as published by
00010 ## the Free Software Foundation; either version 2 of the License, or
00011 ## (at your option) any later version.
00012 
00013 ## This program is distributed in the hope that it will be useful,
00014 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 ## GNU General Public License for more details.
00017 
00018 ## You should have received a copy of the GNU General Public License
00019 ## along with this program; if not, write to the Free Software
00020 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 
00022 import glib
00023 import gobject
00024 import gtk
00025 
00026 class Spinner:
00027     def __init__ (self, image):
00028         self.image = image
00029         frames = []
00030         theme = gtk.icon_theme_get_default ()
00031         icon_info = theme.lookup_icon ("process-working", 22, 0)
00032         if icon_info != None:
00033             size = icon_info.get_base_size ()
00034             icon = icon_info.get_filename ()
00035             try:
00036                 pixbuf = gtk.gdk.pixbuf_new_from_file (icon)
00037                 grid_width = pixbuf.get_width ()
00038                 grid_height = pixbuf.get_height ()
00039                 y = 0
00040                 while y < grid_height:
00041                     x = 0
00042                     while x < grid_width:
00043                         frame = pixbuf.subpixbuf (x, y, size, size)
00044                         frames.append (frame)
00045                         x += size
00046 
00047                     y += size
00048             except gobject.GError:
00049                 # Failed to load icon.
00050                 pass
00051 
00052         self.frames = frames
00053         self.n_frames = len (frames)
00054         self._rest ()
00055 
00056     def _set_frame (self, n):
00057         self._current_frame = n
00058         if self.n_frames < 2:
00059             self.image.clear ()
00060             return
00061 
00062         self.image.set_from_pixbuf (self.frames[n])
00063 
00064     def _rest (self):
00065         self._set_frame (0)
00066 
00067     def _next_frame (self):
00068         n = self._current_frame + 1
00069         if n >= self.n_frames:
00070             n = 1
00071 
00072         gtk.gdk.threads_enter ()
00073         self._set_frame (n)
00074         gtk.gdk.threads_leave ()
00075         return True
00076 
00077     def start (self, timeout=125):
00078         self._task = glib.timeout_add (timeout, self._next_frame)
00079 
00080     def stop (self):
00081         glib.source_remove (self._task)
00082         self._rest ()