<s> import imghdr <EOL> from PyQt4 . QtCore import * <EOL> from PyQt4 . QtGui import * <EOL> from app import theme_manager <EOL> from app import logger <EOL> from app import constant <EOL> SIGNAL_FINISH = SIGNAL ( '<STR_LIT>' ) <EOL> SIGNAL_PROGRESS = SIGNAL ( '<STR_LIT>' ) <EOL> SIGNAL_ZOOM = SIGNAL ( '<STR_LIT>' ) <EOL> log = logger . getLogger ( __name__ ) <EOL> class DownloadTask ( QThread ) : <EOL> def __init__ ( self , url , manager , parent = None ) : <EOL> super ( DownloadTask , self ) . __init__ ( parent ) <EOL> log . info ( '<STR_LIT>' % url ) <EOL> self . url = url <EOL> self . manager = manager <EOL> def reportHook ( self , transferred_blocks , bytes_per_block , total_size ) : <EOL> percentage = int ( transferred_blocks * bytes_per_block / total_size * <NUM_LIT:100> ) <EOL> self . emit ( SIGNAL_PROGRESS , percentage ) <EOL> def run ( self ) : <EOL> path = self . manager . get ( self . url , self . reportHook ) <EOL> log . info ( '<STR_LIT>' % self . url ) <EOL> log . info ( '<STR_LIT>' % path ) <EOL> self . emit ( SIGNAL_FINISH , path ) <EOL> class LoadingIndicator ( QWidget ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , parent = None ) : <EOL> super ( LoadingIndicator , self ) . __init__ ( parent ) <EOL> self . setupUI ( ) <EOL> def setupUI ( self ) : <EOL> vbox = QVBoxLayout ( ) <EOL> vbox . setAlignment ( Qt . AlignCenter ) <EOL> self . setLayout ( vbox ) <EOL> gif = QMovie ( theme_manager . getParameter ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> gif . start ( ) <EOL> label_loading = QLabel ( ) <EOL> label_loading . setMovie ( gif ) <EOL> vbox . addWidget ( label_loading ) <EOL> self . label_percentage = QLabel ( '<STR_LIT>' ) <EOL> self . label_percentage . setAlignment ( Qt . AlignCenter ) <EOL> self . label_percentage . setStyleSheet ( '<STR_LIT>' ) <EOL> vbox . addWidget ( self . label_percentage ) <EOL> def setPercentage ( self , percentage ) : <EOL> self . label_percentage . setText ( '<STR_LIT>' % int ( percentage ) ) <EOL> class ScrollArea ( QScrollArea ) : <EOL> def __init__ ( self , * argv , ** kwargv ) : <EOL> super ( ScrollArea , self ) . __init__ ( * argv , ** kwargv ) <EOL> self . zoom_delta = <NUM_LIT> <EOL> self . ratio = <NUM_LIT:1.0> <EOL> self . original_size = None <EOL> self . image = None <EOL> self . last_pos = None <EOL> self . cursor_magnify = QCursor ( QPixmap ( constant . CURSOR_MAGNIFY ) ) <EOL> self . left_button_down = False <EOL> self . ctrl_down = False <EOL> self . setVerticalScrollBarPolicy ( Qt . ScrollBarAlwaysOff ) <EOL> self . setHorizontalScrollBarPolicy ( Qt . ScrollBarAlwaysOff ) <EOL> self . verticalScrollBar ( ) . setSingleStep ( <NUM_LIT:1> ) <EOL> self . horizontalScrollBar ( ) . setSingleStep ( <NUM_LIT:1> ) <EOL> def setWidget ( self , w ) : <EOL> super ( ScrollArea , self ) . setWidget ( w ) <EOL> if not isinstance ( w , QLabel ) : <EOL> return <EOL> if w . movie ( ) : <EOL> self . image = QMovie ( w . movie ( ) ) <EOL> rect = self . image . frameRect ( ) <EOL> self . original_size = QSize ( rect . width ( ) , rect . height ( ) ) <EOL> else : <EOL> self . image = QPixmap ( w . pixmap ( ) ) <EOL> self . original_size = self . image . size ( ) <EOL> def wheelEvent ( self , ev ) : <EOL> if not self . ctrl_down : <EOL> if ev . delta ( ) > <NUM_LIT:0> : <EOL> direction = <NUM_LIT:1> <EOL> else : <EOL> direction = - <NUM_LIT:1> <EOL> v = self . verticalScrollBar ( ) <EOL> v . setValue ( v . value ( ) - direction * <NUM_LIT> ) <EOL> return <EOL> widget = self . widget ( ) <EOL> if isinstance ( widget , LoadingIndicator ) : <EOL> return <EOL> ratio = self . ratio <EOL> if ev . delta ( ) > <NUM_LIT:0> : <EOL> ratio += self . zoom_delta <EOL> if ratio > <NUM_LIT> : <EOL> ratio = <NUM_LIT> <EOL> else : <EOL> ratio -= self . zoom_delta <EOL> if ratio < <NUM_LIT> : <EOL> ratio = <NUM_LIT> <EOL> width = self . original_size . width ( ) * ratio <EOL> height = self . original_size . height ( ) * ratio <EOL> if widget . movie ( ) : <EOL> pic = QMovie ( self . image ) <EOL> pic . setScaledSize ( QSize ( width , height ) ) <EOL> pic . start ( ) <EOL> widget . setMovie ( pic ) <EOL> else : <EOL> pic = self . image . scaled ( QSize ( width , height ) , transformMode = Qt . SmoothTransformation ) <EOL> widget . setPixmap ( pic ) <EOL> widget . resize ( QSize ( width , height ) ) <EOL> w = self . viewport ( ) . width ( ) <EOL> h = self . viewport ( ) . height ( ) <EOL> x = self . horizontalScrollBar ( ) . value ( ) + w / <NUM_LIT:2> <EOL> y = self . verticalScrollBar ( ) . value ( ) + h / <NUM_LIT:2> <EOL> self . horizontalScrollBar ( ) . setValue ( x * ratio / self . ratio - w / <NUM_LIT:2> ) <EOL> self . verticalScrollBar ( ) . setValue ( y * ratio / self . ratio - h / <NUM_LIT:2> ) <EOL> self . ratio = ratio <EOL> self . emit ( SIGNAL_ZOOM , self . ratio ) <EOL> def mousePressEvent ( self , ev ) : <EOL> if ev . button ( ) == Qt . LeftButton : <EOL> self . last_pos = ev . globalPos ( ) <EOL> self . left_button_down = True <EOL> else : <EOL> self . left_button_down = False <EOL> def mouseReleaseEvent ( self , ev ) : <EOL> self . left_button_down = False <EOL> def mouseMoveEvent ( self , ev ) : <EOL> if not self . left_button_down : <EOL> return <EOL> pos = ev . globalPos ( ) <EOL> delta_x = pos . x ( ) - self . last_pos . x ( ) <EOL> delta_y = pos . y ( ) - self . last_pos . y ( ) <EOL> v = self . verticalScrollBar ( ) <EOL> v . setValue ( v . value ( ) - delta_y ) <EOL> h = self . horizontalScrollBar ( ) <EOL> h . setValue ( h . value ( ) - delta_x ) <EOL> self . last_pos = pos <EOL> def keyPressEvent ( self , ev ) : <EOL> if ev . key ( ) == Qt . Key_Control : <EOL> self . setCursor ( self . cursor_magnify ) <EOL> self . ctrl_down = True <EOL> def keyReleaseEvent ( self , ev ) : <EOL> if ev . key ( ) == Qt . Key_Control : <EOL> self . setCursor ( Qt . ArrowCursor ) <EOL> self . ctrl_down = False <EOL> class PictureViewer ( QDialog ) : <EOL> def __init__ ( self , url , manager , parent = None ) : <EOL> '''<STR_LIT>''' <EOL> super ( PictureViewer , self ) . __init__ ( parent ) <EOL> self . title = url . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> self . setupUI ( ) <EOL> self . resize ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . setWindowTitle ( '<STR_LIT>' % self . title ) <EOL> self . setMouseTracking ( True ) <EOL> self . task = DownloadTask ( url , manager ) <EOL> self . connect ( self . task , SIGNAL_PROGRESS , self . updateProgress ) <EOL> self . connect ( self . task , SIGNAL_FINISH , self . updateUI ) <EOL> self . task . start ( ) <EOL> self . connect ( self . scroll_area , SIGNAL_ZOOM , self . onZoom ) <EOL> def closeEvent ( self , ev ) : <EOL> self . task . terminate ( ) <EOL> self . close ( ) <EOL> def updateProgress ( self , percentage ) : <EOL> self . indicator . setPercentage ( percentage ) <EOL> def updateUI ( self , path ) : <EOL> '''<STR_LIT>''' <EOL> self . indicator . hide ( ) <EOL> self . scroll_area . takeWidget ( ) <EOL> label_pic = QLabel ( ) <EOL> self . image_type = imghdr . what ( path ) <EOL> if self . image_type == '<STR_LIT>' : <EOL> self . image = QMovie ( path ) <EOL> self . image . start ( ) <EOL> label_pic . setMovie ( self . image ) <EOL> rect = self . image . frameRect ( ) <EOL> self . original_size = QSize ( rect . width ( ) , rect . height ( ) ) <EOL> else : <EOL> self . image = QPixmap ( path , self . image_type ) <EOL> label_pic . setPixmap ( self . image ) <EOL> self . original_size = self . image . size ( ) <EOL> self . scroll_area . setWidget ( label_pic ) <EOL> self . resize ( self . original_size ) <EOL> def setupUI ( self ) : <EOL> vbox = QVBoxLayout ( ) <EOL> vbox . setAlignment ( Qt . AlignCenter ) <EOL> vbox . setContentsMargins ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . setLayout ( vbox ) <EOL> self . scroll_area = ScrollArea ( self ) <EOL> self . scroll_area . setAlignment ( Qt . AlignCenter ) <EOL> vbox . addWidget ( self . scroll_area ) <EOL> self . indicator = LoadingIndicator ( ) <EOL> self . scroll_area . setWidget ( self . indicator ) <EOL> def onZoom ( self , ratio ) : <EOL> self . setWindowTitle ( '<STR_LIT>' % ( int ( ratio * <NUM_LIT:100> ) , self . title ) ) </s>
