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.

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