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.

417 lines
14KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Rack List Widget, a custom Qt widget
  4. # Copyright (C) 2011-2019 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 (Global)
  19. import os
  20. # ------------------------------------------------------------------------------------------------------------
  21. # Imports (PyQt5)
  22. from PyQt5.QtCore import Qt, QSize, QRect, QEvent
  23. from PyQt5.QtGui import QColor, QPainter, QPixmap
  24. from PyQt5.QtWidgets import QAbstractItemView, QListWidget, QListWidgetItem, QMessageBox
  25. # ------------------------------------------------------------------------------------------------------------
  26. # Imports (Custom Stuff)
  27. from carla_backend import CUSTOM_DATA_TYPE_PROPERTY, MACOS
  28. from carla_shared import gCarla, CustomMessageBox
  29. from carla_skin import createPluginSlot
  30. # ------------------------------------------------------------------------------------------------------------
  31. # Rack Widget item
  32. class RackListItem(QListWidgetItem):
  33. kRackItemType = QListWidgetItem.UserType + 1
  34. kMinimumWidth = 620
  35. def __init__(self, parent, pluginId):
  36. QListWidgetItem.__init__(self, parent, self.kRackItemType)
  37. self.host = parent.host
  38. # ----------------------------------------------------------------------------------------------------
  39. # Internal stuff
  40. self.fParent = parent
  41. self.fPluginId = pluginId
  42. self.fWidget = None
  43. color = self.host.get_custom_data_value(pluginId, CUSTOM_DATA_TYPE_PROPERTY, "CarlaColor")
  44. skin = self.host.get_custom_data_value(pluginId, CUSTOM_DATA_TYPE_PROPERTY, "CarlaSkin")
  45. compact = bool(self.host.get_custom_data_value(pluginId,
  46. CUSTOM_DATA_TYPE_PROPERTY,
  47. "CarlaSkinIsCompacted") == "true")
  48. if color:
  49. try:
  50. color = tuple(int(i) for i in color.split(";",3))
  51. except Exception as e:
  52. print("Color value decode failed for", color, "error was:", e)
  53. color = None
  54. else:
  55. color = None
  56. self.fOptions = {
  57. 'color' : color,
  58. 'skin' : skin,
  59. 'compact': compact and skin != "classic",
  60. }
  61. self.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEnabled)
  62. #self.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEnabled|Qt.ItemIsDragEnabled)
  63. # ----------------------------------------------------------------------------------------------------
  64. # Set-up GUI
  65. self.recreateWidget(firstInit = True)
  66. # --------------------------------------------------------------------------------------------------------
  67. def close(self):
  68. if self.fWidget is None:
  69. return
  70. widget = self.fWidget
  71. self.fWidget = None
  72. self.fParent.customClearSelection()
  73. self.fParent.setItemWidget(self, None)
  74. widget.fEditDialog.close()
  75. widget.fEditDialog.setParent(None)
  76. widget.fEditDialog.deleteLater()
  77. widget.fEditDialog = None
  78. widget.close()
  79. widget.setParent(None)
  80. widget.deleteLater()
  81. del widget
  82. def getEditDialog(self):
  83. if self.fWidget is None:
  84. return None
  85. return self.fWidget.fEditDialog
  86. def getPluginId(self):
  87. return self.fPluginId
  88. def getWidget(self):
  89. return self.fWidget
  90. def isCompacted(self):
  91. return self.fOptions['compact']
  92. def isGuiShown(self):
  93. if self.fWidget is None or self.fWidget.b_gui is not None:
  94. return None
  95. return self.fWidget.b_gui.isChecked()
  96. # --------------------------------------------------------------------------------------------------------
  97. def setPluginId(self, pluginId):
  98. self.fPluginId = pluginId
  99. if self.fWidget is not None:
  100. self.fWidget.setPluginId(pluginId)
  101. def setSelected(self, select):
  102. if self.fWidget is not None:
  103. self.fWidget.setSelected(select)
  104. QListWidgetItem.setSelected(self, select)
  105. # --------------------------------------------------------------------------------------------------------
  106. def setCompacted(self, compact):
  107. self.fOptions['compact'] = compact
  108. # --------------------------------------------------------------------------------------------------------
  109. def compact(self):
  110. if self.fOptions['compact']:
  111. return
  112. self.recreateWidget(True)
  113. def expand(self):
  114. if not self.fOptions['compact']:
  115. return
  116. self.recreateWidget(True)
  117. def recreateWidget(self, invertCompactOption = False, firstInit = False, newColor = None, newSkin = None):
  118. if invertCompactOption:
  119. self.fOptions['compact'] = not self.fOptions['compact']
  120. if newColor is not None:
  121. self.fOptions['color'] = newColor
  122. if newSkin is not None:
  123. self.fOptions['skin'] = newSkin
  124. wasGuiShown = None
  125. if self.fWidget is not None and self.fWidget.b_gui is not None:
  126. wasGuiShown = self.fWidget.b_gui.isChecked()
  127. self.close()
  128. self.fWidget = createPluginSlot(self.fParent, self.host, self.fPluginId, self.fOptions)
  129. self.fWidget.setFixedHeight(self.fWidget.getFixedHeight())
  130. if wasGuiShown and self.fWidget.b_gui is not None:
  131. self.fWidget.b_gui.setChecked(True)
  132. self.setSizeHint(QSize(self.kMinimumWidth, self.fWidget.getFixedHeight()))
  133. self.fParent.setItemWidget(self, self.fWidget)
  134. if not firstInit:
  135. self.host.set_custom_data(self.fPluginId, CUSTOM_DATA_TYPE_PROPERTY,
  136. "CarlaSkinIsCompacted", "true" if self.fOptions['compact'] else "false")
  137. def recreateWidget2(self, wasCompacted, wasGuiShown):
  138. self.fOptions['compact'] = wasCompacted
  139. self.close()
  140. self.fWidget = createPluginSlot(self.fParent, self.host, self.fPluginId, self.fOptions)
  141. self.fWidget.setFixedHeight(self.fWidget.getFixedHeight())
  142. if wasGuiShown and self.fWidget.b_gui is not None:
  143. self.fWidget.b_gui.setChecked(True)
  144. self.setSizeHint(QSize(self.kMinimumWidth, self.fWidget.getFixedHeight()))
  145. self.fParent.setItemWidget(self, self.fWidget)
  146. self.host.set_custom_data(self.fPluginId, CUSTOM_DATA_TYPE_PROPERTY,
  147. "CarlaSkinIsCompacted", "true" if wasCompacted else "false")
  148. # ------------------------------------------------------------------------------------------------------------
  149. # Rack Widget
  150. class RackListWidget(QListWidget):
  151. def __init__(self, parent):
  152. QListWidget.__init__(self, parent)
  153. self.host = None
  154. self.fParent = None
  155. exts = gCarla.utils.get_supported_file_extensions()
  156. self.fSupportedExtensions = tuple(("." + i) for i in exts)
  157. self.fLastSelectedItem = None
  158. self.fWasLastDragValid = False
  159. self.fPixmapL = QPixmap(":/bitmaps/rack_interior_left.png")
  160. self.fPixmapR = QPixmap(":/bitmaps/rack_interior_right.png")
  161. self.fPixmapWidth = self.fPixmapL.width()
  162. self.setMinimumWidth(RackListItem.kMinimumWidth)
  163. self.setSelectionMode(QAbstractItemView.SingleSelection)
  164. self.setSortingEnabled(False)
  165. self.setDragEnabled(True)
  166. self.setDragDropMode(QAbstractItemView.DropOnly)
  167. self.setDropIndicatorShown(True)
  168. self.viewport().setAcceptDrops(True)
  169. self._updateStyle()
  170. # --------------------------------------------------------------------------------------------------------
  171. def createItem(self, pluginId):
  172. return RackListItem(self, pluginId)
  173. def getPluginCount(self):
  174. return self.fParent.getPluginCount()
  175. def setHostAndParent(self, host, parent):
  176. self.host = host
  177. self.fParent = parent
  178. # --------------------------------------------------------------------------------------------------------
  179. def customClearSelection(self):
  180. self.setCurrentRow(-1)
  181. self.clearSelection()
  182. self.clearFocus()
  183. def isDragUrlValid(self, filename):
  184. lfilename = filename.lower()
  185. if os.path.isdir(filename):
  186. #if os.path.exists(os.path.join(filename, "manifest.ttl")):
  187. #return True
  188. if MACOS and lfilename.endswith(".vst"):
  189. return True
  190. if lfilename.endswith(".vst3") and ".vst3" in self.fSupportedExtensions:
  191. return True
  192. elif os.path.isfile(filename):
  193. if lfilename.endswith(self.fSupportedExtensions):
  194. return True
  195. return False
  196. # --------------------------------------------------------------------------------------------------------
  197. def dragEnterEvent(self, event):
  198. urls = event.mimeData().urls()
  199. for url in urls:
  200. if self.isDragUrlValid(url.toLocalFile()):
  201. self.fWasLastDragValid = True
  202. event.acceptProposedAction()
  203. return
  204. self.fWasLastDragValid = False
  205. QListWidget.dragEnterEvent(self, event)
  206. def dragMoveEvent(self, event):
  207. if not self.fWasLastDragValid:
  208. QListWidget.dragMoveEvent(self, event)
  209. return
  210. event.acceptProposedAction()
  211. tryItem = self.itemAt(event.pos())
  212. if tryItem is not None:
  213. self.setCurrentRow(tryItem.getPluginId())
  214. else:
  215. self.setCurrentRow(-1)
  216. def dragLeaveEvent(self, event):
  217. if self.fWasLastDragValid:
  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 not urls:
  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. continue
  246. if filename.endswith(".carxp"):
  247. gCarla.gui.loadExternalCanvasGroupPositionsIfNeeded(filename)
  248. if tryItem is not None:
  249. self.host.replace_plugin(self.host.get_max_plugin_number())
  250. #tryItem.widget.setActive(True, True, True)
  251. # --------------------------------------------------------------------------------------------------------
  252. def mousePressEvent(self, event):
  253. if self.itemAt(event.pos()) is None and self.currentRow() != -1:
  254. event.accept()
  255. self.customClearSelection()
  256. return
  257. QListWidget.mousePressEvent(self, event)
  258. def changeEvent(self, event):
  259. if event.type() in (QEvent.StyleChange, QEvent.PaletteChange):
  260. self._updateStyle()
  261. QListWidget.changeEvent(self, event)
  262. def paintEvent(self, event):
  263. painter = QPainter(self.viewport())
  264. width = self.width()
  265. height = self.height()
  266. imgL_rect = QRect(0, 0, self.fPixmapWidth, height)
  267. imgR_rect = QRect(width-self.fPixmapWidth, 0, self.fPixmapWidth, height)
  268. painter.setBrush(self.rail_col)
  269. painter.setPen(Qt.NoPen)
  270. painter.drawRects(imgL_rect, imgR_rect)
  271. painter.setCompositionMode(QPainter.CompositionMode_Multiply)
  272. painter.drawTiledPixmap(imgL_rect, self.fPixmapL)
  273. painter.drawTiledPixmap(imgR_rect, self.fPixmapR)
  274. painter.setCompositionMode(QPainter.CompositionMode_Plus)
  275. painter.drawTiledPixmap(imgL_rect, self.fPixmapL)
  276. painter.drawTiledPixmap(imgR_rect, self.fPixmapR)
  277. painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
  278. painter.setPen(self.edge_col)
  279. painter.setBrush(Qt.NoBrush)
  280. painter.drawRect(self.fPixmapWidth, 0, width-self.fPixmapWidth*2, height)
  281. QListWidget.paintEvent(self, event)
  282. def selectionChanged(self, selected, deselected):
  283. for index in deselected.indexes():
  284. item = self.itemFromIndex(index)
  285. if item is not None:
  286. item.setSelected(False)
  287. for index in selected.indexes():
  288. item = self.itemFromIndex(index)
  289. if item is not None:
  290. item.setSelected(True)
  291. QListWidget.selectionChanged(self, selected, deselected)
  292. # --------------------------------------------------------------------------------------------------------
  293. def _updateStyle(self):
  294. palette = self.palette()
  295. bg_color = palette.window().color()
  296. base_color = palette.base().color()
  297. text_color = palette.text().color()
  298. r0,g0,b0,_ = bg_color.getRgb()
  299. r1,g1,b1,_ = text_color.getRgb()
  300. self.rail_col = QColor((r0*3+r1)/4, (g0*3+g1)/4, (b0*3+b1)/4)
  301. self.edge_col = (self.rail_col if self.rail_col.blackF() > base_color.blackF() else base_color).darker(115)
  302. # ------------------------------------------------------------------------------------------------------------