Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.4KB

  1. #!/usr/bin/env python3
  2. # SPDX-FileCopyrightText: 2011-2025 Filipe Coelho <falktx@falktx.com>
  3. # SPDX-License-Identifier: GPL-2.0-or-later
  4. # ---------------------------------------------------------------------------------------------------------------------
  5. # Imports (Global)
  6. import os
  7. from qt_compat import qt_config
  8. if qt_config == 5:
  9. from PyQt5.QtCore import Qt, QT_VERSION, QEvent
  10. from PyQt5.QtGui import QCursor, QMouseEvent
  11. from PyQt5.QtWidgets import QGraphicsView, QMessageBox
  12. elif qt_config == 6:
  13. from PyQt6.QtCore import Qt, QT_VERSION, QEvent
  14. from PyQt6.QtGui import QCursor, QMouseEvent
  15. from PyQt6.QtWidgets import QGraphicsView, QMessageBox
  16. # ---------------------------------------------------------------------------------------------------------------------
  17. # Imports (Custom Stuff)
  18. from carla_backend import CARLA_OS_MAC
  19. from carla_shared import CustomMessageBox, gCarla
  20. # ---------------------------------------------------------------------------------------------------------------------
  21. # Widget Class
  22. class DraggableGraphicsView(QGraphicsView):
  23. def __init__(self, parent):
  24. QGraphicsView.__init__(self, parent)
  25. self.fPanning = False
  26. exts = gCarla.utils.get_supported_file_extensions()
  27. self.fSupportedExtensions = tuple(("." + i) for i in exts)
  28. self.fWasLastDragValid = False
  29. self.setAcceptDrops(True)
  30. # -----------------------------------------------------------------------------------------------------------------
  31. def isDragUrlValid(self, filename):
  32. lfilename = filename.lower()
  33. if os.path.isdir(filename):
  34. #if os.path.exists(os.path.join(filename, "manifest.ttl")):
  35. #return True
  36. if CARLA_OS_MAC and lfilename.endswith(".vst"):
  37. return True
  38. if lfilename.endswith(".vst3") and ".vst3" in self.fSupportedExtensions:
  39. return True
  40. elif os.path.isfile(filename):
  41. if lfilename.endswith(self.fSupportedExtensions):
  42. return True
  43. return False
  44. # -----------------------------------------------------------------------------------------------------------------
  45. def dragEnterEvent(self, event):
  46. urls = event.mimeData().urls()
  47. for url in urls:
  48. if self.isDragUrlValid(url.toLocalFile()):
  49. self.fWasLastDragValid = True
  50. event.acceptProposedAction()
  51. return
  52. self.fWasLastDragValid = False
  53. QGraphicsView.dragEnterEvent(self, event)
  54. def dragMoveEvent(self, event):
  55. if not self.fWasLastDragValid:
  56. QGraphicsView.dragMoveEvent(self, event)
  57. return
  58. event.acceptProposedAction()
  59. def dragLeaveEvent(self, event):
  60. self.fWasLastDragValid = False
  61. QGraphicsView.dragLeaveEvent(self, event)
  62. # -----------------------------------------------------------------------------------------------------------------
  63. def dropEvent(self, event):
  64. event.acceptProposedAction()
  65. urls = event.mimeData().urls()
  66. if len(urls) == 0:
  67. return
  68. for url in urls:
  69. filename = url.toLocalFile()
  70. if not gCarla.gui.host.load_file(filename):
  71. CustomMessageBox(self, QMessageBox.Critical, self.tr("Error"),
  72. self.tr("Failed to load file"),
  73. gCarla.gui.host.get_last_error(), QMessageBox.Ok, QMessageBox.Ok)
  74. # -----------------------------------------------------------------------------------------------------------------
  75. def mousePressEvent(self, event):
  76. if event.button() == Qt.MiddleButton and not (event.modifiers() & Qt.ControlModifier):
  77. buttons = event.buttons()
  78. buttons &= ~Qt.MiddleButton
  79. buttons |= Qt.LeftButton
  80. timestamp = event.timestamp()
  81. self.fPanning = True
  82. self.setDragMode(QGraphicsView.ScrollHandDrag)
  83. else:
  84. timestamp = None
  85. QGraphicsView.mousePressEvent(self, event)
  86. if timestamp is None:
  87. return
  88. if qt_config == 5:
  89. event = QMouseEvent(QEvent.MouseButtonPress,
  90. event.localPos(), event.windowPos(), event.screenPos(),
  91. Qt.LeftButton, Qt.LeftButton,
  92. Qt.NoModifier,
  93. Qt.MouseEventSynthesizedByApplication)
  94. event.setTimestamp(timestamp)
  95. else:
  96. event = QMouseEvent(QEvent.MouseButtonPress,
  97. event.position(), event.scenePosition(), event.globalPosition(),
  98. Qt.LeftButton, Qt.LeftButton,
  99. Qt.NoModifier)
  100. QGraphicsView.mousePressEvent(self, event)
  101. def mouseReleaseEvent(self, event):
  102. QGraphicsView.mouseReleaseEvent(self, event)
  103. if event.button() == Qt.MiddleButton and self.fPanning:
  104. self.fPanning = False
  105. self.setDragMode(QGraphicsView.NoDrag)
  106. self.setCursor(QCursor(Qt.ArrowCursor))
  107. def wheelEvent(self, event):
  108. if event.buttons() & Qt.MiddleButton:
  109. event.ignore()
  110. return
  111. QGraphicsView.wheelEvent(self, event)
  112. # ---------------------------------------------------------------------------------------------------------------------