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.

283 lines
9.2KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Rack List Widget, a custom Qt4 widget
  4. # Copyright (C) 2011-2014 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 (Config)
  19. from carla_config import *
  20. # ------------------------------------------------------------------------------------------------------------
  21. # Imports (Global)
  22. if config_UseQt5:
  23. from PyQt5.QtCore import Qt, QSize
  24. from PyQt5.QtGui import QPainter, QPixmap
  25. from PyQt5.QtWidgets import QAbstractItemView, QFrame, QListWidget, QListWidgetItem
  26. else:
  27. from PyQt4.QtCore import Qt, QSize
  28. from PyQt4.QtGui import QAbstractItemView, QFrame, QListWidget, QListWidgetItem, QPainter, QPixmap
  29. # ------------------------------------------------------------------------------------------------------------
  30. # Imports (Custom Stuff)
  31. from carla_skin import *
  32. # ------------------------------------------------------------------------------------------------------------
  33. # Rack Widget item
  34. class RackListItem(QListWidgetItem):
  35. kRackItemType = QListWidgetItem.UserType + 1
  36. def __init__(self, parent, pluginId, useSkins):
  37. QListWidgetItem.__init__(self, parent, self.kRackItemType)
  38. self.host = parent.host
  39. if False:
  40. # kdevelop likes this :)
  41. parent = RackListWidget()
  42. host = CarlaHostMeta()
  43. self.host = host
  44. self.fWidget = AbstractPluginSlot()
  45. # ----------------------------------------------------------------------------------------------------
  46. # Internal stuff
  47. self.fParent = parent
  48. self.fPluginId = pluginId
  49. self.fUseSkins = useSkins
  50. self.fWidget = None
  51. self.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEnabled)
  52. #self.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEnabled|Qt.ItemIsDragEnabled|Qt.ItemIsDropEnabled)
  53. # ----------------------------------------------------------------------------------------------------
  54. # Set-up GUI
  55. self.recreateWidget()
  56. # --------------------------------------------------------------------------------------------------------
  57. def close(self):
  58. if self.fWidget is None:
  59. return
  60. self.fWidget.fEditDialog.close()
  61. self.fWidget.fEditDialog.setParent(None)
  62. self.fWidget.fEditDialog.deleteLater()
  63. del self.fWidget.fEditDialog
  64. self.fWidget.close()
  65. self.fWidget.setParent(None)
  66. self.fWidget.deleteLater()
  67. del self.fWidget
  68. self.fWidget = None
  69. def getEditDialog(self):
  70. if self.fWidget is None:
  71. return None
  72. return self.fWidget.fEditDialog
  73. def getPluginId(self):
  74. return self.fPluginId
  75. def getWidget(self):
  76. return self.fWidget
  77. # --------------------------------------------------------------------------------------------------------
  78. def setPluginId(self, pluginId):
  79. self.fPluginId = pluginId
  80. if self.fWidget is not None:
  81. self.fWidget.setPluginId(pluginId)
  82. # --------------------------------------------------------------------------------------------------------
  83. def recreateWidget(self):
  84. self.close()
  85. self.fWidget = createPluginSlot(self.fParent, self.host, self.fPluginId, self.fUseSkins)
  86. self.fWidget.setFixedHeight(self.fWidget.getFixedHeight())
  87. self.setSizeHint(QSize(620, self.fWidget.getFixedHeight()))
  88. self.fParent.setItemWidget(self, self.fWidget)
  89. # ------------------------------------------------------------------------------------------------------------
  90. # Rack Widget
  91. class RackListWidget(QListWidget):
  92. def __init__(self, parent):
  93. QListWidget.__init__(self, parent)
  94. self.host = None
  95. if False:
  96. # kdevelop likes this :)
  97. from carla_backend import CarlaHostMeta
  98. host = CarlaHostMeta()
  99. self.host = host
  100. self.fSupportedExtensions = []
  101. self.fWasLastDragValid = False
  102. self.setMinimumWidth(640)
  103. self.setSelectionMode(QAbstractItemView.SingleSelection)
  104. self.setSortingEnabled(False)
  105. self.setDragEnabled(True)
  106. self.setDragDropMode(QAbstractItemView.DropOnly)
  107. self.setDropIndicatorShown(True)
  108. self.viewport().setAcceptDrops(True)
  109. self.setFrameShape(QFrame.NoFrame)
  110. self.setFrameShadow(QFrame.Plain)
  111. self.fPixmapL = QPixmap(":/bitmaps/rack_interior_left.png")
  112. self.fPixmapR = QPixmap(":/bitmaps/rack_interior_right.png")
  113. self.fPixmapWidth = self.fPixmapL.width()
  114. # --------------------------------------------------------------------------------------------------------
  115. def createItem(self, pluginId, useSkins):
  116. return RackListItem(self, pluginId, useSkins)
  117. def setHost(self, host):
  118. self.host = host
  119. exts = host.get_supported_file_extensions().split(";")
  120. exts.append(".dll")
  121. if MACOS:
  122. exts.append(".dylib")
  123. if not WINDOWS:
  124. exts.append(".so")
  125. self.fSupportedExtensions = tuple(i.replace("*","") for i in exts)
  126. # --------------------------------------------------------------------------------------------------------
  127. def isDragUrlValid(self, url):
  128. filename = url.toLocalFile()
  129. if os.path.isdir(filename):
  130. if os.path.exists(os.path.join(filename, "manifest.ttl")):
  131. return True
  132. if filename.lower().endswith((".vst", ".vst3")):
  133. return True
  134. elif os.path.isfile(filename):
  135. if filename.lower().endswith(self.fSupportedExtensions):
  136. return True
  137. return False
  138. # --------------------------------------------------------------------------------------------------------
  139. def dragEnterEvent(self, event):
  140. urls = event.mimeData().urls()
  141. for url in urls:
  142. if self.isDragUrlValid(url):
  143. self.fWasLastDragValid = True
  144. event.acceptProposedAction()
  145. return
  146. self.fWasLastDragValid = False
  147. QListWidget.dragEnterEvent(self, event)
  148. def dragMoveEvent(self, event):
  149. if not self.fWasLastDragValid:
  150. QListWidget.dragMoveEvent(self, event)
  151. return
  152. event.acceptProposedAction()
  153. tryItem = self.itemAt(event.pos())
  154. if tryItem is not None:
  155. self.setCurrentRow(tryItem.getPluginId())
  156. else:
  157. self.setCurrentRow(-1)
  158. def dragLeaveEvent(self, event):
  159. self.fWasLastDragValid = False
  160. QListWidget.dragLeaveEvent(self, event)
  161. # --------------------------------------------------------------------------------------------------------
  162. # FIXME: this needs some attention
  163. # if dropping project file over 1 plugin, load it in rack or patchbay
  164. # if dropping regular files over 1 plugin, keep replacing plugins
  165. def dropEvent(self, event):
  166. event.acceptProposedAction()
  167. urls = event.mimeData().urls()
  168. if len(urls) == 0:
  169. return
  170. tryItem = self.itemAt(event.pos())
  171. if tryItem is not None:
  172. pluginId = tryItem.getPluginId()
  173. else:
  174. pluginId = -1
  175. for url in urls:
  176. if pluginId >= 0:
  177. self.host.replace_plugin(pluginId)
  178. pluginId += 1
  179. if pluginId > self.host.get_current_plugin_count():
  180. pluginId = -1
  181. filename = url.toLocalFile()
  182. if not self.host.load_file(filename):
  183. CustomMessageBox(self, QMessageBox.Critical, self.tr("Error"),
  184. self.tr("Failed to load file"),
  185. self.host.get_last_error(), QMessageBox.Ok, QMessageBox.Ok)
  186. if tryItem is not None:
  187. self.host.replace_plugin(self.host.get_max_plugin_number())
  188. #tryItem.widget.setActive(True, True, True)
  189. # --------------------------------------------------------------------------------------------------------
  190. def mousePressEvent(self, event):
  191. if self.itemAt(event.pos()) is None:
  192. event.accept()
  193. self.setCurrentRow(-1)
  194. return
  195. QListWidget.mousePressEvent(self, event)
  196. def paintEvent(self, event):
  197. painter = QPainter(self.viewport())
  198. painter.drawTiledPixmap(0, 0, self.fPixmapWidth, self.height(), self.fPixmapL)
  199. painter.drawTiledPixmap(self.width()-self.fPixmapWidth-2, 0, self.fPixmapWidth, self.height(), self.fPixmapR)
  200. QListWidget.paintEvent(self, event)
  201. # ------------------------------------------------------------------------------------------------------------