def mousePressEvent(self, event): """ when a mouse button is pressed """ # a left mouse button lets the user drag the color if event.button() == QtCore.Qt.LeftButton: drag = QtGui.QDrag(self) drag.setPixmap(self.grab()) mime = QtCore.QMimeData() mime.setText(self.color) drag.setMimeData(mime) self.setStyleSheet( "background-color: lightgray; border: 2px solid gray") self.setDisabled(True) self.setText("") drag.exec() self.setText(self.color) self.setDisabled(False) if self.color in self.maps: self.setStyleSheet( "text-align: center; border: 2px solid black; " + self.getBackground()) else: self.setStyleSheet( "text-align: center; background-color: %s; border: 2px solid black" % self.color) # a right mouse button opens a color choose menu elif event.button() == QtCore.Qt.RightButton: self.openDialog()
def send(drag_src, data): """ desc: Starts a drag event, and embeds a data dictionary as json text in the mimedata. arguments: drag_src: desc: The source widget. type: QWidget data: desc: A data dictionary. The 'type' key identifies the type of data that is being dropped. type: dict returns: desc: A drag object. The start function is called automatically. type: QDrag """ text = safe_decode(json.dumps(data), enc=u'utf-8') mimedata = QtCore.QMimeData() mimedata.setText(text) drag = QtGui.QDrag(drag_src) drag.setMimeData(mimedata) drag.exec_()
def mouseMoveEvent(self, event): """ Determine if the current movement is a drag. If it is, convert it into a QDrag. If the drag ends inside the tab bar, emit an onMoveTabSignal. If the drag ends outside the tab bar, emit an onDetachTabSignal. :param event: a mouse move event. """ # Determine if the current movement is detected as a drag if not self.drag_start_pos.isNull() and ( (event.pos() - self.drag_start_pos).manhattanLength() < QtWidgets.QApplication.startDragDistance()): self.drag_initiated = True # If the current movement is a drag initiated by the left button if (event.buttons() & QtCore.Qt.LeftButton) and self.drag_initiated: # Stop the move event finish_move_event = QtGui.QMouseEvent(QtCore.QEvent.MouseMove, event.pos(), QtCore.Qt.NoButton, QtCore.Qt.NoButton, QtCore.Qt.NoModifier) QtWidgets.QTabBar.mouseMoveEvent(self, finish_move_event) # Convert the move event into a drag drag = QtGui.QDrag(self) mime_data = QtCore.QMimeData() drag.setMimeData(mime_data) if PYQT4: pixmap = QtGui.QPixmap.grabWindow(self.parentWidget().currentWidget().winId()) else: app = QtWidgets.QApplication.instance() desktop = app.desktop() screen_number = desktop.screenNumber(self.parentWidget().currentWidget()) screen = app.screens()[screen_number] # Create the appearance of dragging the tab content pixmap = QtGui.QScreen.grabWindow(screen, self.parentWidget().currentWidget().winId()) target_pixmap = QtGui.QPixmap(pixmap.size()) target_pixmap.fill(QtCore.Qt.transparent) painter = QtGui.QPainter(target_pixmap) painter.setOpacity(0.85) painter.drawPixmap(0, 0, pixmap) painter.end() drag.setPixmap(target_pixmap) # Initiate the drag drop_action = drag.exec_(QtCore.Qt.MoveAction | QtCore.Qt.CopyAction) # For Linux: Here, drag.exec_() will not return MoveAction on Linux. So it # must be set manually if self.drag_end_pos.x() != 0 and self.drag_end_pos.y() != 0: drop_action = QtCore.Qt.MoveAction # If the drag completed inside the tab bar, move the selected tab to the new position if drop_action == QtCore.Qt.MoveAction: if not self.drag_end_pos.isNull(): event.accept() self.onMoveTabSignal.emit(self.tabAt(self.drag_start_pos), self.tabAt(self.drag_end_pos)) else: QtWidgets.QTabBar.mouseMoveEvent(self, event)
def mouseMoveEvent(self, event): # Determine if the current movement is detected as a drag if not self.dragStartPos.isNull() and ( (event.pos() - self.dragStartPos).manhattanLength() > QtWidgets.QApplication.startDragDistance()): self.dragInitiated = True # If the current movement is a drag initiated by the left button if (((event.buttons() & QtCore.Qt.LeftButton)) and self.dragInitiated): # Stop the move event finishMoveEvent = QtGui.QMouseEvent(QtCore.QEvent.MouseMove, event.pos(), QtCore.Qt.NoButton, QtCore.Qt.NoButton, QtCore.Qt.NoModifier) super().mouseMoveEvent(finishMoveEvent) # Convert the move event into a drag drag = QtGui.QDrag(self) mimeData = QtCore.QMimeData() mimeData.setData('action', b'application/tab-detach') drag.setMimeData(mimeData) #Create the appearance of dragging the tab content pixmap = self.parentWidget().grab() targetPixmap = QtGui.QPixmap(pixmap.size()) targetPixmap.fill(QtCore.Qt.transparent) painter = QtGui.QPainter(targetPixmap) painter.setOpacity(0.85) painter.drawPixmap(0, 0, pixmap) painter.end() drag.setPixmap(targetPixmap) # Initiate the drag dropAction = drag.exec_(QtCore.Qt.MoveAction | QtCore.Qt.CopyAction) # If the drag completed outside of the tab bar, detach the tab and move # the content to the current cursor position if dropAction == QtCore.Qt.IgnoreAction: event.accept() self.onDetachTabSignal.emit(self.tabAt(self.dragStartPos), self.mouseCursor.pos()) # Else if the drag completed inside the tab bar, move the selected tab to the new position elif dropAction == QtCore.Qt.MoveAction: if not self.dragDropedPos.isNull(): event.accept() self.onMoveTabSignal.emit( self.tabAt(self.dragStartPos), self.tabAt(self.dragDropedPos)) else: super().mouseMoveEvent(event)
def mouseMoveEvent(self, event: QtGui.QMouseEvent): if event.buttons() != QtCore.Qt.LeftButton: event.ignore() return if (sub(event.pos(), self.dragStartPos) ).manhattanLength() < QtWidgets.QApplication.startDragDistance(): event.ignore() return self.move_me.emit(self) mime_data = QtCore.QMimeData() mime_data.setObjectName('frame') drag = QtGui.QDrag(self) drag.setMimeData(mime_data) drop_action = drag.exec_(QtCore.Qt.MoveAction)
def DoFileExportDragDrop(window, page_key, media, alt_down): drop_source = QG.QDrag(window) data_object = QMimeDataHydrusFiles() # new_options = HG.client_controller.new_options do_secret_discord_dnd_fix = new_options.GetBoolean( 'secret_discord_dnd_fix') and alt_down # client_files_manager = HG.client_controller.client_files_manager original_paths = [] total_size = 0 for m in media: hash = m.GetHash() mime = m.GetMime() total_size += m.GetSize() original_path = client_files_manager.GetFilePath( hash, mime, check_file_exists=False) original_paths.append(original_path) # discord_dnd_fix_possible = new_options.GetBoolean( 'discord_dnd_fix' ) and len(original_paths) <= 50 and total_size < 200 * 1048576 temp_dir = HG.client_controller.temp_dir if do_secret_discord_dnd_fix: dnd_paths = original_paths flags = QC.Qt.MoveAction elif discord_dnd_fix_possible and os.path.exists(temp_dir): dnd_paths = [] for original_path in original_paths: filename = os.path.basename(original_path) dnd_path = os.path.join(temp_dir, filename) if not os.path.exists(dnd_path): HydrusPaths.MirrorFile(original_path, dnd_path) dnd_paths.append(dnd_path) flags = QC.Qt.MoveAction | QC.Qt.CopyAction else: dnd_paths = original_paths flags = QC.Qt.CopyAction uri_list = [] for path in dnd_paths: uri_list.append(QC.QUrl.fromLocalFile(path)) data_object.setUrls(uri_list) # hashes = [m.GetHash() for m in media] data_object.setHydrusFiles(page_key, hashes) # old way of doing this that makes some external programs (discord) reject it ''' if page_key is None: encoded_page_key = None else: encoded_page_key = page_key.hex() data_obj = ( encoded_page_key, [ hash.hex() for hash in hashes ] ) data_str = json.dumps( data_obj ) data_bytes = bytes( data_str, 'utf-8' ) data_object.setData( 'application/hydrus-media', data_bytes ) ''' # drop_source.setMimeData(data_object) result = drop_source.exec_(flags, QC.Qt.CopyAction) return result
def test_qdrag_functions(qtbot): """Test functions mapping for QtGui.QDrag.""" assert QtGui.QDrag.exec_ is not None drag = QtGui.QDrag(None) drag.exec_()