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.

canvasport.py 19KB

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