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.

351 lines
12KB

  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. kMinimumWidth = 620
  37. def __init__(self, parent, pluginId, useSkins):
  38. QListWidgetItem.__init__(self, parent, self.kRackItemType)
  39. self.host = parent.host
  40. if False:
  41. # kdevelop likes this :)
  42. parent = RackListWidget()
  43. host = CarlaHostNull()
  44. self.host = host
  45. self.fWidget = AbstractPluginSlot()
  46. # ----------------------------------------------------------------------------------------------------
  47. # Internal stuff
  48. self.fParent = parent
  49. self.fPluginId = pluginId
  50. self.fWidget = None
  51. self.fOptions = {
  52. 'compact': False,
  53. 'useSkins': useSkins
  54. }
  55. for i in range(self.host.get_custom_data_count(pluginId)):
  56. cdata = self.host.get_custom_data(pluginId, i)
  57. if cdata['type'] == CUSTOM_DATA_TYPE_PROPERTY and cdata['key'] == "CarlaSkinIsCompacted":
  58. self.fOptions['compact'] = bool(cdata['value'] == "true")
  59. break
  60. self.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEnabled)
  61. #self.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEnabled|Qt.ItemIsDragEnabled)
  62. # ----------------------------------------------------------------------------------------------------
  63. # Set-up GUI
  64. self.recreateWidget(firstInit = True)
  65. # --------------------------------------------------------------------------------------------------------
  66. def close(self):
  67. if self.fWidget is None:
  68. return
  69. widget = self.fWidget
  70. self.fWidget = None
  71. self.fParent.customClearSelection()
  72. self.fParent.setItemWidget(self, None)
  73. widget.fEditDialog.close()
  74. widget.fEditDialog.setParent(None)
  75. widget.fEditDialog.deleteLater()
  76. del widget.fEditDialog
  77. widget.close()
  78. widget.setParent(None)
  79. widget.deleteLater()
  80. del widget
  81. def getEditDialog(self):
  82. if self.fWidget is None:
  83. return None
  84. return self.fWidget.fEditDialog
  85. def getPluginId(self):
  86. return self.fPluginId
  87. def getWidget(self):
  88. return self.fWidget
  89. def isCompacted(self):
  90. return self.fOptions['compact']
  91. def isUsingSkins(self):
  92. return self.fOptions['useSkins']
  93. # --------------------------------------------------------------------------------------------------------
  94. def setPluginId(self, pluginId):
  95. self.fPluginId = pluginId
  96. if self.fWidget is not None:
  97. self.fWidget.setPluginId(pluginId)
  98. def setSelected(self, select):
  99. if self.fWidget is not None:
  100. self.fWidget.setSelected(select)
  101. QListWidgetItem.setSelected(self, select)
  102. # --------------------------------------------------------------------------------------------------------
  103. def setCompacted(self, compact):
  104. self.fOptions['compact'] = compact
  105. def setUsingSkins(self, useSkins):
  106. self.fOptions['useSkins'] = useSkins
  107. # --------------------------------------------------------------------------------------------------------
  108. def recreateWidget(self, invertCompactOption = False, firstInit = False):
  109. if invertCompactOption:
  110. self.fOptions['compact'] = not self.fOptions['compact']
  111. wasGuiShown = None
  112. if self.fWidget is not None and self.fWidget.b_gui is not None:
  113. wasGuiShown = self.fWidget.b_gui.isChecked()
  114. self.close()
  115. self.fWidget = createPluginSlot(self.fParent, self.host, self.fPluginId, self.fOptions)
  116. self.fWidget.setFixedHeight(self.fWidget.getFixedHeight())
  117. if wasGuiShown == True and self.fWidget.b_gui is not None:
  118. self.fWidget.b_gui.setChecked(True)
  119. self.setSizeHint(QSize(self.kMinimumWidth, self.fWidget.getFixedHeight()))
  120. self.fParent.setItemWidget(self, self.fWidget)
  121. if not firstInit:
  122. self.host.set_custom_data(self.fPluginId, CUSTOM_DATA_TYPE_PROPERTY,
  123. "CarlaSkinIsCompacted", "true" if self.fOptions['compact'] else "false")
  124. # ------------------------------------------------------------------------------------------------------------
  125. # Rack Widget
  126. class RackListWidget(QListWidget):
  127. def __init__(self, parent):
  128. QListWidget.__init__(self, parent)
  129. self.host = None
  130. if False:
  131. # kdevelop likes this :)
  132. from carla_backend import CarlaHostMeta
  133. host = CarlaHostNull()
  134. self.host = host
  135. exts = gCarla.utils.get_supported_file_extensions().split(";")
  136. #exts.append(".dll")
  137. #if MACOS:
  138. #exts.append(".dylib")
  139. #if not WINDOWS:
  140. #exts.append(".so")
  141. self.fSupportedExtensions = tuple(i.replace("*","").lower() for i in exts)
  142. self.fLastSelectedItem = None
  143. self.fWasLastDragValid = False
  144. self.fPixmapL = QPixmap(":/bitmaps/rack_interior_left.png")
  145. self.fPixmapR = QPixmap(":/bitmaps/rack_interior_right.png")
  146. self.fPixmapWidth = self.fPixmapL.width()
  147. self.setMinimumWidth(RackListItem.kMinimumWidth)
  148. self.setSelectionMode(QAbstractItemView.SingleSelection)
  149. self.setSortingEnabled(False)
  150. self.setDragEnabled(True)
  151. self.setDragDropMode(QAbstractItemView.DropOnly)
  152. self.setDropIndicatorShown(True)
  153. self.viewport().setAcceptDrops(True)
  154. self.setFrameShape(QFrame.NoFrame)
  155. self.setFrameShadow(QFrame.Plain)
  156. # --------------------------------------------------------------------------------------------------------
  157. def createItem(self, pluginId, useSkins):
  158. return RackListItem(self, pluginId, useSkins)
  159. def setHost(self, host):
  160. self.host = host
  161. # --------------------------------------------------------------------------------------------------------
  162. def customClearSelection(self):
  163. self.setCurrentRow(-1)
  164. self.clearSelection()
  165. self.clearFocus()
  166. def isDragUrlValid(self, url):
  167. filename = url.toLocalFile()
  168. #if os.path.isdir(filename):
  169. #if os.path.exists(os.path.join(filename, "manifest.ttl")):
  170. #return True
  171. #if MACOS and filename.lower().endswith((".vst", ".vst3")):
  172. #return True
  173. if os.path.isfile(filename):
  174. if filename.lower().endswith(self.fSupportedExtensions):
  175. return True
  176. return False
  177. # --------------------------------------------------------------------------------------------------------
  178. def dragEnterEvent(self, event):
  179. urls = event.mimeData().urls()
  180. for url in urls:
  181. if self.isDragUrlValid(url):
  182. self.fWasLastDragValid = True
  183. event.acceptProposedAction()
  184. return
  185. self.fWasLastDragValid = False
  186. QListWidget.dragEnterEvent(self, event)
  187. def dragMoveEvent(self, event):
  188. if not self.fWasLastDragValid:
  189. QListWidget.dragMoveEvent(self, event)
  190. return
  191. event.acceptProposedAction()
  192. tryItem = self.itemAt(event.pos())
  193. if tryItem is not None:
  194. self.setCurrentRow(tryItem.getPluginId())
  195. else:
  196. self.setCurrentRow(-1)
  197. def dragLeaveEvent(self, event):
  198. self.fWasLastDragValid = False
  199. QListWidget.dragLeaveEvent(self, event)
  200. # --------------------------------------------------------------------------------------------------------
  201. # FIXME: this needs some attention
  202. # if dropping project file over 1 plugin, load it in rack or patchbay
  203. # if dropping regular files over 1 plugin, keep replacing plugins
  204. def dropEvent(self, event):
  205. event.acceptProposedAction()
  206. urls = event.mimeData().urls()
  207. if len(urls) == 0:
  208. return
  209. tryItem = self.itemAt(event.pos())
  210. if tryItem is not None:
  211. pluginId = tryItem.getPluginId()
  212. else:
  213. pluginId = -1
  214. for url in urls:
  215. if pluginId >= 0:
  216. self.host.replace_plugin(pluginId)
  217. pluginId += 1
  218. if pluginId > self.host.get_current_plugin_count():
  219. pluginId = -1
  220. filename = url.toLocalFile()
  221. if not self.host.load_file(filename):
  222. CustomMessageBox(self, QMessageBox.Critical, self.tr("Error"),
  223. self.tr("Failed to load file"),
  224. self.host.get_last_error(), QMessageBox.Ok, QMessageBox.Ok)
  225. if tryItem is not None:
  226. self.host.replace_plugin(self.host.get_max_plugin_number())
  227. #tryItem.widget.setActive(True, True, True)
  228. # --------------------------------------------------------------------------------------------------------
  229. def mousePressEvent(self, event):
  230. if self.itemAt(event.pos()) is None and self.currentRow() != -1:
  231. event.accept()
  232. self.customClearSelection()
  233. return
  234. QListWidget.mousePressEvent(self, event)
  235. def paintEvent(self, event):
  236. painter = QPainter(self.viewport())
  237. painter.drawTiledPixmap(0, 0, self.fPixmapWidth, self.height(), self.fPixmapL)
  238. painter.drawTiledPixmap(self.width()-self.fPixmapWidth-2, 0, self.fPixmapWidth, self.height(), self.fPixmapR)
  239. QListWidget.paintEvent(self, event)
  240. # --------------------------------------------------------------------------------------------------------
  241. def selectionChanged(self, selected, deselected):
  242. for index in deselected.indexes():
  243. item = self.itemFromIndex(index)
  244. if item is not None:
  245. item.setSelected(False)
  246. for index in selected.indexes():
  247. item = self.itemFromIndex(index)
  248. if item is not None:
  249. item.setSelected(True)
  250. QListWidget.selectionChanged(self, selected, deselected)
  251. # ------------------------------------------------------------------------------------------------------------