def _get_action(self, event): """ Work out what action on what object to perform for a drop event """ # default values to return action = None to_node = None to_object = None to_index = None data = None control = self._controller # Get the tree widget item under the cursor. nid = self.itemAt(event.pos()) if nid is None: if control.hide_root: nid = self.invisibleRootItem() else: return (action, to_node, to_object, to_index, data) # Check that the target is not the source of a child of the source. if self._dragging is not None: pnid = nid while pnid is not None: if pnid is self._dragging: return (action, to_node, to_object, to_index, data) pnid = pnid.parent() data = PyMimeData.coerce(event.mimeData()).instance() _, node, obj = control._get_node_data(nid) if event.proposedAction() == QtCore.Qt.MoveAction and \ control._is_droppable(node, obj, data, False): # append to node being dropped on # print control._is_droppable(node, obj, data, False) action = 'append' to_node = node to_object = obj to_index = None else: # get parent of node being dropped on to_node, to_object, to_index = control._node_index(nid) if to_node is None: # no parent, can't do anything action = None elif control._is_droppable(to_node, to_object, data, True): # insert into the parent of the node being dropped on action = 'insert' elif control._is_droppable(to_node, to_object, data, False): # append to the parent of the node being dropped on action = 'append' else: # parent can't be modified, can't do anything action = None return (action, to_node, to_object, to_index, data)
def dragEnterEvent(self, e): """ Reimplemented to see if the current drag can be handled by the tree. """ # Assume the drag is invalid. e.ignore() # Check if we have a python object instance, we might be interested data = PyMimeData.coerce(e.mimeData()).instance() if data is None: return # We might be able to handle it (but it depends on what the final # target is). e.acceptProposedAction()
def startDrag(self, actions): """ Reimplemented to start the drag of a tree widget item. """ nid = self.currentItem() if nid is None: return self._dragging = nid _, node, obj = self._controller._get_node_data(nid) # Convert the item being dragged to MIME data. drag_object = node.get_drag_object(obj) md = PyMimeData.coerce(drag_object) # Render the item being dragged as a pixmap. nid_rect = self.visualItemRect(nid) rect = nid_rect.intersected(self.viewport().rect()) pm = QtGui.QPixmap(rect.size()) pm.fill(self.palette().base().color()) painter = QtGui.QPainter(pm) option = self.viewOptions() option.state |= QtGui.QStyle.State_Selected option.rect = QtCore.QRect(nid_rect.topLeft() - rect.topLeft(), nid_rect.size()) self.itemDelegate().paint(painter, option, self.indexFromItem(nid)) painter.end() # Calculate the hotspot so that the pixmap appears on top of the # original item. hspos = self.viewport().mapFromGlobal(QtGui.QCursor.pos()) - \ nid_rect.topLeft() # Start the drag. drag = QtGui.QDrag(self) drag.setMimeData(md) drag.setPixmap(pm) drag.setHotSpot(hspos) drag.exec_(actions)