<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from PyQt4 import QtCore , QtGui <EOL> from . spreadsheet_helpers import CellHelpers , CellResizer <EOL> class StandardWidgetHeaderView ( QtGui . QHeaderView ) : <EOL> """<STR_LIT>""" <EOL> THICKNESS = <NUM_LIT:30> <EOL> MINIMUM_SIZE = <NUM_LIT:50> <EOL> fitToWindow = True <EOL> def __init__ ( self , orientation , parent = None ) : <EOL> """<STR_LIT>""" <EOL> QtGui . QHeaderView . __init__ ( self , orientation , parent ) <EOL> self . setMovable ( True ) <EOL> self . setFont ( QtGui . QFont ( "<STR_LIT>" , <NUM_LIT:12> , QtGui . QFont . Bold ) ) <EOL> self . resizeSections ( QtGui . QHeaderView . Stretch ) <EOL> self . setClickable ( True ) <EOL> self . setHighlightSections ( True ) <EOL> if orientation == QtCore . Qt . Vertical : <EOL> self . setDefaultAlignment ( QtCore . Qt . AlignHCenter | <EOL> QtCore . Qt . AlignVCenter ) <EOL> self . connect ( self , QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . section_resized ) <EOL> self . _target_size = None <EOL> section_sizes = None <EOL> def read_section_sizes ( self ) : <EOL> if ( self . section_sizes is None or <EOL> len ( self . section_sizes ) != self . count ( ) ) : <EOL> self . section_sizes = [ float ( self . sectionSize ( self . logicalIndex ( i ) ) ) <EOL> for i in xrange ( self . count ( ) ) ] <EOL> _resizing = False <EOL> def section_resized ( self , log_index , old_size , new_size ) : <EOL> if self . _resizing : <EOL> return <EOL> else : <EOL> self . _resizing = True <EOL> try : <EOL> self . _section_resized ( log_index , old_size , new_size ) <EOL> finally : <EOL> self . _resizing = False <EOL> def resize_right_rows ( self , vis_index ) : <EOL> if self . _resizing : <EOL> return <EOL> else : <EOL> self . _resizing = True <EOL> try : <EOL> self . _resize_right_rows ( vis_index ) <EOL> finally : <EOL> self . _resizing = False <EOL> def _section_resized ( self , log_index , old_size , new_size ) : <EOL> """<STR_LIT>""" <EOL> if not self . fitToWindow or self . _target_size is None : <EOL> return <EOL> vis_index = self . visualIndex ( log_index ) <EOL> if vis_index == self . count ( ) - <NUM_LIT:1> : <EOL> self . resizeSection ( log_index , old_size ) <EOL> return <EOL> orig_new_size = new_size <EOL> if new_size < old_size and new_size < self . MINIMUM_SIZE : <EOL> new_size = self . MINIMUM_SIZE <EOL> if self . _target_size is None : <EOL> return <EOL> if new_size > old_size : <EOL> min_right = <NUM_LIT:0> <EOL> for i in xrange ( vis_index + <NUM_LIT:1> , self . count ( ) ) : <EOL> if not self . isSectionHidden ( self . logicalIndex ( i ) ) : <EOL> min_right += self . MINIMUM_SIZE <EOL> pos = self . sectionPosition ( log_index ) <EOL> total_right = self . _target_size - pos - new_size <EOL> if total_right < min_right : <EOL> new_size = self . _target_size - pos - min_right <EOL> if new_size != orig_new_size : <EOL> self . resizeSection ( log_index , new_size ) <EOL> self . read_section_sizes ( ) <EOL> self . section_sizes [ vis_index ] = float ( new_size ) <EOL> self . _resize_right_rows ( vis_index + <NUM_LIT:1> ) <EOL> def _resize_right_rows ( self , vis_index ) : <EOL> self . read_section_sizes ( ) <EOL> previous_space = sum ( self . section_sizes [ vis_index : ] ) <EOL> new_space = self . _target_size - sum ( self . section_sizes [ : vis_index ] ) <EOL> if new_space > previous_space : <EOL> allocated_space = new_space - previous_space <EOL> for i , size in enumerate ( self . section_sizes [ vis_index : ] , vis_index ) : <EOL> size += allocated_space * ( size / previous_space ) <EOL> self . section_sizes [ i ] = size <EOL> self . resizeSection ( self . logicalIndex ( i ) , size ) <EOL> else : <EOL> reclaimed_space = previous_space - new_space <EOL> for i , size in enumerate ( self . section_sizes [ vis_index : ] , vis_index ) : <EOL> size -= reclaimed_space * ( <EOL> ( size - self . MINIMUM_SIZE ) / previous_space ) <EOL> self . section_sizes [ i ] = size <EOL> self . resizeSection ( self . logicalIndex ( i ) , size ) <EOL> def sizeHint ( self ) : <EOL> """<STR_LIT>""" <EOL> size = QtGui . QHeaderView . sizeHint ( self ) <EOL> if self . orientation ( ) == QtCore . Qt . Vertical : <EOL> size . setWidth ( self . THICKNESS ) <EOL> else : <EOL> size . setHeight ( self . THICKNESS ) <EOL> return size <EOL> class StandardWidgetItemDelegate ( QtGui . QItemDelegate ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , table ) : <EOL> """<STR_LIT>""" <EOL> self . table = table <EOL> self . padding = <NUM_LIT:4> <EOL> QtGui . QItemDelegate . __init__ ( self , None ) <EOL> def setPadding ( self , padding ) : <EOL> """<STR_LIT>""" <EOL> if self . padding != padding : <EOL> self . padding = padding <EOL> def updateEditorGeometry ( self , editor , option , index ) : <EOL> """<STR_LIT>""" <EOL> rect = self . table . visualRect ( index ) <EOL> rect . adjust ( self . padding , self . padding , - self . padding , - self . padding ) <EOL> editor . setGeometry ( rect ) <EOL> editor . setFixedSize ( rect . width ( ) , rect . height ( ) ) <EOL> def paint ( self , painter , option , index ) : <EOL> """<STR_LIT>""" <EOL> QtGui . QItemDelegate . paint ( self , painter , option , index ) <EOL> if ( ( index . row ( ) , index . column ( ) ) == self . table . activeCell ) : <EOL> painter . save ( ) <EOL> painter . setPen ( QtGui . QPen ( QtGui . QBrush ( <EOL> QtGui . QColor ( <NUM_LIT> * <NUM_LIT:255> , <NUM_LIT> * <NUM_LIT:255> , <NUM_LIT> * <NUM_LIT:255> ) ) , self . padding ) ) <EOL> r = self . table . visualRect ( index ) <EOL> painter . setClipRegion ( QtGui . QRegion ( r ) ) <EOL> r . adjust ( self . padding // <NUM_LIT:2> , self . padding // <NUM_LIT:2> , <EOL> - self . padding // <NUM_LIT:2> , - self . padding // <NUM_LIT:2> ) <EOL> painter . drawRoundedRect ( r , self . padding , self . padding ) <EOL> painter . restore ( ) <EOL> class StandardWidgetSheet ( QtGui . QTableWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rows = <NUM_LIT:0> , cols = <NUM_LIT:0> , parent = None ) : <EOL> """<STR_LIT>""" <EOL> QtGui . QTableWidget . __init__ ( self , <NUM_LIT:0> , <NUM_LIT:0> , parent ) <EOL> self . setSelectionMode ( QtGui . QAbstractItemView . NoSelection ) <EOL> self . fitToWindow = False <EOL> self . setEditTriggers ( QtGui . QAbstractItemView . NoEditTriggers ) <EOL> self . setHorizontalHeader ( StandardWidgetHeaderView ( QtCore . Qt . Horizontal , <EOL> self ) ) <EOL> self . horizontalHeader ( ) . setSelectionModel ( self . selectionModel ( ) ) <EOL> self . connect ( self . horizontalHeader ( ) , <EOL> QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . updateColumnLabels ) <EOL> self . connect ( self . horizontalHeader ( ) , <EOL> QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . columnMoved ) <EOL> self . connect ( self . horizontalHeader ( ) , <EOL> QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . forceColumnMultiSelect ) <EOL> self . setVerticalHeader ( StandardWidgetHeaderView ( QtCore . Qt . Vertical , <EOL> self ) ) <EOL> self . verticalHeader ( ) . setSelectionModel ( self . selectionModel ( ) ) <EOL> self . connect ( self . verticalHeader ( ) , <EOL> QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . updateRowLabels ) <EOL> self . connect ( self . verticalHeader ( ) , <EOL> QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . rowMoved ) <EOL> self . connect ( self . verticalHeader ( ) , <EOL> QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . forceRowMultiSelect ) <EOL> cornerButton = self . findChild ( QtGui . QAbstractButton ) <EOL> if cornerButton : <EOL> self . connect ( cornerButton , <EOL> QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . forceSheetSelect ) <EOL> self . delegate = StandardWidgetItemDelegate ( self ) <EOL> self . setItemDelegate ( self . delegate ) <EOL> self . helpers = CellHelpers ( parent , CellResizer ( self ) ) <EOL> self . setRowCount ( rows ) <EOL> self . setColumnCount ( cols ) <EOL> self . setFitToWindow ( True ) <EOL> self . connect ( self , <EOL> QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . selectCell ) <EOL> self . activeCell = ( - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> def forceColumnMultiSelect ( self , logicalIndex ) : <EOL> """<STR_LIT>""" <EOL> if ( self . selectionModel ( ) . isColumnSelected ( logicalIndex , QtCore . QModelIndex ( ) ) ) : <EOL> self . selectionModel ( ) . select ( self . model ( ) . index ( <NUM_LIT:0> , logicalIndex ) , <EOL> QtGui . QItemSelectionModel . Deselect | <EOL> QtGui . QItemSelectionModel . Columns ) <EOL> else : <EOL> self . selectionModel ( ) . select ( self . model ( ) . index ( <NUM_LIT:0> , logicalIndex ) , <EOL> QtGui . QItemSelectionModel . Select | <EOL> QtGui . QItemSelectionModel . Columns ) <EOL> def forceRowMultiSelect ( self , logicalIndex ) : <EOL> """<STR_LIT>""" <EOL> if ( self . selectionModel ( ) . isRowSelected ( logicalIndex , QtCore . QModelIndex ( ) ) ) : <EOL> self . selectionModel ( ) . select ( self . model ( ) . index ( logicalIndex , <NUM_LIT:0> ) , <EOL> QtGui . QItemSelectionModel . Deselect | <EOL> QtGui . QItemSelectionModel . Rows ) <EOL> else : <EOL> self . selectionModel ( ) . select ( self . model ( ) . index ( logicalIndex , <NUM_LIT:0> ) , <EOL> QtGui . QItemSelectionModel . Select | <EOL> QtGui . QItemSelectionModel . Rows ) <EOL> def forceSheetSelect ( self ) : <EOL> """<STR_LIT>""" <EOL> totalCells = self . rowCount ( ) * self . columnCount ( ) <EOL> if ( len ( self . selectionModel ( ) . selectedIndexes ( ) ) < totalCells ) : <EOL> self . selectionModel ( ) . select ( <EOL> QtGui . QItemSelection ( self . model ( ) . index ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> self . model ( ) . index ( self . rowCount ( ) - <NUM_LIT:1> , <EOL> self . columnCount ( ) - <NUM_LIT:1> ) ) , <EOL> QtGui . QItemSelectionModel . Select ) <EOL> else : <EOL> self . selectionModel ( ) . clearSelection ( ) <EOL> def updateHeaderStatus ( self ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> self . horizontalHeader ( ) . setVisible ( self . columnCount ( ) > <NUM_LIT:1> or <EOL> self . rowCount ( ) > <NUM_LIT:1> ) <EOL> self . verticalHeader ( ) . setVisible ( self . columnCount ( ) > <NUM_LIT:1> or <EOL> self . rowCount ( ) > <NUM_LIT:1> ) <EOL> def updateRowLabels ( self , oldCount , newCount ) : <EOL> """<STR_LIT>""" <EOL> vLabels = [ ] <EOL> vIdx = self . verticalHeader ( ) . visualIndex <EOL> for i in xrange ( newCount ) : <EOL> vLabels . append ( str ( vIdx ( i ) + <NUM_LIT:1> ) ) <EOL> self . setVerticalHeaderLabels ( vLabels ) <EOL> self . updateHeaderStatus ( ) <EOL> def rowMoved ( self , row , old , new ) : <EOL> """<STR_LIT>""" <EOL> self . updateRowLabels ( self . rowCount ( ) , self . rowCount ( ) ) <EOL> def updateColumnLabels ( self , oldCount , newCount ) : <EOL> """<STR_LIT>""" <EOL> hLabels = [ ] <EOL> vIdx = self . horizontalHeader ( ) . visualIndex <EOL> for i in xrange ( newCount ) : <EOL> hLabels . append ( chr ( vIdx ( i ) + ord ( '<STR_LIT:A>' ) ) ) <EOL> self . setHorizontalHeaderLabels ( hLabels ) <EOL> self . updateHeaderStatus ( ) <EOL> def columnMoved ( self , row , old , new ) : <EOL> """<STR_LIT>""" <EOL> self . updateColumnLabels ( self . columnCount ( ) , self . columnCount ( ) ) <EOL> def setFitToWindow ( self , fit = True ) : <EOL> """<STR_LIT>""" <EOL> if fit != self . fitToWindow : <EOL> self . fitToWindow = fit <EOL> self . horizontalHeader ( ) . fitToWindow = fit <EOL> self . horizontalHeader ( ) . _target_size = None <EOL> self . verticalHeader ( ) . fitToWindow = fit <EOL> self . verticalHeader ( ) . _target_size = None <EOL> if not fit : <EOL> width = self . columnWidth ( self . columnCount ( ) - <NUM_LIT:1> ) <EOL> height = self . rowHeight ( self . rowCount ( ) - <NUM_LIT:1> ) <EOL> self . setColumnWidth ( self . columnCount ( ) - <NUM_LIT:1> , width ) <EOL> self . setRowHeight ( self . rowCount ( ) - <NUM_LIT:1> , height ) <EOL> self . stretchCells ( ) <EOL> policy = ( QtCore . Qt . ScrollBarAlwaysOff if fit <EOL> else QtCore . Qt . ScrollBarAlwaysOn ) <EOL> self . setHorizontalScrollBarPolicy ( policy ) <EOL> self . setVerticalScrollBarPolicy ( policy ) <EOL> def showEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . stretchCells ( ) <EOL> def stretchCells ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . fitToWindow : <EOL> self . horizontalHeader ( ) . resizeSections ( QtGui . QHeaderView . Stretch ) <EOL> self . verticalHeader ( ) . resizeSections ( QtGui . QHeaderView . Stretch ) <EOL> def showHelpers ( self , show , row , col ) : <EOL> """<STR_LIT>""" <EOL> if self . helpers . isInteracting ( ) : <EOL> return <EOL> if show : <EOL> if row >= <NUM_LIT:0> and col >= <NUM_LIT:0> : <EOL> self . helpers . snapTo ( row , col ) <EOL> self . helpers . adjustPosition ( ) <EOL> self . helpers . show ( ) <EOL> else : <EOL> self . helpers . hide ( ) <EOL> else : <EOL> self . helpers . hide ( ) <EOL> def resizeEvent ( self , e ) : <EOL> if not self . fitToWindow : <EOL> return <EOL> thickness = StandardWidgetHeaderView . THICKNESS <EOL> self . horizontalHeader ( ) . _target_size = self . size ( ) . width ( ) - thickness <EOL> self . horizontalHeader ( ) . resize_right_rows ( <NUM_LIT:0> ) <EOL> self . verticalHeader ( ) . _target_size = self . size ( ) . height ( ) - thickness <EOL> self . verticalHeader ( ) . resize_right_rows ( <NUM_LIT:0> ) <EOL> def getRealLocation ( self , vRow , vCol , visual = False ) : <EOL> """<STR_LIT>""" <EOL> if visual : <EOL> ( row , col ) = ( vRow , vCol ) <EOL> else : <EOL> row = self . verticalHeader ( ) . logicalIndex ( vRow ) <EOL> col = self . horizontalHeader ( ) . logicalIndex ( vCol ) <EOL> cellSet = set ( ) <EOL> for r in xrange ( self . rowCount ( ) ) : <EOL> for c in xrange ( self . columnCount ( ) ) : <EOL> cellSet . add ( ( r , c ) ) <EOL> for r in xrange ( self . rowCount ( ) ) : <EOL> for c in xrange ( self . columnCount ( ) ) : <EOL> if ( r , c ) not in cellSet : <EOL> continue <EOL> rect = self . visualRect ( self . model ( ) . index ( r , c ) ) <EOL> rSpan = self . rowSpan ( r , c ) <EOL> cSpan = self . columnSpan ( r , c ) <EOL> for rs in xrange ( rSpan ) : <EOL> for cs in xrange ( cSpan ) : <EOL> if ( row == r + rs ) and ( col == c + cs ) : <EOL> return ( r , c ) <EOL> if ( r + rs , c + cs ) in cellSet : <EOL> cellSet . remove ( ( r + rs , c + cs ) ) <EOL> return ( - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> def getCell ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> return self . cellWidget ( * self . getRealLocation ( row , col ) ) <EOL> def getCellRect ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> idx = self . model ( ) . index ( * self . getRealLocation ( row , col ) ) <EOL> return self . visualRect ( idx ) <EOL> def getCellGlobalRect ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> rect = self . getCellRect ( row , col ) <EOL> rect . moveTo ( self . viewport ( ) . mapToGlobal ( rect . topLeft ( ) ) ) <EOL> return rect <EOL> def setCellByWidget ( self , row , col , cellWidget ) : <EOL> """<STR_LIT>""" <EOL> if cellWidget : <EOL> cellWidget . setMinimumSize ( QtCore . QSize ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> cellWidget . setMaximumSize ( QtCore . QSize ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> cellWidget . setParent ( self ) <EOL> ( row , col ) = self . getRealLocation ( row , col ) <EOL> index = self . model ( ) . index ( row , col ) <EOL> self . setCellWidget ( row , col , cellWidget ) <EOL> if cellWidget : <EOL> self . delegate . updateEditorGeometry ( cellWidget , None , index ) <EOL> if ( row , col ) == self . activeCell : <EOL> self . setActiveCell ( row , col ) <EOL> def selectCell ( self , row , col , toggling ) : <EOL> """<STR_LIT>""" <EOL> ( row , col ) = self . getRealLocation ( row , col , visual = True ) <EOL> if toggling : <EOL> self . selectionModel ( ) . setCurrentIndex ( self . model ( ) . index ( row , col ) , <EOL> QtGui . QItemSelectionModel . Toggle ) <EOL> if ( self . selectionModel ( ) . isSelected ( self . model ( ) . index ( row , col ) ) ) : <EOL> self . setActiveCell ( row , col ) <EOL> else : <EOL> self . setActiveCell ( - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> else : <EOL> if len ( self . selectionModel ( ) . selectedIndexes ( ) ) <= <NUM_LIT:1> : <EOL> self . selectionModel ( ) . setCurrentIndex ( <EOL> self . model ( ) . index ( row , col ) , <EOL> QtGui . QItemSelectionModel . ClearAndSelect ) <EOL> self . setActiveCell ( row , col ) <EOL> self . viewport ( ) . repaint ( ) <EOL> def setActiveCell ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> self . activeCell = ( row , col ) <EOL> toolBar = self . parent ( ) . getCellToolBar ( row , col ) <EOL> if toolBar : <EOL> toolBar . snapTo ( row , col ) <EOL> self . parent ( ) . toolBar . setCellToolBar ( toolBar ) <EOL> def adjustWidgetGeometry ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> cellWidget = self . getCell ( row , col ) <EOL> if cellWidget : <EOL> index = self . model ( ) . index ( * self . getRealLocation ( row , col ) ) <EOL> self . delegate . updateEditorGeometry ( cellWidget , None , index ) </s>
