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.

468 lines
18KB

  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 math import floor
  20. from PyQt5.QtCore import qCritical, Qt, QLineF, QPointF, QRectF, QTimer
  21. from PyQt5.QtGui import QCursor, QFont, QFontMetrics, QPainter, QPainterPath, QPen, QPolygonF
  22. from PyQt5.QtWidgets import QGraphicsItem, QMenu
  23. # ------------------------------------------------------------------------------------------------------------
  24. # Imports (Custom)
  25. from . import (
  26. canvas,
  27. features,
  28. options,
  29. port_mode2str,
  30. port_type2str,
  31. CanvasPortType,
  32. ANTIALIASING_FULL,
  33. ACTION_PORT_INFO,
  34. ACTION_PORT_RENAME,
  35. ACTION_PORTS_CONNECT,
  36. ACTION_PORTS_DISCONNECT,
  37. PORT_MODE_INPUT,
  38. PORT_MODE_OUTPUT,
  39. PORT_TYPE_AUDIO_JACK,
  40. PORT_TYPE_MIDI_ALSA,
  41. PORT_TYPE_MIDI_JACK,
  42. PORT_TYPE_PARAMETER,
  43. )
  44. from .canvasbezierlinemov import CanvasBezierLineMov
  45. from .canvaslinemov import CanvasLineMov
  46. from .theme import Theme
  47. from .utils import CanvasGetFullPortName, CanvasGetPortConnectionList
  48. # ------------------------------------------------------------------------------------------------------------
  49. class CanvasPort(QGraphicsItem):
  50. def __init__(self, group_id, port_id, port_name, port_mode, port_type, is_alternate, parent):
  51. QGraphicsItem.__init__(self, parent)
  52. # Save Variables, useful for later
  53. self.m_group_id = group_id
  54. self.m_port_id = port_id
  55. self.m_port_mode = port_mode
  56. self.m_port_type = port_type
  57. self.m_port_name = port_name
  58. self.m_is_alternate = is_alternate
  59. # Base Variables
  60. self.m_port_width = 15
  61. self.m_port_height = canvas.theme.port_height
  62. self.m_port_font = QFont()
  63. self.m_port_font.setFamily(canvas.theme.port_font_name)
  64. self.m_port_font.setPixelSize(canvas.theme.port_font_size)
  65. self.m_port_font.setWeight(canvas.theme.port_font_state)
  66. self.m_line_mov = None
  67. self.m_hover_item = None
  68. self.m_mouse_down = False
  69. self.m_cursor_moving = False
  70. self.setFlags(QGraphicsItem.ItemIsSelectable)
  71. if options.auto_select_items:
  72. self.setAcceptHoverEvents(True)
  73. def getGroupId(self):
  74. return self.m_group_id
  75. def getPortId(self):
  76. return self.m_port_id
  77. def getPortMode(self):
  78. return self.m_port_mode
  79. def getPortType(self):
  80. return self.m_port_type
  81. def getPortName(self):
  82. return self.m_port_name
  83. def getFullPortName(self):
  84. return self.parentItem().getGroupName() + ":" + self.m_port_name
  85. def getPortWidth(self):
  86. return self.m_port_width
  87. def getPortHeight(self):
  88. return self.m_port_height
  89. def setPortMode(self, port_mode):
  90. self.m_port_mode = port_mode
  91. self.update()
  92. def setPortType(self, port_type):
  93. self.m_port_type = port_type
  94. self.update()
  95. def setPortName(self, port_name):
  96. if QFontMetrics(self.m_port_font).width(port_name) < QFontMetrics(self.m_port_font).width(self.m_port_name):
  97. QTimer.singleShot(0, canvas.scene.update)
  98. self.m_port_name = port_name
  99. self.update()
  100. def setPortWidth(self, port_width):
  101. if port_width < self.m_port_width:
  102. QTimer.singleShot(0, canvas.scene.update)
  103. self.m_port_width = port_width
  104. self.update()
  105. def type(self):
  106. return CanvasPortType
  107. def hoverEnterEvent(self, event):
  108. if options.auto_select_items:
  109. self.setSelected(True)
  110. QGraphicsItem.hoverEnterEvent(self, event)
  111. def hoverLeaveEvent(self, event):
  112. if options.auto_select_items:
  113. self.setSelected(False)
  114. QGraphicsItem.hoverLeaveEvent(self, event)
  115. def mousePressEvent(self, event):
  116. self.m_hover_item = None
  117. self.m_mouse_down = bool(event.button() == Qt.LeftButton)
  118. self.m_cursor_moving = False
  119. QGraphicsItem.mousePressEvent(self, event)
  120. def mouseMoveEvent(self, event):
  121. if not self.m_mouse_down:
  122. QGraphicsItem.mouseMoveEvent(self, event)
  123. return
  124. event.accept()
  125. if not self.m_cursor_moving:
  126. self.setCursor(QCursor(Qt.CrossCursor))
  127. self.m_cursor_moving = True
  128. for connection in canvas.connection_list:
  129. if (
  130. (connection.group_out_id == self.m_group_id and
  131. connection.port_out_id == self.m_port_id)
  132. or
  133. (connection.group_in_id == self.m_group_id and
  134. connection.port_in_id == self.m_port_id)
  135. ):
  136. connection.widget.setLocked(True)
  137. if not self.m_line_mov:
  138. if options.use_bezier_lines:
  139. self.m_line_mov = CanvasBezierLineMov(self.m_port_mode, self.m_port_type, self)
  140. else:
  141. self.m_line_mov = CanvasLineMov(self.m_port_mode, self.m_port_type, self)
  142. canvas.last_z_value += 1
  143. self.m_line_mov.setZValue(canvas.last_z_value)
  144. canvas.last_z_value += 1
  145. self.parentItem().setZValue(canvas.last_z_value)
  146. item = None
  147. items = canvas.scene.items(event.scenePos(), Qt.ContainsItemShape, Qt.AscendingOrder)
  148. #for i in range(len(items)):
  149. for _, itemx in enumerate(items):
  150. if itemx.type() != CanvasPortType:
  151. continue
  152. if itemx == self:
  153. continue
  154. if item is None or itemx.parentItem().zValue() > item.parentItem().zValue():
  155. item = itemx
  156. if self.m_hover_item and self.m_hover_item != item:
  157. self.m_hover_item.setSelected(False)
  158. if item is not None:
  159. if item.getPortMode() != self.m_port_mode and item.getPortType() == self.m_port_type:
  160. item.setSelected(True)
  161. self.m_hover_item = item
  162. else:
  163. self.m_hover_item = None
  164. else:
  165. self.m_hover_item = None
  166. self.m_line_mov.updateLinePos(event.scenePos())
  167. def mouseReleaseEvent(self, event):
  168. if self.m_mouse_down:
  169. if self.m_line_mov is not None:
  170. item = self.m_line_mov
  171. self.m_line_mov = None
  172. canvas.scene.removeItem(item)
  173. del item
  174. for connection in canvas.connection_list:
  175. if (
  176. (connection.group_out_id == self.m_group_id and
  177. connection.port_out_id == self.m_port_id)
  178. or
  179. (connection.group_in_id == self.m_group_id and
  180. connection.port_in_id == self.m_port_id)
  181. ):
  182. connection.widget.setLocked(False)
  183. if self.m_hover_item:
  184. # TODO: a better way to check already existing connection
  185. for connection in canvas.connection_list:
  186. hover_group_id = self.m_hover_item.getGroupId()
  187. hover_port_id = self.m_hover_item.getPortId()
  188. # FIXME clean ths big if stuff
  189. if (
  190. (connection.group_out_id == self.m_group_id and
  191. connection.port_out_id == self.m_port_id and
  192. connection.group_in_id == hover_group_id and
  193. connection.port_in_id == hover_port_id)
  194. or
  195. (connection.group_out_id == hover_group_id and
  196. connection.port_out_id == hover_port_id and
  197. connection.group_in_id == self.m_group_id and
  198. connection.port_in_id == self.m_port_id)
  199. ):
  200. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  201. break
  202. else:
  203. if self.m_port_mode == PORT_MODE_OUTPUT:
  204. conn = "%i:%i:%i:%i" % (self.m_group_id, self.m_port_id,
  205. self.m_hover_item.getGroupId(), self.m_hover_item.getPortId())
  206. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  207. else:
  208. conn = "%i:%i:%i:%i" % (self.m_hover_item.getGroupId(),
  209. self.m_hover_item.getPortId(), self.m_group_id, self.m_port_id)
  210. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  211. canvas.scene.clearSelection()
  212. if self.m_cursor_moving:
  213. self.unsetCursor()
  214. self.m_hover_item = None
  215. self.m_mouse_down = False
  216. self.m_cursor_moving = False
  217. QGraphicsItem.mouseReleaseEvent(self, event)
  218. def contextMenuEvent(self, event):
  219. event.accept()
  220. canvas.scene.clearSelection()
  221. self.setSelected(True)
  222. menu = QMenu()
  223. discMenu = QMenu("Disconnect", menu)
  224. conn_list = CanvasGetPortConnectionList(self.m_group_id, self.m_port_id)
  225. if len(conn_list) > 0:
  226. for conn_id, group_id, port_id in conn_list:
  227. act_x_disc = discMenu.addAction(CanvasGetFullPortName(group_id, port_id))
  228. act_x_disc.setData(conn_id)
  229. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  230. else:
  231. act_x_disc = discMenu.addAction("No connections")
  232. act_x_disc.setEnabled(False)
  233. menu.addMenu(discMenu)
  234. act_x_disc_all = menu.addAction("Disconnect &All")
  235. act_x_sep_1 = menu.addSeparator()
  236. act_x_info = menu.addAction("Get &Info")
  237. act_x_rename = menu.addAction("&Rename")
  238. if not features.port_info:
  239. act_x_info.setVisible(False)
  240. if not features.port_rename:
  241. act_x_rename.setVisible(False)
  242. if not (features.port_info and features.port_rename):
  243. act_x_sep_1.setVisible(False)
  244. act_selected = menu.exec_(event.screenPos())
  245. if act_selected == act_x_disc_all:
  246. self.triggerDisconnect(conn_list)
  247. elif act_selected == act_x_info:
  248. canvas.callback(ACTION_PORT_INFO, self.m_group_id, self.m_port_id, "")
  249. elif act_selected == act_x_rename:
  250. canvas.callback(ACTION_PORT_RENAME, self.m_group_id, self.m_port_id, "")
  251. def setPortSelected(self, yesno):
  252. for connection in canvas.connection_list:
  253. if (
  254. (connection.group_out_id == self.m_group_id and
  255. connection.port_out_id == self.m_port_id)
  256. or
  257. (connection.group_in_id == self.m_group_id and
  258. connection.port_in_id == self.m_port_id)
  259. ):
  260. connection.widget.updateLineSelected()
  261. def itemChange(self, change, value):
  262. if change == QGraphicsItem.ItemSelectedHasChanged:
  263. self.setPortSelected(value)
  264. return QGraphicsItem.itemChange(self, change, value)
  265. def triggerDisconnect(self, conn_list=None):
  266. if not conn_list:
  267. conn_list = CanvasGetPortConnectionList(self.m_group_id, self.m_port_id)
  268. for conn_id, in conn_list:
  269. canvas.callback(ACTION_PORTS_DISCONNECT, conn_id, 0, "")
  270. def boundingRect(self):
  271. return QRectF(0, 0, self.m_port_width + 12, self.m_port_height)
  272. def paint(self, painter, option, widget):
  273. painter.save()
  274. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  275. selected = self.isSelected()
  276. theme = canvas.theme
  277. if self.m_port_type == PORT_TYPE_AUDIO_JACK:
  278. poly_color = theme.port_audio_jack_bg_sel if selected else theme.port_audio_jack_bg
  279. poly_pen = theme.port_audio_jack_pen_sel if selected else theme.port_audio_jack_pen
  280. text_pen = theme.port_audio_jack_text_sel if selected else theme.port_audio_jack_text
  281. conn_pen = QPen(theme.port_audio_jack_pen_sel)
  282. elif self.m_port_type == PORT_TYPE_MIDI_JACK:
  283. poly_color = theme.port_midi_jack_bg_sel if selected else theme.port_midi_jack_bg
  284. poly_pen = theme.port_midi_jack_pen_sel if selected else theme.port_midi_jack_pen
  285. text_pen = theme.port_midi_jack_text_sel if selected else theme.port_midi_jack_text
  286. conn_pen = QPen(theme.port_midi_jack_pen_sel)
  287. elif self.m_port_type == PORT_TYPE_MIDI_ALSA:
  288. poly_color = theme.port_midi_alsa_bg_sel if selected else theme.port_midi_alsa_bg
  289. poly_pen = theme.port_midi_alsa_pen_sel if selected else theme.port_midi_alsa_pen
  290. text_pen = theme.port_midi_alsa_text_sel if selected else theme.port_midi_alsa_text
  291. conn_pen = QPen(theme.port_midi_alsa_pen_sel)
  292. elif self.m_port_type == PORT_TYPE_PARAMETER:
  293. poly_color = theme.port_parameter_bg_sel if selected else theme.port_parameter_bg
  294. poly_pen = theme.port_parameter_pen_sel if selected else theme.port_parameter_pen
  295. text_pen = theme.port_parameter_text_sel if selected else theme.port_parameter_text
  296. conn_pen = QPen(theme.port_parameter_pen_sel)
  297. else:
  298. qCritical("PatchCanvas::CanvasPort.paint() - invalid port type '%s'" % port_type2str(self.m_port_type))
  299. return
  300. # To prevent quality worsening
  301. poly_pen = QPen(poly_pen)
  302. poly_pen.setWidthF(poly_pen.widthF() + 0.00001)
  303. if self.m_is_alternate:
  304. poly_color = poly_color.darker(180)
  305. #poly_pen.setColor(poly_pen.color().darker(110))
  306. #text_pen.setColor(text_pen.color()) #.darker(150))
  307. #conn_pen.setColor(conn_pen.color()) #.darker(150))
  308. lineHinting = poly_pen.widthF() / 2
  309. poly_locx = [0, 0, 0, 0, 0]
  310. poly_corner_xhinting = (float(canvas.theme.port_height)/2) % floor(float(canvas.theme.port_height)/2)
  311. if poly_corner_xhinting == 0:
  312. poly_corner_xhinting = 0.5 * (1 - 7 / (float(canvas.theme.port_height)/2))
  313. if self.m_port_mode == PORT_MODE_INPUT:
  314. text_pos = QPointF(3, canvas.theme.port_text_ypos)
  315. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  316. poly_locx[0] = lineHinting
  317. poly_locx[1] = self.m_port_width + 5 - lineHinting
  318. poly_locx[2] = self.m_port_width + 12 - poly_corner_xhinting
  319. poly_locx[3] = self.m_port_width + 5 - lineHinting
  320. poly_locx[4] = lineHinting
  321. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  322. poly_locx[0] = lineHinting
  323. poly_locx[1] = self.m_port_width + 5 - lineHinting
  324. poly_locx[2] = self.m_port_width + 5 - lineHinting
  325. poly_locx[3] = self.m_port_width + 5 - lineHinting
  326. poly_locx[4] = lineHinting
  327. else:
  328. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  329. return
  330. elif self.m_port_mode == PORT_MODE_OUTPUT:
  331. text_pos = QPointF(9, canvas.theme.port_text_ypos)
  332. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  333. poly_locx[0] = self.m_port_width + 12 - lineHinting
  334. poly_locx[1] = 7 + lineHinting
  335. poly_locx[2] = 0 + poly_corner_xhinting
  336. poly_locx[3] = 7 + lineHinting
  337. poly_locx[4] = self.m_port_width + 12 - lineHinting
  338. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  339. poly_locx[0] = self.m_port_width + 12 - lineHinting
  340. poly_locx[1] = 5 + lineHinting
  341. poly_locx[2] = 5 + lineHinting
  342. poly_locx[3] = 5 + lineHinting
  343. poly_locx[4] = self.m_port_width + 12 - lineHinting
  344. else:
  345. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  346. return
  347. else:
  348. qCritical("PatchCanvas::CanvasPort.paint() - invalid port mode '%s'" % port_mode2str(self.m_port_mode))
  349. return
  350. polygon = QPolygonF()
  351. polygon += QPointF(poly_locx[0], lineHinting)
  352. polygon += QPointF(poly_locx[1], lineHinting)
  353. polygon += QPointF(poly_locx[2], float(canvas.theme.port_height)/2)
  354. polygon += QPointF(poly_locx[3], canvas.theme.port_height - lineHinting)
  355. polygon += QPointF(poly_locx[4], canvas.theme.port_height - lineHinting)
  356. polygon += QPointF(poly_locx[0], lineHinting)
  357. if canvas.theme.port_bg_pixmap:
  358. portRect = polygon.boundingRect().adjusted(-lineHinting+1, -lineHinting+1, lineHinting-1, lineHinting-1)
  359. portPos = portRect.topLeft()
  360. painter.drawTiledPixmap(portRect, canvas.theme.port_bg_pixmap, portPos)
  361. else:
  362. painter.setBrush(poly_color) #.lighter(200))
  363. painter.setPen(poly_pen)
  364. painter.drawPolygon(polygon)
  365. painter.setPen(text_pen)
  366. painter.setFont(self.m_port_font)
  367. painter.drawText(text_pos, self.m_port_name)
  368. if canvas.theme.idx == Theme.THEME_OOSTUDIO and canvas.theme.port_bg_pixmap:
  369. conn_pen.setCosmetic(True)
  370. conn_pen.setWidthF(0.4)
  371. painter.setPen(conn_pen)
  372. if self.m_port_mode == PORT_MODE_INPUT:
  373. connLineX = portRect.left()+1
  374. else:
  375. connLineX = portRect.right()-1
  376. conn_path = QPainterPath()
  377. conn_path.addRect(QRectF(connLineX-1, portRect.top(), 2, portRect.height()))
  378. painter.fillPath(conn_path, conn_pen.brush())
  379. painter.drawLine(QLineF(connLineX, portRect.top(), connLineX, portRect.bottom()))
  380. painter.restore()
  381. # ------------------------------------------------------------------------------------------------------------