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.

152 lines
5.3KB

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