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.

148 lines
5.1KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Middle-click draggable QGraphicsView
  4. # Copyright (C) 2016-2019 Filipe Coelho <falktx@falktx.com>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. # ------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. from PyQt5.QtCore import Qt
  20. from PyQt5.QtGui import QCursor, QMouseEvent
  21. from PyQt5.QtWidgets import QGraphicsView
  22. # ------------------------------------------------------------------------------------------------------------
  23. # Imports (Custom Stuff)
  24. from carla_shared import *
  25. # ------------------------------------------------------------------------------------------------------------
  26. # Widget Class
  27. class DraggableGraphicsView(QGraphicsView):
  28. def __init__(self, parent):
  29. QGraphicsView.__init__(self, parent)
  30. self.fPanning = False
  31. self.fCtrlDown = False
  32. try:
  33. self.fMiddleButton = Qt.MiddleButton
  34. except:
  35. self.fMiddleButton = Qt.MidButton
  36. exts = gCarla.utils.get_supported_file_extensions()
  37. self.fSupportedExtensions = tuple(("." + i) for i in exts)
  38. self.fWasLastDragValid = False
  39. self.setAcceptDrops(True)
  40. # --------------------------------------------------------------------------------------------------------
  41. def isDragUrlValid(self, filename):
  42. lfilename = filename.lower()
  43. if os.path.isdir(filename):
  44. #if os.path.exists(os.path.join(filename, "manifest.ttl")):
  45. #return True
  46. if MACOS and lfilename.endswith(".vst"):
  47. return True
  48. elif lfilename.endswith(".vst3") and ".vst3" in self.fSupportedExtensions:
  49. return True
  50. elif os.path.isfile(filename):
  51. if lfilename.endswith(self.fSupportedExtensions):
  52. return True
  53. return False
  54. # --------------------------------------------------------------------------------------------------------
  55. def dragEnterEvent(self, event):
  56. urls = event.mimeData().urls()
  57. print(urls)
  58. for url in urls:
  59. if self.isDragUrlValid(url.toLocalFile()):
  60. self.fWasLastDragValid = True
  61. event.acceptProposedAction()
  62. return
  63. self.fWasLastDragValid = False
  64. QGraphicsView.dragEnterEvent(self, event)
  65. def dragMoveEvent(self, event):
  66. if not self.fWasLastDragValid:
  67. QGraphicsView.dragMoveEvent(self, event)
  68. return
  69. event.acceptProposedAction()
  70. def dragLeaveEvent(self, event):
  71. self.fWasLastDragValid = False
  72. QGraphicsView.dragLeaveEvent(self, event)
  73. # --------------------------------------------------------------------------------------------------------
  74. def dropEvent(self, event):
  75. event.acceptProposedAction()
  76. urls = event.mimeData().urls()
  77. if len(urls) == 0:
  78. return
  79. for url in urls:
  80. filename = url.toLocalFile()
  81. if not gCarla.gui.host.load_file(filename):
  82. CustomMessageBox(self, QMessageBox.Critical, self.tr("Error"),
  83. self.tr("Failed to load file"),
  84. gCarla.gui.host.get_last_error(), QMessageBox.Ok, QMessageBox.Ok)
  85. # --------------------------------------------------------------------------------------------------------
  86. def mousePressEvent(self, event):
  87. if event.button() == self.fMiddleButton and not self.fCtrlDown:
  88. self.fPanning = True
  89. self.setDragMode(QGraphicsView.ScrollHandDrag)
  90. event = QMouseEvent(event.type(), event.localPos(), event.windowPos(), event.screenPos(),
  91. Qt.LeftButton, Qt.LeftButton, event.modifiers(), Qt.MouseEventSynthesizedByApplication)
  92. QGraphicsView.mousePressEvent(self, event)
  93. def mouseReleaseEvent(self, event):
  94. QGraphicsView.mouseReleaseEvent(self, event)
  95. if not self.fPanning:
  96. return
  97. self.fPanning = False
  98. self.setDragMode(QGraphicsView.NoDrag)
  99. self.setCursor(QCursor(Qt.ArrowCursor))
  100. # --------------------------------------------------------------------------------------------------------
  101. def keyPressEvent(self, event):
  102. if event.key() == Qt.Key_Control:
  103. self.fCtrlDown = True
  104. QGraphicsView.keyPressEvent(self, event)
  105. def keyReleaseEvent(self, event):
  106. if event.key() == Qt.Key_Control:
  107. self.fCtrlDown = False
  108. QGraphicsView.keyReleaseEvent(self, event)