Package dtk :: Package ui :: Module cache_pixbuf

Source Code for Module dtk.ui.cache_pixbuf

 1  #! /usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3   
 4  # Copyright (C) 2011 ~ 2012 Deepin, Inc. 
 5  #               2011 ~ 2012 Wang Yong 
 6  #  
 7  # Author:     Wang Yong <lazycat.manatee@gmail.com> 
 8  # Maintainer: Wang Yong <lazycat.manatee@gmail.com> 
 9  #  
10  # This program is free software: you can redistribute it and/or modify 
11  # it under the terms of the GNU General Public License as published by 
12  # the Free Software Foundation, either version 3 of the License, or 
13  # any later version. 
14  #  
15  # This program is distributed in the hope that it will be useful, 
16  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
18  # GNU General Public License for more details. 
19  #  
20  # You should have received a copy of the GNU General Public License 
21  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
22   
23  import gtk 
24   
25 -class CachePixbuf(object):
26 ''' 27 Cache pixbuf use to cache pixbuf to avoid new pixbuf generate by scale_simple. 28 29 gtk.gdk.pixbuf.scale_simple is function will make application very slow, 30 31 We use CachePixbuf increase the call times of gtk.gdk.pixbuf.scale_simple. 32 ''' 33
34 - def __init__(self):
35 ''' 36 Init cache pixbuf. 37 ''' 38 self.pixbuf = None 39 self.cache_pixbuf = None 40 self.scale_width = None 41 self.scale_height = None 42 self.vertical_mirror = False 43 self.horizontal_mirror = False
44
45 - def scale(self, pixbuf, scale_width, scale_height, vertical_mirror=False, horizontal_mirror=False):
46 ''' 47 Scale with given sizce and return new pixbuf. 48 49 @param pixbuf: Original pixbuf. 50 @param scale_width: Scale width of pixbuf. 51 @param scale_height: Scale height of pixbuf. 52 @param vertical_mirror: Whether pixbuf mirror vertically. 53 @param horizontal_mirror: Whether pixbuf mirror horizontally. 54 ''' 55 if self.pixbuf != pixbuf or self.scale_width != scale_width or self.scale_height != scale_height: 56 # Record init value. 57 self.pixbuf = pixbuf # pixbuf always is same as create from file 58 self.scale_width = scale_width 59 self.scale_height = scale_height 60 61 # Scale size. 62 self.cache_pixbuf = pixbuf.scale_simple(scale_width, scale_height, gtk.gdk.INTERP_BILINEAR) 63 64 # If vertical_mirror default is True, vertical mirror *original* pixbuf first. 65 if self.vertical_mirror: 66 self.cache_pixbuf = self.cache_pixbuf.flip(True) 67 68 # If horizontal_mirror default is True, horizontal mirror *original* pixbuf first. 69 if self.horizontal_mirror: 70 self.cache_pixbuf = self.cache_pixbuf.flip(False) 71 72 # If vertical_mirror changed, vertical mirror *self* first 73 if self.vertical_mirror != vertical_mirror: 74 self.vertical_mirror = vertical_mirror 75 self.cache_pixbuf = self.cache_pixbuf.flip(True) 76 77 # If horizontal_mirror changed, horizontal mirror *self* first 78 if self.horizontal_mirror != horizontal_mirror: 79 self.horizontal_mirror = horizontal_mirror 80 self.cache_pixbuf = self.cache_pixbuf.flip(False)
81
82 - def get_cache(self):
83 ''' 84 Get pixbuf cache. 85 86 @return: Return cache pixbuf. 87 ''' 88 return self.cache_pixbuf
89