<s> """<STR_LIT>""" <EOL> import pyui <EOL> import copy <EOL> from pyui . desktop import getDesktop , getTheme , getRenderer <EOL> class GridPanel ( pyui . widgets . Panel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , visibleWidth , visibleHeight , useColumnHeaders = <NUM_LIT:1> , useRowHeaders = <NUM_LIT:1> ) : <EOL> self . vWidth = visibleWidth <EOL> self . vHeight = visibleHeight <EOL> self . scrollPos = <NUM_LIT:0> <EOL> pyui . widgets . Panel . __init__ ( self ) <EOL> self . setLayout ( pyui . layouts . BorderLayoutManager ( ) ) <EOL> self . cheader = ColumnHeaders ( visibleWidth ) <EOL> self . rheader = RowHeaders ( visibleHeight ) <EOL> self . scrollBar = pyui . widgets . VScroll ( ) <EOL> self . scrollBar . resize ( <NUM_LIT:10> , <NUM_LIT:50> ) <EOL> self . cellPanel = CellPanel ( visibleWidth , visibleHeight ) <EOL> if useColumnHeaders : <EOL> self . addChild ( self . cheader , pyui . layouts . BorderLayoutManager . NORTH ) <EOL> if useRowHeaders : <EOL> self . addChild ( self . rheader , pyui . layouts . BorderLayoutManager . WEST ) <EOL> self . addChild ( self . cellPanel , pyui . layouts . BorderLayoutManager . CENTER ) <EOL> self . addChild ( self . scrollBar , pyui . layouts . BorderLayoutManager . EAST ) <EOL> self . pack ( ) <EOL> def resize ( self , w , h ) : <EOL> print "<STR_LIT>" , w , h <EOL> pyui . widgets . Panel . resize ( self , w , h ) <EOL> def setColumnName ( self , columnNum , name ) : <EOL> self . cheader . setColumnName ( columnNum , name ) <EOL> def setRowName ( self , rowNum , name ) : <EOL> self . rheader . setRowName ( rowNum , name ) <EOL> def getCellAt ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> return self . cellPanel . getCellAt ( x , y ) <EOL> def putCellAt ( self , widget , x , y ) : <EOL> """<STR_LIT>""" <EOL> return self . cellPanel . putCellAt ( widget , x , y ) <EOL> def removeCellAt ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> return self . cellPanel . removeCellAt ( x , y ) <EOL> def findCellAt ( self , posX , posY ) : <EOL> """<STR_LIT>""" <EOL> return self . cellPanel . findCellAt ( posX , posY ) <EOL> def findCoordinatesAt ( self , posX , posY ) : <EOL> """<STR_LIT>""" <EOL> return self . cellPanel . findCoordinatesAt ( posX , posY ) <EOL> def clear ( self ) : <EOL> return self . cellPanel . clear ( ) <EOL> class CellPanel ( pyui . widgets . Panel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vWidth , vHeight ) : <EOL> pyui . widgets . Panel . __init__ ( self ) <EOL> self . vWidth = float ( vWidth ) <EOL> self . vHeight = float ( vHeight ) <EOL> self . cells = { } <EOL> self . scrollPos = <NUM_LIT:0> <EOL> self . cellWidth = <NUM_LIT:1> <EOL> self . cellHeight = <NUM_LIT:1> <EOL> self . numRows = vHeight <EOL> self . registerEvent ( pyui . locals . SCROLLPOS , self . onScroll ) <EOL> def resize ( self , width , height ) : <EOL> pyui . widgets . Panel . resize ( self , width , height ) <EOL> self . cellWidth = self . windowRect [ <NUM_LIT:2> ] / self . vWidth <EOL> self . cellHeight = self . windowRect [ <NUM_LIT:3> ] / self . vHeight <EOL> self . setupAllCells ( ) <EOL> def setupAllCells ( self ) : <EOL> for key in self . cells . keys ( ) : <EOL> if key [ <NUM_LIT:1> ] >= self . scrollPos and key [ <NUM_LIT:1> ] < self . scrollPos + self . vHeight : <EOL> self . setupCell ( self . cells [ key ] , key [ <NUM_LIT:0> ] , key [ <NUM_LIT:1> ] ) <EOL> self . cells [ key ] . setShow ( <NUM_LIT:1> ) <EOL> else : <EOL> self . cells [ key ] . setShow ( <NUM_LIT:0> ) <EOL> def getCellAt ( self , x , y ) : <EOL> return self . cells . get ( ( x , y ) , None ) <EOL> def removeCellAt ( self , x , y ) : <EOL> cell = self . cells . get ( ( x , y ) , None ) <EOL> if cell : <EOL> cell . destroy ( ) <EOL> self . children . remove ( cell ) <EOL> del self . cells [ ( x , y ) ] <EOL> self . setDirty ( <NUM_LIT:1> ) <EOL> def clear ( self ) : <EOL> tmp = copy . copy ( self . children ) <EOL> for cell in tmp : <EOL> self . removeCellAt ( cell . gridPosition [ <NUM_LIT:0> ] , cell . gridPosition [ <NUM_LIT:1> ] ) <EOL> def putCellAt ( self , widget , x , y ) : <EOL> if self . cells . has_key ( ( x , y ) ) : <EOL> print "<STR_LIT>" % ( x , y ) <EOL> return <NUM_LIT:0> <EOL> self . addChild ( widget ) <EOL> self . cells [ ( x , y ) ] = widget <EOL> self . setupCell ( widget , x , y ) <EOL> if y > self . numRows : <EOL> self . numRows = y + <NUM_LIT:1> <EOL> self . parent . scrollBar . setNumItems ( y + <NUM_LIT:1> , self . vHeight ) <EOL> return <NUM_LIT:1> <EOL> def setupCell ( self , widget , x , y ) : <EOL> """<STR_LIT>""" <EOL> if y >= self . scrollPos and y < self . scrollPos + self . vHeight : <EOL> widget . setShow ( <NUM_LIT:1> ) <EOL> else : <EOL> widget . setShow ( <NUM_LIT:0> ) <EOL> widget . gridPosition = ( x , y ) <EOL> widget . moveto ( self . cellWidth * x + <NUM_LIT:2> , <EOL> self . cellHeight * ( y - self . scrollPos ) + <NUM_LIT:2> ) <EOL> widget . resize ( self . cellWidth - <NUM_LIT:4> , self . cellHeight - <NUM_LIT:4> ) <EOL> def onScroll ( self , event ) : <EOL> if event . id == self . parent . scrollBar . id : <EOL> self . scrollPos = event . pos <EOL> self . setupAllCells ( ) <EOL> self . setDirty ( <NUM_LIT:1> ) <EOL> self . window . setDirty ( <NUM_LIT:1> ) <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def findCellAt ( self , posX , posY ) : <EOL> """<STR_LIT>""" <EOL> x = int ( ( posX - self . rect [ <NUM_LIT:0> ] ) / self . cellWidth ) <EOL> y = int ( ( posY - self . rect [ <NUM_LIT:1> ] ) / self . cellHeight ) + self . scrollPos <EOL> return self . cells . get ( ( x , y ) , None ) <EOL> def findCoordinatesAt ( self , posX , posY ) : <EOL> x = int ( ( posX - self . rect [ <NUM_LIT:0> ] ) / self . cellWidth ) <EOL> y = int ( ( posY - self . rect [ <NUM_LIT:1> ] ) / self . cellHeight ) + self . scrollPos <EOL> return ( x , y ) <EOL> def draw ( self , renderer ) : <EOL> """<STR_LIT>""" <EOL> for key in self . cells . keys ( ) : <EOL> if key [ <NUM_LIT:1> ] >= self . scrollPos and key [ <NUM_LIT:1> ] < self . scrollPos + self . vHeight : <EOL> self . cells [ key ] . draw ( renderer ) <EOL> xpos = self . windowRect [ <NUM_LIT:0> ] <EOL> ypos = self . windowRect [ <NUM_LIT:1> ] <EOL> w = self . windowRect [ <NUM_LIT:2> ] <EOL> h = self . windowRect [ <NUM_LIT:3> ] <EOL> cellw = w / self . vWidth <EOL> cellh = h / self . vHeight <EOL> for x in range ( <NUM_LIT:0> , self . vWidth + <NUM_LIT:1> ) : <EOL> renderer . drawLine ( xpos + x * cellw , ypos , xpos + x * cellw , ypos + h , pyui . colors . white ) <EOL> for y in range ( <NUM_LIT:0> , self . vHeight ) : <EOL> renderer . drawLine ( xpos , ypos + y * cellh , xpos + w , ypos + y * cellh , pyui . colors . white ) <EOL> class ColumnHeaders ( pyui . widgets . Panel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , numColumns ) : <EOL> pyui . widgets . Panel . __init__ ( self ) <EOL> self . setLayout ( pyui . layouts . TableLayoutManager ( numColumns , <NUM_LIT:1> ) ) <EOL> for i in range ( <NUM_LIT:0> , numColumns ) : <EOL> self . addChild ( pyui . widgets . Button ( "<STR_LIT>" ) , ( i , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . resize ( self . rect [ <NUM_LIT:2> ] , <NUM_LIT> ) <EOL> def setColumnName ( self , columnNum , name ) : <EOL> self . children [ columnNum ] . setText ( name ) <EOL> class RowHeaders ( pyui . widgets . Panel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , numRows ) : <EOL> pyui . widgets . Panel . __init__ ( self ) <EOL> self . setLayout ( pyui . layouts . TableLayoutManager ( <NUM_LIT:1> , numRows ) ) <EOL> for i in range ( <NUM_LIT:0> , numRows ) : <EOL> self . addChild ( pyui . widgets . Button ( "<STR_LIT>" % i ) , ( <NUM_LIT:0> , i , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . resize ( <NUM_LIT> , self . rect [ <NUM_LIT:3> ] ) <EOL> def setRowName ( self , rowNum , name ) : <EOL> self . children [ rowNum ] . setText ( name ) </s>
