From cfa1bf23e420c50f874661c0b76a05d9a66265bc Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 17 May 2020 03:11:08 +0100 Subject: [PATCH] Allow drag&drop in canvas area Signed-off-by: falkTX --- .../frontend/widgets/draggablegraphicsview.py | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/source/frontend/widgets/draggablegraphicsview.py b/source/frontend/widgets/draggablegraphicsview.py index 82c05d8e5..57b9ff5ce 100644 --- a/source/frontend/widgets/draggablegraphicsview.py +++ b/source/frontend/widgets/draggablegraphicsview.py @@ -23,6 +23,11 @@ from PyQt5.QtCore import Qt from PyQt5.QtGui import QCursor, QMouseEvent from PyQt5.QtWidgets import QGraphicsView +# ------------------------------------------------------------------------------------------------------------ +# Imports (Custom Stuff) + +from carla_shared import * + # ------------------------------------------------------------------------------------------------------------ # Widget Class @@ -38,6 +43,78 @@ class DraggableGraphicsView(QGraphicsView): except: self.fMiddleButton = Qt.MidButton + exts = gCarla.utils.get_supported_file_extensions() + + self.fSupportedExtensions = tuple(("." + i) for i in exts) + self.fWasLastDragValid = False + + self.setAcceptDrops(True) + + # -------------------------------------------------------------------------------------------------------- + + def isDragUrlValid(self, filename): + lfilename = filename.lower() + + if os.path.isdir(filename): + #if os.path.exists(os.path.join(filename, "manifest.ttl")): + #return True + if MACOS and lfilename.endswith(".vst"): + return True + elif lfilename.endswith(".vst3") and ".vst3" in self.fSupportedExtensions: + return True + + elif os.path.isfile(filename): + if lfilename.endswith(self.fSupportedExtensions): + return True + + return False + + # -------------------------------------------------------------------------------------------------------- + + def dragEnterEvent(self, event): + urls = event.mimeData().urls() + print(urls) + + for url in urls: + if self.isDragUrlValid(url.toLocalFile()): + self.fWasLastDragValid = True + event.acceptProposedAction() + return + + self.fWasLastDragValid = False + QGraphicsView.dragEnterEvent(self, event) + + def dragMoveEvent(self, event): + if not self.fWasLastDragValid: + QGraphicsView.dragMoveEvent(self, event) + return + + event.acceptProposedAction() + + def dragLeaveEvent(self, event): + self.fWasLastDragValid = False + QGraphicsView.dragLeaveEvent(self, event) + + # -------------------------------------------------------------------------------------------------------- + + def dropEvent(self, event): + event.acceptProposedAction() + + urls = event.mimeData().urls() + + if len(urls) == 0: + return + + for url in urls: + filename = url.toLocalFile() + + if not gCarla.gui.host.load_file(filename): + CustomMessageBox(self, QMessageBox.Critical, self.tr("Error"), + self.tr("Failed to load file"), + gCarla.gui.host.get_last_error(), QMessageBox.Ok, QMessageBox.Ok) + + # -------------------------------------------------------------------------------------------------------- + def mousePressEvent(self, event): if event.button() == self.fMiddleButton and not self.fCtrlDown: self.fPanning = True @@ -56,6 +133,8 @@ class DraggableGraphicsView(QGraphicsView): self.setDragMode(QGraphicsView.NoDrag) self.setCursor(QCursor(Qt.ArrowCursor)) + # -------------------------------------------------------------------------------------------------------- + def keyPressEvent(self, event): if event.key() == Qt.Key_Control: self.fCtrlDown = True