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.

787 lines
28KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # PatchBay Canvas engine using QGraphicsView/Scene
  4. # Copyright (C) 2010-2021 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 pyqtSignal, pyqtSlot, qCritical, QT_VERSION, Qt, QPointF, QRectF, QTimer
  20. from PyQt5.QtGui import QCursor, QFont, QFontMetrics, QImage, QLinearGradient, QPainter, QPen
  21. from PyQt5.QtWidgets import QGraphicsItem, QGraphicsObject, QMenu
  22. # ------------------------------------------------------------------------------------------------------------
  23. # Backwards-compatible horizontalAdvance/width call, depending on Qt version
  24. def fontHorizontalAdvance(font, string):
  25. if QT_VERSION >= 0x50b00:
  26. return QFontMetrics(font).horizontalAdvance(string)
  27. return QFontMetrics(font).width(string)
  28. # ------------------------------------------------------------------------------------------------------------
  29. # Imports (Custom)
  30. from . import (
  31. canvas,
  32. features,
  33. options,
  34. port_dict_t,
  35. CanvasBoxType,
  36. ANTIALIASING_FULL,
  37. ACTION_PLUGIN_EDIT,
  38. ACTION_PLUGIN_SHOW_UI,
  39. ACTION_PLUGIN_CLONE,
  40. ACTION_PLUGIN_REMOVE,
  41. ACTION_PLUGIN_RENAME,
  42. ACTION_PLUGIN_REPLACE,
  43. ACTION_GROUP_INFO,
  44. ACTION_GROUP_JOIN,
  45. ACTION_GROUP_SPLIT,
  46. ACTION_GROUP_RENAME,
  47. ACTION_PORTS_DISCONNECT,
  48. ACTION_INLINE_DISPLAY,
  49. EYECANDY_FULL,
  50. PORT_MODE_NULL,
  51. PORT_MODE_INPUT,
  52. PORT_MODE_OUTPUT,
  53. PORT_TYPE_NULL,
  54. PORT_TYPE_AUDIO_JACK,
  55. PORT_TYPE_MIDI_ALSA,
  56. PORT_TYPE_MIDI_JACK,
  57. PORT_TYPE_PARAMETER,
  58. MAX_PLUGIN_ID_ALLOWED,
  59. )
  60. from .canvasboxshadow import CanvasBoxShadow
  61. from .canvasicon import CanvasIcon
  62. from .canvasport import CanvasPort
  63. from .theme import Theme
  64. from .utils import CanvasItemFX, CanvasGetFullPortName, CanvasGetPortConnectionList
  65. # ------------------------------------------------------------------------------------------------------------
  66. class cb_line_t(object):
  67. def __init__(self, line, connection_id):
  68. self.line = line
  69. self.connection_id = connection_id
  70. # ------------------------------------------------------------------------------------------------------------
  71. class CanvasBox(QGraphicsObject):
  72. # signals
  73. positionChanged = pyqtSignal(int, bool, int, int)
  74. # enums
  75. INLINE_DISPLAY_DISABLED = 0
  76. INLINE_DISPLAY_ENABLED = 1
  77. INLINE_DISPLAY_CACHED = 2
  78. def __init__(self, group_id, group_name, icon, parent=None):
  79. QGraphicsObject.__init__(self)
  80. self.setParentItem(parent)
  81. # Save Variables, useful for later
  82. self.m_group_id = group_id
  83. self.m_group_name = group_name
  84. # plugin Id, < 0 if invalid
  85. self.m_plugin_id = -1
  86. self.m_plugin_ui = False
  87. self.m_plugin_inline = self.INLINE_DISPLAY_DISABLED
  88. # Base Variables
  89. self.p_width = 50
  90. self.p_width_in = 0
  91. self.p_width_out = 0
  92. self.p_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing + 1
  93. self.m_last_pos = QPointF()
  94. self.m_split = False
  95. self.m_split_mode = PORT_MODE_NULL
  96. self.m_cursor_moving = False
  97. self.m_forced_split = False
  98. self.m_mouse_down = False
  99. self.m_inline_image = None
  100. self.m_inline_scaling = 1.0
  101. self.m_inline_first = True
  102. self.m_will_signal_pos_change = False
  103. self.m_port_list_ids = []
  104. self.m_connection_lines = []
  105. # Set Font
  106. self.m_font_name = QFont()
  107. self.m_font_name.setFamily(canvas.theme.box_font_name)
  108. self.m_font_name.setPixelSize(canvas.theme.box_font_size)
  109. self.m_font_name.setWeight(canvas.theme.box_font_state)
  110. self.m_font_port = QFont()
  111. self.m_font_port.setFamily(canvas.theme.port_font_name)
  112. self.m_font_port.setPixelSize(canvas.theme.port_font_size)
  113. self.m_font_port.setWeight(canvas.theme.port_font_state)
  114. # Icon
  115. if canvas.theme.box_use_icon:
  116. self.icon_svg = CanvasIcon(icon, self.m_group_name, self)
  117. else:
  118. self.icon_svg = None
  119. # Shadow
  120. if options.eyecandy and QT_VERSION >= 0x50c00:
  121. self.shadow = CanvasBoxShadow(self.toGraphicsObject())
  122. self.shadow.setFakeParent(self)
  123. self.setGraphicsEffect(self.shadow)
  124. else:
  125. self.shadow = None
  126. # Final touches
  127. self.setFlags(QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
  128. # Wait for at least 1 port
  129. if options.auto_hide_groups:
  130. self.setVisible(False)
  131. if options.auto_select_items:
  132. self.setAcceptHoverEvents(True)
  133. self.updatePositions()
  134. self.visibleChanged.connect(self.slot_signalPositionChangedLater)
  135. self.xChanged.connect(self.slot_signalPositionChangedLater)
  136. self.yChanged.connect(self.slot_signalPositionChangedLater)
  137. canvas.scene.addItem(self)
  138. QTimer.singleShot(0, self.fixPos)
  139. def getGroupId(self):
  140. return self.m_group_id
  141. def getGroupName(self):
  142. return self.m_group_name
  143. def isSplit(self):
  144. return self.m_split
  145. def getSplitMode(self):
  146. return self.m_split_mode
  147. def getPortCount(self):
  148. return len(self.m_port_list_ids)
  149. def getPortList(self):
  150. return self.m_port_list_ids
  151. def redrawInlineDisplay(self):
  152. if self.m_plugin_inline == self.INLINE_DISPLAY_CACHED:
  153. self.m_plugin_inline = self.INLINE_DISPLAY_ENABLED
  154. self.update()
  155. def removeAsPlugin(self):
  156. #del self.m_inline_image
  157. #self.m_inline_image = None
  158. #self.m_inline_scaling = 1.0
  159. self.m_plugin_id = -1
  160. self.m_plugin_ui = False
  161. self.m_plugin_inline = self.INLINE_DISPLAY_DISABLED
  162. def setAsPlugin(self, plugin_id, hasUI, hasInlineDisplay):
  163. if hasInlineDisplay and not options.inline_displays:
  164. hasInlineDisplay = False
  165. if not hasInlineDisplay:
  166. self.m_inline_image = None
  167. self.m_inline_scaling = 1.0
  168. self.m_plugin_id = plugin_id
  169. self.m_plugin_ui = hasUI
  170. self.m_plugin_inline = self.INLINE_DISPLAY_ENABLED if hasInlineDisplay else self.INLINE_DISPLAY_DISABLED
  171. self.update()
  172. def setIcon(self, icon):
  173. if self.icon_svg is not None:
  174. self.icon_svg.setIcon(icon, self.m_group_name)
  175. def setSplit(self, split, mode=PORT_MODE_NULL):
  176. self.m_split = split
  177. self.m_split_mode = mode
  178. def setGroupName(self, group_name):
  179. self.m_group_name = group_name
  180. self.updatePositions()
  181. def setShadowOpacity(self, opacity):
  182. if self.shadow is not None:
  183. self.shadow.setOpacity(opacity)
  184. def addPortFromGroup(self, port_id, port_mode, port_type, port_name, is_alternate):
  185. if len(self.m_port_list_ids) == 0:
  186. if options.auto_hide_groups:
  187. if options.eyecandy == EYECANDY_FULL:
  188. CanvasItemFX(self, True, False)
  189. self.blockSignals(True)
  190. self.setVisible(True)
  191. self.blockSignals(False)
  192. new_widget = CanvasPort(self.m_group_id, port_id, port_name, port_mode, port_type, is_alternate, self)
  193. port_dict = port_dict_t()
  194. port_dict.group_id = self.m_group_id
  195. port_dict.port_id = port_id
  196. port_dict.port_name = port_name
  197. port_dict.port_mode = port_mode
  198. port_dict.port_type = port_type
  199. port_dict.is_alternate = is_alternate
  200. port_dict.widget = new_widget
  201. self.m_port_list_ids.append(port_id)
  202. return new_widget
  203. def removePortFromGroup(self, port_id):
  204. if port_id in self.m_port_list_ids:
  205. self.m_port_list_ids.remove(port_id)
  206. else:
  207. qCritical("PatchCanvas::CanvasBox.removePort(%i) - unable to find port to remove" % port_id)
  208. return
  209. if len(self.m_port_list_ids) > 0:
  210. self.updatePositions()
  211. elif self.isVisible():
  212. if options.auto_hide_groups:
  213. if options.eyecandy == EYECANDY_FULL:
  214. CanvasItemFX(self, False, False)
  215. else:
  216. self.blockSignals(True)
  217. self.setVisible(False)
  218. self.blockSignals(False)
  219. def addLineFromGroup(self, line, connection_id):
  220. new_cbline = cb_line_t(line, connection_id)
  221. self.m_connection_lines.append(new_cbline)
  222. def removeLineFromGroup(self, connection_id):
  223. for connection in self.m_connection_lines:
  224. if connection.connection_id == connection_id:
  225. self.m_connection_lines.remove(connection)
  226. return
  227. qCritical("PatchCanvas::CanvasBox.removeLineFromGroup(%i) - unable to find line to remove" % connection_id)
  228. def checkItemPos(self):
  229. if canvas.size_rect.isNull():
  230. return
  231. pos = self.scenePos()
  232. if (canvas.size_rect.contains(pos) and
  233. canvas.size_rect.contains(pos + QPointF(self.p_width, self.p_height))):
  234. return
  235. if pos.x() < canvas.size_rect.x():
  236. self.setPos(canvas.size_rect.x(), pos.y())
  237. elif pos.x() + self.p_width > canvas.size_rect.width():
  238. self.setPos(canvas.size_rect.width() - self.p_width, pos.y())
  239. pos = self.scenePos()
  240. if pos.y() < canvas.size_rect.y():
  241. self.setPos(pos.x(), canvas.size_rect.y())
  242. elif pos.y() + self.p_height > canvas.size_rect.height():
  243. self.setPos(pos.x(), canvas.size_rect.height() - self.p_height)
  244. def removeIconFromScene(self):
  245. if self.icon_svg is None:
  246. return
  247. item = self.icon_svg
  248. self.icon_svg = None
  249. canvas.scene.removeItem(item)
  250. del item
  251. def updatePositions(self):
  252. self.prepareGeometryChange()
  253. # Check Text Name size
  254. app_name_size = fontHorizontalAdvance(self.m_font_name, self.m_group_name) + 30
  255. self.p_width = max(50, app_name_size)
  256. # Get Port List
  257. port_list = []
  258. for port in canvas.port_list:
  259. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  260. port_list.append(port)
  261. if len(port_list) == 0:
  262. self.p_height = canvas.theme.box_header_height
  263. self.p_width_in = 0
  264. self.p_width_out = 0
  265. else:
  266. max_in_width = max_out_width = 0
  267. port_spacing = canvas.theme.port_height + canvas.theme.port_spacing
  268. # Get Max Box Width, vertical ports re-positioning
  269. port_types = (PORT_TYPE_AUDIO_JACK, PORT_TYPE_MIDI_JACK, PORT_TYPE_MIDI_ALSA, PORT_TYPE_PARAMETER)
  270. last_in_type = last_out_type = PORT_TYPE_NULL
  271. last_in_pos = last_out_pos = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  272. for port_type in port_types:
  273. for port in port_list:
  274. if port.port_type != port_type:
  275. continue
  276. size = fontHorizontalAdvance(self.m_font_port, port.port_name)
  277. if port.port_mode == PORT_MODE_INPUT:
  278. max_in_width = max(max_in_width, size)
  279. if port.port_type != last_in_type:
  280. if last_in_type != PORT_TYPE_NULL:
  281. last_in_pos += canvas.theme.port_spacingT
  282. last_in_type = port.port_type
  283. port.widget.setY(last_in_pos)
  284. last_in_pos += port_spacing
  285. elif port.port_mode == PORT_MODE_OUTPUT:
  286. max_out_width = max(max_out_width, size)
  287. if port.port_type != last_out_type:
  288. if last_out_type != PORT_TYPE_NULL:
  289. last_out_pos += canvas.theme.port_spacingT
  290. last_out_type = port.port_type
  291. port.widget.setY(last_out_pos)
  292. last_out_pos += port_spacing
  293. self.p_width = max(self.p_width, 30 + max_in_width + max_out_width)
  294. self.p_width_in = max_in_width
  295. self.p_width_out = max_out_width
  296. self.p_height = max(last_in_pos, last_out_pos)
  297. self.p_height += max(canvas.theme.port_spacing, canvas.theme.port_spacingT) - canvas.theme.port_spacing
  298. self.p_height += canvas.theme.box_pen.width()
  299. self.repositionPorts(port_list)
  300. self.repaintLines(True)
  301. self.update()
  302. def repositionPorts(self, port_list = None):
  303. if port_list is None:
  304. port_list = []
  305. for port in canvas.port_list:
  306. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  307. port_list.append(port)
  308. # Horizontal ports re-positioning
  309. inX = canvas.theme.port_offset
  310. outX = self.p_width - self.p_width_out - canvas.theme.port_offset - 12
  311. for port in port_list:
  312. if port.port_mode == PORT_MODE_INPUT:
  313. port.widget.setX(inX)
  314. port.widget.setPortWidth(self.p_width_in)
  315. elif port.port_mode == PORT_MODE_OUTPUT:
  316. port.widget.setX(outX)
  317. port.widget.setPortWidth(self.p_width_out)
  318. def repaintLines(self, forced=False):
  319. if self.pos() != self.m_last_pos or forced:
  320. for connection in self.m_connection_lines:
  321. connection.line.updateLinePos()
  322. self.m_last_pos = self.pos()
  323. def resetLinesZValue(self):
  324. for connection in canvas.connection_list:
  325. if connection.port_out_id in self.m_port_list_ids and connection.port_in_id in self.m_port_list_ids:
  326. z_value = canvas.last_z_value
  327. else:
  328. z_value = canvas.last_z_value - 1
  329. connection.widget.setZValue(z_value)
  330. def triggerSignalPositionChanged(self):
  331. self.positionChanged.emit(self.m_group_id, self.m_split, int(self.x()), int(self.y()))
  332. self.m_will_signal_pos_change = False
  333. @pyqtSlot()
  334. def slot_signalPositionChangedLater(self):
  335. if self.m_will_signal_pos_change:
  336. return
  337. self.m_will_signal_pos_change = True
  338. QTimer.singleShot(0, self.triggerSignalPositionChanged)
  339. def type(self):
  340. return CanvasBoxType
  341. def contextMenuEvent(self, event):
  342. event.accept()
  343. menu = QMenu()
  344. # Conenct menu stuff
  345. connMenu = QMenu("Connect", menu)
  346. our_port_types = []
  347. our_port_outs = {
  348. PORT_TYPE_AUDIO_JACK: [],
  349. PORT_TYPE_MIDI_JACK: [],
  350. PORT_TYPE_MIDI_ALSA: [],
  351. PORT_TYPE_PARAMETER: [],
  352. }
  353. for port in canvas.port_list:
  354. if port.group_id != self.m_group_id:
  355. continue
  356. if port.port_mode != PORT_MODE_OUTPUT:
  357. continue
  358. if port.port_id not in self.m_port_list_ids:
  359. continue
  360. if port.port_type not in our_port_types:
  361. our_port_types.append(port.port_type)
  362. our_port_outs[port.port_type].append((port.group_id, port.port_id))
  363. if len(our_port_types) != 0:
  364. act_x_conn = None
  365. for group in canvas.group_list:
  366. if self.m_group_id == group.group_id:
  367. continue
  368. has_ports = False
  369. target_ports = {
  370. PORT_TYPE_AUDIO_JACK: [],
  371. PORT_TYPE_MIDI_JACK: [],
  372. PORT_TYPE_MIDI_ALSA: [],
  373. PORT_TYPE_PARAMETER: [],
  374. }
  375. for port in canvas.port_list:
  376. if port.group_id != group.group_id:
  377. continue
  378. if port.port_mode != PORT_MODE_INPUT:
  379. continue
  380. if port.port_type not in our_port_types:
  381. continue
  382. has_ports = True
  383. target_ports[port.port_type].append((port.group_id, port.port_id))
  384. if not has_ports:
  385. continue
  386. act_x_conn = connMenu.addAction(group.group_name)
  387. act_x_conn.setData((our_port_outs, target_ports))
  388. act_x_conn.triggered.connect(canvas.qobject.PortContextMenuConnect)
  389. if act_x_conn is None:
  390. act_x_disc = connMenu.addAction("Nothing to connect to")
  391. act_x_disc.setEnabled(False)
  392. else:
  393. act_x_disc = connMenu.addAction("No output ports")
  394. act_x_disc.setEnabled(False)
  395. # Disconnect menu stuff
  396. discMenu = QMenu("Disconnect", menu)
  397. conn_list = []
  398. conn_list_ids = []
  399. for port_id in self.m_port_list_ids:
  400. tmp_conn_list = CanvasGetPortConnectionList(self.m_group_id, port_id)
  401. for tmp_conn_id, tmp_group_id, tmp_port_id in tmp_conn_list:
  402. if tmp_conn_id not in conn_list_ids:
  403. conn_list.append((tmp_conn_id, tmp_group_id, tmp_port_id))
  404. conn_list_ids.append(tmp_conn_id)
  405. if len(conn_list) > 0:
  406. for conn_id, group_id, port_id in conn_list:
  407. act_x_disc = discMenu.addAction(CanvasGetFullPortName(group_id, port_id))
  408. act_x_disc.setData(conn_id)
  409. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  410. else:
  411. act_x_disc = discMenu.addAction("No connections")
  412. act_x_disc.setEnabled(False)
  413. menu.addMenu(connMenu)
  414. menu.addMenu(discMenu)
  415. act_x_disc_all = menu.addAction("Disconnect &All")
  416. act_x_sep1 = menu.addSeparator()
  417. act_x_info = menu.addAction("Info")
  418. act_x_rename = menu.addAction("Rename")
  419. act_x_sep2 = menu.addSeparator()
  420. act_x_split_join = menu.addAction("Join" if self.m_split else "Split")
  421. if not features.group_info:
  422. act_x_info.setVisible(False)
  423. if not features.group_rename:
  424. act_x_rename.setVisible(False)
  425. if not (features.group_info and features.group_rename):
  426. act_x_sep1.setVisible(False)
  427. if self.m_plugin_id >= 0 and self.m_plugin_id <= MAX_PLUGIN_ID_ALLOWED:
  428. menu.addSeparator()
  429. act_p_edit = menu.addAction("Edit")
  430. act_p_ui = menu.addAction("Show Custom UI")
  431. menu.addSeparator()
  432. act_p_clone = menu.addAction("Clone")
  433. act_p_rename = menu.addAction("Rename...")
  434. act_p_replace = menu.addAction("Replace...")
  435. act_p_remove = menu.addAction("Remove")
  436. if not self.m_plugin_ui:
  437. act_p_ui.setVisible(False)
  438. else:
  439. act_p_edit = act_p_ui = None
  440. act_p_clone = act_p_rename = None
  441. act_p_replace = act_p_remove = None
  442. haveIns = haveOuts = False
  443. for port in canvas.port_list:
  444. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  445. if port.port_mode == PORT_MODE_INPUT:
  446. haveIns = True
  447. elif port.port_mode == PORT_MODE_OUTPUT:
  448. haveOuts = True
  449. if not (self.m_split or bool(haveIns and haveOuts)):
  450. act_x_sep2.setVisible(False)
  451. act_x_split_join.setVisible(False)
  452. act_selected = menu.exec_(event.screenPos())
  453. if act_selected is None:
  454. pass
  455. elif act_selected == act_x_disc_all:
  456. for conn_id in conn_list_ids:
  457. canvas.callback(ACTION_PORTS_DISCONNECT, conn_id, 0, "")
  458. elif act_selected == act_x_info:
  459. canvas.callback(ACTION_GROUP_INFO, self.m_group_id, 0, "")
  460. elif act_selected == act_x_rename:
  461. canvas.callback(ACTION_GROUP_RENAME, self.m_group_id, 0, "")
  462. elif act_selected == act_x_split_join:
  463. if self.m_split:
  464. canvas.callback(ACTION_GROUP_JOIN, self.m_group_id, 0, "")
  465. else:
  466. canvas.callback(ACTION_GROUP_SPLIT, self.m_group_id, 0, "")
  467. elif act_selected == act_p_edit:
  468. canvas.callback(ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  469. elif act_selected == act_p_ui:
  470. canvas.callback(ACTION_PLUGIN_SHOW_UI, self.m_plugin_id, 0, "")
  471. elif act_selected == act_p_clone:
  472. canvas.callback(ACTION_PLUGIN_CLONE, self.m_plugin_id, 0, "")
  473. elif act_selected == act_p_rename:
  474. canvas.callback(ACTION_PLUGIN_RENAME, self.m_plugin_id, 0, "")
  475. elif act_selected == act_p_replace:
  476. canvas.callback(ACTION_PLUGIN_REPLACE, self.m_plugin_id, 0, "")
  477. elif act_selected == act_p_remove:
  478. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  479. def keyPressEvent(self, event):
  480. if self.m_plugin_id >= 0 and event.key() == Qt.Key_Delete:
  481. event.accept()
  482. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  483. return
  484. QGraphicsObject.keyPressEvent(self, event)
  485. def hoverEnterEvent(self, event):
  486. if options.auto_select_items:
  487. if len(canvas.scene.selectedItems()) > 0:
  488. canvas.scene.clearSelection()
  489. self.setSelected(True)
  490. QGraphicsObject.hoverEnterEvent(self, event)
  491. def mouseDoubleClickEvent(self, event):
  492. if self.m_plugin_id >= 0:
  493. event.accept()
  494. canvas.callback(ACTION_PLUGIN_SHOW_UI if self.m_plugin_ui else ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  495. return
  496. QGraphicsObject.mouseDoubleClickEvent(self, event)
  497. def mousePressEvent(self, event):
  498. if event.button() == Qt.MiddleButton or event.source() == Qt.MouseEventSynthesizedByApplication:
  499. event.ignore()
  500. return
  501. canvas.last_z_value += 1
  502. self.setZValue(canvas.last_z_value)
  503. self.resetLinesZValue()
  504. self.m_cursor_moving = False
  505. if event.button() == Qt.RightButton:
  506. event.accept()
  507. canvas.scene.clearSelection()
  508. self.setSelected(True)
  509. self.m_mouse_down = False
  510. return
  511. elif event.button() == Qt.LeftButton:
  512. if self.sceneBoundingRect().contains(event.scenePos()):
  513. self.m_mouse_down = True
  514. else:
  515. # FIXME: Check if still valid: Fix a weird Qt behaviour with right-click mouseMove
  516. self.m_mouse_down = False
  517. event.ignore()
  518. return
  519. else:
  520. self.m_mouse_down = False
  521. QGraphicsObject.mousePressEvent(self, event)
  522. def mouseMoveEvent(self, event):
  523. if self.m_mouse_down:
  524. if not self.m_cursor_moving:
  525. self.setCursor(QCursor(Qt.SizeAllCursor))
  526. self.m_cursor_moving = True
  527. self.repaintLines()
  528. QGraphicsObject.mouseMoveEvent(self, event)
  529. def mouseReleaseEvent(self, event):
  530. if self.m_cursor_moving:
  531. self.unsetCursor()
  532. QTimer.singleShot(0, self.fixPos)
  533. self.m_mouse_down = False
  534. self.m_cursor_moving = False
  535. QGraphicsObject.mouseReleaseEvent(self, event)
  536. def fixPos(self):
  537. self.blockSignals(True)
  538. self.setX(round(self.x()))
  539. self.setY(round(self.y()))
  540. self.blockSignals(False)
  541. def boundingRect(self):
  542. return QRectF(0, 0, self.p_width, self.p_height)
  543. def paint(self, painter, option, widget):
  544. painter.save()
  545. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  546. rect = QRectF(0, 0, self.p_width, self.p_height)
  547. # Draw rectangle
  548. pen = QPen(canvas.theme.box_pen_sel if self.isSelected() else canvas.theme.box_pen)
  549. pen.setWidthF(pen.widthF() + 0.00001)
  550. painter.setPen(pen)
  551. lineHinting = pen.widthF() / 2
  552. if canvas.theme.box_bg_type == Theme.THEME_BG_GRADIENT:
  553. box_gradient = QLinearGradient(0, 0, 0, self.p_height)
  554. box_gradient.setColorAt(0, canvas.theme.box_bg_1)
  555. box_gradient.setColorAt(1, canvas.theme.box_bg_2)
  556. painter.setBrush(box_gradient)
  557. else:
  558. painter.setBrush(canvas.theme.box_bg_1)
  559. rect.adjust(lineHinting, lineHinting, -lineHinting, -lineHinting)
  560. painter.drawRect(rect)
  561. # Draw plugin inline display if supported
  562. self.paintInlineDisplay(painter)
  563. # Draw pixmap header
  564. rect.setHeight(canvas.theme.box_header_height)
  565. if canvas.theme.box_header_pixmap:
  566. painter.setPen(Qt.NoPen)
  567. painter.setBrush(canvas.theme.box_bg_2)
  568. # outline
  569. rect.adjust(lineHinting, lineHinting, -lineHinting, -lineHinting)
  570. painter.drawRect(rect)
  571. rect.adjust(1, 1, -1, 0)
  572. painter.drawTiledPixmap(rect, canvas.theme.box_header_pixmap, rect.topLeft())
  573. # Draw text
  574. painter.setFont(self.m_font_name)
  575. if self.isSelected():
  576. painter.setPen(canvas.theme.box_text_sel)
  577. else:
  578. painter.setPen(canvas.theme.box_text)
  579. if canvas.theme.box_use_icon:
  580. textPos = QPointF(25, canvas.theme.box_text_ypos)
  581. else:
  582. appNameSize = fontHorizontalAdvance(self.m_font_name, self.m_group_name)
  583. rem = self.p_width - appNameSize
  584. textPos = QPointF(rem/2, canvas.theme.box_text_ypos)
  585. painter.drawText(textPos, self.m_group_name)
  586. self.repaintLines()
  587. painter.restore()
  588. def paintInlineDisplay(self, painter):
  589. if self.m_plugin_inline == self.INLINE_DISPLAY_DISABLED:
  590. return
  591. if not options.inline_displays:
  592. return
  593. inwidth = self.p_width - 16 - self.p_width_in - self.p_width_out
  594. inheight = self.p_height - 3 - canvas.theme.box_header_height - canvas.theme.box_header_spacing - canvas.theme.port_spacing
  595. scaling = canvas.scene.getScaleFactor() * canvas.scene.getDevicePixelRatioF()
  596. if self.m_plugin_id >= 0 and self.m_plugin_id <= MAX_PLUGIN_ID_ALLOWED and (
  597. self.m_plugin_inline == self.INLINE_DISPLAY_ENABLED or self.m_inline_scaling != scaling):
  598. if self.m_inline_first:
  599. size = "%i:%i" % (int(50*scaling), int(50*scaling))
  600. else:
  601. size = "%i:%i" % (int(inwidth*scaling), int(inheight*scaling))
  602. data = canvas.callback(ACTION_INLINE_DISPLAY, self.m_plugin_id, 0, size)
  603. if data is None:
  604. return
  605. self.m_inline_image = QImage(data['data'], data['width'], data['height'], data['stride'],
  606. QImage.Format_ARGB32)
  607. self.m_inline_scaling = scaling
  608. self.m_plugin_inline = self.INLINE_DISPLAY_CACHED
  609. # make room for inline display, in a square shape
  610. if self.m_inline_first:
  611. self.m_inline_first = False
  612. aspectRatio = data['width'] / data['height']
  613. self.p_height = int(max(50*scaling, self.p_height))
  614. self.p_width += int(max(0, min((80 - 14)*scaling, (inheight-inwidth) * aspectRatio * scaling)))
  615. self.repositionPorts()
  616. self.repaintLines(True)
  617. self.update()
  618. return
  619. if self.m_inline_image is None:
  620. print("ERROR: inline display image is None for", self.m_plugin_id, self.m_group_name)
  621. return
  622. swidth = self.m_inline_image.width() / scaling
  623. sheight = self.m_inline_image.height() / scaling
  624. srcx = int(self.p_width_in + (self.p_width - self.p_width_in - self.p_width_out) / 2 - swidth / 2)
  625. srcy = int(canvas.theme.box_header_height + canvas.theme.box_header_spacing + 1 + (inheight - sheight) / 2)
  626. painter.drawImage(QRectF(srcx, srcy, swidth, sheight), self.m_inline_image)
  627. # ------------------------------------------------------------------------------------------------------------