Ejemplo n.º 1
0
    def dropEvent(self, e: QDropEvent):
        """Handle Drag and Drop events. The :py:class:`QDropEvent`
        performs the actual drop operation.

        We support the following operations: if an Image is dropped,
        we will use it as input image for the :py:class:`Toolbox`.
        """
        mimeData = e.mimeData()
        if mimeData.hasUrls() and self._toolbox:
            # 1. We just consider the first URL, even if multiple URLs
            #    are provided.
            url = mimeData.urls()[0]
            LOG.info("Drag and dropped %d URLs, took first: %s",
                     len(mimeData.urls()), url)

            # 2. Convert the URL to a local filename
            filename = url.toLocalFile()
            description = f"Dropped in image ({filename})"
            LOG.info("Converted URL to local filename: '%s'", filename)

            # 3. Set this file as input image for the toolbox.
            self._toolbox.set_input_from_file(filename,
                                              description=description)

            # 4. Mark the Drop action as accepted (we actually perform
            #    a CopyAction, no matter what was proposed)
            if e.proposedAction() == Qt.CopyAction:
                e.acceptProposedAction()
            else:
                # If you set a drop action that is not one of the
                # possible actions, the drag and drop operation will
                # default to a copy operation.
                e.setDropAction(Qt.CopyAction)
                e.accept()