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.

429 lines
15KB

  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. if not filename:
  185. return False
  186. if filename[-1] == '/':
  187. filename = filename[:-1]
  188. lfilename = filename.lower()
  189. if os.path.isdir(filename):
  190. #if os.path.exists(os.path.join(filename, "manifest.ttl")):
  191. #return True
  192. if MACOS and lfilename.endswith(".vst"):
  193. return True
  194. if lfilename.endswith(".vst3") and ".vst3" in self.fSupportedExtensions:
  195. return True
  196. elif os.path.isfile(filename):
  197. if lfilename.endswith(self.fSupportedExtensions):
  198. return True
  199. return False
  200. # --------------------------------------------------------------------------------------------------------
  201. def dragEnterEvent(self, event):
  202. urls = event.mimeData().urls()
  203. for url in urls:
  204. if self.isDragUrlValid(url.toLocalFile()):
  205. self.fWasLastDragValid = True
  206. event.acceptProposedAction()
  207. return
  208. self.fWasLastDragValid = False
  209. QListWidget.dragEnterEvent(self, event)
  210. def dragMoveEvent(self, event):
  211. if not self.fWasLastDragValid:
  212. QListWidget.dragMoveEvent(self, event)
  213. return
  214. event.acceptProposedAction()
  215. tryItem = self.itemAt(event.pos())
  216. if tryItem is not None:
  217. self.setCurrentRow(tryItem.getPluginId())
  218. else:
  219. self.setCurrentRow(-1)
  220. def dragLeaveEvent(self, event):
  221. if self.fWasLastDragValid:
  222. self.fWasLastDragValid = False
  223. QListWidget.dragLeaveEvent(self, event)
  224. # --------------------------------------------------------------------------------------------------------
  225. # FIXME: this needs some attention
  226. # if dropping project file over 1 plugin, load it in rack or patchbay
  227. # if dropping regular files over 1 plugin, keep replacing plugins
  228. def dropEvent(self, event):
  229. event.acceptProposedAction()
  230. urls = event.mimeData().urls()
  231. if not urls:
  232. return
  233. tryItem = self.itemAt(event.pos())
  234. if tryItem is not None:
  235. pluginId = tryItem.getPluginId()
  236. else:
  237. pluginId = -1
  238. for url in urls:
  239. if pluginId >= 0:
  240. self.host.replace_plugin(pluginId)
  241. pluginId += 1
  242. if pluginId > self.host.get_current_plugin_count():
  243. pluginId = -1
  244. filename = url.toLocalFile()
  245. if not filename:
  246. continue
  247. if filename[-1] == '/':
  248. filename = filename[:-1]
  249. if not self.host.load_file(filename):
  250. CustomMessageBox(self, QMessageBox.Critical, self.tr("Error"),
  251. self.tr("Failed to load file"),
  252. self.host.get_last_error(), QMessageBox.Ok, QMessageBox.Ok)
  253. continue
  254. if filename.endswith(".carxp"):
  255. gCarla.gui.loadExternalCanvasGroupPositionsIfNeeded(filename)
  256. if tryItem is not None:
  257. self.host.replace_plugin(self.host.get_max_plugin_number())
  258. #tryItem.widget.setActive(True, True, True)
  259. # --------------------------------------------------------------------------------------------------------
  260. def mousePressEvent(self, event):
  261. if self.itemAt(event.pos()) is None and self.currentRow() != -1:
  262. event.accept()
  263. self.customClearSelection()
  264. return
  265. QListWidget.mousePressEvent(self, event)
  266. def changeEvent(self, event):
  267. if event.type() in (QEvent.StyleChange, QEvent.PaletteChange):
  268. self._updateStyle()
  269. QListWidget.changeEvent(self, event)
  270. def paintEvent(self, event):
  271. painter = QPainter(self.viewport())
  272. width = self.width()
  273. height = self.height()
  274. imgL_rect = QRect(0, 0, self.fPixmapWidth, height)
  275. imgR_rect = QRect(width-self.fPixmapWidth, 0, self.fPixmapWidth, height)
  276. painter.setBrush(self.rail_col)
  277. painter.setPen(Qt.NoPen)
  278. painter.drawRects(imgL_rect, imgR_rect)
  279. painter.setCompositionMode(QPainter.CompositionMode_Multiply)
  280. painter.drawTiledPixmap(imgL_rect, self.fPixmapL)
  281. painter.drawTiledPixmap(imgR_rect, self.fPixmapR)
  282. painter.setCompositionMode(QPainter.CompositionMode_Plus)
  283. painter.drawTiledPixmap(imgL_rect, self.fPixmapL)
  284. painter.drawTiledPixmap(imgR_rect, self.fPixmapR)
  285. painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
  286. painter.setPen(self.edge_col)
  287. painter.setBrush(Qt.NoBrush)
  288. painter.drawRect(self.fPixmapWidth, 0, width-self.fPixmapWidth*2, height)
  289. QListWidget.paintEvent(self, event)
  290. def selectionChanged(self, selected, deselected):
  291. for index in deselected.indexes():
  292. item = self.itemFromIndex(index)
  293. if item is not None:
  294. item.setSelected(False)
  295. for index in selected.indexes():
  296. item = self.itemFromIndex(index)
  297. if item is not None:
  298. item.setSelected(True)
  299. QListWidget.selectionChanged(self, selected, deselected)
  300. # --------------------------------------------------------------------------------------------------------
  301. def _updateStyle(self):
  302. palette = self.palette()
  303. bg_color = palette.window().color()
  304. base_color = palette.base().color()
  305. text_color = palette.text().color()
  306. r0,g0,b0,_ = bg_color.getRgb()
  307. r1,g1,b1,_ = text_color.getRgb()
  308. self.rail_col = QColor((r0*3+r1)/4, (g0*3+g1)/4, (b0*3+b1)/4)
  309. self.edge_col = (self.rail_col if self.rail_col.blackF() > base_color.blackF() else base_color).darker(115)
  310. # ------------------------------------------------------------------------------------------------------------