Advertisement
Guest User

draggin.py

a guest
Jan 25th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.68 KB | None | 0 0
  1. '''
  2. An elementary breadboard for experimenting with Qt's Drag'n'Drop classes.
  3. '''
  4. from PyQt5.QtCore import (
  5.     Qt,
  6.     QMimeData,
  7.     QPoint,
  8.     QTime
  9. )
  10. from PyQt5.QtGui import QDrag
  11. from PyQt5.QtWidgets import (
  12.     QApplication,
  13.     QHBoxLayout,
  14.     QLabel,
  15.     QMainWindow,
  16.     QWidget
  17. )
  18.  
  19. class SorcWidj(QLabel):
  20.     '''A simple drag-source with ability
  21.    to recognize the start of a drag motion
  22.    and implement the drag.'''
  23.     def __init__(self,text):
  24.         super().__init__()
  25.         self.setText(text)
  26.         self.mouse_down = False # has a left-click happened yet?
  27.         self.mouse_posn = QPoint() # if so, this was where...
  28.         self.mouse_time = QTime() # ...and this was when.
  29.  
  30.     def mousePressEvent(self,event):
  31.         if event.button() == Qt.LeftButton :
  32.             self.mouse_down = True # we are left-clicked-upon
  33.             self.mouse_posn = event.pos() # here and...
  34.             self.mouse_time.start() # ...now
  35.         event.ignore()
  36.         super().mousePressEvent(event) # pass it on up
  37.  
  38.     def mouseReleaseEvent(self,event):
  39.         # Mouse released in our rectangle, clear any drag info.
  40.         self.mouse_down = False
  41.         event.ignore()
  42.         super().mouseReleaseEvent(event) # pass it on up
  43.  
  44.     def dragTargetChange(self, qob):
  45.         # print signal from QDrag::targetChanged
  46.         t = type(qob)
  47.         print('target moved to',t)
  48.  
  49.     def doSomeDraggin(self):
  50.         # Make a scaled pixmap of our widget to put under the cursor.
  51.         thumb = self.grab().scaledToHeight(50)
  52.         # Create the QDrag object and set its cursor over the pixmap
  53.         dragster = QDrag(self)
  54.         dragster.setPixmap(thumb)
  55.         dragster.setHotSpot(QPoint(thumb.width()/2,thumb.height()/2))
  56.         # Create some data to be dragged and load it in the dragster.
  57.         md = QMimeData()
  58.         md.setText(self.text())
  59.         dragster.setMimeData(md)
  60.         # Experiment: can we catch the target-change signal?
  61.         dragster.targetChanged.connect(self.dragTargetChange)
  62.         # Initiate the drag, which really is a form of modal dialog,
  63.         # experimentally allowing any of the possible actions.
  64.         # Result is supposed to be the action performed at the drop.
  65.         act = dragster.exec_(Qt.MoveAction) # Qt.LinkAction Qt.CopyAction
  66.         # Display the results of the drag.
  67.         targ = dragster.target() # s.b. the widget that received the drop
  68.         src = dragster.source() # s.b. this very widget
  69.         print('action ',int(act),'target ',type(targ), 'source ',type(src))
  70.         return
  71.  
  72.     def mouseMoveEvent(self,event):
  73.         if self.mouse_down :
  74.             # Mouse left-clicked and is now moving. Is this the start of a
  75.             # drag? Note time since the click and approximate distance moved
  76.             # since the click and test against the app's standard.
  77.             t = self.mouse_time.elapsed()
  78.             m = (event.pos() - self.mouse_posn).manhattanLength()
  79.             if t >= QApplication.startDragTime() \
  80.             or m >= QApplication.startDragDistance() :
  81.                 # Yes, a proper drag is indicated. Commence dragging.
  82.                 self.doSomeDraggin()
  83.                 event.accept()
  84.                 return
  85.         # Move does not (yet) constitute a drag, ignore it.
  86.         event.ignore()
  87.         super().mouseMoveEvent(event)
  88.  
  89. class TargWidj(QLabel):
  90.     '''A simple class that can detect an incoming drag
  91.    and accept it, but only if it's a Copy of plain text.'''
  92.     def __init__(self,text):
  93.         super().__init__()
  94.         self.setAcceptDrops(True)
  95.         self.setText(text)
  96.         self.setStyleSheet('')
  97.     def dragEnterEvent(self, event):
  98.         print('drag enter')
  99.         dbg = event.proposedAction()
  100.         if (event.proposedAction() & Qt.CopyAction) \
  101.         and (event.mimeData().hasFormat("text/plain")) :
  102.             self.setStyleSheet('background: green;')
  103.             event.acceptProposedAction()
  104.         else:
  105.             self.setStyleSheet('background: red;')
  106.     def dragMoveEvent(self, event):
  107.         print('drag moving')
  108.     def dragLeaveEvent(self, event):
  109.         print('drag leaving')
  110.         self.setStyleSheet('')
  111.     def dropEvent(self, event):
  112.         print('dropping')
  113.         self.setText( event.mimeData().text() )
  114.         self.setStyleSheet('')
  115.  
  116. def main():
  117.     import sys
  118.     app = QApplication(sys.argv)
  119.     main = QMainWindow()
  120.     central = QWidget()
  121.     hbo = QHBoxLayout()
  122.     hbo.addWidget( SorcWidj('Source Widget') )
  123.     hbo.addWidget( TargWidj('Target') )
  124.     central.setLayout(hbo)
  125.     main.setCentralWidget( central )
  126.     main.show()
  127.     app.exec_()
  128.  
  129. if __name__ == '__main__' :
  130.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement