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.

591 lines
21KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # PatchBay Canvas engine using QGraphicsView/Scene
  4. # Copyright (C) 2010-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 qCritical, Qt, QPointF, QRectF, QTimer
  20. from PyQt5.QtGui import QCursor, QFont, QFontMetrics, QLinearGradient, QPainter, QPen
  21. from PyQt5.QtWidgets import QGraphicsItem, QMenu
  22. # ------------------------------------------------------------------------------------------------------------
  23. # Imports (Custom)
  24. from . import (
  25. canvas,
  26. features,
  27. options,
  28. port_dict_t,
  29. CanvasBoxType,
  30. ANTIALIASING_FULL,
  31. ACTION_PLUGIN_EDIT,
  32. ACTION_PLUGIN_SHOW_UI,
  33. ACTION_PLUGIN_CLONE,
  34. ACTION_PLUGIN_REMOVE,
  35. ACTION_PLUGIN_RENAME,
  36. ACTION_PLUGIN_REPLACE,
  37. ACTION_GROUP_INFO,
  38. ACTION_GROUP_JOIN,
  39. ACTION_GROUP_SPLIT,
  40. ACTION_GROUP_RENAME,
  41. ACTION_PORTS_DISCONNECT,
  42. EYECANDY_FULL,
  43. PORT_MODE_NULL,
  44. PORT_MODE_INPUT,
  45. PORT_MODE_OUTPUT,
  46. PORT_TYPE_NULL,
  47. PORT_TYPE_AUDIO_JACK,
  48. PORT_TYPE_MIDI_ALSA,
  49. PORT_TYPE_MIDI_JACK,
  50. PORT_TYPE_PARAMETER,
  51. MAX_PLUGIN_ID_ALLOWED,
  52. )
  53. from .canvasboxshadow import CanvasBoxShadow
  54. from .canvasicon import CanvasIcon
  55. from .canvasport import CanvasPort
  56. from .theme import Theme
  57. from .utils import CanvasItemFX, CanvasGetFullPortName, CanvasGetPortConnectionList
  58. #from .canvasportglow import CanvasPortGlow
  59. # ------------------------------------------------------------------------------------------------------------
  60. class cb_line_t(object):
  61. def __init__(self, line, connection_id):
  62. self.line = line
  63. self.connection_id = connection_id
  64. # ------------------------------------------------------------------------------------------------------------
  65. class CanvasBox(QGraphicsItem):
  66. def __init__(self, group_id, group_name, icon, parent=None):
  67. QGraphicsItem.__init__(self, parent)
  68. # Save Variables, useful for later
  69. self.m_group_id = group_id
  70. self.m_group_name = group_name
  71. # plugin Id, < 0 if invalid
  72. self.m_plugin_id = -1
  73. self.m_plugin_ui = False
  74. # Base Variables
  75. self.p_width = 50
  76. self.p_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing + 1
  77. self.m_last_pos = QPointF()
  78. self.m_splitted = False
  79. self.m_splitted_mode = PORT_MODE_NULL
  80. self.m_cursor_moving = False
  81. self.m_forced_split = False
  82. self.m_mouse_down = False
  83. self.m_port_list_ids = []
  84. self.m_connection_lines = []
  85. # Set Font
  86. self.m_font_name = QFont()
  87. self.m_font_name.setFamily(canvas.theme.box_font_name)
  88. self.m_font_name.setPixelSize(canvas.theme.box_font_size)
  89. self.m_font_name.setWeight(canvas.theme.box_font_state)
  90. self.m_font_port = QFont()
  91. self.m_font_port.setFamily(canvas.theme.port_font_name)
  92. self.m_font_port.setPixelSize(canvas.theme.port_font_size)
  93. self.m_font_port.setWeight(canvas.theme.port_font_state)
  94. # Icon
  95. if canvas.theme.box_use_icon:
  96. self.icon_svg = CanvasIcon(icon, self.m_group_name, self)
  97. else:
  98. self.icon_svg = None
  99. # Shadow
  100. if options.eyecandy:
  101. self.shadow = CanvasBoxShadow(self.toGraphicsObject())
  102. self.shadow.setFakeParent(self)
  103. self.setGraphicsEffect(self.shadow)
  104. else:
  105. self.shadow = None
  106. # Final touches
  107. self.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
  108. # Wait for at least 1 port
  109. if options.auto_hide_groups:
  110. self.setVisible(False)
  111. self.setFlag(QGraphicsItem.ItemIsFocusable, True)
  112. if options.auto_select_items:
  113. self.setAcceptHoverEvents(True)
  114. self.updatePositions()
  115. canvas.scene.addItem(self)
  116. QTimer.singleShot(0, self.fixPos)
  117. def getGroupId(self):
  118. return self.m_group_id
  119. def getGroupName(self):
  120. return self.m_group_name
  121. def isSplitted(self):
  122. return self.m_splitted
  123. def getSplittedMode(self):
  124. return self.m_splitted_mode
  125. def getPortCount(self):
  126. return len(self.m_port_list_ids)
  127. def getPortList(self):
  128. return self.m_port_list_ids
  129. def setAsPlugin(self, plugin_id, hasUi):
  130. self.m_plugin_id = plugin_id
  131. self.m_plugin_ui = hasUi
  132. def setIcon(self, icon):
  133. if self.icon_svg is not None:
  134. self.icon_svg.setIcon(icon, self.m_group_name)
  135. def setSplit(self, split, mode=PORT_MODE_NULL):
  136. self.m_splitted = split
  137. self.m_splitted_mode = mode
  138. def setGroupName(self, group_name):
  139. self.m_group_name = group_name
  140. self.updatePositions()
  141. def setShadowOpacity(self, opacity):
  142. if self.shadow:
  143. self.shadow.setOpacity(opacity)
  144. def addPortFromGroup(self, port_id, port_mode, port_type, port_name, is_alternate):
  145. if len(self.m_port_list_ids) == 0:
  146. if options.auto_hide_groups:
  147. if options.eyecandy == EYECANDY_FULL:
  148. CanvasItemFX(self, True, False)
  149. self.setVisible(True)
  150. new_widget = CanvasPort(self.m_group_id, port_id, port_name, port_mode, port_type, is_alternate, self)
  151. port_dict = port_dict_t()
  152. port_dict.group_id = self.m_group_id
  153. port_dict.port_id = port_id
  154. port_dict.port_name = port_name
  155. port_dict.port_mode = port_mode
  156. port_dict.port_type = port_type
  157. port_dict.is_alternate = is_alternate
  158. port_dict.widget = new_widget
  159. self.m_port_list_ids.append(port_id)
  160. return new_widget
  161. def removePortFromGroup(self, port_id):
  162. if port_id in self.m_port_list_ids:
  163. self.m_port_list_ids.remove(port_id)
  164. else:
  165. qCritical("PatchCanvas::CanvasBox.removePort(%i) - unable to find port to remove" % port_id)
  166. return
  167. if len(self.m_port_list_ids) > 0:
  168. self.updatePositions()
  169. elif self.isVisible():
  170. if options.auto_hide_groups:
  171. if options.eyecandy == EYECANDY_FULL:
  172. CanvasItemFX(self, False, False)
  173. else:
  174. self.setVisible(False)
  175. def addLineFromGroup(self, line, connection_id):
  176. new_cbline = cb_line_t(line, connection_id)
  177. self.m_connection_lines.append(new_cbline)
  178. def removeLineFromGroup(self, connection_id):
  179. for connection in self.m_connection_lines:
  180. if connection.connection_id == connection_id:
  181. self.m_connection_lines.remove(connection)
  182. return
  183. qCritical("PatchCanvas::CanvasBox.removeLineFromGroup(%i) - unable to find line to remove" % connection_id)
  184. def checkItemPos(self):
  185. if not canvas.size_rect.isNull():
  186. pos = self.scenePos()
  187. if not (canvas.size_rect.contains(pos) and
  188. canvas.size_rect.contains(pos + QPointF(self.p_width, self.p_height))):
  189. if pos.x() < canvas.size_rect.x():
  190. self.setPos(canvas.size_rect.x(), pos.y())
  191. elif pos.x() + self.p_width > canvas.size_rect.width():
  192. self.setPos(canvas.size_rect.width() - self.p_width, pos.y())
  193. pos = self.scenePos()
  194. if pos.y() < canvas.size_rect.y():
  195. self.setPos(pos.x(), canvas.size_rect.y())
  196. elif pos.y() + self.p_height > canvas.size_rect.height():
  197. self.setPos(pos.x(), canvas.size_rect.height() - self.p_height)
  198. def removeIconFromScene(self):
  199. if self.icon_svg is None:
  200. return
  201. item = self.icon_svg
  202. self.icon_svg = None
  203. canvas.scene.removeItem(item)
  204. del item
  205. def updatePositions(self):
  206. self.prepareGeometryChange()
  207. # Check Text Name size
  208. app_name_size = QFontMetrics(self.m_font_name).width(self.m_group_name) + 30
  209. self.p_width = max(50, app_name_size)
  210. # Get Port List
  211. port_list = []
  212. for port in canvas.port_list:
  213. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  214. port_list.append(port)
  215. if len(port_list) == 0:
  216. self.p_height = canvas.theme.box_header_height
  217. else:
  218. max_in_width = max_out_width = 0
  219. port_spacing = canvas.theme.port_height + canvas.theme.port_spacing
  220. # Get Max Box Width, vertical ports re-positioning
  221. port_types = [PORT_TYPE_AUDIO_JACK, PORT_TYPE_MIDI_JACK, PORT_TYPE_MIDI_ALSA, PORT_TYPE_PARAMETER]
  222. last_in_type = last_out_type = PORT_TYPE_NULL
  223. last_in_pos = last_out_pos = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  224. for port_type in port_types:
  225. for port in port_list:
  226. if port.port_type != port_type:
  227. continue
  228. size = QFontMetrics(self.m_font_port).width(port.port_name)
  229. if port.port_mode == PORT_MODE_INPUT:
  230. max_in_width = max(max_in_width, size)
  231. if port.port_type != last_in_type:
  232. if last_in_type != PORT_TYPE_NULL:
  233. last_in_pos += canvas.theme.port_spacingT
  234. last_in_type = port.port_type
  235. port.widget.setY(last_in_pos)
  236. last_in_pos += port_spacing
  237. elif port.port_mode == PORT_MODE_OUTPUT:
  238. max_out_width = max(max_out_width, size)
  239. if port.port_type != last_out_type:
  240. if last_out_type != PORT_TYPE_NULL:
  241. last_out_pos += canvas.theme.port_spacingT
  242. last_out_type = port.port_type
  243. port.widget.setY(last_out_pos)
  244. last_out_pos += port_spacing
  245. self.p_width = max(self.p_width, 30 + max_in_width + max_out_width)
  246. # Horizontal ports re-positioning
  247. inX = canvas.theme.port_offset
  248. outX = self.p_width - max_out_width - canvas.theme.port_offset - 12
  249. for port_type in port_types:
  250. for port in port_list:
  251. if port.port_mode == PORT_MODE_INPUT:
  252. port.widget.setX(inX)
  253. port.widget.setPortWidth(max_in_width)
  254. elif port.port_mode == PORT_MODE_OUTPUT:
  255. port.widget.setX(outX)
  256. port.widget.setPortWidth(max_out_width)
  257. self.p_height = max(last_in_pos, last_out_pos)
  258. self.p_height += max(canvas.theme.port_spacing, canvas.theme.port_spacingT) - canvas.theme.port_spacing
  259. self.p_height += canvas.theme.box_pen.widthF()
  260. self.repaintLines(True)
  261. self.update()
  262. def repaintLines(self, forced=False):
  263. if self.pos() != self.m_last_pos or forced:
  264. for connection in self.m_connection_lines:
  265. connection.line.updateLinePos()
  266. self.m_last_pos = self.pos()
  267. def resetLinesZValue(self):
  268. for connection in canvas.connection_list:
  269. if connection.port_out_id in self.m_port_list_ids and connection.port_in_id in self.m_port_list_ids:
  270. z_value = canvas.last_z_value
  271. else:
  272. z_value = canvas.last_z_value - 1
  273. connection.widget.setZValue(z_value)
  274. def type(self):
  275. return CanvasBoxType
  276. def contextMenuEvent(self, event):
  277. event.accept()
  278. menu = QMenu()
  279. discMenu = QMenu("Disconnect", menu)
  280. conn_list = []
  281. conn_list_ids = []
  282. for port_id in self.m_port_list_ids:
  283. tmp_conn_list = CanvasGetPortConnectionList(self.m_group_id, port_id)
  284. for tmp_conn_id, tmp_group_id, tmp_port_id in tmp_conn_list:
  285. if tmp_conn_id not in conn_list_ids:
  286. conn_list.append((tmp_conn_id, tmp_group_id, tmp_port_id))
  287. conn_list_ids.append(tmp_conn_id)
  288. if len(conn_list) > 0:
  289. for conn_id, group_id, port_id in conn_list:
  290. act_x_disc = discMenu.addAction(CanvasGetFullPortName(group_id, port_id))
  291. act_x_disc.setData(conn_id)
  292. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  293. else:
  294. act_x_disc = discMenu.addAction("No connections")
  295. act_x_disc.setEnabled(False)
  296. menu.addMenu(discMenu)
  297. act_x_disc_all = menu.addAction("Disconnect &All")
  298. act_x_sep1 = menu.addSeparator()
  299. act_x_info = menu.addAction("Info")
  300. act_x_rename = menu.addAction("Rename")
  301. act_x_sep2 = menu.addSeparator()
  302. act_x_split_join = menu.addAction("Join" if self.m_splitted else "Split")
  303. if not features.group_info:
  304. act_x_info.setVisible(False)
  305. if not features.group_rename:
  306. act_x_rename.setVisible(False)
  307. if not (features.group_info and features.group_rename):
  308. act_x_sep1.setVisible(False)
  309. if self.m_plugin_id >= 0 and self.m_plugin_id <= MAX_PLUGIN_ID_ALLOWED:
  310. menu.addSeparator()
  311. act_p_edit = menu.addAction("Edit")
  312. act_p_ui = menu.addAction("Show Custom UI")
  313. menu.addSeparator()
  314. act_p_clone = menu.addAction("Clone")
  315. act_p_rename = menu.addAction("Rename...")
  316. act_p_replace = menu.addAction("Replace...")
  317. act_p_remove = menu.addAction("Remove")
  318. if not self.m_plugin_ui:
  319. act_p_ui.setVisible(False)
  320. else:
  321. act_p_edit = act_p_ui = None
  322. act_p_clone = act_p_rename = None
  323. act_p_replace = act_p_remove = None
  324. haveIns = haveOuts = False
  325. for port in canvas.port_list:
  326. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  327. if port.port_mode == PORT_MODE_INPUT:
  328. haveIns = True
  329. elif port.port_mode == PORT_MODE_OUTPUT:
  330. haveOuts = True
  331. if not (self.m_splitted or bool(haveIns and haveOuts)):
  332. act_x_sep2.setVisible(False)
  333. act_x_split_join.setVisible(False)
  334. act_selected = menu.exec_(event.screenPos())
  335. if act_selected is None:
  336. pass
  337. elif act_selected == act_x_disc_all:
  338. for conn_id in conn_list_ids:
  339. canvas.callback(ACTION_PORTS_DISCONNECT, conn_id, 0, "")
  340. elif act_selected == act_x_info:
  341. canvas.callback(ACTION_GROUP_INFO, self.m_group_id, 0, "")
  342. elif act_selected == act_x_rename:
  343. canvas.callback(ACTION_GROUP_RENAME, self.m_group_id, 0, "")
  344. elif act_selected == act_x_split_join:
  345. if self.m_splitted:
  346. canvas.callback(ACTION_GROUP_JOIN, self.m_group_id, 0, "")
  347. else:
  348. canvas.callback(ACTION_GROUP_SPLIT, self.m_group_id, 0, "")
  349. elif act_selected == act_p_edit:
  350. canvas.callback(ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  351. elif act_selected == act_p_ui:
  352. canvas.callback(ACTION_PLUGIN_SHOW_UI, self.m_plugin_id, 0, "")
  353. elif act_selected == act_p_clone:
  354. canvas.callback(ACTION_PLUGIN_CLONE, self.m_plugin_id, 0, "")
  355. elif act_selected == act_p_rename:
  356. canvas.callback(ACTION_PLUGIN_RENAME, self.m_plugin_id, 0, "")
  357. elif act_selected == act_p_replace:
  358. canvas.callback(ACTION_PLUGIN_REPLACE, self.m_plugin_id, 0, "")
  359. elif act_selected == act_p_remove:
  360. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  361. def keyPressEvent(self, event):
  362. if self.m_plugin_id >= 0 and event.key() == Qt.Key_Delete:
  363. event.accept()
  364. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  365. return
  366. QGraphicsItem.keyPressEvent(self, event)
  367. def hoverEnterEvent(self, event):
  368. if options.auto_select_items:
  369. if len(canvas.scene.selectedItems()) > 0:
  370. canvas.scene.clearSelection()
  371. self.setSelected(True)
  372. QGraphicsItem.hoverEnterEvent(self, event)
  373. def mouseDoubleClickEvent(self, event):
  374. if self.m_plugin_id >= 0:
  375. event.accept()
  376. canvas.callback(ACTION_PLUGIN_SHOW_UI if self.m_plugin_ui else ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  377. return
  378. QGraphicsItem.mouseDoubleClickEvent(self, event)
  379. def mousePressEvent(self, event):
  380. canvas.last_z_value += 1
  381. self.setZValue(canvas.last_z_value)
  382. self.resetLinesZValue()
  383. self.m_cursor_moving = False
  384. if event.button() == Qt.RightButton:
  385. event.accept()
  386. canvas.scene.clearSelection()
  387. self.setSelected(True)
  388. self.m_mouse_down = False
  389. return
  390. elif event.button() == Qt.LeftButton:
  391. if self.sceneBoundingRect().contains(event.scenePos()):
  392. self.m_mouse_down = True
  393. else:
  394. # FIXME: Check if still valid: Fix a weird Qt behaviour with right-click mouseMove
  395. self.m_mouse_down = False
  396. event.ignore()
  397. return
  398. else:
  399. self.m_mouse_down = False
  400. QGraphicsItem.mousePressEvent(self, event)
  401. def mouseMoveEvent(self, event):
  402. if self.m_mouse_down:
  403. if not self.m_cursor_moving:
  404. self.setCursor(QCursor(Qt.SizeAllCursor))
  405. self.m_cursor_moving = True
  406. self.repaintLines()
  407. QGraphicsItem.mouseMoveEvent(self, event)
  408. def mouseReleaseEvent(self, event):
  409. if self.m_cursor_moving:
  410. self.unsetCursor()
  411. self.fixPos()
  412. self.m_mouse_down = False
  413. self.m_cursor_moving = False
  414. QGraphicsItem.mouseReleaseEvent(self, event)
  415. def moveEvent(self, event):
  416. if not self.m_mouse_down:
  417. self.fixPos()
  418. def fixPos(self):
  419. self.setX(round(self.x()))
  420. self.setY(round(self.y()))
  421. def boundingRect(self):
  422. return QRectF(0, 0, self.p_width, self.p_height)
  423. def paint(self, painter, option, widget):
  424. painter.save()
  425. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  426. rect = QRectF(0, 0, self.p_width, self.p_height)
  427. # Draw rectangle
  428. pen = QPen(canvas.theme.box_pen_sel if self.isSelected() else canvas.theme.box_pen)
  429. pen.setWidthF(pen.widthF() + 0.00001)
  430. painter.setPen(pen)
  431. lineHinting = pen.widthF() / 2
  432. if canvas.theme.box_bg_type == Theme.THEME_BG_GRADIENT:
  433. box_gradient = QLinearGradient(0, 0, 0, self.p_height)
  434. box_gradient.setColorAt(0, canvas.theme.box_bg_1)
  435. box_gradient.setColorAt(1, canvas.theme.box_bg_2)
  436. painter.setBrush(box_gradient)
  437. else:
  438. painter.setBrush(canvas.theme.box_bg_1)
  439. rect.adjust(lineHinting, lineHinting, -lineHinting, -lineHinting)
  440. painter.drawRect(rect)
  441. # Draw pixmap header
  442. rect.setHeight(canvas.theme.box_header_height)
  443. if canvas.theme.box_header_pixmap:
  444. painter.setPen(Qt.NoPen)
  445. painter.setBrush(canvas.theme.box_bg_2)
  446. # outline
  447. rect.adjust(lineHinting, lineHinting, -lineHinting, -lineHinting)
  448. painter.drawRect(rect)
  449. rect.adjust(1, 1, -1, 0)
  450. painter.drawTiledPixmap(rect, canvas.theme.box_header_pixmap, rect.topLeft())
  451. # Draw text
  452. painter.setFont(self.m_font_name)
  453. if self.isSelected():
  454. painter.setPen(canvas.theme.box_text_sel)
  455. else:
  456. painter.setPen(canvas.theme.box_text)
  457. if canvas.theme.box_use_icon:
  458. textPos = QPointF(25, canvas.theme.box_text_ypos)
  459. else:
  460. appNameSize = QFontMetrics(self.m_font_name).width(self.m_group_name)
  461. rem = self.p_width - appNameSize
  462. textPos = QPointF(rem/2, canvas.theme.box_text_ypos)
  463. painter.drawText(textPos, self.m_group_name)
  464. self.repaintLines()
  465. painter.restore()
  466. # ------------------------------------------------------------------------------------------------------------