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.

379 lines
13KB

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