Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from PySide.QtGui import *
- from PySide.QtCore import *
- class HorizontalAnimator( QWidget ):
- '''A widget where other widgets may slide in or out animated'''
- def __init__(self, parent=None):
- super( HorizontalAnimator, self ).__init__( parent )
- self.widgets = []
- self.visibleWidget = None
- self.end = 0
- def addWidget(self, widget):
- '''Add this widget to the animator, but hide it at the beginning'''
- widget.setParent( self )
- widget.setGeometry(-widget.sizeHint().width(),
- 0,
- widget.sizeHint().width(),
- widget.sizeHint().height())
- self.widgets.append( widget )
- widget.hide()
- def change(self, newWidget):
- '''Slide in the new widget and slide out the old one'''
- if not newWidget == self.visibleWidget:
- # Slide in
- newSize = QSize( newWidget.sizeHint().width(), self.height() )
- self.resize( newSize )
- newWidget.show()
- self.animGroup = QParallelAnimationGroup()
- slideInAnimation = self.getMoveAnimation( newWidget,
- -newWidget.sizeHint().width(),
- 0 )
- self.animGroup.addAnimation( slideInAnimation )
- # Slide out
- oldWidget = self.visibleWidget
- if oldWidget:
- slideOutAnimation = self.getMoveAnimation( oldWidget,
- 0,
- newWidget.sizeHint().width() )
- self.animGroup.addAnimation( slideOutAnimation )
- slideOutAnimation.finished.connect( oldWidget.hide )
- self.animGroup.start()
- self.visibleWidget = newWidget
- def getMoveAnimation(self, widget, start, end):
- '''
- Horizontal animation, moves the widget
- from "start" x-position to "end" x-position.
- '''
- moveAnimation = QPropertyAnimation(widget, 'pos')
- moveAnimation.setDuration(500)
- moveAnimation.setStartValue(QPoint(start, 0))
- moveAnimation.setEndValue(QPoint(end, 0))
- moveAnimation.setEasingCurve(QEasingCurve.OutCubic)
- return moveAnimation
- class WidgetPage(QWidget):
- def __init__( self, parent=None ):
- '''Vertical stack of widgets (simulates a column)'''
- super( WidgetPage, self ).__init__( parent )
- self.grid = QVBoxLayout( self )
- def addWidget(self, widget):
- '''Add this widget at the end of the stack'''
- self.grid.addWidget( widget )
- class ToolBrowser( QWidget ):
- '''AppStore like panel'''
- def __init__( self, data ):
- super( ToolBrowser, self ).__init__()
- self.data = data
- self.pageDict = {}
- self.initUI()
- def initUI( self ):
- '''Build UI of the MainWindow'''
- # LAYOUT WITH BUTTONS ON LEFT AND ANIMATED PAGE ON RIGHT
- self.setLayout( QHBoxLayout() )
- self.btnLayout = QVBoxLayout()
- self.layout().addLayout( self.btnLayout )
- self.animator = HorizontalAnimator( self )
- # CREATE BUTTONS AND PAGES BASED ON SUPPLIED DATA
- for container in sorted(self.data):
- categoryPage = WidgetPage(self)
- # CREATE CONTAINER BUTTON AND LINK IT TO PAGE
- containerButton = QPushButton(container)
- self.pageDict[containerButton] = categoryPage
- containerButton.clicked.connect(self.pageChangeRequest)
- self.btnLayout.addWidget(containerButton)
- print containerButton.parent()
- # CREATE BUTTONS FOR EACH CATEGORY IN CONTAINER
- for category in self.data[container]:
- categoryPage.addWidget( QPushButton( category ) )
- # ADD PAGE TO ANIMATOR WIDGET
- self.animator.addWidget( categoryPage )
- self.layout().addWidget( self.animator )
- self.resize(350, 400)
- self.setWindowTitle('Tool Browser')
- def pageChangeRequest(self):
- '''The user wants to change the visible page'''
- button = self.sender()
- page = self.pageDict[button]
- self.animator.change(page)
- # or as a one-liner:
- # self.animator.change(self.pageDict[self.sender()])
- def main():
- data = dict(
- containerA= (
- dict(CatAA=['A1', 'B1', 'C1'],
- CatAB=['A2', 'B2', 'C2'],
- CatAC=['A3', 'B3', 'C3'])),
- containerB= (
- dict(CatBA=['A4', 'B4', 'C4'],
- CatBB=['A5', 'B5', 'C5'],
- CatBC=['A6', 'B6', 'C6'])),
- containerC= (
- dict(CatCA=['A7', 'B7', 'C7'],
- CatCB=['A8', 'B8', 'C8'],
- CatCC=['A9', 'B9', 'C9'])))
- app = QApplication(sys.argv)
- ex = ToolBrowser( data )
- ex.show()
- sys.exit(app.exec_())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement