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.

racklistwidget.py 14KB

9 years ago
9 years ago
9 years ago
Rack enhancements 2 (#770) Squashed commit of the following: commit 0449cd63f7c356684903f784be434191df9c4392 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 02:19:23 2018 +0500 carla skin: fancy item selection commit 84a7b527a7eb79de1da215d6350ba5c639a46477 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 02:10:49 2018 +0500 carla skin: drawOutline: fixed positioning and optimization commit 30681d1523d088a728ba702c3af42ef97da2ddd2 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 01:20:53 2018 +0500 carla skin: drawOutline: set brush only once commit 63d2ab2e2189efdfa1c5e57b1519706184aa14bc Author: Nikita Zlobin <nick87720z@gmail.com> Date: Thu Oct 4 00:10:39 2018 +0500 carla skin: more neat QColor to tuple conversion commit 2137b781093177891d43bcf84cbcca9f7a398ba6 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 3 16:35:48 2018 +0500 rack item: Skin-independent colorization support Color menu item tints background color, preserving all visible relief. No need to lock it for some skins. commit 13fa4f2c34f789c43fa54c6657bb3dd018fcac80 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 10 15:00:58 2018 +0500 rack list: Colorized padding commit 9964af199453ce45ae8a8c10828a79d11870e011 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Sep 28 17:48:17 2018 +0500 rack list: Colorized rails commit d212577057c5a914f52fd38a9d35645517e09d46 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 10 14:12:36 2018 +0500 rack list: Set frame in ui form commit 9a7e06098899ea5ac7e510b4374e3f88aeae8282 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Sep 28 18:44:49 2018 +0500 rack list: Set common frame
6 years ago
9 years ago
Rack enhancements 2 (#770) Squashed commit of the following: commit 0449cd63f7c356684903f784be434191df9c4392 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 02:19:23 2018 +0500 carla skin: fancy item selection commit 84a7b527a7eb79de1da215d6350ba5c639a46477 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 02:10:49 2018 +0500 carla skin: drawOutline: fixed positioning and optimization commit 30681d1523d088a728ba702c3af42ef97da2ddd2 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 01:20:53 2018 +0500 carla skin: drawOutline: set brush only once commit 63d2ab2e2189efdfa1c5e57b1519706184aa14bc Author: Nikita Zlobin <nick87720z@gmail.com> Date: Thu Oct 4 00:10:39 2018 +0500 carla skin: more neat QColor to tuple conversion commit 2137b781093177891d43bcf84cbcca9f7a398ba6 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 3 16:35:48 2018 +0500 rack item: Skin-independent colorization support Color menu item tints background color, preserving all visible relief. No need to lock it for some skins. commit 13fa4f2c34f789c43fa54c6657bb3dd018fcac80 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 10 15:00:58 2018 +0500 rack list: Colorized padding commit 9964af199453ce45ae8a8c10828a79d11870e011 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Sep 28 17:48:17 2018 +0500 rack list: Colorized rails commit d212577057c5a914f52fd38a9d35645517e09d46 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 10 14:12:36 2018 +0500 rack list: Set frame in ui form commit 9a7e06098899ea5ac7e510b4374e3f88aeae8282 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Sep 28 18:44:49 2018 +0500 rack list: Set common frame
6 years ago
Rack enhancements 2 (#770) Squashed commit of the following: commit 0449cd63f7c356684903f784be434191df9c4392 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 02:19:23 2018 +0500 carla skin: fancy item selection commit 84a7b527a7eb79de1da215d6350ba5c639a46477 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 02:10:49 2018 +0500 carla skin: drawOutline: fixed positioning and optimization commit 30681d1523d088a728ba702c3af42ef97da2ddd2 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 01:20:53 2018 +0500 carla skin: drawOutline: set brush only once commit 63d2ab2e2189efdfa1c5e57b1519706184aa14bc Author: Nikita Zlobin <nick87720z@gmail.com> Date: Thu Oct 4 00:10:39 2018 +0500 carla skin: more neat QColor to tuple conversion commit 2137b781093177891d43bcf84cbcca9f7a398ba6 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 3 16:35:48 2018 +0500 rack item: Skin-independent colorization support Color menu item tints background color, preserving all visible relief. No need to lock it for some skins. commit 13fa4f2c34f789c43fa54c6657bb3dd018fcac80 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 10 15:00:58 2018 +0500 rack list: Colorized padding commit 9964af199453ce45ae8a8c10828a79d11870e011 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Sep 28 17:48:17 2018 +0500 rack list: Colorized rails commit d212577057c5a914f52fd38a9d35645517e09d46 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 10 14:12:36 2018 +0500 rack list: Set frame in ui form commit 9a7e06098899ea5ac7e510b4374e3f88aeae8282 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Sep 28 18:44:49 2018 +0500 rack list: Set common frame
6 years ago
Rack enhancements 2 (#770) Squashed commit of the following: commit 0449cd63f7c356684903f784be434191df9c4392 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 02:19:23 2018 +0500 carla skin: fancy item selection commit 84a7b527a7eb79de1da215d6350ba5c639a46477 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 02:10:49 2018 +0500 carla skin: drawOutline: fixed positioning and optimization commit 30681d1523d088a728ba702c3af42ef97da2ddd2 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Oct 5 01:20:53 2018 +0500 carla skin: drawOutline: set brush only once commit 63d2ab2e2189efdfa1c5e57b1519706184aa14bc Author: Nikita Zlobin <nick87720z@gmail.com> Date: Thu Oct 4 00:10:39 2018 +0500 carla skin: more neat QColor to tuple conversion commit 2137b781093177891d43bcf84cbcca9f7a398ba6 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 3 16:35:48 2018 +0500 rack item: Skin-independent colorization support Color menu item tints background color, preserving all visible relief. No need to lock it for some skins. commit 13fa4f2c34f789c43fa54c6657bb3dd018fcac80 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 10 15:00:58 2018 +0500 rack list: Colorized padding commit 9964af199453ce45ae8a8c10828a79d11870e011 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Sep 28 17:48:17 2018 +0500 rack list: Colorized rails commit d212577057c5a914f52fd38a9d35645517e09d46 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Wed Oct 10 14:12:36 2018 +0500 rack list: Set frame in ui form commit 9a7e06098899ea5ac7e510b4374e3f88aeae8282 Author: Nikita Zlobin <nick87720z@gmail.com> Date: Fri Sep 28 18:44:49 2018 +0500 rack list: Set common frame
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. # ------------------------------------------------------------------------------------------------------------