Advertisement
Guest User

Animated widgets with only one widget

a guest
Apr 11th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.30 KB | None | 0 0
  1. import sys
  2. from PySide.QtGui import *
  3. from PySide.QtCore import *
  4.  
  5.  
  6. class HorizontalAnimator( QWidget ):
  7.     '''A widget where other widgets may slide in or out animated'''
  8.    
  9.     def __init__(self, parent=None):
  10.         super( HorizontalAnimator, self ).__init__( parent )
  11.         self.widgets = []
  12.         self.visibleWidget = None
  13.         self.end = 0
  14.        
  15.  
  16.     def addWidget(self, widget):
  17.         '''Add this widget to the animator, but hide it at the beginning'''
  18.        
  19.         widget.setParent( self )
  20.         widget.setGeometry(-widget.sizeHint().width(),
  21.                            0,
  22.                            widget.sizeHint().width(),
  23.                            widget.sizeHint().height())
  24.         self.widgets.append( widget )
  25.         widget.hide()
  26.        
  27.     def change(self, newWidget):
  28.         '''Slide in the new widget and slide out the old one'''
  29.        
  30.         if not newWidget == self.visibleWidget:
  31.             # Slide in
  32.             newSize = QSize( newWidget.sizeHint().width(), self.height() )
  33.             self.resize( newSize )
  34.             newWidget.show()
  35.             self.animGroup = QParallelAnimationGroup()
  36.            
  37.             slideInAnimation = self.getMoveAnimation( newWidget,
  38.                                                           -newWidget.sizeHint().width(),
  39.                                                           0 )
  40.             self.animGroup.addAnimation( slideInAnimation )
  41.            
  42.             # Slide out
  43.             oldWidget = self.visibleWidget
  44.             if oldWidget:
  45.                 slideOutAnimation = self.getMoveAnimation( oldWidget,
  46.                                                                0,
  47.                                                                newWidget.sizeHint().width() )
  48.                 self.animGroup.addAnimation( slideOutAnimation )
  49.                 slideOutAnimation.finished.connect( oldWidget.hide )
  50.                
  51.             self.animGroup.start()
  52.             self.visibleWidget = newWidget
  53.            
  54.     def getMoveAnimation(self, widget, start, end):
  55.         '''
  56.        Horizontal animation, moves the widget
  57.        from "start" x-position to "end" x-position.
  58.        '''
  59.        
  60.         moveAnimation = QPropertyAnimation(widget, 'pos')
  61.         moveAnimation.setDuration(500)
  62.         moveAnimation.setStartValue(QPoint(start, 0))
  63.         moveAnimation.setEndValue(QPoint(end, 0))
  64.         moveAnimation.setEasingCurve(QEasingCurve.OutCubic)
  65.         return moveAnimation
  66.    
  67.    
  68. class WidgetPage(QWidget):
  69.    
  70.     def __init__( self, parent=None ):
  71.         '''Vertical stack of widgets (simulates a column)'''
  72.         super( WidgetPage, self ).__init__( parent )
  73.         self.grid = QVBoxLayout( self )
  74.  
  75.     def addWidget(self, widget):
  76.         '''Add this widget at the end of the stack'''
  77.         self.grid.addWidget( widget )
  78.        
  79.  
  80. class ToolBrowser( QWidget ):
  81.     '''AppStore like panel'''
  82.  
  83.     def __init__( self, data ):
  84.         super( ToolBrowser, self ).__init__()
  85.         self.data = data
  86.         self.pageDict = {}
  87.         self.initUI()
  88.  
  89.     def initUI( self ):
  90.         '''Build UI of the MainWindow'''
  91.        
  92.         # LAYOUT WITH BUTTONS ON LEFT AND ANIMATED PAGE ON RIGHT
  93.         self.setLayout( QHBoxLayout() )
  94.         self.btnLayout = QVBoxLayout()
  95.         self.layout().addLayout( self.btnLayout )
  96.         self.animator = HorizontalAnimator( self )
  97.  
  98.         # CREATE BUTTONS AND PAGES BASED ON SUPPLIED DATA
  99.         for container in sorted(self.data):
  100.             categoryPage = WidgetPage(self)
  101.            
  102.             # CREATE CONTAINER BUTTON AND LINK IT TO PAGE
  103.             containerButton = QPushButton(container)
  104.             self.pageDict[containerButton] = categoryPage
  105.             containerButton.clicked.connect(self.pageChangeRequest)
  106.             self.btnLayout.addWidget(containerButton)
  107.             print containerButton.parent()
  108.  
  109.             # CREATE BUTTONS FOR EACH CATEGORY IN CONTAINER
  110.             for category in self.data[container]:
  111.                 categoryPage.addWidget( QPushButton( category ) )
  112.  
  113.             # ADD PAGE TO ANIMATOR WIDGET
  114.             self.animator.addWidget( categoryPage )
  115.  
  116.         self.layout().addWidget( self.animator )
  117.        
  118.  
  119.         self.resize(350, 400)
  120.         self.setWindowTitle('Tool Browser')
  121.        
  122.     def pageChangeRequest(self):
  123.         '''The user wants to change the visible page'''
  124.        
  125.         button = self.sender()
  126.         page = self.pageDict[button]
  127.         self.animator.change(page)
  128.        
  129.         # or as a one-liner:
  130.         # self.animator.change(self.pageDict[self.sender()])
  131.        
  132.        
  133.        
  134. def main():
  135.     data = dict(
  136.         containerA= (
  137.            dict(CatAA=['A1', 'B1', 'C1'],
  138.                 CatAB=['A2', 'B2', 'C2'],
  139.                 CatAC=['A3', 'B3', 'C3'])),
  140.         containerB= (
  141.            dict(CatBA=['A4', 'B4', 'C4'],
  142.                 CatBB=['A5', 'B5', 'C5'],
  143.                 CatBC=['A6', 'B6', 'C6'])),
  144.         containerC= (
  145.            dict(CatCA=['A7', 'B7', 'C7'],
  146.                 CatCB=['A8', 'B8', 'C8'],
  147.                 CatCC=['A9', 'B9', 'C9'])))
  148.    
  149.     app = QApplication(sys.argv)
  150.     ex = ToolBrowser( data )
  151.     ex.show()
  152.     sys.exit(app.exec_())
  153.  
  154.  
  155. if __name__ == '__main__':
  156.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement