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.

359 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 compact(self):
  109. if self.fOptions['compact']:
  110. return
  111. self.recreateWidget(True)
  112. def expand(self):
  113. if not self.fOptions['compact']:
  114. return
  115. self.recreateWidget(True)
  116. def recreateWidget(self, invertCompactOption = False, firstInit = False):
  117. if invertCompactOption:
  118. self.fOptions['compact'] = not self.fOptions['compact']
  119. wasGuiShown = None
  120. if self.fWidget is not None and self.fWidget.b_gui is not None:
  121. wasGuiShown = self.fWidget.b_gui.isChecked()
  122. self.close()
  123. self.fWidget = createPluginSlot(self.fParent, self.host, self.fPluginId, self.fOptions)
  124. self.fWidget.setFixedHeight(self.fWidget.getFixedHeight())
  125. if wasGuiShown == True and self.fWidget.b_gui is not None:
  126. self.fWidget.b_gui.setChecked(True)
  127. self.setSizeHint(QSize(self.kMinimumWidth, self.fWidget.getFixedHeight()))
  128. self.fParent.setItemWidget(self, self.fWidget)
  129. if not firstInit:
  130. self.host.set_custom_data(self.fPluginId, CUSTOM_DATA_TYPE_PROPERTY,
  131. "CarlaSkinIsCompacted", "true" if self.fOptions['compact'] else "false")
  132. # ------------------------------------------------------------------------------------------------------------
  133. # Rack Widget
  134. class RackListWidget(QListWidget):
  135. def __init__(self, parent):
  136. QListWidget.__init__(self, parent)
  137. self.host = None
  138. if False:
  139. # kdevelop likes this :)
  140. from carla_backend import CarlaHostMeta
  141. host = CarlaHostNull()
  142. self.host = host
  143. exts = gCarla.utils.get_supported_file_extensions().split(";")
  144. if WINDOWS or (LINUX and not MACOS):
  145. # FIXME not for disabled bridges
  146. exts.append(".dll")
  147. if not (MACOS or WINDOWS):
  148. exts.append(".so")
  149. self.fSupportedExtensions = tuple(i.replace("*","").lower() for i in exts)
  150. self.fLastSelectedItem = None
  151. self.fWasLastDragValid = False
  152. self.fPixmapL = QPixmap(":/bitmaps/rack_interior_left.png")
  153. self.fPixmapR = QPixmap(":/bitmaps/rack_interior_right.png")
  154. self.fPixmapWidth = self.fPixmapL.width()
  155. self.setMinimumWidth(RackListItem.kMinimumWidth)
  156. self.setSelectionMode(QAbstractItemView.SingleSelection)
  157. self.setSortingEnabled(False)
  158. self.setDragEnabled(True)
  159. self.setDragDropMode(QAbstractItemView.DropOnly)
  160. self.setDropIndicatorShown(True)
  161. self.viewport().setAcceptDrops(True)
  162. self.setFrameShape(QFrame.NoFrame)
  163. self.setFrameShadow(QFrame.Plain)
  164. # --------------------------------------------------------------------------------------------------------
  165. def createItem(self, pluginId, useSkins):
  166. return RackListItem(self, pluginId, useSkins)
  167. def setHost(self, host):
  168. self.host = host
  169. # --------------------------------------------------------------------------------------------------------
  170. def customClearSelection(self):
  171. self.setCurrentRow(-1)
  172. self.clearSelection()
  173. self.clearFocus()
  174. def isDragUrlValid(self, filename):
  175. if os.path.isdir(filename):
  176. #if os.path.exists(os.path.join(filename, "manifest.ttl")):
  177. #return True
  178. if MACOS and filename.lower().endswith(".vst"):
  179. return True
  180. elif os.path.isfile(filename):
  181. if filename.lower().endswith(self.fSupportedExtensions):
  182. return True
  183. return False
  184. # --------------------------------------------------------------------------------------------------------
  185. def dragEnterEvent(self, event):
  186. urls = event.mimeData().urls()
  187. for url in urls:
  188. if self.isDragUrlValid(url.toLocalFile()):
  189. self.fWasLastDragValid = True
  190. event.acceptProposedAction()
  191. return
  192. self.fWasLastDragValid = False
  193. QListWidget.dragEnterEvent(self, event)
  194. def dragMoveEvent(self, event):
  195. if not self.fWasLastDragValid:
  196. QListWidget.dragMoveEvent(self, event)
  197. return
  198. event.acceptProposedAction()
  199. tryItem = self.itemAt(event.pos())
  200. if tryItem is not None:
  201. self.setCurrentRow(tryItem.getPluginId())
  202. else:
  203. self.setCurrentRow(-1)
  204. def dragLeaveEvent(self, event):
  205. self.fWasLastDragValid = False
  206. QListWidget.dragLeaveEvent(self, event)
  207. # --------------------------------------------------------------------------------------------------------
  208. # FIXME: this needs some attention
  209. # if dropping project file over 1 plugin, load it in rack or patchbay
  210. # if dropping regular files over 1 plugin, keep replacing plugins
  211. def dropEvent(self, event):
  212. event.acceptProposedAction()
  213. urls = event.mimeData().urls()
  214. if len(urls) == 0:
  215. return
  216. tryItem = self.itemAt(event.pos())
  217. if tryItem is not None:
  218. pluginId = tryItem.getPluginId()
  219. else:
  220. pluginId = -1
  221. for url in urls:
  222. if pluginId >= 0:
  223. self.host.replace_plugin(pluginId)
  224. pluginId += 1
  225. if pluginId > self.host.get_current_plugin_count():
  226. pluginId = -1
  227. filename = url.toLocalFile()
  228. if not self.host.load_file(filename):
  229. CustomMessageBox(self, QMessageBox.Critical, self.tr("Error"),
  230. self.tr("Failed to load file"),
  231. self.host.get_last_error(), QMessageBox.Ok, QMessageBox.Ok)
  232. if tryItem is not None:
  233. self.host.replace_plugin(self.host.get_max_plugin_number())
  234. #tryItem.widget.setActive(True, True, True)
  235. # --------------------------------------------------------------------------------------------------------
  236. def mousePressEvent(self, event):
  237. if self.itemAt(event.pos()) is None and self.currentRow() != -1:
  238. event.accept()
  239. self.customClearSelection()
  240. return
  241. QListWidget.mousePressEvent(self, event)
  242. def paintEvent(self, event):
  243. painter = QPainter(self.viewport())
  244. painter.drawTiledPixmap(0, 0, self.fPixmapWidth, self.height(), self.fPixmapL)
  245. painter.drawTiledPixmap(self.width()-self.fPixmapWidth-2, 0, self.fPixmapWidth, self.height(), self.fPixmapR)
  246. QListWidget.paintEvent(self, event)
  247. # --------------------------------------------------------------------------------------------------------
  248. def selectionChanged(self, selected, deselected):
  249. for index in deselected.indexes():
  250. item = self.itemFromIndex(index)
  251. if item is not None:
  252. item.setSelected(False)
  253. for index in selected.indexes():
  254. item = self.itemFromIndex(index)
  255. if item is not None:
  256. item.setSelected(True)
  257. QListWidget.selectionChanged(self, selected, deselected)
  258. # ------------------------------------------------------------------------------------------------------------