def mouseMoveEvent(self, event): """Drag and drop layer with mouse movement. Parameters ---------- event : qtpy.QtCore.QEvent Event from the Qt context. """ position = np.array([event.pos().x(), event.pos().y()]) distance = np.linalg.norm(position - self._drag_start_position) if (distance < QApplication.startDragDistance() or self._drag_name is None): return mimeData = QMimeData() mimeData.setText(self._drag_name) drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(event.pos() - self.rect().topLeft()) drag.exec_() # Check if dragged layer still exists or was deleted during drag names = [layer.name for layer in self.layers] dragged_layer_exists = self._drag_name in names if self._drag_name is not None and dragged_layer_exists: index = self.layers.index(self._drag_name) layer = self.layers[index] self._ensure_visible(layer)
def mouseMoveEvent(self, event): distance = (event.pos() - self.dragStartPosition).manhattanLength() if distance < QApplication.startDragDistance(): return mimeData = QMimeData() if not self.layer.selected: name = self.layer.name else: name = '' for layer in self.layer.viewer.layers: if layer.selected: name = layer.name + '; ' + name name = name[:-2] mimeData.setText(name) drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(event.pos() - self.rect().topLeft()) dropAction = drag.exec_(Qt.MoveAction | Qt.CopyAction) if dropAction == Qt.CopyAction: if not self.layer.selected: index = self.layer.viewer.layers.index(self.layer) self.layer.viewer.layers.pop(index) else: self.layer.viewer.layers.remove_selected()
def mouseMoveEvent(self, event): from qtpy.QtCore import QMimeData, QByteArray from qtpy.QtGui import QPixmap, QDrag if not (event.button() != Qt.LeftButton and isinstance(self.dragLabel, QLabel)): return if (event.pos() - self.dragStartPosition ).manhattanLength() < QApplication.startDragDistance(): return axis_index = self.filters_layout.indexOf(self.dragLabel) // 2 # prepare hotSpot, mimeData and pixmap objects mimeData = QMimeData() mimeData.setText(self.dragLabel.text()) mimeData.setData("application/x-axis-index", QByteArray.number(axis_index)) pixmap = QPixmap(self.dragLabel.size()) self.dragLabel.render(pixmap) # prepare drag object drag = QDrag(self) drag.setMimeData(mimeData) drag.setPixmap(pixmap) drag.setHotSpot(event.pos() - self.dragStartPosition) drag.exec_(Qt.MoveAction | Qt.CopyAction, Qt.CopyAction)
def mouseMoveEvent(self, event): position = np.array([event.pos().x(), event.pos().y()]) distance = np.linalg.norm(position - self.drag_start_position) if (distance < QApplication.startDragDistance() or self.drag_name is None): return mimeData = QMimeData() mimeData.setText(self.drag_name) drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(event.pos() - self.rect().topLeft()) dropAction = drag.exec_()
def mouseMoveEvent(self, event): position = np.array([event.pos().x(), event.pos().y()]) distance = np.linalg.norm(position - self.drag_start_position) if (distance < QApplication.startDragDistance() or self.drag_name is None): return mimeData = QMimeData() mimeData.setText(self.drag_name) drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(event.pos() - self.rect().topLeft()) drag.exec_() if self.drag_name is not None: index = self.layers.index(self.drag_name) layer = self.layers[index] self._ensure_visible(layer)
def drag_with_pixmap(list_widget: QListWidget) -> QDrag: """Create a QDrag object with a pixmap of the currently select list item. This method is useful when you have a QListWidget that displays custom widgets for each QListWidgetItem instance in the list (usually by calling ``QListWidget.setItemWidget(item, widget)``). When used in a ``QListWidget.startDrag`` method, this function creates a QDrag object that shows an image of the item being dragged (rather than an empty rectangle). Parameters ---------- list_widget : QListWidget The QListWidget for which to create a QDrag object. Returns ------- QDrag A QDrag instance with a pixmap of the currently selected item. Examples -------- >>> class QListWidget: ... def startDrag(self, supportedActions): ... drag = drag_with_pixmap(self) ... drag.exec_(supportedActions, Qt.MoveAction) """ drag = QDrag(list_widget) drag.setMimeData(list_widget.mimeData(list_widget.selectedItems())) size = list_widget.viewport().visibleRegion().boundingRect().size() pixmap = QPixmap(size) pixmap.fill(Qt.transparent) painter = QPainter(pixmap) for index in list_widget.selectedIndexes(): rect = list_widget.visualRect(index) painter.drawPixmap(rect, list_widget.viewport().grab(rect)) painter.end() drag.setPixmap(pixmap) drag.setHotSpot(list_widget.viewport().mapFromGlobal(QCursor.pos())) return drag