<s> from PyQt4 . QtGui import * <EOL> from PyQt4 . QtCore import * <EOL> import PyQt4 . QtCore as QtCore <EOL> import PyQt4 . QtGui as QtGui <EOL> from electrum import bmp , pyqrnative <EOL> class QRCodeWidget ( QWidget ) : <EOL> def __init__ ( self , data = None ) : <EOL> QWidget . __init__ ( self ) <EOL> self . addr = None <EOL> self . qr = None <EOL> if data : <EOL> self . set_addr ( data ) <EOL> self . update_qr ( ) <EOL> def set_addr ( self , addr ) : <EOL> if self . addr != addr : <EOL> if len ( addr ) < <NUM_LIT> : <EOL> MinSize = <NUM_LIT> <EOL> else : <EOL> MinSize = <NUM_LIT> <EOL> self . setMinimumSize ( MinSize , MinSize ) <EOL> self . addr = addr <EOL> self . qr = None <EOL> self . update ( ) <EOL> def update_qr ( self ) : <EOL> if self . addr and not self . qr : <EOL> for size in range ( len ( pyqrnative . QRUtil . PATTERN_POSITION_TABLE ) ) : <EOL> try : <EOL> self . qr = pyqrnative . QRCode ( size , pyqrnative . QRErrorCorrectLevel . L ) <EOL> self . qr . addData ( self . addr ) <EOL> self . qr . make ( ) <EOL> break <EOL> except Exception : <EOL> self . qr = None <EOL> continue <EOL> self . update ( ) <EOL> def paintEvent ( self , e ) : <EOL> if not self . addr : <EOL> return <EOL> black = QColor ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ) <EOL> white = QColor ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> if not self . qr : <EOL> qp = QtGui . QPainter ( ) <EOL> qp . begin ( self ) <EOL> qp . setBrush ( white ) <EOL> qp . setPen ( white ) <EOL> qp . drawRect ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ) <EOL> qp . end ( ) <EOL> return <EOL> k = self . qr . getModuleCount ( ) <EOL> qp = QtGui . QPainter ( ) <EOL> qp . begin ( self ) <EOL> r = qp . viewport ( ) <EOL> boxsize = min ( r . width ( ) , r . height ( ) ) * <NUM_LIT> / k <EOL> size = k * boxsize <EOL> left = ( r . width ( ) - size ) / <NUM_LIT:2> <EOL> top = ( r . height ( ) - size ) / <NUM_LIT:2> <EOL> margin = <NUM_LIT:10> <EOL> qp . setBrush ( white ) <EOL> qp . drawRect ( left - margin , top - margin , size + ( margin * <NUM_LIT:2> ) , size + ( margin * <NUM_LIT:2> ) ) <EOL> for r in range ( k ) : <EOL> for c in range ( k ) : <EOL> if self . qr . isDark ( r , c ) : <EOL> qp . setBrush ( black ) <EOL> qp . setPen ( black ) <EOL> else : <EOL> qp . setBrush ( white ) <EOL> qp . setPen ( white ) <EOL> qp . drawRect ( left + c * boxsize , top + r * boxsize , boxsize , boxsize ) <EOL> qp . end ( ) </s>
