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 ItemButton( QPushButton ):
- '''A custom button object that is dragable and holds an item which represents the data for the respective tool'''
- def __init__( self, item, parent=None ):
- '''item is a dictionary representing a downloadable tool'''
- super( ItemButton, self ).__init__( parent )
- self.item = item
- self.setText( item['name'] )
- self.setAcceptDrops( True ) #ONLY NEEDED TO BE ABLE TO IDENTIFY "SLOPPY" CLICKS
- def openDetailsPage( self ):
- print 'Opens Item page for %s - not yet implemented' % self.item
- def mouseMoveEvent( self, event ):
- '''Enable drag&drop of buttons'''
- self.startDragPos = event.pos()
- print 'started drag at:', self.startDragPos
- self.setDown( False ) #PREVENT THE BUTTON FORM STAYING DOWN
- mimeData = QMimeData() # IMPLEMENT THIS TO BE ABLE TO DROP INTO NUKE
- drag = QDrag( self )
- drag.setMimeData( mimeData )
- drag.setHotSpot( event.pos() - self.rect().topLeft() )
- dropAction = drag.start( Qt.CopyAction )
- def dragEnterEvent( self, event ):
- #ONLY NEEDED TO BE ABLE TO IDENTIFY "SLOPPY" CLICKS
- event.accept()
- def dropEvent( self, event ):
- #ONLY NEEDED TO BE ABLE TO IDENTIFY "SLOPPY" CLICKS
- print 'stopped drag at:', event.pos()
- point = event.pos() - self.startDragPos
- if point.manhattanLength() < 5:
- # EXECUTE MOUSE CLICK EVEN IF CURSOR MOVED A LITTLE BIT DURING CLICK
- self.openDetailsPage()
- event.accept()
- def mouseReleaseEvent( self, event ):
- '''when button is released after static click (no drag)'''
- QPushButton.mousePressEvent( self, event ) # NEEDED TO MAKE BUTTON APPEAR TO BE PRESSED
- if event.button() == Qt.LeftButton:
- self.openDetailsPage()
- self.setDown( False )
- def main():
- app = QApplication( sys.argv )
- item = { 'name':'test item' }
- w = QWidget()
- w.setLayout( QVBoxLayout() )
- w.layout().addWidget( ItemButton( item ) )
- w.show()
- sys.exit(app.exec_())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement