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.

270 lines
9.0KB

  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. self.fWidget.fEditDialog.close()
  59. self.fWidget.close()
  60. def getEditDialog(self):
  61. return self.fWidget.fEditDialog
  62. def getPluginId(self):
  63. return self.fPluginId
  64. def getWidget(self):
  65. return self.fWidget
  66. # --------------------------------------------------------------------------------------------------------
  67. def setPluginId(self, pluginId):
  68. self.fPluginId = pluginId
  69. self.fWidget.setPluginId(pluginId)
  70. # --------------------------------------------------------------------------------------------------------
  71. def recreateWidget(self):
  72. if self.fWidget is not None:
  73. self.fWidget.fEditDialog.close()
  74. self.fWidget.close()
  75. del self.fWidget.fEditDialog
  76. del self.fWidget
  77. self.fWidget = createPluginSlot(self.fParent, self.host, self.fPluginId, self.fUseSkins)
  78. self.fWidget.setFixedHeight(self.fWidget.getFixedHeight())
  79. self.setSizeHint(QSize(700, self.fWidget.getFixedHeight()))
  80. self.fParent.setItemWidget(self, self.fWidget)
  81. # ------------------------------------------------------------------------------------------------------------
  82. # Rack Widget
  83. class RackListWidget(QListWidget):
  84. def __init__(self, parent):
  85. QListWidget.__init__(self, parent)
  86. self.host = None
  87. if False:
  88. # kdevelop likes this :)
  89. from carla_backend import CarlaHostMeta
  90. host = CarlaHostMeta()
  91. self.host = host
  92. self.fSupportedExtensions = []
  93. self.fWasLastDragValid = False
  94. self.setMinimumWidth(740)
  95. self.setSelectionMode(QAbstractItemView.SingleSelection)
  96. self.setSortingEnabled(False)
  97. self.setDragEnabled(True)
  98. self.setDragDropMode(QAbstractItemView.DropOnly)
  99. self.setDropIndicatorShown(True)
  100. self.viewport().setAcceptDrops(True)
  101. self.setFrameShape(QFrame.NoFrame)
  102. self.setFrameShadow(QFrame.Plain)
  103. self.fPixmapL = QPixmap(":/bitmaps/rack_interior_left.png")
  104. self.fPixmapR = QPixmap(":/bitmaps/rack_interior_right.png")
  105. self.fPixmapWidth = self.fPixmapL.width()
  106. # --------------------------------------------------------------------------------------------------------
  107. def createItem(self, pluginId, useSkins):
  108. return RackListItem(self, pluginId, useSkins)
  109. def setHost(self, host):
  110. self.host = host
  111. exts = host.get_supported_file_extensions().split(";")
  112. exts.append(".dll")
  113. if MACOS:
  114. exts.append(".dylib")
  115. if not WINDOWS:
  116. exts.append(".so")
  117. self.fSupportedExtensions = tuple(i.replace("*","") for i in exts)
  118. # --------------------------------------------------------------------------------------------------------
  119. def isDragUrlValid(self, url):
  120. filename = url.toLocalFile()
  121. if os.path.isdir(filename):
  122. if os.path.exists(os.path.join(filename, "manifest.ttl")):
  123. return True
  124. if filename.lower().endswith((".vst", ".vst3")):
  125. return True
  126. elif os.path.isfile(filename):
  127. if filename.lower().endswith(self.fSupportedExtensions):
  128. return True
  129. return False
  130. # --------------------------------------------------------------------------------------------------------
  131. def dragEnterEvent(self, event):
  132. urls = event.mimeData().urls()
  133. for url in urls:
  134. if self.isDragUrlValid(url):
  135. self.fWasLastDragValid = True
  136. event.acceptProposedAction()
  137. return
  138. self.fWasLastDragValid = False
  139. QListWidget.dragEnterEvent(self, event)
  140. def dragMoveEvent(self, event):
  141. if not self.fWasLastDragValid:
  142. QListWidget.dragMoveEvent(self, event)
  143. return
  144. event.acceptProposedAction()
  145. tryItem = self.itemAt(event.pos())
  146. if tryItem is not None:
  147. self.setCurrentRow(tryItem.getPluginId())
  148. else:
  149. self.setCurrentRow(-1)
  150. def dragLeaveEvent(self, event):
  151. self.fWasLastDragValid = False
  152. QListWidget.dragLeaveEvent(self, event)
  153. # --------------------------------------------------------------------------------------------------------
  154. # FIXME: this needs some attention
  155. # if dropping project file over 1 plugin, load it in rack or patchbay
  156. # if dropping regular files over 1 plugin, keep replacing plugins
  157. def dropEvent(self, event):
  158. event.acceptProposedAction()
  159. urls = event.mimeData().urls()
  160. if len(urls) == 0:
  161. return
  162. tryItem = self.itemAt(event.pos())
  163. if tryItem is not None:
  164. pluginId = tryItem.getPluginId()
  165. else:
  166. pluginId = -1
  167. for url in urls:
  168. if pluginId >= 0:
  169. self.host.replace_plugin(pluginId)
  170. pluginId += 1
  171. if pluginId > self.host.get_current_plugin_count():
  172. pluginId = -1
  173. filename = url.toLocalFile()
  174. if not self.host.load_file(filename):
  175. CustomMessageBox(self, QMessageBox.Critical, self.tr("Error"),
  176. self.tr("Failed to load file"),
  177. self.host.get_last_error(), QMessageBox.Ok, QMessageBox.Ok)
  178. if tryItem is not None:
  179. self.host.replace_plugin(self.host.get_max_plugin_number())
  180. #tryItem.widget.setActive(True, True, True)
  181. # --------------------------------------------------------------------------------------------------------
  182. def mousePressEvent(self, event):
  183. if self.itemAt(event.pos()) is None:
  184. event.accept()
  185. self.setCurrentRow(-1)
  186. return
  187. QListWidget.mousePressEvent(self, event)
  188. def paintEvent(self, event):
  189. painter = QPainter(self.viewport())
  190. painter.drawTiledPixmap(0, 0, self.fPixmapWidth, self.height(), self.fPixmapL)
  191. painter.drawTiledPixmap(self.width()-self.fPixmapWidth-2, 0, self.fPixmapWidth, self.height(), self.fPixmapR)
  192. QListWidget.paintEvent(self, event)
  193. # ------------------------------------------------------------------------------------------------------------