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 12KB

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