<s> from __future__ import division <EOL> import os <EOL> import cairocffi <EOL> from . import base <EOL> from . . import bar <EOL> class Image ( base . _Widget , base . MarginMixin ) : <EOL> """<STR_LIT>""" <EOL> orientations = base . ORIENTATION_BOTH <EOL> defaults = [ <EOL> ( "<STR_LIT>" , True , "<STR_LIT>" ) , <EOL> ( "<STR_LIT:filename>" , None , "<STR_LIT>" ) , <EOL> ] <EOL> def __init__ ( self , length = bar . CALCULATED , width = None , ** config ) : <EOL> if width is not None : <EOL> base . deprecated ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> length = width <EOL> base . _Widget . __init__ ( self , length , ** config ) <EOL> self . add_defaults ( Image . defaults ) <EOL> self . add_defaults ( base . MarginMixin . defaults ) <EOL> self . _widget_defaults [ "<STR_LIT>" ] = <NUM_LIT:0> <EOL> def _configure ( self , qtile , bar ) : <EOL> base . _Widget . _configure ( self , qtile , bar ) <EOL> if not self . filename : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . filename = os . path . expanduser ( self . filename ) <EOL> try : <EOL> self . image = cairocffi . ImageSurface . create_from_png ( self . filename ) <EOL> except MemoryError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % ( self . filename ) ) <EOL> self . pattern = cairocffi . SurfacePattern ( self . image ) <EOL> self . image_width = self . image . get_width ( ) <EOL> self . image_height = self . image . get_height ( ) <EOL> if self . scale : <EOL> if self . bar . horizontal : <EOL> new_height = self . bar . height - ( self . margin_y * <NUM_LIT:2> ) <EOL> if new_height and self . image_height != new_height : <EOL> scaler = cairocffi . Matrix ( ) <EOL> sp = self . image_height / new_height <EOL> self . image_height = new_height <EOL> self . image_width = int ( self . image_width / sp ) <EOL> scaler . scale ( sp , sp ) <EOL> self . pattern . set_matrix ( scaler ) <EOL> else : <EOL> new_width = self . bar . width - ( self . margin_x * <NUM_LIT:2> ) <EOL> if new_width and self . image_width != new_width : <EOL> scaler = cairocffi . Matrix ( ) <EOL> sp = self . image_width / new_width <EOL> self . image_width = new_width <EOL> self . image_height = int ( self . image_height / sp ) <EOL> scaler . scale ( sp , sp ) <EOL> self . pattern . set_matrix ( scaler ) <EOL> def draw ( self ) : <EOL> self . drawer . clear ( self . bar . background ) <EOL> self . drawer . ctx . save ( ) <EOL> self . drawer . ctx . translate ( self . margin_x , self . margin_y ) <EOL> self . drawer . ctx . set_source ( self . pattern ) <EOL> self . drawer . ctx . paint ( ) <EOL> self . drawer . ctx . restore ( ) <EOL> if self . bar . horizontal : <EOL> self . drawer . draw ( offsetx = self . offset , width = self . width ) <EOL> else : <EOL> self . drawer . draw ( offsety = self . offset , height = self . width ) <EOL> def calculate_length ( self ) : <EOL> if self . bar . horizontal : <EOL> return self . image_width + ( self . margin_x * <NUM_LIT:2> ) <EOL> else : <EOL> return self . image_height + ( self . margin_y * <NUM_LIT:2> ) </s>
