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.

411 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. from PyQt5.QtCore import Qt, QSize, QRect, QEvent
  20. from PyQt5.QtGui import QPainter, QPixmap
  21. from PyQt5.QtWidgets import QAbstractItemView, QFrame, QListWidget, QListWidgetItem
  22. # ------------------------------------------------------------------------------------------------------------
  23. # Imports (Custom Stuff)
  24. from carla_skin import *
  25. # ------------------------------------------------------------------------------------------------------------
  26. # Rack Widget item
  27. class RackListItem(QListWidgetItem):
  28. kRackItemType = QListWidgetItem.UserType + 1
  29. kMinimumWidth = 620
  30. def __init__(self, parent, pluginId):
  31. QListWidgetItem.__init__(self, parent, self.kRackItemType)
  32. self.host = parent.host
  33. if False:
  34. # kdevelop likes this :)
  35. parent = RackListWidget()
  36. host = CarlaHostNull()
  37. self.host = host
  38. self.fWidget = AbstractPluginSlot()
  39. # ----------------------------------------------------------------------------------------------------
  40. # Internal stuff
  41. self.fParent = parent
  42. self.fPluginId = pluginId
  43. self.fWidget = None
  44. color = self.host.get_custom_data_value(pluginId, CUSTOM_DATA_TYPE_PROPERTY, "CarlaColor")
  45. skin = self.host.get_custom_data_value(pluginId, CUSTOM_DATA_TYPE_PROPERTY, "CarlaSkin")
  46. compact = bool(self.host.get_custom_data_value(pluginId, CUSTOM_DATA_TYPE_PROPERTY, "CarlaSkinIsCompacted") == "true")
  47. if color:
  48. try:
  49. color = tuple(int(i) for i in color.split(";",3))
  50. except:
  51. print("Color value decode failed for", color)
  52. color = None
  53. else:
  54. color = None
  55. self.fOptions = {
  56. 'color' : color,
  57. 'skin' : skin,
  58. 'compact': compact and skin != "classic",
  59. }
  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 isGuiShown(self):
  92. if self.fWidget is None or self.fWidget.b_gui is not None:
  93. return None
  94. return self.fWidget.b_gui.isChecked()
  95. # --------------------------------------------------------------------------------------------------------
  96. def setPluginId(self, pluginId):
  97. self.fPluginId = pluginId
  98. if self.fWidget is not None:
  99. self.fWidget.setPluginId(pluginId)
  100. def setSelected(self, select):
  101. if self.fWidget is not None:
  102. self.fWidget.setSelected(select)
  103. QListWidgetItem.setSelected(self, select)
  104. # --------------------------------------------------------------------------------------------------------
  105. def setCompacted(self, compact):
  106. self.fOptions['compact'] = compact
  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, newColor = None, newSkin = None):
  117. if invertCompactOption:
  118. self.fOptions['compact'] = not self.fOptions['compact']
  119. if newColor is not None:
  120. self.fOptions['color'] = newColor
  121. if newSkin is not None:
  122. self.fOptions['skin'] = newSkin
  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.updateStyle()
  173. # --------------------------------------------------------------------------------------------------------
  174. def createItem(self, pluginId):
  175. return RackListItem(self, pluginId)
  176. def getPluginCount(self):
  177. return self.fParent.getPluginCount()
  178. def setHostAndParent(self, host, parent):
  179. self.host = host
  180. self.fParent = parent
  181. # --------------------------------------------------------------------------------------------------------
  182. def customClearSelection(self):
  183. self.setCurrentRow(-1)
  184. self.clearSelection()
  185. self.clearFocus()
  186. def isDragUrlValid(self, filename):
  187. if os.path.isdir(filename):
  188. #if os.path.exists(os.path.join(filename, "manifest.ttl")):
  189. #return True
  190. if MACOS and filename.lower().endswith((".vst", ".vst3")):
  191. return True
  192. elif os.path.isfile(filename):
  193. if filename.lower().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. self.fWasLastDragValid = False
  218. QListWidget.dragLeaveEvent(self, event)
  219. # --------------------------------------------------------------------------------------------------------
  220. # FIXME: this needs some attention
  221. # if dropping project file over 1 plugin, load it in rack or patchbay
  222. # if dropping regular files over 1 plugin, keep replacing plugins
  223. def dropEvent(self, event):
  224. event.acceptProposedAction()
  225. urls = event.mimeData().urls()
  226. if len(urls) == 0:
  227. return
  228. tryItem = self.itemAt(event.pos())
  229. if tryItem is not None:
  230. pluginId = tryItem.getPluginId()
  231. else:
  232. pluginId = -1
  233. for url in urls:
  234. if pluginId >= 0:
  235. self.host.replace_plugin(pluginId)
  236. pluginId += 1
  237. if pluginId > self.host.get_current_plugin_count():
  238. pluginId = -1
  239. filename = url.toLocalFile()
  240. if not self.host.load_file(filename):
  241. CustomMessageBox(self, QMessageBox.Critical, self.tr("Error"),
  242. self.tr("Failed to load file"),
  243. self.host.get_last_error(), QMessageBox.Ok, QMessageBox.Ok)
  244. if tryItem is not None:
  245. self.host.replace_plugin(self.host.get_max_plugin_number())
  246. #tryItem.widget.setActive(True, True, True)
  247. # --------------------------------------------------------------------------------------------------------
  248. def mousePressEvent(self, event):
  249. if self.itemAt(event.pos()) is None and self.currentRow() != -1:
  250. event.accept()
  251. self.customClearSelection()
  252. return
  253. QListWidget.mousePressEvent(self, event)
  254. def changeEvent(self, event):
  255. if event.type() in (QEvent.StyleChange, QEvent.PaletteChange):
  256. self.updateStyle()
  257. QWidget.changeEvent(self, event)
  258. def paintEvent(self, event):
  259. painter = QPainter(self.viewport())
  260. width = self.width()
  261. height = self.height()
  262. imgL_rect = QRect(0, 0, self.fPixmapWidth, height)
  263. imgR_rect = QRect(width-self.fPixmapWidth, 0, self.fPixmapWidth, height)
  264. painter.setBrush(self.rail_color)
  265. painter.setPen(Qt.NoPen)
  266. painter.drawRects(imgL_rect, imgR_rect)
  267. painter.setCompositionMode(QPainter.CompositionMode_Multiply)
  268. painter.drawTiledPixmap(imgL_rect, self.fPixmapL)
  269. painter.drawTiledPixmap(imgR_rect, self.fPixmapR)
  270. painter.setCompositionMode(QPainter.CompositionMode_Plus)
  271. painter.drawTiledPixmap(imgL_rect, self.fPixmapL)
  272. painter.drawTiledPixmap(imgR_rect, self.fPixmapR)
  273. painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
  274. painter.setPen(self.edge_color)
  275. painter.setBrush(Qt.NoBrush)
  276. painter.drawRect(self.fPixmapWidth, 0, width-self.fPixmapWidth*2, height)
  277. QListWidget.paintEvent(self, event)
  278. # --------------------------------------------------------------------------------------------------------
  279. def updateStyle(self):
  280. palette = self.palette()
  281. bg_color = palette.window().color()
  282. base_color = palette.base().color()
  283. text_color = palette.text().color()
  284. r0,g0,b0,a = bg_color.getRgb()
  285. r1,g1,b1,a = text_color.getRgb()
  286. self.rail_color = QColor((r0*3+r1)/4, (g0*3+g1)/4, (b0*3+b1)/4)
  287. self.edge_color = self.rail_color.darker(115) if self.rail_color.blackF() > base_color.blackF() else base_color.darker(115)
  288. def selectionChanged(self, selected, deselected):
  289. for index in deselected.indexes():
  290. item = self.itemFromIndex(index)
  291. if item is not None:
  292. item.setSelected(False)
  293. for index in selected.indexes():
  294. item = self.itemFromIndex(index)
  295. if item is not None:
  296. item.setSelected(True)
  297. QListWidget.selectionChanged(self, selected, deselected)
  298. # ------------------------------------------------------------------------------------------------------------