Advertisement
Guest User

Untitled

a guest
Apr 15th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. import sys
  2. from PySide.QtGui import *
  3. from PySide.QtCore import *
  4.  
  5. class ItemButton( QPushButton ):
  6.     '''A custom button object that is dragable and holds an item which represents the data for the respective tool'''
  7.  
  8.     def __init__( self, item, parent=None ):
  9.         '''item is a dictionary representing a downloadable tool'''
  10.         super( ItemButton, self ).__init__( parent )
  11.         self.item = item
  12.         self.setText( item['name'] )
  13.         self.setAcceptDrops( True )        #ONLY NEEDED TO BE ABLE TO IDENTIFY "SLOPPY" CLICKS
  14.  
  15.     def openDetailsPage( self ):
  16.         print 'Opens Item page for %s - not yet implemented' % self.item
  17.  
  18.     def mouseMoveEvent( self, event ):
  19.         '''Enable drag&drop of buttons'''
  20.         self.startDragPos = event.pos()
  21.         print 'started drag at:', self.startDragPos
  22.         self.setDown( False ) #PREVENT THE BUTTON FORM STAYING DOWN
  23.         mimeData = QMimeData() # IMPLEMENT THIS TO BE ABLE TO DROP INTO NUKE
  24.         drag = QDrag( self )
  25.         drag.setMimeData( mimeData )
  26.         drag.setHotSpot( event.pos() - self.rect().topLeft() )
  27.         dropAction = drag.start( Qt.CopyAction )
  28.  
  29.     def dragEnterEvent( self, event ):
  30.         #ONLY NEEDED TO BE ABLE TO IDENTIFY "SLOPPY" CLICKS
  31.         event.accept()
  32.  
  33.     def dropEvent( self, event ):
  34.         #ONLY NEEDED TO BE ABLE TO IDENTIFY "SLOPPY" CLICKS
  35.         print 'stopped drag at:', event.pos()
  36.         point = event.pos() - self.startDragPos
  37.         if point.manhattanLength() < 5:
  38.             # EXECUTE MOUSE CLICK EVEN IF CURSOR MOVED A LITTLE BIT DURING CLICK
  39.             self.openDetailsPage()
  40.         event.accept()
  41.  
  42.     def mouseReleaseEvent( self, event ):
  43.         '''when button is released after static click (no drag)'''
  44.         QPushButton.mousePressEvent( self, event ) # NEEDED TO MAKE BUTTON APPEAR TO BE PRESSED
  45.         if event.button() == Qt.LeftButton:
  46.             self.openDetailsPage()
  47.             self.setDown( False )
  48.  
  49. def main():  
  50.     app = QApplication( sys.argv )
  51.     item = { 'name':'test item' }
  52.     w = QWidget()
  53.     w.setLayout( QVBoxLayout() )
  54.     w.layout().addWidget( ItemButton( item ) )
  55.     w.show()
  56.     sys.exit(app.exec_())
  57.      
  58.      
  59. if __name__ == '__main__':
  60.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement