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.

740 lines
27KB

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