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.

2843 lines
100KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # PatchBay Canvas engine using QGraphicsView/Scene
  4. # Copyright (C) 2010-2014 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 (Config)
  19. from carla_config import *
  20. # ------------------------------------------------------------------------------------------------------------
  21. # Imports (Global)
  22. from math import floor
  23. if config_UseQt5:
  24. from PyQt5.QtCore import pyqtSignal, pyqtSlot, qCritical, qFatal, qWarning, Qt, QObject
  25. from PyQt5.QtCore import QAbstractAnimation, QLineF, QPointF, QRectF, QSizeF, QSettings, QTimer
  26. from PyQt5.QtGui import QColor, QLinearGradient, QPen, QPolygonF, QPainter, QPainterPath
  27. from PyQt5.QtGui import QCursor, QFont, QFontMetrics
  28. from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer
  29. from PyQt5.QtWidgets import QGraphicsScene, QGraphicsItem, QGraphicsLineItem, QGraphicsPathItem
  30. from PyQt5.QtWidgets import QGraphicsColorizeEffect, QGraphicsDropShadowEffect, QMenu
  31. else:
  32. from PyQt4.QtCore import pyqtSignal, pyqtSlot, qCritical, qFatal, qWarning, Qt, QObject
  33. from PyQt4.QtCore import QAbstractAnimation, QLineF, QPointF, QRectF, QSizeF, QSettings, QTimer
  34. from PyQt4.QtGui import QColor, QLinearGradient, QPen, QPolygonF, QPainter, QPainterPath
  35. from PyQt4.QtGui import QCursor, QFont, QFontMetrics
  36. from PyQt4.QtGui import QGraphicsScene, QGraphicsItem, QGraphicsLineItem, QGraphicsPathItem
  37. from PyQt4.QtGui import QGraphicsColorizeEffect, QGraphicsDropShadowEffect, QMenu
  38. from PyQt4.QtSvg import QGraphicsSvgItem, QSvgRenderer
  39. # ------------------------------------------------------------------------------------------------------------
  40. # Imports (Theme)
  41. from patchcanvas_theme import *
  42. # ------------------------------------------------------------------------------
  43. # patchcanvas-api.h
  44. # Maximum Id for a plugin, treated as invalid/zero if above this value
  45. MAX_PLUGIN_ID_ALLOWED = 0x7FF
  46. # Port Mode
  47. PORT_MODE_NULL = 0
  48. PORT_MODE_INPUT = 1
  49. PORT_MODE_OUTPUT = 2
  50. # Port Type
  51. PORT_TYPE_NULL = 0
  52. PORT_TYPE_AUDIO_JACK = 1
  53. PORT_TYPE_MIDI_JACK = 2
  54. PORT_TYPE_MIDI_ALSA = 3
  55. PORT_TYPE_PARAMETER = 4
  56. # Callback Action
  57. ACTION_GROUP_INFO = 0 # group_id, N, N
  58. ACTION_GROUP_RENAME = 1 # group_id, N, N
  59. ACTION_GROUP_SPLIT = 2 # group_id, N, N
  60. ACTION_GROUP_JOIN = 3 # group_id, N, N
  61. ACTION_PORT_INFO = 4 # group_id, port_id, N
  62. ACTION_PORT_RENAME = 5 # group_id, port_id, N
  63. ACTION_PORTS_CONNECT = 6 # N, N, "outG:outP:inG:inP"
  64. ACTION_PORTS_DISCONNECT = 7 # conn_id, N, N
  65. ACTION_PLUGIN_CLONE = 8 # plugin_id, N, N
  66. ACTION_PLUGIN_EDIT = 9 # plugin_id, N, N
  67. ACTION_PLUGIN_RENAME = 10 # plugin_id, N, N
  68. ACTION_PLUGIN_REPLACE = 11 # plugin_id, N, N
  69. ACTION_PLUGIN_REMOVE = 12 # plugin_id, N, N
  70. ACTION_PLUGIN_SHOW_UI = 13 # plugin_id, N, N
  71. ACTION_BG_RIGHT_CLICK = 14 # N, N, N
  72. # Icon
  73. ICON_APPLICATION = 0
  74. ICON_HARDWARE = 1
  75. ICON_DISTRHO = 2
  76. ICON_FILE = 3
  77. ICON_PLUGIN = 4
  78. ICON_LADISH_ROOM = 5
  79. # Split Option
  80. SPLIT_UNDEF = 0
  81. SPLIT_NO = 1
  82. SPLIT_YES = 2
  83. # Antialiasing Option
  84. ANTIALIASING_NONE = 0
  85. ANTIALIASING_SMALL = 1
  86. ANTIALIASING_FULL = 2
  87. # Eye-Candy Option
  88. EYECANDY_NONE = 0
  89. EYECANDY_SMALL = 1
  90. EYECANDY_FULL = 2
  91. # Canvas options
  92. class options_t(object):
  93. __slots__ = [
  94. 'theme_name',
  95. 'auto_hide_groups',
  96. 'auto_select_items',
  97. 'use_bezier_lines',
  98. 'antialiasing',
  99. 'eyecandy'
  100. ]
  101. # Canvas features
  102. class features_t(object):
  103. __slots__ = [
  104. 'group_info',
  105. 'group_rename',
  106. 'port_info',
  107. 'port_rename',
  108. 'handle_group_pos'
  109. ]
  110. # ------------------------------------------------------------------------------
  111. # patchcanvas.h
  112. # object types
  113. CanvasBoxType = QGraphicsItem.UserType + 1
  114. CanvasIconType = QGraphicsItem.UserType + 2
  115. CanvasPortType = QGraphicsItem.UserType + 3
  116. CanvasLineType = QGraphicsItem.UserType + 4
  117. CanvasBezierLineType = QGraphicsItem.UserType + 5
  118. CanvasLineMovType = QGraphicsItem.UserType + 6
  119. CanvasBezierLineMovType = QGraphicsItem.UserType + 7
  120. # object lists
  121. class group_dict_t(object):
  122. __slots__ = [
  123. 'group_id',
  124. 'group_name',
  125. 'split',
  126. 'icon',
  127. 'plugin_id',
  128. 'plugin_ui',
  129. 'widgets'
  130. ]
  131. class port_dict_t(object):
  132. __slots__ = [
  133. 'group_id',
  134. 'port_id',
  135. 'port_name',
  136. 'port_mode',
  137. 'port_type',
  138. 'is_alternate',
  139. 'widget'
  140. ]
  141. class connection_dict_t(object):
  142. __slots__ = [
  143. 'connection_id',
  144. 'group_in_id',
  145. 'port_in_id',
  146. 'group_out_id',
  147. 'port_out_id',
  148. 'widget'
  149. ]
  150. class animation_dict_t(object):
  151. __slots__ = [
  152. 'animation',
  153. 'item'
  154. ]
  155. # Main Canvas object
  156. class Canvas(object):
  157. __slots__ = [
  158. 'scene',
  159. 'callback',
  160. 'debug',
  161. 'last_z_value',
  162. 'last_connection_id',
  163. 'initial_pos',
  164. 'size_rect',
  165. 'group_list',
  166. 'port_list',
  167. 'connection_list',
  168. 'animation_list',
  169. 'qobject',
  170. 'settings',
  171. 'theme',
  172. 'initiated'
  173. ]
  174. # ------------------------------------------------------------------------------
  175. # patchcanvas.cpp
  176. class CanvasObject(QObject):
  177. def __init__(self, parent=None):
  178. QObject.__init__(self, parent)
  179. @pyqtSlot()
  180. def AnimationFinishedShow(self):
  181. animation = self.sender()
  182. if animation:
  183. animation.forceStop()
  184. canvas.animation_list.remove(animation)
  185. @pyqtSlot()
  186. def AnimationFinishedHide(self):
  187. animation = self.sender()
  188. if animation:
  189. animation.forceStop()
  190. canvas.animation_list.remove(animation)
  191. item = animation.item()
  192. if item: item.hide()
  193. @pyqtSlot()
  194. def AnimationFinishedDestroy(self):
  195. animation = self.sender()
  196. if animation:
  197. animation.forceStop()
  198. canvas.animation_list.remove(animation)
  199. item = animation.item()
  200. if item: CanvasRemoveItemFX(item)
  201. @pyqtSlot()
  202. def PortContextMenuDisconnect(self):
  203. try:
  204. connectionId = int(self.sender().data())
  205. except:
  206. return
  207. CanvasCallback(ACTION_PORTS_DISCONNECT, connectionId, 0, "")
  208. # Global objects
  209. canvas = Canvas()
  210. canvas.qobject = None
  211. canvas.settings = None
  212. canvas.theme = None
  213. canvas.initiated = False
  214. canvas.group_list = []
  215. canvas.port_list = []
  216. canvas.connection_list = []
  217. canvas.animation_list = []
  218. options = options_t()
  219. options.theme_name = getDefaultThemeName()
  220. options.auto_hide_groups = False
  221. options.auto_select_items = False
  222. options.use_bezier_lines = True
  223. options.antialiasing = ANTIALIASING_SMALL
  224. options.eyecandy = EYECANDY_SMALL
  225. features = features_t()
  226. features.group_info = False
  227. features.group_rename = False
  228. features.port_info = False
  229. features.port_rename = False
  230. features.handle_group_pos = False
  231. # Internal functions
  232. def bool2str(check):
  233. return "True" if check else "False"
  234. def port_mode2str(port_mode):
  235. if port_mode == PORT_MODE_NULL:
  236. return "PORT_MODE_NULL"
  237. elif port_mode == PORT_MODE_INPUT:
  238. return "PORT_MODE_INPUT"
  239. elif port_mode == PORT_MODE_OUTPUT:
  240. return "PORT_MODE_OUTPUT"
  241. else:
  242. return "PORT_MODE_???"
  243. def port_type2str(port_type):
  244. if port_type == PORT_TYPE_NULL:
  245. return "PORT_TYPE_NULL"
  246. elif port_type == PORT_TYPE_AUDIO_JACK:
  247. return "PORT_TYPE_AUDIO_JACK"
  248. elif port_type == PORT_TYPE_MIDI_JACK:
  249. return "PORT_TYPE_MIDI_JACK"
  250. elif port_type == PORT_TYPE_MIDI_ALSA:
  251. return "PORT_TYPE_MIDI_ALSA"
  252. elif port_type == PORT_TYPE_PARAMETER:
  253. return "PORT_TYPE_MIDI_PARAMETER"
  254. else:
  255. return "PORT_TYPE_???"
  256. def icon2str(icon):
  257. if icon == ICON_APPLICATION:
  258. return "ICON_APPLICATION"
  259. elif icon == ICON_HARDWARE:
  260. return "ICON_HARDWARE"
  261. elif icon == ICON_DISTRHO:
  262. return "ICON_DISTRHO"
  263. elif icon == ICON_FILE:
  264. return "ICON_FILE"
  265. elif icon == ICON_PLUGIN:
  266. return "ICON_PLUGIN"
  267. elif icon == ICON_LADISH_ROOM:
  268. return "ICON_LADISH_ROOM"
  269. else:
  270. return "ICON_???"
  271. def split2str(split):
  272. if split == SPLIT_UNDEF:
  273. return "SPLIT_UNDEF"
  274. elif split == SPLIT_NO:
  275. return "SPLIT_NO"
  276. elif split == SPLIT_YES:
  277. return "SPLIT_YES"
  278. else:
  279. return "SPLIT_???"
  280. def getStoredCanvasPosition(key, fallback_pos):
  281. try:
  282. return canvas.settings.value("CanvasPositions/" + key, fallback_pos, type=QPointF)
  283. except:
  284. return fallback_pos
  285. def getStoredCanvasSplit(group_name, fallback_split_mode):
  286. try:
  287. return canvas.settings.value("CanvasPositions/%s_SPLIT" % group_name, fallback_split_mode, type=int)
  288. except:
  289. return fallback_split_mode
  290. # PatchCanvas API
  291. def setOptions(new_options):
  292. if canvas.initiated: return
  293. options.theme_name = new_options.theme_name
  294. options.auto_hide_groups = new_options.auto_hide_groups
  295. options.auto_select_items = new_options.auto_select_items
  296. options.use_bezier_lines = new_options.use_bezier_lines
  297. options.antialiasing = new_options.antialiasing
  298. options.eyecandy = new_options.eyecandy
  299. def setFeatures(new_features):
  300. if canvas.initiated: return
  301. features.group_info = new_features.group_info
  302. features.group_rename = new_features.group_rename
  303. features.port_info = new_features.port_info
  304. features.port_rename = new_features.port_rename
  305. features.handle_group_pos = new_features.handle_group_pos
  306. def init(appName, scene, callback, debug=False):
  307. if debug:
  308. print("PatchCanvas::init(\"%s\", %s, %s, %s)" % (appName, scene, callback, bool2str(debug)))
  309. if canvas.initiated:
  310. qCritical("PatchCanvas::init() - already initiated")
  311. return
  312. if not callback:
  313. qFatal("PatchCanvas::init() - fatal error: callback not set")
  314. return
  315. canvas.scene = scene
  316. canvas.callback = callback
  317. canvas.debug = debug
  318. canvas.last_z_value = 0
  319. canvas.last_connection_id = 0
  320. canvas.initial_pos = QPointF(0, 0)
  321. canvas.size_rect = QRectF()
  322. if not canvas.qobject: canvas.qobject = CanvasObject()
  323. if not canvas.settings: canvas.settings = QSettings("falkTX", appName)
  324. if canvas.theme:
  325. del canvas.theme
  326. canvas.theme = None
  327. for i in range(Theme.THEME_MAX):
  328. this_theme_name = getThemeName(i)
  329. if this_theme_name == options.theme_name:
  330. canvas.theme = Theme(i)
  331. break
  332. if not canvas.theme:
  333. canvas.theme = Theme(getDefaultTheme())
  334. canvas.scene.updateTheme()
  335. canvas.initiated = True
  336. def clear():
  337. if canvas.debug:
  338. print("PatchCanvas::clear()")
  339. group_list_ids = []
  340. port_list_ids = []
  341. connection_list_ids = []
  342. for group in canvas.group_list:
  343. group_list_ids.append(group.group_id)
  344. for port in canvas.port_list:
  345. port_list_ids.append((port.group_id, port.port_id))
  346. for connection in canvas.connection_list:
  347. connection_list_ids.append(connection.connection_id)
  348. for idx in connection_list_ids:
  349. disconnectPorts(idx)
  350. for group_id, port_id in port_list_ids:
  351. removePort(group_id, port_id)
  352. for idx in group_list_ids:
  353. removeGroup(idx)
  354. canvas.last_z_value = 0
  355. canvas.last_connection_id = 0
  356. canvas.group_list = []
  357. canvas.port_list = []
  358. canvas.connection_list = []
  359. canvas.scene.clearSelection()
  360. animatedItems = []
  361. for animation in canvas.animation_list:
  362. animatedItems.append(animation.item())
  363. for item in canvas.scene.items():
  364. if item.type() != CanvasIconType and item not in animatedItems:
  365. canvas.scene.removeItem(item)
  366. del item
  367. canvas.initiated = False
  368. QTimer.singleShot(0, canvas.scene.update)
  369. def setInitialPos(x, y):
  370. if canvas.debug:
  371. print("PatchCanvas::setInitialPos(%i, %i)" % (x, y))
  372. canvas.initial_pos.setX(x)
  373. canvas.initial_pos.setY(y)
  374. def setCanvasSize(x, y, width, height):
  375. if canvas.debug:
  376. print("PatchCanvas::setCanvasSize(%i, %i, %i, %i)" % (x, y, width, height))
  377. canvas.size_rect.setX(x)
  378. canvas.size_rect.setY(y)
  379. canvas.size_rect.setWidth(width)
  380. canvas.size_rect.setHeight(height)
  381. def addGroup(group_id, group_name, split=SPLIT_UNDEF, icon=ICON_APPLICATION):
  382. if canvas.debug:
  383. print("PatchCanvas::addGroup(%i, %s, %s, %s)" % (group_id, group_name.encode(), split2str(split), icon2str(icon)))
  384. for group in canvas.group_list:
  385. if group.group_id == group_id:
  386. qWarning("PatchCanvas::addGroup(%i, %s, %s, %s) - group already exists" % (group_id, group_name.encode(), split2str(split), icon2str(icon)))
  387. return
  388. if split == SPLIT_UNDEF:
  389. isHardware = bool(icon == ICON_HARDWARE)
  390. if features.handle_group_pos:
  391. split = getStoredCanvasSplit(group_name, SPLIT_YES if isHardware else split)
  392. elif isHardware:
  393. split = SPLIT_YES
  394. group_box = CanvasBox(group_id, group_name, icon)
  395. group_dict = group_dict_t()
  396. group_dict.group_id = group_id
  397. group_dict.group_name = group_name
  398. group_dict.split = bool(split == SPLIT_YES)
  399. group_dict.icon = icon
  400. group_dict.plugin_id = -1
  401. group_dict.plugin_ui = False
  402. group_dict.widgets = [group_box, None]
  403. if split == SPLIT_YES:
  404. group_box.setSplit(True, PORT_MODE_OUTPUT)
  405. if features.handle_group_pos:
  406. group_box.setPos(getStoredCanvasPosition(group_name + "_OUTPUT", CanvasGetNewGroupPos(False)))
  407. else:
  408. group_box.setPos(CanvasGetNewGroupPos(False))
  409. group_sbox = CanvasBox(group_id, group_name, icon)
  410. group_sbox.setSplit(True, PORT_MODE_INPUT)
  411. group_dict.widgets[1] = group_sbox
  412. if features.handle_group_pos:
  413. group_sbox.setPos(getStoredCanvasPosition(group_name + "_INPUT", CanvasGetNewGroupPos(True)))
  414. else:
  415. group_sbox.setPos(CanvasGetNewGroupPos(True))
  416. canvas.last_z_value += 1
  417. group_sbox.setZValue(canvas.last_z_value)
  418. if options.eyecandy == EYECANDY_FULL and not options.auto_hide_groups:
  419. CanvasItemFX(group_sbox, True, False)
  420. group_sbox.checkItemPos()
  421. else:
  422. group_box.setSplit(False)
  423. if features.handle_group_pos:
  424. group_box.setPos(getStoredCanvasPosition(group_name, CanvasGetNewGroupPos(False)))
  425. else:
  426. # Special ladish fake-split groups
  427. horizontal = bool(icon == ICON_HARDWARE or icon == ICON_LADISH_ROOM)
  428. group_box.setPos(CanvasGetNewGroupPos(horizontal))
  429. group_box.checkItemPos()
  430. canvas.last_z_value += 1
  431. group_box.setZValue(canvas.last_z_value)
  432. canvas.group_list.append(group_dict)
  433. if options.eyecandy == EYECANDY_FULL and not options.auto_hide_groups:
  434. return CanvasItemFX(group_box, True, False)
  435. QTimer.singleShot(0, canvas.scene.update)
  436. def removeGroup(group_id):
  437. if canvas.debug:
  438. print("PatchCanvas::removeGroup(%i)" % group_id)
  439. for group in canvas.group_list:
  440. if group.group_id == group_id:
  441. item = group.widgets[0]
  442. group_name = group.group_name
  443. if group.split:
  444. s_item = group.widgets[1]
  445. if features.handle_group_pos:
  446. canvas.settings.setValue("CanvasPositions/%s_OUTPUT" % group_name, item.pos())
  447. canvas.settings.setValue("CanvasPositions/%s_INPUT" % group_name, s_item.pos())
  448. canvas.settings.setValue("CanvasPositions/%s_SPLIT" % group_name, SPLIT_YES)
  449. if options.eyecandy == EYECANDY_FULL:
  450. CanvasItemFX(s_item, False, True)
  451. else:
  452. s_item.removeIconFromScene()
  453. canvas.scene.removeItem(s_item)
  454. del s_item
  455. else:
  456. if features.handle_group_pos:
  457. canvas.settings.setValue("CanvasPositions/%s" % group_name, item.pos())
  458. canvas.settings.setValue("CanvasPositions/%s_SPLIT" % group_name, SPLIT_NO)
  459. if options.eyecandy == EYECANDY_FULL:
  460. CanvasItemFX(item, False, True)
  461. else:
  462. item.removeIconFromScene()
  463. canvas.scene.removeItem(item)
  464. del item
  465. canvas.group_list.remove(group)
  466. QTimer.singleShot(0, canvas.scene.update)
  467. return
  468. qCritical("PatchCanvas::removeGroup(%i) - unable to find group to remove" % group_id)
  469. def renameGroup(group_id, new_group_name):
  470. if canvas.debug:
  471. print("PatchCanvas::renameGroup(%i, %s)" % (group_id, new_group_name.encode()))
  472. for group in canvas.group_list:
  473. if group.group_id == group_id:
  474. group.group_name = new_group_name
  475. group.widgets[0].setGroupName(new_group_name)
  476. if group.split and group.widgets[1]:
  477. group.widgets[1].setGroupName(new_group_name)
  478. QTimer.singleShot(0, canvas.scene.update)
  479. return
  480. qCritical("PatchCanvas::renameGroup(%i, %s) - unable to find group to rename" % (group_id, new_group_name.encode()))
  481. def splitGroup(group_id):
  482. if canvas.debug:
  483. print("PatchCanvas::splitGroup(%i)" % group_id)
  484. item = None
  485. group_name = ""
  486. group_icon = ICON_APPLICATION
  487. plugin_id = -1
  488. plugin_ui = False
  489. ports_data = []
  490. conns_data = []
  491. # Step 1 - Store all Item data
  492. for group in canvas.group_list:
  493. if group.group_id == group_id:
  494. if group.split:
  495. qCritical("PatchCanvas::splitGroup(%i) - group is already splitted" % group_id)
  496. return
  497. item = group.widgets[0]
  498. group_name = group.group_name
  499. group_icon = group.icon
  500. plugin_id = group.plugin_id
  501. plugin_ui = group.plugin_ui
  502. break
  503. if not item:
  504. qCritical("PatchCanvas::splitGroup(%i) - unable to find group to split" % group_id)
  505. return
  506. port_list_ids = list(item.getPortList())
  507. for port in canvas.port_list:
  508. if port.port_id in port_list_ids:
  509. port_dict = port_dict_t()
  510. port_dict.group_id = port.group_id
  511. port_dict.port_id = port.port_id
  512. port_dict.port_name = port.port_name
  513. port_dict.port_mode = port.port_mode
  514. port_dict.port_type = port.port_type
  515. port_dict.is_alternate = port.is_alternate
  516. port_dict.widget = None
  517. ports_data.append(port_dict)
  518. for connection in canvas.connection_list:
  519. if connection.port_out_id in port_list_ids or connection.port_in_id in port_list_ids:
  520. connection_dict = connection_dict_t()
  521. connection_dict.connection_id = connection.connection_id
  522. connection_dict.group_in_id = connection.group_in_id
  523. connection_dict.port_in_id = connection.port_in_id
  524. connection_dict.group_out_id = connection.group_out_id
  525. connection_dict.port_out_id = connection.port_out_id
  526. connection_dict.widget = None
  527. conns_data.append(connection_dict)
  528. # Step 2 - Remove Item and Children
  529. for conn in conns_data:
  530. disconnectPorts(conn.connection_id)
  531. for port_id in port_list_ids:
  532. removePort(group_id, port_id)
  533. removeGroup(group_id)
  534. # Step 3 - Re-create Item, now splitted
  535. addGroup(group_id, group_name, SPLIT_YES, group_icon)
  536. if plugin_id >= 0:
  537. setGroupAsPlugin(group_id, plugin_id, plugin_ui)
  538. for port in ports_data:
  539. addPort(group_id, port.port_id, port.port_name, port.port_mode, port.port_type, port.is_alternate)
  540. for conn in conns_data:
  541. connectPorts(conn.connection_id, conn.group_out_id, conn.port_out_id, conn.group_in_id, conn.port_in_id)
  542. QTimer.singleShot(0, canvas.scene.update)
  543. def joinGroup(group_id):
  544. if canvas.debug:
  545. print("PatchCanvas::joinGroup(%i)" % group_id)
  546. item = None
  547. s_item = None
  548. group_name = ""
  549. group_icon = ICON_APPLICATION
  550. plugin_id = -1
  551. plugin_ui = False
  552. ports_data = []
  553. conns_data = []
  554. # Step 1 - Store all Item data
  555. for group in canvas.group_list:
  556. if group.group_id == group_id:
  557. if not group.split:
  558. qCritical("PatchCanvas::joinGroup(%i) - group is not splitted" % group_id)
  559. return
  560. item = group.widgets[0]
  561. s_item = group.widgets[1]
  562. group_name = group.group_name
  563. group_icon = group.icon
  564. plugin_id = group.plugin_id
  565. plugin_ui = group.plugin_ui
  566. break
  567. # FIXME
  568. if not (item and s_item):
  569. qCritical("PatchCanvas::joinGroup(%i) - unable to find groups to join" % group_id)
  570. return
  571. port_list_ids = list(item.getPortList())
  572. port_list_idss = s_item.getPortList()
  573. for port_id in port_list_idss:
  574. if port_id not in port_list_ids:
  575. port_list_ids.append(port_id)
  576. for port in canvas.port_list:
  577. if port.port_id in port_list_ids:
  578. port_dict = port_dict_t()
  579. port_dict.group_id = port.group_id
  580. port_dict.port_id = port.port_id
  581. port_dict.port_name = port.port_name
  582. port_dict.port_mode = port.port_mode
  583. port_dict.port_type = port.port_type
  584. port_dict.is_alternate = port.is_alternate
  585. port_dict.widget = None
  586. ports_data.append(port_dict)
  587. for connection in canvas.connection_list:
  588. if connection.port_out_id in port_list_ids or connection.port_in_id in port_list_ids:
  589. connection_dict = connection_dict_t()
  590. connection_dict.connection_id = connection.connection_id
  591. connection_dict.group_in_id = connection.group_in_id
  592. connection_dict.port_in_id = connection.port_in_id
  593. connection_dict.group_out_id = connection.group_out_id
  594. connection_dict.port_out_id = connection.port_out_id
  595. connection_dict.widget = None
  596. conns_data.append(connection_dict)
  597. # Step 2 - Remove Item and Children
  598. for conn in conns_data:
  599. disconnectPorts(conn.connection_id)
  600. for port_id in port_list_ids:
  601. removePort(group_id, port_id)
  602. removeGroup(group_id)
  603. # Step 3 - Re-create Item, now together
  604. addGroup(group_id, group_name, SPLIT_NO, group_icon)
  605. if plugin_id >= 0:
  606. setGroupAsPlugin(group_id, plugin_id, plugin_ui)
  607. for port in ports_data:
  608. addPort(group_id, port.port_id, port.port_name, port.port_mode, port.port_type, port.is_alternate)
  609. for conn in conns_data:
  610. connectPorts(conn.connection_id, conn.group_out_id, conn.port_out_id, conn.group_in_id, conn.port_in_id)
  611. QTimer.singleShot(0, canvas.scene.update)
  612. def getGroupPos(group_id, port_mode=PORT_MODE_OUTPUT):
  613. if canvas.debug:
  614. print("PatchCanvas::getGroupPos(%i, %s)" % (group_id, port_mode2str(port_mode)))
  615. for group in canvas.group_list:
  616. if group.group_id == group_id:
  617. return group.widgets[1 if (group.split and port_mode == PORT_MODE_INPUT) else 0].pos()
  618. qCritical("PatchCanvas::getGroupPos(%i, %s) - unable to find group" % (group_id, port_mode2str(port_mode)))
  619. return QPointF(0, 0)
  620. def saveGroupPositions():
  621. if canvas.debug:
  622. print("PatchCanvas::getGroupPositions()")
  623. ret = []
  624. for group in canvas.group_list:
  625. if group.split:
  626. pos1 = group.widgets[0].pos()
  627. pos2 = group.widgets[1].pos()
  628. else:
  629. pos1 = group.widgets[0].pos()
  630. pos2 = QPointF(0, 0)
  631. ret.append({
  632. "name" : group.group_name,
  633. "pos1x": pos1.x(),
  634. "pos1y": pos1.y(),
  635. "pos2x": pos2.x(),
  636. "pos2y": pos2.y(),
  637. "split": group.split,
  638. })
  639. return ret
  640. def restoreGroupPositions(dataList):
  641. if canvas.debug:
  642. print("PatchCanvas::restoreGroupPositions(...)")
  643. map = {}
  644. for group in canvas.group_list:
  645. map[group.group_name] = group
  646. for data in dataList:
  647. name = data['name']
  648. group = map.get(name, None)
  649. if group is None:
  650. continue
  651. group.widgets[0].setPos(data['pos1x'], data['pos1y'])
  652. if group.split and group.widgets[1]:
  653. group.widgets[1].setPos(data['pos2x'], data['pos2y'])
  654. def setGroupPos(group_id, group_pos_x, group_pos_y):
  655. setGroupPosFull(group_id, group_pos_x, group_pos_y, group_pos_x, group_pos_y)
  656. def setGroupPosFull(group_id, group_pos_x_o, group_pos_y_o, group_pos_x_i, group_pos_y_i):
  657. if canvas.debug:
  658. print("PatchCanvas::setGroupPos(%i, %i, %i, %i, %i)" % (group_id, group_pos_x_o, group_pos_y_o, group_pos_x_i, group_pos_y_i))
  659. for group in canvas.group_list:
  660. if group.group_id == group_id:
  661. group.widgets[0].setPos(group_pos_x_o, group_pos_y_o)
  662. if group.split and group.widgets[1]:
  663. group.widgets[1].setPos(group_pos_x_i, group_pos_y_i)
  664. QTimer.singleShot(0, canvas.scene.update)
  665. return
  666. qCritical("PatchCanvas::setGroupPos(%i, %i, %i, %i, %i) - unable to find group to reposition" % (group_id, group_pos_x_o, group_pos_y_o, group_pos_x_i, group_pos_y_i))
  667. def setGroupIcon(group_id, icon):
  668. if canvas.debug:
  669. print("PatchCanvas::setGroupIcon(%i, %s)" % (group_id, icon2str(icon)))
  670. for group in canvas.group_list:
  671. if group.group_id == group_id:
  672. group.icon = icon
  673. group.widgets[0].setIcon(icon)
  674. if group.split and group.widgets[1]:
  675. group.widgets[1].setIcon(icon)
  676. QTimer.singleShot(0, canvas.scene.update)
  677. return
  678. qCritical("PatchCanvas::setGroupIcon(%i, %s) - unable to find group to change icon" % (group_id, icon2str(icon)))
  679. def setGroupAsPlugin(group_id, plugin_id, hasUi):
  680. if canvas.debug:
  681. print("PatchCanvas::setGroupAsPlugin(%i, %i, %s)" % (group_id, plugin_id, bool2str(hasUi)))
  682. for group in canvas.group_list:
  683. if group.group_id == group_id:
  684. group.plugin_id = plugin_id
  685. group.plugin_ui = hasUi
  686. group.widgets[0].setAsPlugin(plugin_id, hasUi)
  687. if group.split and group.widgets[1]:
  688. group.widgets[1].setAsPlugin(plugin_id, hasUi)
  689. return
  690. qCritical("PatchCanvas::setGroupAsPlugin(%i, %i, %s) - unable to find group to set as plugin" % (group_id, plugin_id, bool2str(hasUi)))
  691. def addPort(group_id, port_id, port_name, port_mode, port_type, is_alternate=False):
  692. if canvas.debug:
  693. print("PatchCanvas::addPort(%i, %i, %s, %s, %s, %s)" % (group_id, port_id, port_name.encode(), port_mode2str(port_mode), port_type2str(port_type), bool2str(is_alternate)))
  694. for port in canvas.port_list:
  695. if port.group_id == group_id and port.port_id == port_id:
  696. qWarning("PatchCanvas::addPort(%i, %i, %s, %s, %s) - port already exists" % (group_id, port_id, port_name.encode(), port_mode2str(port_mode), port_type2str(port_type)))
  697. return
  698. box_widget = None
  699. port_widget = None
  700. for group in canvas.group_list:
  701. if group.group_id == group_id:
  702. if group.split and group.widgets[0].getSplittedMode() != port_mode and group.widgets[1]:
  703. n = 1
  704. else:
  705. n = 0
  706. box_widget = group.widgets[n]
  707. port_widget = box_widget.addPortFromGroup(port_id, port_mode, port_type, port_name, is_alternate)
  708. break
  709. if not (box_widget and port_widget):
  710. qCritical("PatchCanvas::addPort(%i, %i, %s, %s, %s) - Unable to find parent group" % (group_id, port_id, port_name.encode(), port_mode2str(port_mode), port_type2str(port_type)))
  711. return
  712. port_dict = port_dict_t()
  713. port_dict.group_id = group_id
  714. port_dict.port_id = port_id
  715. port_dict.port_name = port_name
  716. port_dict.port_mode = port_mode
  717. port_dict.port_type = port_type
  718. port_dict.is_alternate = is_alternate
  719. port_dict.widget = port_widget
  720. canvas.port_list.append(port_dict)
  721. box_widget.updatePositions()
  722. if options.eyecandy == EYECANDY_FULL:
  723. return CanvasItemFX(port_widget, True, False)
  724. QTimer.singleShot(0, canvas.scene.update)
  725. def removePort(group_id, port_id):
  726. if canvas.debug:
  727. print("PatchCanvas::removePort(%i, %i)" % (group_id, port_id))
  728. for port in canvas.port_list:
  729. if port.group_id == group_id and port.port_id == port_id:
  730. item = port.widget
  731. item.parentItem().removePortFromGroup(port_id)
  732. canvas.scene.removeItem(item)
  733. canvas.port_list.remove(port)
  734. del item
  735. QTimer.singleShot(0, canvas.scene.update)
  736. return
  737. qCritical("PatchCanvas::removePort(%i, %i) - Unable to find port to remove" % (group_id, port_id))
  738. def renamePort(group_id, port_id, new_port_name):
  739. if canvas.debug:
  740. print("PatchCanvas::renamePort(%i, %i, %s)" % (group_id, port_id, new_port_name.encode()))
  741. for port in canvas.port_list:
  742. if port.group_id == group_id and port.port_id == port_id:
  743. port.port_name = new_port_name
  744. port.widget.setPortName(new_port_name)
  745. port.widget.parentItem().updatePositions()
  746. QTimer.singleShot(0, canvas.scene.update)
  747. return
  748. qCritical("PatchCanvas::renamePort(%i, %i, %s) - Unable to find port to rename" % (group_id, port_id, new_port_name.encode()))
  749. def connectPorts(connection_id, group_out_id, port_out_id, group_in_id, port_in_id):
  750. if canvas.debug:
  751. print("PatchCanvas::connectPorts(%i, %i, %i, %i, %i)" % (connection_id, group_out_id, port_out_id, group_in_id, port_in_id))
  752. port_out = None
  753. port_in = None
  754. port_out_parent = None
  755. port_in_parent = None
  756. for port in canvas.port_list:
  757. if port.group_id == group_out_id and port.port_id == port_out_id:
  758. port_out = port.widget
  759. port_out_parent = port_out.parentItem()
  760. elif port.group_id == group_in_id and port.port_id == port_in_id:
  761. port_in = port.widget
  762. port_in_parent = port_in.parentItem()
  763. # FIXME
  764. if not (port_out and port_in):
  765. qCritical("PatchCanvas::connectPorts(%i, %i, %i, %i, %i) - unable to find ports to connect" % (connection_id, group_out_id, port_out_id, group_in_id, port_in_id))
  766. return
  767. connection_dict = connection_dict_t()
  768. connection_dict.connection_id = connection_id
  769. connection_dict.group_in_id = group_in_id
  770. connection_dict.port_in_id = port_in_id
  771. connection_dict.group_out_id = group_out_id
  772. connection_dict.port_out_id = port_out_id
  773. if options.use_bezier_lines:
  774. connection_dict.widget = CanvasBezierLine(port_out, port_in, None)
  775. else:
  776. connection_dict.widget = CanvasLine(port_out, port_in, None)
  777. canvas.scene.addItem(connection_dict.widget)
  778. port_out_parent.addLineFromGroup(connection_dict.widget, connection_id)
  779. port_in_parent.addLineFromGroup(connection_dict.widget, connection_id)
  780. canvas.last_z_value += 1
  781. port_out_parent.setZValue(canvas.last_z_value)
  782. port_in_parent.setZValue(canvas.last_z_value)
  783. canvas.last_z_value += 1
  784. connection_dict.widget.setZValue(canvas.last_z_value)
  785. canvas.connection_list.append(connection_dict)
  786. if options.eyecandy == EYECANDY_FULL:
  787. item = connection_dict.widget
  788. return CanvasItemFX(item, True, False)
  789. QTimer.singleShot(0, canvas.scene.update)
  790. def disconnectPorts(connection_id):
  791. if canvas.debug:
  792. print("PatchCanvas::disconnectPorts(%i)" % connection_id)
  793. line = None
  794. item1 = None
  795. item2 = None
  796. group1id = port1id = 0
  797. group2id = port2id = 0
  798. for connection in canvas.connection_list:
  799. if connection.connection_id == connection_id:
  800. group1id = connection.group_out_id
  801. group2id = connection.group_in_id
  802. port1id = connection.port_out_id
  803. port2id = connection.port_in_id
  804. line = connection.widget
  805. canvas.connection_list.remove(connection)
  806. break
  807. if not line:
  808. qCritical("PatchCanvas::disconnectPorts(%i) - unable to find connection ports" % connection_id)
  809. return
  810. for port in canvas.port_list:
  811. if port.group_id == group1id and port.port_id == port1id:
  812. item1 = port.widget
  813. break
  814. if not item1:
  815. qCritical("PatchCanvas::disconnectPorts(%i) - unable to find output port" % connection_id)
  816. return
  817. for port in canvas.port_list:
  818. if port.group_id == group2id and port.port_id == port2id:
  819. item2 = port.widget
  820. break
  821. if not item2:
  822. qCritical("PatchCanvas::disconnectPorts(%i) - unable to find input port" % connection_id)
  823. return
  824. item1.parentItem().removeLineFromGroup(connection_id)
  825. item2.parentItem().removeLineFromGroup(connection_id)
  826. if options.eyecandy == EYECANDY_FULL:
  827. return CanvasItemFX(line, False, True)
  828. canvas.scene.removeItem(line)
  829. del line
  830. QTimer.singleShot(0, canvas.scene.update)
  831. def arrange():
  832. if canvas.debug:
  833. print("PatchCanvas::arrange()")
  834. def updateZValues():
  835. if canvas.debug:
  836. print("PatchCanvas::updateZValues()")
  837. for group in canvas.group_list:
  838. group.widgets[0].resetLinesZValue()
  839. if group.split and group.widgets[1]:
  840. group.widgets[1].resetLinesZValue()
  841. def handlePluginRemoved(plugin_id):
  842. if canvas.debug:
  843. print("PatchCanvas::handlePluginRemoved(%i)" % plugin_id)
  844. for group in canvas.group_list:
  845. if group.plugin_id < plugin_id or group.plugin_id > MAX_PLUGIN_ID_ALLOWED:
  846. continue
  847. group.plugin_id -= 1
  848. group.widgets[0].m_plugin_id -= 1
  849. if group.split and group.widgets[1]:
  850. group.widgets[1].m_plugin_id -= 1
  851. def handleAllPluginsRemoved():
  852. if canvas.debug:
  853. print("PatchCanvas::handleAllPluginsRemoved()")
  854. for group in canvas.group_list:
  855. if group.plugin_id > MAX_PLUGIN_ID_ALLOWED:
  856. continue
  857. group.plugin_id = -1
  858. group.plugin_ui = False
  859. group.widgets[0].m_plugin_id = -1
  860. group.widgets[0].m_plugin_ui = False
  861. if group.split and group.widgets[1]:
  862. group.widgets[1].m_plugin_id = -1
  863. group.widgets[1].m_plugin_ui = False
  864. # Extra Internal functions
  865. def CanvasGetNewGroupPos(horizontal):
  866. if canvas.debug:
  867. print("PatchCanvas::CanvasGetNewGroupPos(%s)" % bool2str(horizontal))
  868. new_pos = QPointF(canvas.initial_pos.x(), canvas.initial_pos.y())
  869. items = canvas.scene.items()
  870. break_loop = False
  871. while not break_loop:
  872. break_for = False
  873. for i in range(len(items)):
  874. item = items[i]
  875. if item and item.type() == CanvasBoxType:
  876. if item.sceneBoundingRect().contains(new_pos):
  877. if horizontal:
  878. new_pos += QPointF(item.boundingRect().width() + 15, 0)
  879. else:
  880. new_pos += QPointF(0, item.boundingRect().height() + 15)
  881. break_for = True
  882. break
  883. if i >= len(items) - 1 and not break_for:
  884. break_loop = True
  885. return new_pos
  886. def CanvasGetFullPortName(group_id, port_id):
  887. if canvas.debug:
  888. print("PatchCanvas::CanvasGetFullPortName(%i, %i)" % (group_id, port_id))
  889. for port in canvas.port_list:
  890. if port.group_id == group_id and port.port_id == port_id:
  891. group_id = port.group_id
  892. for group in canvas.group_list:
  893. if group.group_id == group_id:
  894. return group.group_name + ":" + port.port_name
  895. break
  896. qCritical("PatchCanvas::CanvasGetFullPortName(%i, %i) - unable to find port" % (group_id, port_id))
  897. return ""
  898. def CanvasGetPortConnectionList(group_id, port_id):
  899. if canvas.debug:
  900. print("PatchCanvas::CanvasGetPortConnectionList(%i, %i)" % (group_id, port_id))
  901. conn_list = []
  902. for connection in canvas.connection_list:
  903. if connection.group_out_id == group_id and connection.port_out_id == port_id:
  904. conn_list.append((connection.connection_id, connection.group_in_id, connection.port_in_id))
  905. elif connection.group_in_id == group_id and connection.port_in_id == port_id:
  906. conn_list.append((connection.connection_id, connection.group_out_id, connection.port_out_id))
  907. return conn_list
  908. def CanvasCallback(action, value1, value2, value_str):
  909. if canvas.debug:
  910. print("PatchCanvas::CanvasCallback(%i, %i, %i, %s)" % (action, value1, value2, value_str.encode()))
  911. canvas.callback(action, value1, value2, value_str)
  912. def CanvasItemFX(item, show, destroy):
  913. if canvas.debug:
  914. print("PatchCanvas::CanvasItemFX(%s, %s, %s)" % (item, bool2str(show), bool2str(destroy)))
  915. # Check if the item already has an animation
  916. for animation in canvas.animation_list:
  917. if animation.item() == item:
  918. animation.forceStop()
  919. canvas.animation_list.remove(animation)
  920. del animation
  921. break
  922. animation = CanvasFadeAnimation(item, show)
  923. animation.setDuration(750 if show else 500)
  924. if show:
  925. animation.finished.connect(canvas.qobject.AnimationFinishedShow)
  926. else:
  927. if destroy:
  928. animation.finished.connect(canvas.qobject.AnimationFinishedDestroy)
  929. else:
  930. animation.finished.connect(canvas.qobject.AnimationFinishedHide)
  931. canvas.animation_list.append(animation)
  932. animation.start()
  933. def CanvasRemoveItemFX(item):
  934. if canvas.debug:
  935. print("PatchCanvas::CanvasRemoveItemFX(%s)" % item)
  936. if item.type() == CanvasBoxType:
  937. item.removeIconFromScene()
  938. canvas.scene.removeItem(item)
  939. del item
  940. QTimer.singleShot(0, canvas.scene.update)
  941. # ------------------------------------------------------------------------------
  942. # patchscene.cpp
  943. class PatchScene(QGraphicsScene):
  944. scaleChanged = pyqtSignal(float)
  945. sceneGroupMoved = pyqtSignal(int, int, QPointF)
  946. pluginSelected = pyqtSignal(list)
  947. def __init__(self, parent, view):
  948. QGraphicsScene.__init__(self, parent)
  949. self.m_ctrl_down = False
  950. self.m_mouse_down_init = False
  951. self.m_mouse_rubberband = False
  952. self.m_mid_button_down = False
  953. self.m_pointer_border = QRectF(0.0, 0.0, 1.0, 1.0)
  954. self.addRubberBand()
  955. self.m_view = view
  956. if not self.m_view:
  957. qFatal("PatchCanvas::PatchScene() - invalid view")
  958. self.selectionChanged.connect(self.slot_selectionChanged)
  959. def addRubberBand(self):
  960. self.m_rubberband = self.addRect(QRectF(0, 0, 0, 0))
  961. self.m_rubberband.setZValue(-1)
  962. self.m_rubberband.hide()
  963. self.m_rubberband_selection = False
  964. self.m_rubberband_orig_point = QPointF(0, 0)
  965. def clear(self):
  966. QGraphicsScene.clear(self)
  967. # Re-add rubberband, that just got deleted
  968. self.addRubberBand()
  969. def fixScaleFactor(self):
  970. scale = self.m_view.transform().m11()
  971. if scale > 3.0:
  972. self.m_view.resetTransform()
  973. self.m_view.scale(3.0, 3.0)
  974. elif scale < 0.2:
  975. self.m_view.resetTransform()
  976. self.m_view.scale(0.2, 0.2)
  977. self.scaleChanged.emit(self.m_view.transform().m11())
  978. def updateTheme(self):
  979. self.setBackgroundBrush(canvas.theme.canvas_bg)
  980. self.m_rubberband.setPen(canvas.theme.rubberband_pen)
  981. self.m_rubberband.setBrush(canvas.theme.rubberband_brush)
  982. def zoom_fit(self):
  983. min_x = min_y = max_x = max_y = None
  984. first_value = True
  985. items_list = self.items()
  986. if len(items_list) > 0:
  987. for item in items_list:
  988. if item and item.isVisible() and item.type() == CanvasBoxType:
  989. pos = item.scenePos()
  990. rect = item.boundingRect()
  991. if first_value:
  992. min_x = pos.x()
  993. min_y = pos.y()
  994. max_x = pos.x() + rect.width()
  995. max_y = pos.y() + rect.height()
  996. else:
  997. min_x = min( min_x, pos.x() )
  998. min_y = min( min_y, pos.y() )
  999. max_x = max( max_x, pos.x() + rect.width() )
  1000. max_y = max( max_y, pos.y() + rect.height() )
  1001. first_value = False
  1002. if not first_value:
  1003. self.m_view.fitInView(min_x, min_y, abs(max_x - min_x), abs(max_y - min_y), Qt.KeepAspectRatio)
  1004. self.fixScaleFactor()
  1005. def zoom_in(self):
  1006. if self.m_view.transform().m11() < 3.0:
  1007. self.m_view.scale(1.2, 1.2)
  1008. self.scaleChanged.emit(self.m_view.transform().m11())
  1009. def zoom_out(self):
  1010. if self.m_view.transform().m11() > 0.2:
  1011. self.m_view.scale(0.8, 0.8)
  1012. self.scaleChanged.emit(self.m_view.transform().m11())
  1013. def zoom_reset(self):
  1014. self.m_view.resetTransform()
  1015. self.scaleChanged.emit(1.0)
  1016. @pyqtSlot()
  1017. def slot_selectionChanged(self):
  1018. items_list = self.selectedItems()
  1019. if len(items_list) == 0:
  1020. self.pluginSelected.emit([])
  1021. return
  1022. plugin_list = []
  1023. for item in items_list:
  1024. if item and item.isVisible():
  1025. group_item = None
  1026. if item.type() == CanvasBoxType:
  1027. group_item = item
  1028. elif item.type() == CanvasPortType:
  1029. group_item = item.parentItem()
  1030. #elif item.type() in (CanvasLineType, CanvasBezierLineType, CanvasLineMovType, CanvasBezierLineMovType):
  1031. #plugin_list = []
  1032. #break
  1033. if group_item is not None and group_item.m_plugin_id >= 0:
  1034. plugin_id = group_item.m_plugin_id
  1035. if plugin_id > MAX_PLUGIN_ID_ALLOWED:
  1036. plugin_id = 0
  1037. plugin_list.append(plugin_id)
  1038. self.pluginSelected.emit(plugin_list)
  1039. def keyPressEvent(self, event):
  1040. if not self.m_view:
  1041. return event.ignore()
  1042. if event.key() == Qt.Key_Control:
  1043. self.m_ctrl_down = True
  1044. elif event.key() == Qt.Key_Home:
  1045. self.zoom_fit()
  1046. return event.accept()
  1047. elif self.m_ctrl_down:
  1048. if event.key() == Qt.Key_Plus:
  1049. self.zoom_in()
  1050. return event.accept()
  1051. elif event.key() == Qt.Key_Minus:
  1052. self.zoom_out()
  1053. return event.accept()
  1054. elif event.key() == Qt.Key_1:
  1055. self.zoom_reset()
  1056. return event.accept()
  1057. QGraphicsScene.keyPressEvent(self, event)
  1058. def keyReleaseEvent(self, event):
  1059. if event.key() == Qt.Key_Control:
  1060. self.m_ctrl_down = False
  1061. QGraphicsScene.keyReleaseEvent(self, event)
  1062. def mousePressEvent(self, event):
  1063. self.m_mouse_down_init = bool(event.button() == Qt.LeftButton)
  1064. self.m_mouse_rubberband = False
  1065. if event.button() == Qt.MidButton and self.m_ctrl_down:
  1066. self.m_mid_button_down = True
  1067. pos = event.scenePos()
  1068. self.m_pointer_border.moveTo(floor(pos.x()), floor(pos.y()))
  1069. items = self.items(self.m_pointer_border)
  1070. for item in items:
  1071. if item and item.type() in [CanvasLineType, CanvasBezierLineType]:
  1072. item.triggerDisconnect()
  1073. QGraphicsScene.mousePressEvent(self, event)
  1074. def mouseMoveEvent(self, event):
  1075. if self.m_mouse_down_init:
  1076. self.m_mouse_down_init = False
  1077. self.m_mouse_rubberband = bool(len(self.selectedItems()) == 0)
  1078. if self.m_mouse_rubberband:
  1079. if not self.m_rubberband_selection:
  1080. self.m_rubberband.show()
  1081. self.m_rubberband_selection = True
  1082. self.m_rubberband_orig_point = event.scenePos()
  1083. pos = event.scenePos()
  1084. x = min( pos.x(), self.m_rubberband_orig_point.x() )
  1085. y = min( pos.y(), self.m_rubberband_orig_point.y() )
  1086. lineHinting = canvas.theme.rubberband_pen.widthF() / 2
  1087. self.m_rubberband.setRect( x+lineHinting, y+lineHinting,
  1088. abs(pos.x() - self.m_rubberband_orig_point.x()),
  1089. abs(pos.y() - self.m_rubberband_orig_point.y()) )
  1090. return event.accept()
  1091. if self.m_mid_button_down and self.m_ctrl_down:
  1092. trail = QPolygonF([event.scenePos(), event.lastScenePos(), event.scenePos()])
  1093. items = self.items(trail)
  1094. for item in items:
  1095. if item and item.type() in [CanvasLineType, CanvasBezierLineType]:
  1096. item.triggerDisconnect()
  1097. QGraphicsScene.mouseMoveEvent(self, event)
  1098. def mouseReleaseEvent(self, event):
  1099. if event.button() == Qt.MidButton:
  1100. self.m_mid_button_down = False
  1101. return event.accept()
  1102. if self.m_rubberband_selection:
  1103. items_list = self.items()
  1104. if len(items_list) > 0:
  1105. for item in items_list:
  1106. if item and item.isVisible() and item.type() == CanvasBoxType:
  1107. item_rect = item.sceneBoundingRect()
  1108. item_top_left = QPointF(item_rect.x(), item_rect.y())
  1109. item_bottom_right = QPointF(item_rect.x() + item_rect.width(), item_rect.y() + item_rect.height())
  1110. if self.m_rubberband.contains(item_top_left) and self.m_rubberband.contains(item_bottom_right):
  1111. item.setSelected(True)
  1112. self.m_rubberband.hide()
  1113. self.m_rubberband.setRect(0, 0, 0, 0)
  1114. self.m_rubberband_selection = False
  1115. else:
  1116. items_list = self.selectedItems()
  1117. for item in items_list:
  1118. if item and item.isVisible() and item.type() == CanvasBoxType:
  1119. item.checkItemPos()
  1120. self.sceneGroupMoved.emit(item.getGroupId(), item.getSplittedMode(), item.scenePos())
  1121. if len(items_list) > 1:
  1122. canvas.scene.update()
  1123. self.m_mouse_down_init = False
  1124. self.m_mouse_rubberband = False
  1125. QGraphicsScene.mouseReleaseEvent(self, event)
  1126. def wheelEvent(self, event):
  1127. if not self.m_view:
  1128. return event.ignore()
  1129. if self.m_ctrl_down:
  1130. factor = 1.41 ** (event.delta() / 240.0)
  1131. self.m_view.scale(factor, factor)
  1132. self.fixScaleFactor()
  1133. return event.accept()
  1134. QGraphicsScene.wheelEvent(self, event)
  1135. def contextMenuEvent(self, event):
  1136. if len(self.selectedItems()) == 0:
  1137. event.accept()
  1138. canvas.callback(ACTION_BG_RIGHT_CLICK, 0, 0, "")
  1139. else:
  1140. QGraphicsScene.contextMenuEvent(self, event)
  1141. # ------------------------------------------------------------------------------
  1142. # canvasfadeanimation.cpp
  1143. class CanvasFadeAnimation(QAbstractAnimation):
  1144. def __init__(self, item, show):
  1145. QAbstractAnimation.__init__(self)
  1146. self.m_show = show
  1147. self.m_duration = 0
  1148. self.m_item = item
  1149. def item(self):
  1150. return self.m_item
  1151. def forceStop(self):
  1152. self.blockSignals(True)
  1153. self.stop()
  1154. def setDuration(self, time):
  1155. if self.m_item.opacity() == 0 and not self.m_show:
  1156. self._duration = 0
  1157. else:
  1158. self.m_item.show()
  1159. self.m_duration = time
  1160. def duration(self):
  1161. return self.m_duration
  1162. def updateCurrentTime(self, time):
  1163. if self.m_duration == 0:
  1164. return
  1165. if self.m_show:
  1166. value = float(time) / self.m_duration
  1167. else:
  1168. value = 1.0 - (float(time) / self.m_duration)
  1169. try:
  1170. self.m_item.setOpacity(value)
  1171. except:
  1172. print("Error: failed to animate canvas item, already destroyed?")
  1173. self.forceStop()
  1174. canvas.animation_list.remove(self)
  1175. return
  1176. if self.m_item.type() == CanvasBoxType:
  1177. self.m_item.setShadowOpacity(value)
  1178. def updateDirection(self, direction):
  1179. pass
  1180. def updateState(self, oldState, newState):
  1181. pass
  1182. # ------------------------------------------------------------------------------
  1183. # canvasline.cpp
  1184. class CanvasLine(QGraphicsLineItem):
  1185. def __init__(self, item1, item2, parent):
  1186. QGraphicsLineItem.__init__(self, parent)
  1187. self.item1 = item1
  1188. self.item2 = item2
  1189. self.m_locked = False
  1190. self.m_lineSelected = False
  1191. self.setGraphicsEffect(None)
  1192. self.updateLinePos()
  1193. def isLocked(self):
  1194. return self.m_locked
  1195. def setLocked(self, yesno):
  1196. self.m_locked = yesno
  1197. def isLineSelected(self):
  1198. return self.m_lineSelected
  1199. def setLineSelected(self, yesno):
  1200. if self.m_locked:
  1201. return
  1202. if options.eyecandy == EYECANDY_FULL:
  1203. if yesno:
  1204. self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
  1205. else:
  1206. self.setGraphicsEffect(None)
  1207. self.m_lineSelected = yesno
  1208. self.updateLineGradient()
  1209. def triggerDisconnect(self):
  1210. for connection in canvas.connection_list:
  1211. if (connection.port_out_id == self.item1.getPortId() and connection.port_in_id == self.item2.getPortId()):
  1212. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  1213. break
  1214. def updateLinePos(self):
  1215. if self.item1.getPortMode() == PORT_MODE_OUTPUT:
  1216. rect1 = self.item1.sceneBoundingRect()
  1217. rect2 = self.item2.sceneBoundingRect()
  1218. line = QLineF(rect1.right(),
  1219. rect1.top() + float(canvas.theme.port_height)/2,
  1220. rect2.left(),
  1221. rect2.top() + float(canvas.theme.port_height)/2)
  1222. self.setLine(line)
  1223. self.m_lineSelected = False
  1224. self.updateLineGradient()
  1225. def type(self):
  1226. return CanvasLineType
  1227. def updateLineGradient(self):
  1228. pos_top = self.boundingRect().top()
  1229. pos_bot = self.boundingRect().bottom()
  1230. if self.item2.scenePos().y() >= self.item1.scenePos().y():
  1231. pos1 = 0
  1232. pos2 = 1
  1233. else:
  1234. pos1 = 1
  1235. pos2 = 0
  1236. port_type1 = self.item1.getPortType()
  1237. port_type2 = self.item2.getPortType()
  1238. port_gradient = QLinearGradient(0, pos_top, 0, pos_bot)
  1239. if port_type1 == PORT_TYPE_AUDIO_JACK:
  1240. port_gradient.setColorAt(pos1, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1241. elif port_type1 == PORT_TYPE_MIDI_JACK:
  1242. port_gradient.setColorAt(pos1, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1243. elif port_type1 == PORT_TYPE_MIDI_ALSA:
  1244. port_gradient.setColorAt(pos1, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1245. elif port_type1 == PORT_TYPE_PARAMETER:
  1246. port_gradient.setColorAt(pos1, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1247. if port_type2 == PORT_TYPE_AUDIO_JACK:
  1248. port_gradient.setColorAt(pos2, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1249. elif port_type2 == PORT_TYPE_MIDI_JACK:
  1250. port_gradient.setColorAt(pos2, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1251. elif port_type2 == PORT_TYPE_MIDI_ALSA:
  1252. port_gradient.setColorAt(pos2, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1253. elif port_type2 == PORT_TYPE_PARAMETER:
  1254. port_gradient.setColorAt(pos2, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1255. self.setPen(QPen(port_gradient, 2))
  1256. def paint(self, painter, option, widget):
  1257. painter.save()
  1258. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1259. QGraphicsLineItem.paint(self, painter, option, widget)
  1260. painter.restore()
  1261. # ------------------------------------------------------------------------------
  1262. # canvasbezierline.cpp
  1263. class CanvasBezierLine(QGraphicsPathItem):
  1264. def __init__(self, item1, item2, parent):
  1265. QGraphicsPathItem.__init__(self, parent)
  1266. self.item1 = item1
  1267. self.item2 = item2
  1268. self.m_locked = False
  1269. self.m_lineSelected = False
  1270. self.setBrush(QColor(0, 0, 0, 0))
  1271. self.setGraphicsEffect(None)
  1272. self.updateLinePos()
  1273. def isLocked(self):
  1274. return self.m_locked
  1275. def setLocked(self, yesno):
  1276. self.m_locked = yesno
  1277. def isLineSelected(self):
  1278. return self.m_lineSelected
  1279. def setLineSelected(self, yesno):
  1280. if self.m_locked:
  1281. return
  1282. if options.eyecandy == EYECANDY_FULL:
  1283. if yesno:
  1284. self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
  1285. else:
  1286. self.setGraphicsEffect(None)
  1287. self.m_lineSelected = yesno
  1288. self.updateLineGradient()
  1289. def triggerDisconnect(self):
  1290. for connection in canvas.connection_list:
  1291. if (connection.port_out_id == self.item1.getPortId() and connection.port_in_id == self.item2.getPortId()):
  1292. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  1293. break
  1294. def updateLinePos(self):
  1295. if self.item1.getPortMode() == PORT_MODE_OUTPUT:
  1296. rect1 = self.item1.sceneBoundingRect()
  1297. rect2 = self.item2.sceneBoundingRect()
  1298. item1_x = rect1.right()
  1299. item2_x = rect2.left()
  1300. item1_y = rect1.top() + float(canvas.theme.port_height)/2
  1301. item2_y = rect2.top() + float(canvas.theme.port_height)/2
  1302. item1_new_x = item1_x + abs(item1_x - item2_x) / 2
  1303. item2_new_x = item2_x - abs(item1_x - item2_x) / 2
  1304. path = QPainterPath(QPointF(item1_x, item1_y))
  1305. path.cubicTo(item1_new_x, item1_y, item2_new_x, item2_y, item2_x, item2_y)
  1306. self.setPath(path)
  1307. self.m_lineSelected = False
  1308. self.updateLineGradient()
  1309. def type(self):
  1310. return CanvasBezierLineType
  1311. def updateLineGradient(self):
  1312. pos_top = self.boundingRect().top()
  1313. pos_bot = self.boundingRect().bottom()
  1314. if self.item2.scenePos().y() >= self.item1.scenePos().y():
  1315. pos1 = 0
  1316. pos2 = 1
  1317. else:
  1318. pos1 = 1
  1319. pos2 = 0
  1320. port_type1 = self.item1.getPortType()
  1321. port_type2 = self.item2.getPortType()
  1322. port_gradient = QLinearGradient(0, pos_top, 0, pos_bot)
  1323. if port_type1 == PORT_TYPE_AUDIO_JACK:
  1324. port_gradient.setColorAt(pos1, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1325. elif port_type1 == PORT_TYPE_MIDI_JACK:
  1326. port_gradient.setColorAt(pos1, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1327. elif port_type1 == PORT_TYPE_MIDI_ALSA:
  1328. port_gradient.setColorAt(pos1, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1329. elif port_type1 == PORT_TYPE_PARAMETER:
  1330. port_gradient.setColorAt(pos1, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1331. if port_type2 == PORT_TYPE_AUDIO_JACK:
  1332. port_gradient.setColorAt(pos2, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1333. elif port_type2 == PORT_TYPE_MIDI_JACK:
  1334. port_gradient.setColorAt(pos2, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1335. elif port_type2 == PORT_TYPE_MIDI_ALSA:
  1336. port_gradient.setColorAt(pos2, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1337. elif port_type2 == PORT_TYPE_PARAMETER:
  1338. port_gradient.setColorAt(pos2, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1339. self.setPen(QPen(port_gradient, 2))
  1340. def paint(self, painter, option, widget):
  1341. painter.save()
  1342. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1343. QGraphicsPathItem.paint(self, painter, option, widget)
  1344. painter.restore()
  1345. # ------------------------------------------------------------------------------
  1346. # canvaslivemov.cpp
  1347. class CanvasLineMov(QGraphicsLineItem):
  1348. def __init__(self, port_mode, port_type, parent):
  1349. QGraphicsLineItem.__init__(self, parent)
  1350. self.m_port_mode = port_mode
  1351. self.m_port_type = port_type
  1352. # Port position doesn't change while moving around line
  1353. self.p_lineX = self.scenePos().x()
  1354. self.p_lineY = self.scenePos().y()
  1355. self.p_width = self.parentItem().getPortWidth()
  1356. if port_type == PORT_TYPE_AUDIO_JACK:
  1357. pen = QPen(canvas.theme.line_audio_jack, 2)
  1358. elif port_type == PORT_TYPE_MIDI_JACK:
  1359. pen = QPen(canvas.theme.line_midi_jack, 2)
  1360. elif port_type == PORT_TYPE_MIDI_ALSA:
  1361. pen = QPen(canvas.theme.line_midi_alsa, 2)
  1362. elif port_type == PORT_TYPE_PARAMETER:
  1363. pen = QPen(canvas.theme.line_parameter, 2)
  1364. else:
  1365. qWarning("PatchCanvas::CanvasLineMov(%s, %s, %s) - invalid port type" % (port_mode2str(port_mode), port_type2str(port_type), parent))
  1366. pen = QPen(Qt.black)
  1367. self.setPen(pen)
  1368. def updateLinePos(self, scenePos):
  1369. item_pos = [0, 0]
  1370. if self.m_port_mode == PORT_MODE_INPUT:
  1371. item_pos[0] = 0
  1372. item_pos[1] = float(canvas.theme.port_height)/2
  1373. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1374. item_pos[0] = self.p_width + 12
  1375. item_pos[1] = float(canvas.theme.port_height)/2
  1376. else:
  1377. return
  1378. line = QLineF(item_pos[0], item_pos[1], scenePos.x() - self.p_lineX, scenePos.y() - self.p_lineY)
  1379. self.setLine(line)
  1380. def type(self):
  1381. return CanvasLineMovType
  1382. def paint(self, painter, option, widget):
  1383. painter.save()
  1384. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1385. QGraphicsLineItem.paint(self, painter, option, widget)
  1386. painter.restore()
  1387. # ------------------------------------------------------------------------------
  1388. # canvasbezierlinemov.cpp
  1389. class CanvasBezierLineMov(QGraphicsPathItem):
  1390. def __init__(self, port_mode, port_type, parent):
  1391. QGraphicsPathItem.__init__(self, parent)
  1392. self.m_port_mode = port_mode
  1393. self.m_port_type = port_type
  1394. # Port position doesn't change while moving around line
  1395. self.p_itemX = self.scenePos().x()
  1396. self.p_itemY = self.scenePos().y()
  1397. self.p_width = self.parentItem().getPortWidth()
  1398. if port_type == PORT_TYPE_AUDIO_JACK:
  1399. pen = QPen(canvas.theme.line_audio_jack, 2)
  1400. elif port_type == PORT_TYPE_MIDI_JACK:
  1401. pen = QPen(canvas.theme.line_midi_jack, 2)
  1402. elif port_type == PORT_TYPE_MIDI_ALSA:
  1403. pen = QPen(canvas.theme.line_midi_alsa, 2)
  1404. elif port_type == PORT_TYPE_PARAMETER:
  1405. pen = QPen(canvas.theme.line_parameter, 2)
  1406. else:
  1407. qWarning("PatchCanvas::CanvasBezierLineMov(%s, %s, %s) - invalid port type" % (port_mode2str(port_mode), port_type2str(port_type), parent))
  1408. pen = QPen(Qt.black)
  1409. self.setBrush(QColor(0, 0, 0, 0))
  1410. self.setPen(pen)
  1411. def updateLinePos(self, scenePos):
  1412. if self.m_port_mode == PORT_MODE_INPUT:
  1413. old_x = 0
  1414. old_y = float(canvas.theme.port_height)/2
  1415. mid_x = abs(scenePos.x() - self.p_itemX) / 2
  1416. new_x = old_x - mid_x
  1417. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1418. old_x = self.p_width + 12
  1419. old_y = float(canvas.theme.port_height)/2
  1420. mid_x = abs(scenePos.x() - (self.p_itemX + old_x)) / 2
  1421. new_x = old_x + mid_x
  1422. else:
  1423. return
  1424. final_x = scenePos.x() - self.p_itemX
  1425. final_y = scenePos.y() - self.p_itemY
  1426. path = QPainterPath(QPointF(old_x, old_y))
  1427. path.cubicTo(new_x, old_y, new_x, final_y, final_x, final_y)
  1428. self.setPath(path)
  1429. def type(self):
  1430. return CanvasBezierLineMovType
  1431. def paint(self, painter, option, widget):
  1432. painter.save()
  1433. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1434. QGraphicsPathItem.paint(self, painter, option, widget)
  1435. painter.restore()
  1436. # ------------------------------------------------------------------------------
  1437. # canvasport.cpp
  1438. class CanvasPort(QGraphicsItem):
  1439. def __init__(self, group_id, port_id, port_name, port_mode, port_type, is_alternate, parent):
  1440. QGraphicsItem.__init__(self, parent)
  1441. # Save Variables, useful for later
  1442. self.m_group_id = group_id
  1443. self.m_port_id = port_id
  1444. self.m_port_mode = port_mode
  1445. self.m_port_type = port_type
  1446. self.m_port_name = port_name
  1447. self.m_is_alternate = is_alternate
  1448. # Base Variables
  1449. self.m_port_width = 15
  1450. self.m_port_height = canvas.theme.port_height
  1451. self.m_port_font = QFont()
  1452. self.m_port_font.setFamily(canvas.theme.port_font_name)
  1453. self.m_port_font.setPixelSize(canvas.theme.port_font_size)
  1454. self.m_port_font.setWeight(canvas.theme.port_font_state)
  1455. self.m_line_mov = None
  1456. self.m_hover_item = None
  1457. self.m_last_selected_state = False
  1458. self.m_mouse_down = False
  1459. self.m_cursor_moving = False
  1460. self.setFlags(QGraphicsItem.ItemIsSelectable)
  1461. if options.auto_select_items:
  1462. self.setAcceptHoverEvents(True)
  1463. def getGroupId(self):
  1464. return self.m_group_id
  1465. def getPortId(self):
  1466. return self.m_port_id
  1467. def getPortMode(self):
  1468. return self.m_port_mode
  1469. def getPortType(self):
  1470. return self.m_port_type
  1471. def getPortName(self):
  1472. return self.m_port_name
  1473. def getFullPortName(self):
  1474. return self.parentItem().getGroupName() + ":" + self.m_port_name
  1475. def getPortWidth(self):
  1476. return self.m_port_width
  1477. def getPortHeight(self):
  1478. return self.m_port_height
  1479. def setPortMode(self, port_mode):
  1480. self.m_port_mode = port_mode
  1481. self.update()
  1482. def setPortType(self, port_type):
  1483. self.m_port_type = port_type
  1484. self.update()
  1485. def setPortName(self, port_name):
  1486. if QFontMetrics(self.m_port_font).width(port_name) < QFontMetrics(self.m_port_font).width(self.m_port_name):
  1487. QTimer.singleShot(0, canvas.scene.update)
  1488. self.m_port_name = port_name
  1489. self.update()
  1490. def setPortWidth(self, port_width):
  1491. if port_width < self.m_port_width:
  1492. QTimer.singleShot(0, canvas.scene.update)
  1493. self.m_port_width = port_width
  1494. self.update()
  1495. def type(self):
  1496. return CanvasPortType
  1497. def hoverEnterEvent(self, event):
  1498. if options.auto_select_items:
  1499. self.setSelected(True)
  1500. QGraphicsItem.hoverEnterEvent(self, event)
  1501. def hoverLeaveEvent(self, event):
  1502. if options.auto_select_items:
  1503. self.setSelected(False)
  1504. QGraphicsItem.hoverLeaveEvent(self, event)
  1505. def mousePressEvent(self, event):
  1506. self.m_hover_item = None
  1507. self.m_mouse_down = bool(event.button() == Qt.LeftButton)
  1508. self.m_cursor_moving = False
  1509. QGraphicsItem.mousePressEvent(self, event)
  1510. def mouseMoveEvent(self, event):
  1511. if self.m_mouse_down:
  1512. if not self.m_cursor_moving:
  1513. self.setCursor(QCursor(Qt.CrossCursor))
  1514. self.m_cursor_moving = True
  1515. for connection in canvas.connection_list:
  1516. if ((connection.group_out_id == self.m_group_id and connection.port_out_id == self.m_port_id) or
  1517. (connection.group_in_id == self.m_group_id and connection.port_in_id == self.m_port_id)):
  1518. connection.widget.setLocked(True)
  1519. if not self.m_line_mov:
  1520. if options.use_bezier_lines:
  1521. self.m_line_mov = CanvasBezierLineMov(self.m_port_mode, self.m_port_type, self)
  1522. else:
  1523. self.m_line_mov = CanvasLineMov(self.m_port_mode, self.m_port_type, self)
  1524. canvas.last_z_value += 1
  1525. self.m_line_mov.setZValue(canvas.last_z_value)
  1526. canvas.last_z_value += 1
  1527. self.parentItem().setZValue(canvas.last_z_value)
  1528. item = None
  1529. items = canvas.scene.items(event.scenePos(), Qt.ContainsItemShape, Qt.AscendingOrder)
  1530. for i in range(len(items)):
  1531. if items[i].type() == CanvasPortType:
  1532. if items[i] != self:
  1533. if not item:
  1534. item = items[i]
  1535. elif items[i].parentItem().zValue() > item.parentItem().zValue():
  1536. item = items[i]
  1537. if self.m_hover_item and self.m_hover_item != item:
  1538. self.m_hover_item.setSelected(False)
  1539. if item:
  1540. if item.getPortMode() != self.m_port_mode and item.getPortType() == self.m_port_type:
  1541. item.setSelected(True)
  1542. self.m_hover_item = item
  1543. else:
  1544. self.m_hover_item = None
  1545. else:
  1546. self.m_hover_item = None
  1547. self.m_line_mov.updateLinePos(event.scenePos())
  1548. return event.accept()
  1549. QGraphicsItem.mouseMoveEvent(self, event)
  1550. def mouseReleaseEvent(self, event):
  1551. if self.m_mouse_down:
  1552. if self.m_line_mov is not None:
  1553. item = self.m_line_mov
  1554. self.m_line_mov = None
  1555. canvas.scene.removeItem(item)
  1556. del item
  1557. for connection in canvas.connection_list:
  1558. if ((connection.group_out_id == self.m_group_id and connection.port_out_id == self.m_port_id) or
  1559. (connection.group_in_id == self.m_group_id and connection.port_in_id == self.m_port_id)):
  1560. connection.widget.setLocked(False)
  1561. if self.m_hover_item:
  1562. # TODO: a better way to check already existing connection
  1563. for connection in canvas.connection_list:
  1564. hover_group_id = self.m_hover_item.getGroupId()
  1565. hover_port_id = self.m_hover_item.getPortId()
  1566. if (
  1567. (connection.group_out_id == self.m_group_id and connection.port_out_id == self.m_port_id and
  1568. connection.group_in_id == hover_group_id and connection.port_in_id == hover_port_id) or
  1569. (connection.group_out_id == hover_group_id and connection.port_out_id == hover_port_id and
  1570. connection.group_in_id == self.m_group_id and connection.port_in_id == self.m_port_id)
  1571. ):
  1572. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  1573. break
  1574. else:
  1575. if self.m_port_mode == PORT_MODE_OUTPUT:
  1576. conn = "%i:%i:%i:%i" % (self.m_group_id, self.m_port_id, self.m_hover_item.getGroupId(), self.m_hover_item.getPortId())
  1577. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  1578. else:
  1579. conn = "%i:%i:%i:%i" % (self.m_hover_item.getGroupId(), self.m_hover_item.getPortId(), self.m_group_id, self.m_port_id)
  1580. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  1581. canvas.scene.clearSelection()
  1582. if self.m_cursor_moving:
  1583. self.setCursor(QCursor(Qt.ArrowCursor))
  1584. self.m_hover_item = None
  1585. self.m_mouse_down = False
  1586. self.m_cursor_moving = False
  1587. QGraphicsItem.mouseReleaseEvent(self, event)
  1588. def contextMenuEvent(self, event):
  1589. canvas.scene.clearSelection()
  1590. self.setSelected(True)
  1591. menu = QMenu()
  1592. discMenu = QMenu("Disconnect", menu)
  1593. conn_list = CanvasGetPortConnectionList(self.m_group_id, self.m_port_id)
  1594. if len(conn_list) > 0:
  1595. for conn_id, group_id, port_id in conn_list:
  1596. act_x_disc = discMenu.addAction(CanvasGetFullPortName(group_id, port_id))
  1597. act_x_disc.setData(conn_id)
  1598. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  1599. else:
  1600. act_x_disc = discMenu.addAction("No connections")
  1601. act_x_disc.setEnabled(False)
  1602. menu.addMenu(discMenu)
  1603. act_x_disc_all = menu.addAction("Disconnect &All")
  1604. act_x_sep_1 = menu.addSeparator()
  1605. act_x_info = menu.addAction("Get &Info")
  1606. act_x_rename = menu.addAction("&Rename")
  1607. if not features.port_info:
  1608. act_x_info.setVisible(False)
  1609. if not features.port_rename:
  1610. act_x_rename.setVisible(False)
  1611. if not (features.port_info and features.port_rename):
  1612. act_x_sep_1.setVisible(False)
  1613. act_selected = menu.exec_(event.screenPos())
  1614. if act_selected == act_x_disc_all:
  1615. for conn_id, group_id, port_id in conn_list:
  1616. canvas.callback(ACTION_PORTS_DISCONNECT, conn_id, 0, "")
  1617. elif act_selected == act_x_info:
  1618. canvas.callback(ACTION_PORT_INFO, self.m_group_id, self.m_port_id, "")
  1619. elif act_selected == act_x_rename:
  1620. canvas.callback(ACTION_PORT_RENAME, self.m_group_id, self.m_port_id, "")
  1621. event.accept()
  1622. def boundingRect(self):
  1623. return QRectF(0, 0, self.m_port_width + 12, self.m_port_height)
  1624. def paint(self, painter, option, widget):
  1625. painter.save()
  1626. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  1627. # FIXME: would be more correct to take line width from Pen, loaded to painter,
  1628. # but this needs some code rearrangement
  1629. lineHinting = canvas.theme.port_audio_jack_pen.widthF() / 2
  1630. poly_locx = [0, 0, 0, 0, 0]
  1631. poly_corner_xhinting = (float(canvas.theme.port_height)/2) % floor(float(canvas.theme.port_height)/2)
  1632. if poly_corner_xhinting == 0:
  1633. poly_corner_xhinting = 0.5 * (1 - 7 / (float(canvas.theme.port_height)/2))
  1634. if self.m_port_mode == PORT_MODE_INPUT:
  1635. text_pos = QPointF(3, canvas.theme.port_text_ypos)
  1636. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  1637. poly_locx[0] = lineHinting
  1638. poly_locx[1] = self.m_port_width + 5 - lineHinting
  1639. poly_locx[2] = self.m_port_width + 12 - poly_corner_xhinting
  1640. poly_locx[3] = self.m_port_width + 5 - lineHinting
  1641. poly_locx[4] = lineHinting
  1642. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  1643. poly_locx[0] = lineHinting
  1644. poly_locx[1] = self.m_port_width + 5 - lineHinting
  1645. poly_locx[2] = self.m_port_width + 5 - lineHinting
  1646. poly_locx[3] = self.m_port_width + 5 - lineHinting
  1647. poly_locx[4] = lineHinting
  1648. else:
  1649. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  1650. return
  1651. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1652. text_pos = QPointF(9, canvas.theme.port_text_ypos)
  1653. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  1654. poly_locx[0] = self.m_port_width + 12 - lineHinting
  1655. poly_locx[1] = 7 + lineHinting
  1656. poly_locx[2] = 0 + poly_corner_xhinting
  1657. poly_locx[3] = 7 + lineHinting
  1658. poly_locx[4] = self.m_port_width + 12 - lineHinting
  1659. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  1660. poly_locx[0] = self.m_port_width + 12 - lineHinting
  1661. poly_locx[1] = 5 + lineHinting
  1662. poly_locx[2] = 5 + lineHinting
  1663. poly_locx[3] = 5 + lineHinting
  1664. poly_locx[4] = self.m_port_width + 12 - lineHinting
  1665. else:
  1666. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  1667. return
  1668. else:
  1669. qCritical("PatchCanvas::CanvasPort.paint() - invalid port mode '%s'" % port_mode2str(self.m_port_mode))
  1670. return
  1671. if self.m_port_type == PORT_TYPE_AUDIO_JACK:
  1672. poly_color = canvas.theme.port_audio_jack_bg_sel if self.isSelected() else canvas.theme.port_audio_jack_bg
  1673. poly_pen = canvas.theme.port_audio_jack_pen_sel if self.isSelected() else canvas.theme.port_audio_jack_pen
  1674. text_pen = canvas.theme.port_audio_jack_text_sel if self.isSelected() else canvas.theme.port_audio_jack_text
  1675. conn_pen = canvas.theme.port_audio_jack_pen_sel
  1676. elif self.m_port_type == PORT_TYPE_MIDI_JACK:
  1677. poly_color = canvas.theme.port_midi_jack_bg_sel if self.isSelected() else canvas.theme.port_midi_jack_bg
  1678. poly_pen = canvas.theme.port_midi_jack_pen_sel if self.isSelected() else canvas.theme.port_midi_jack_pen
  1679. text_pen = canvas.theme.port_midi_jack_text_sel if self.isSelected() else canvas.theme.port_midi_jack_text
  1680. conn_pen = canvas.theme.port_midi_jack_pen_sel
  1681. elif self.m_port_type == PORT_TYPE_MIDI_ALSA:
  1682. poly_color = canvas.theme.port_midi_alsa_bg_sel if self.isSelected() else canvas.theme.port_midi_alsa_bg
  1683. poly_pen = canvas.theme.port_midi_alsa_pen_sel if self.isSelected() else canvas.theme.port_midi_alsa_pen
  1684. text_pen = canvas.theme.port_midi_alsa_text_sel if self.isSelected() else canvas.theme.port_midi_alsa_text
  1685. conn_pen = canvas.theme.port_midi_alsa_pen_sel
  1686. elif self.m_port_type == PORT_TYPE_PARAMETER:
  1687. poly_color = canvas.theme.port_parameter_bg_sel if self.isSelected() else canvas.theme.port_parameter_bg
  1688. poly_pen = canvas.theme.port_parameter_pen_sel if self.isSelected() else canvas.theme.port_parameter_pen
  1689. text_pen = canvas.theme.port_parameter_text_sel if self.isSelected() else canvas.theme.port_parameter_text
  1690. conn_pen = canvas.theme.port_parameter_pen_sel
  1691. else:
  1692. qCritical("PatchCanvas::CanvasPort.paint() - invalid port type '%s'" % port_type2str(self.m_port_type))
  1693. return
  1694. if self.m_is_alternate:
  1695. poly_color = poly_color.darker(180)
  1696. #poly_pen.setColor(poly_pen.color().darker(110))
  1697. #text_pen.setColor(text_pen.color()) #.darker(150))
  1698. #conn_pen.setColor(conn_pen.color()) #.darker(150))
  1699. polygon = QPolygonF()
  1700. polygon += QPointF(poly_locx[0], lineHinting)
  1701. polygon += QPointF(poly_locx[1], lineHinting)
  1702. polygon += QPointF(poly_locx[2], float(canvas.theme.port_height)/2)
  1703. polygon += QPointF(poly_locx[3], canvas.theme.port_height - lineHinting)
  1704. polygon += QPointF(poly_locx[4], canvas.theme.port_height - lineHinting)
  1705. polygon += QPointF(poly_locx[0], lineHinting)
  1706. if canvas.theme.port_bg_pixmap:
  1707. portRect = polygon.boundingRect()
  1708. portPos = portRect.topLeft()
  1709. painter.drawTiledPixmap(portRect, canvas.theme.port_bg_pixmap, portPos)
  1710. else:
  1711. painter.setBrush(poly_color) #.lighter(200))
  1712. painter.setPen(poly_pen)
  1713. painter.drawPolygon(polygon)
  1714. painter.setPen(text_pen)
  1715. painter.setFont(self.m_port_font)
  1716. painter.drawText(text_pos, self.m_port_name)
  1717. if self.isSelected() != self.m_last_selected_state:
  1718. for connection in canvas.connection_list:
  1719. if ((connection.group_out_id == self.m_group_id and connection.port_out_id == self.m_port_id) or
  1720. (connection.group_in_id == self.m_group_id and connection.port_in_id == self.m_port_id)):
  1721. connection.widget.setLineSelected(self.isSelected())
  1722. if canvas.theme.idx == Theme.THEME_OOSTUDIO and canvas.theme.port_bg_pixmap:
  1723. painter.setPen(Qt.NoPen)
  1724. painter.setBrush(conn_pen.brush())
  1725. if self.m_port_mode == PORT_MODE_INPUT:
  1726. connRect = QRectF(portRect.topLeft(), QSizeF(2, portRect.height()))
  1727. else:
  1728. connRect = QRectF(QPointF(portRect.right()-2, portRect.top()), QSizeF(2, portRect.height()))
  1729. painter.drawRect(connRect)
  1730. self.m_last_selected_state = self.isSelected()
  1731. painter.restore()
  1732. # ------------------------------------------------------------------------------
  1733. # canvasbox.cpp
  1734. class cb_line_t(object):
  1735. __slots__ = [
  1736. 'line',
  1737. 'connection_id'
  1738. ]
  1739. class CanvasBox(QGraphicsItem):
  1740. def __init__(self, group_id, group_name, icon, parent=None):
  1741. QGraphicsItem.__init__(self, parent)
  1742. # Save Variables, useful for later
  1743. self.m_group_id = group_id
  1744. self.m_group_name = group_name
  1745. # plugin Id, < 0 if invalid
  1746. self.m_plugin_id = -1
  1747. self.m_plugin_ui = False
  1748. # Base Variables
  1749. self.p_width = 50
  1750. self.p_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing + 1
  1751. self.m_last_pos = QPointF()
  1752. self.m_splitted = False
  1753. self.m_splitted_mode = PORT_MODE_NULL
  1754. self.m_cursor_moving = False
  1755. self.m_forced_split = False
  1756. self.m_mouse_down = False
  1757. self.m_port_list_ids = []
  1758. self.m_connection_lines = []
  1759. # Set Font
  1760. self.m_font_name = QFont()
  1761. self.m_font_name.setFamily(canvas.theme.box_font_name)
  1762. self.m_font_name.setPixelSize(canvas.theme.box_font_size)
  1763. self.m_font_name.setWeight(canvas.theme.box_font_state)
  1764. self.m_font_port = QFont()
  1765. self.m_font_port.setFamily(canvas.theme.port_font_name)
  1766. self.m_font_port.setPixelSize(canvas.theme.port_font_size)
  1767. self.m_font_port.setWeight(canvas.theme.port_font_state)
  1768. # Icon
  1769. if canvas.theme.box_use_icon:
  1770. self.icon_svg = CanvasIcon(icon, self.m_group_name, self)
  1771. else:
  1772. self.icon_svg = None
  1773. # Shadow
  1774. if options.eyecandy:
  1775. self.shadow = CanvasBoxShadow(self.toGraphicsObject())
  1776. self.shadow.setFakeParent(self)
  1777. self.setGraphicsEffect(self.shadow)
  1778. else:
  1779. self.shadow = None
  1780. # Final touches
  1781. self.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
  1782. # Wait for at least 1 port
  1783. if options.auto_hide_groups:
  1784. self.setVisible(False)
  1785. self.setFlag(QGraphicsItem.ItemIsFocusable, True)
  1786. if options.auto_select_items:
  1787. self.setAcceptHoverEvents(True)
  1788. self.updatePositions()
  1789. canvas.scene.addItem(self)
  1790. def getGroupId(self):
  1791. return self.m_group_id
  1792. def getGroupName(self):
  1793. return self.m_group_name
  1794. def isSplitted(self):
  1795. return self.m_splitted
  1796. def getSplittedMode(self):
  1797. return self.m_splitted_mode
  1798. def getPortCount(self):
  1799. return len(self.m_port_list_ids)
  1800. def getPortList(self):
  1801. return self.m_port_list_ids
  1802. def setAsPlugin(self, plugin_id, hasUi):
  1803. self.m_plugin_id = plugin_id
  1804. self.m_plugin_ui = hasUi
  1805. def setIcon(self, icon):
  1806. if self.icon_svg is not None:
  1807. self.icon_svg.setIcon(icon, self.m_group_name)
  1808. def setSplit(self, split, mode=PORT_MODE_NULL):
  1809. self.m_splitted = split
  1810. self.m_splitted_mode = mode
  1811. def setGroupName(self, group_name):
  1812. self.m_group_name = group_name
  1813. self.updatePositions()
  1814. def setShadowOpacity(self, opacity):
  1815. if self.shadow:
  1816. self.shadow.setOpacity(opacity)
  1817. def addPortFromGroup(self, port_id, port_mode, port_type, port_name, is_alternate):
  1818. if len(self.m_port_list_ids) == 0:
  1819. if options.auto_hide_groups:
  1820. if options.eyecandy == EYECANDY_FULL:
  1821. CanvasItemFX(self, True, False)
  1822. self.setVisible(True)
  1823. new_widget = CanvasPort(self.m_group_id, port_id, port_name, port_mode, port_type, is_alternate, self)
  1824. port_dict = port_dict_t()
  1825. port_dict.group_id = self.m_group_id
  1826. port_dict.port_id = port_id
  1827. port_dict.port_name = port_name
  1828. port_dict.port_mode = port_mode
  1829. port_dict.port_type = port_type
  1830. port_dict.is_alternate = is_alternate
  1831. port_dict.widget = new_widget
  1832. self.m_port_list_ids.append(port_id)
  1833. return new_widget
  1834. def removePortFromGroup(self, port_id):
  1835. if port_id in self.m_port_list_ids:
  1836. self.m_port_list_ids.remove(port_id)
  1837. else:
  1838. qCritical("PatchCanvas::CanvasBox.removePort(%i) - unable to find port to remove" % port_id)
  1839. return
  1840. if len(self.m_port_list_ids) > 0:
  1841. self.updatePositions()
  1842. elif self.isVisible():
  1843. if options.auto_hide_groups:
  1844. if options.eyecandy == EYECANDY_FULL:
  1845. CanvasItemFX(self, False, False)
  1846. else:
  1847. self.setVisible(False)
  1848. def addLineFromGroup(self, line, connection_id):
  1849. new_cbline = cb_line_t()
  1850. new_cbline.line = line
  1851. new_cbline.connection_id = connection_id
  1852. self.m_connection_lines.append(new_cbline)
  1853. def removeLineFromGroup(self, connection_id):
  1854. for connection in self.m_connection_lines:
  1855. if connection.connection_id == connection_id:
  1856. self.m_connection_lines.remove(connection)
  1857. return
  1858. qCritical("PatchCanvas::CanvasBox.removeLineFromGroup(%i) - unable to find line to remove" % connection_id)
  1859. def checkItemPos(self):
  1860. if not canvas.size_rect.isNull():
  1861. pos = self.scenePos()
  1862. if not (canvas.size_rect.contains(pos) and canvas.size_rect.contains(pos + QPointF(self.p_width, self.p_height))):
  1863. if pos.x() < canvas.size_rect.x():
  1864. self.setPos(canvas.size_rect.x(), pos.y())
  1865. elif pos.x() + self.p_width > canvas.size_rect.width():
  1866. self.setPos(canvas.size_rect.width() - self.p_width, pos.y())
  1867. pos = self.scenePos()
  1868. if pos.y() < canvas.size_rect.y():
  1869. self.setPos(pos.x(), canvas.size_rect.y())
  1870. elif pos.y() + self.p_height > canvas.size_rect.height():
  1871. self.setPos(pos.x(), canvas.size_rect.height() - self.p_height)
  1872. def removeIconFromScene(self):
  1873. if self.icon_svg is None:
  1874. return
  1875. item = self.icon_svg
  1876. self.icon_svg = None
  1877. canvas.scene.removeItem(item)
  1878. del item
  1879. def updatePositions(self):
  1880. self.prepareGeometryChange()
  1881. # Check Text Name size
  1882. app_name_size = QFontMetrics(self.m_font_name).width(self.m_group_name) + 30
  1883. self.p_width = max( 50, app_name_size )
  1884. # Get Port List
  1885. port_list = []
  1886. for port in canvas.port_list:
  1887. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  1888. port_list.append(port)
  1889. if len(port_list) == 0:
  1890. self.p_height = canvas.theme.box_header_height
  1891. else:
  1892. max_in_width = max_out_width = 0
  1893. port_spacing = canvas.theme.port_height + canvas.theme.port_spacing
  1894. # Get Max Box Width, vertical ports re-positioning
  1895. port_types = [PORT_TYPE_AUDIO_JACK, PORT_TYPE_MIDI_JACK, PORT_TYPE_MIDI_ALSA, PORT_TYPE_PARAMETER]
  1896. last_in_type = last_out_type = PORT_TYPE_NULL
  1897. last_in_pos = last_out_pos = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1898. for port_type in port_types:
  1899. for port in port_list:
  1900. if port.port_type != port_type:
  1901. continue
  1902. size = QFontMetrics(self.m_font_port).width(port.port_name)
  1903. if port.port_mode == PORT_MODE_INPUT:
  1904. max_in_width = max( max_in_width, size )
  1905. if port.port_type != last_in_type:
  1906. if last_in_type != PORT_TYPE_NULL:
  1907. last_in_pos += canvas.theme.port_spacingT
  1908. last_in_type = port.port_type
  1909. port.widget.setY(last_in_pos)
  1910. last_in_pos += port_spacing
  1911. elif port.port_mode == PORT_MODE_OUTPUT:
  1912. max_out_width = max( max_out_width, size )
  1913. if port.port_type != last_out_type:
  1914. if last_out_type != PORT_TYPE_NULL:
  1915. last_out_pos += canvas.theme.port_spacingT
  1916. last_out_type = port.port_type
  1917. port.widget.setY(last_out_pos)
  1918. last_out_pos += port_spacing
  1919. self.p_width = max(self.p_width, 30 + max_in_width + max_out_width)
  1920. # Horizontal ports re-positioning
  1921. inX = 1 + canvas.theme.port_offset
  1922. outX = self.p_width - max_out_width - canvas.theme.port_offset - 13
  1923. for port_type in port_types:
  1924. for port in port_type[1]:
  1925. if port.port_mode == PORT_MODE_INPUT:
  1926. port.widget.setX(inX)
  1927. port.widget.setPortWidth(max_in_width)
  1928. elif port.port_mode == PORT_MODE_OUTPUT:
  1929. port.widget.setX(outX)
  1930. port.widget.setPortWidth(max_out_width)
  1931. self.p_height = max(last_in_pos, last_out_pos)
  1932. self.p_height += max(canvas.theme.port_spacing, canvas.theme.port_spacingT) - canvas.theme.port_spacing
  1933. self.p_height += canvas.theme.box_pen.widthF()
  1934. self.repaintLines(True)
  1935. self.update()
  1936. def repaintLines(self, forced=False):
  1937. if self.pos() != self.m_last_pos or forced:
  1938. for connection in self.m_connection_lines:
  1939. connection.line.updateLinePos()
  1940. self.m_last_pos = self.pos()
  1941. def resetLinesZValue(self):
  1942. for connection in canvas.connection_list:
  1943. if connection.port_out_id in self.m_port_list_ids and connection.port_in_id in self.m_port_list_ids:
  1944. z_value = canvas.last_z_value
  1945. else:
  1946. z_value = canvas.last_z_value - 1
  1947. connection.widget.setZValue(z_value)
  1948. def type(self):
  1949. return CanvasBoxType
  1950. def contextMenuEvent(self, event):
  1951. menu = QMenu()
  1952. discMenu = QMenu("Disconnect", menu)
  1953. conn_list = []
  1954. conn_list_ids = []
  1955. for port_id in self.m_port_list_ids:
  1956. tmp_conn_list = CanvasGetPortConnectionList(self.m_group_id, port_id)
  1957. for conn_id, group_id, port_id in tmp_conn_list:
  1958. if conn_id not in conn_list_ids:
  1959. conn_list.append((conn_id, group_id, port_id))
  1960. conn_list_ids.append(conn_id)
  1961. if len(conn_list) > 0:
  1962. for conn_id, group_id, port_id in conn_list:
  1963. act_x_disc = discMenu.addAction(CanvasGetFullPortName(group_id, port_id))
  1964. act_x_disc.setData(conn_id)
  1965. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  1966. else:
  1967. act_x_disc = discMenu.addAction("No connections")
  1968. act_x_disc.setEnabled(False)
  1969. menu.addMenu(discMenu)
  1970. act_x_disc_all = menu.addAction("Disconnect &All")
  1971. act_x_sep1 = menu.addSeparator()
  1972. act_x_info = menu.addAction("Info")
  1973. act_x_rename = menu.addAction("Rename")
  1974. act_x_sep2 = menu.addSeparator()
  1975. act_x_split_join = menu.addAction("Join" if self.m_splitted else "Split")
  1976. if not features.group_info:
  1977. act_x_info.setVisible(False)
  1978. if not features.group_rename:
  1979. act_x_rename.setVisible(False)
  1980. if not (features.group_info and features.group_rename):
  1981. act_x_sep1.setVisible(False)
  1982. if self.m_plugin_id >= 0 and self.m_plugin_id <= MAX_PLUGIN_ID_ALLOWED:
  1983. menu.addSeparator()
  1984. act_p_edit = menu.addAction("Edit")
  1985. act_p_ui = menu.addAction("Show Custom UI")
  1986. menu.addSeparator()
  1987. act_p_clone = menu.addAction("Clone")
  1988. act_p_rename = menu.addAction("Rename...")
  1989. act_p_replace = menu.addAction("Replace...")
  1990. act_p_remove = menu.addAction("Remove")
  1991. if not self.m_plugin_ui:
  1992. act_p_ui.setVisible(False)
  1993. else:
  1994. act_p_edit = act_p_ui = None
  1995. act_p_clone = act_p_rename = None
  1996. act_p_replace = act_p_remove = None
  1997. haveIns = haveOuts = False
  1998. for port in canvas.port_list:
  1999. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  2000. if port.port_mode == PORT_MODE_INPUT:
  2001. haveIns = True
  2002. elif port.port_mode == PORT_MODE_OUTPUT:
  2003. haveOuts = True
  2004. if not (self.m_splitted or bool(haveIns and haveOuts)):
  2005. act_x_sep2.setVisible(False)
  2006. act_x_split_join.setVisible(False)
  2007. act_selected = menu.exec_(event.screenPos())
  2008. if act_selected is None:
  2009. pass
  2010. elif act_selected == act_x_disc_all:
  2011. for conn_id in conn_list_ids:
  2012. canvas.callback(ACTION_PORTS_DISCONNECT, conn_id, 0, "")
  2013. elif act_selected == act_x_info:
  2014. canvas.callback(ACTION_GROUP_INFO, self.m_group_id, 0, "")
  2015. elif act_selected == act_x_rename:
  2016. canvas.callback(ACTION_GROUP_RENAME, self.m_group_id, 0, "")
  2017. elif act_selected == act_x_split_join:
  2018. if self.m_splitted:
  2019. canvas.callback(ACTION_GROUP_JOIN, self.m_group_id, 0, "")
  2020. else:
  2021. canvas.callback(ACTION_GROUP_SPLIT, self.m_group_id, 0, "")
  2022. elif act_selected == act_p_edit:
  2023. canvas.callback(ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  2024. elif act_selected == act_p_ui:
  2025. canvas.callback(ACTION_PLUGIN_SHOW_UI, self.m_plugin_id, 0, "")
  2026. elif act_selected == act_p_clone:
  2027. canvas.callback(ACTION_PLUGIN_CLONE, self.m_plugin_id, 0, "")
  2028. elif act_selected == act_p_rename:
  2029. canvas.callback(ACTION_PLUGIN_RENAME, self.m_plugin_id, 0, "")
  2030. elif act_selected == act_p_replace:
  2031. canvas.callback(ACTION_PLUGIN_REPLACE, self.m_plugin_id, 0, "")
  2032. elif act_selected == act_p_remove:
  2033. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  2034. event.accept()
  2035. def keyPressEvent(self, event):
  2036. if self.m_plugin_id >= 0 and event.key() == Qt.Key_Delete:
  2037. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  2038. return event.accept()
  2039. QGraphicsItem.keyPressEvent(self, event)
  2040. def hoverEnterEvent(self, event):
  2041. if options.auto_select_items:
  2042. if len(canvas.scene.selectedItems()) > 0:
  2043. canvas.scene.clearSelection()
  2044. self.setSelected(True)
  2045. QGraphicsItem.hoverEnterEvent(self, event)
  2046. def mouseDoubleClickEvent(self, event):
  2047. if self.m_plugin_id >= 0:
  2048. canvas.callback(ACTION_PLUGIN_SHOW_UI if self.m_plugin_ui else ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  2049. return event.accept()
  2050. QGraphicsItem.mouseDoubleClickEvent(self, event)
  2051. def mousePressEvent(self, event):
  2052. canvas.last_z_value += 1
  2053. self.setZValue(canvas.last_z_value)
  2054. self.resetLinesZValue()
  2055. self.m_cursor_moving = False
  2056. if event.button() == Qt.RightButton:
  2057. canvas.scene.clearSelection()
  2058. self.setSelected(True)
  2059. self.m_mouse_down = False
  2060. return event.accept()
  2061. elif event.button() == Qt.LeftButton:
  2062. if self.sceneBoundingRect().contains(event.scenePos()):
  2063. self.m_mouse_down = True
  2064. else:
  2065. # Fix a weird Qt behaviour with right-click mouseMove
  2066. self.m_mouse_down = False
  2067. return event.ignore()
  2068. else:
  2069. self.m_mouse_down = False
  2070. QGraphicsItem.mousePressEvent(self, event)
  2071. def mouseMoveEvent(self, event):
  2072. if self.m_mouse_down:
  2073. if not self.m_cursor_moving:
  2074. self.setCursor(QCursor(Qt.SizeAllCursor))
  2075. self.m_cursor_moving = True
  2076. self.repaintLines()
  2077. QGraphicsItem.mouseMoveEvent(self, event)
  2078. def mouseReleaseEvent(self, event):
  2079. if self.m_cursor_moving:
  2080. self.setCursor(QCursor(Qt.ArrowCursor))
  2081. self.setX(round(self.x()))
  2082. self.setY(round(self.y()))
  2083. self.m_mouse_down = False
  2084. self.m_cursor_moving = False
  2085. QGraphicsItem.mouseReleaseEvent(self, event)
  2086. def boundingRect(self):
  2087. return QRectF(0, 0, self.p_width, self.p_height)
  2088. def paint(self, painter, option, widget):
  2089. painter.save()
  2090. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  2091. rect = QRectF(0, 0, self.p_width, self.p_height)
  2092. # Draw rectangle
  2093. pen = canvas.theme.box_pen_sel if self.isSelected() else canvas.theme.box_pen
  2094. painter.setPen(pen)
  2095. lineHinting = pen.widthF() / 2
  2096. if canvas.theme.box_bg_type == Theme.THEME_BG_GRADIENT:
  2097. box_gradient = QLinearGradient(0, 0, 0, self.p_height)
  2098. box_gradient.setColorAt(0, canvas.theme.box_bg_1)
  2099. box_gradient.setColorAt(1, canvas.theme.box_bg_2)
  2100. painter.setBrush(box_gradient)
  2101. else:
  2102. painter.setBrush(canvas.theme.box_bg_1)
  2103. rect.adjust(lineHinting, lineHinting, -lineHinting, -lineHinting)
  2104. painter.drawRect(rect)
  2105. # Draw pixmap header
  2106. rect.setHeight(canvas.theme.box_header_height)
  2107. if canvas.theme.box_header_pixmap:
  2108. painter.setPen(Qt.NoPen)
  2109. painter.setBrush(canvas.theme.box_bg_2)
  2110. # outline
  2111. rect.adjust(lineHinting, lineHinting, -lineHinting, -lineHinting)
  2112. painter.drawRect(rect)
  2113. rect.adjust(1, 1, -1, 0)
  2114. painter.drawTiledPixmap(rect, canvas.theme.box_header_pixmap, rect.topLeft())
  2115. # Draw text
  2116. painter.setFont(self.m_font_name)
  2117. if self.isSelected():
  2118. painter.setPen(canvas.theme.box_text_sel)
  2119. else:
  2120. painter.setPen(canvas.theme.box_text)
  2121. if canvas.theme.box_use_icon:
  2122. textPos = QPointF(25, canvas.theme.box_text_ypos)
  2123. else:
  2124. appNameSize = QFontMetrics(self.m_font_name).width(self.m_group_name)
  2125. rem = self.p_width - appNameSize
  2126. textPos = QPointF(rem/2, canvas.theme.box_text_ypos)
  2127. painter.drawText(textPos, self.m_group_name)
  2128. self.repaintLines()
  2129. painter.restore()
  2130. # ------------------------------------------------------------------------------
  2131. # canvasicon.cpp
  2132. class CanvasIcon(QGraphicsSvgItem):
  2133. def __init__(self, icon, name, parent):
  2134. QGraphicsSvgItem.__init__(self, parent)
  2135. self.m_renderer = None
  2136. self.p_size = QRectF(0, 0, 0, 0)
  2137. self.m_colorFX = QGraphicsColorizeEffect(self)
  2138. self.m_colorFX.setColor(canvas.theme.box_text.color())
  2139. self.setGraphicsEffect(self.m_colorFX)
  2140. self.setIcon(icon, name)
  2141. def setIcon(self, icon, name):
  2142. name = name.lower()
  2143. icon_path = ""
  2144. if icon == ICON_APPLICATION:
  2145. self.p_size = QRectF(3, 2, 19, 18)
  2146. if "audacious" in name:
  2147. icon_path = ":/scalable/pb_audacious.svg"
  2148. self.p_size = QRectF(5, 4, 16, 16)
  2149. elif "clementine" in name:
  2150. icon_path = ":/scalable/pb_clementine.svg"
  2151. self.p_size = QRectF(5, 4, 16, 16)
  2152. elif "distrho" in name:
  2153. icon_path = ":/scalable/pb_distrho.svg"
  2154. self.p_size = QRectF(5, 4, 14, 14)
  2155. elif "jamin" in name:
  2156. icon_path = ":/scalable/pb_jamin.svg"
  2157. self.p_size = QRectF(5, 3, 16, 16)
  2158. elif "mplayer" in name:
  2159. icon_path = ":/scalable/pb_mplayer.svg"
  2160. self.p_size = QRectF(5, 4, 16, 16)
  2161. elif "vlc" in name:
  2162. icon_path = ":/scalable/pb_vlc.svg"
  2163. self.p_size = QRectF(5, 3, 16, 16)
  2164. else:
  2165. icon_path = ":/scalable/pb_generic.svg"
  2166. self.p_size = QRectF(4, 3, 16, 16)
  2167. elif icon == ICON_HARDWARE:
  2168. icon_path = ":/scalable/pb_hardware.svg"
  2169. self.p_size = QRectF(5, 2, 16, 16)
  2170. elif icon == ICON_DISTRHO:
  2171. icon_path = ":/scalable/pb_distrho.svg"
  2172. self.p_size = QRectF(5, 4, 14, 14)
  2173. elif icon == ICON_FILE:
  2174. icon_path = ":/scalable/pb_file.svg"
  2175. self.p_size = QRectF(5, 4, 12, 14)
  2176. elif icon == ICON_PLUGIN:
  2177. icon_path = ":/scalable/pb_plugin.svg"
  2178. self.p_size = QRectF(5, 4, 14, 14)
  2179. elif icon == ICON_LADISH_ROOM:
  2180. # TODO - make a unique ladish-room icon
  2181. icon_path = ":/scalable/pb_hardware.svg"
  2182. self.p_size = QRectF(5, 2, 16, 16)
  2183. else:
  2184. self.p_size = QRectF(0, 0, 0, 0)
  2185. qCritical("PatchCanvas::CanvasIcon.setIcon(%s, %s) - unsupported icon requested" % (icon2str(icon), name.encode()))
  2186. return
  2187. self.m_renderer = QSvgRenderer(icon_path, canvas.scene)
  2188. self.setSharedRenderer(self.m_renderer)
  2189. self.update()
  2190. def type(self):
  2191. return CanvasIconType
  2192. def boundingRect(self):
  2193. return self.p_size
  2194. def paint(self, painter, option, widget):
  2195. if self.m_renderer:
  2196. painter.save()
  2197. painter.setRenderHint(QPainter.Antialiasing, False)
  2198. painter.setRenderHint(QPainter.TextAntialiasing, False)
  2199. self.m_renderer.render(painter, self.p_size)
  2200. painter.restore()
  2201. else:
  2202. QGraphicsSvgItem.paint(self, painter, option, widget)
  2203. # ------------------------------------------------------------------------------
  2204. # canvasportglow.cpp
  2205. class CanvasPortGlow(QGraphicsDropShadowEffect):
  2206. def __init__(self, port_type, parent):
  2207. QGraphicsDropShadowEffect.__init__(self, parent)
  2208. self.setBlurRadius(12)
  2209. self.setOffset(0, 0)
  2210. if port_type == PORT_TYPE_AUDIO_JACK:
  2211. self.setColor(canvas.theme.line_audio_jack_glow)
  2212. elif port_type == PORT_TYPE_MIDI_JACK:
  2213. self.setColor(canvas.theme.line_midi_jack_glow)
  2214. elif port_type == PORT_TYPE_MIDI_ALSA:
  2215. self.setColor(canvas.theme.line_midi_alsa_glow)
  2216. elif port_type == PORT_TYPE_PARAMETER:
  2217. self.setColor(canvas.theme.line_parameter_glow)
  2218. # ------------------------------------------------------------------------------
  2219. # canvasboxshadow.cpp
  2220. class CanvasBoxShadow(QGraphicsDropShadowEffect):
  2221. def __init__(self, parent):
  2222. QGraphicsDropShadowEffect.__init__(self, parent)
  2223. self.m_fakeParent = None
  2224. self.setBlurRadius(20)
  2225. self.setColor(canvas.theme.box_shadow)
  2226. self.setOffset(0, 0)
  2227. def setFakeParent(self, fakeParent):
  2228. self.m_fakeParent = fakeParent
  2229. def setOpacity(self, opacity):
  2230. color = QColor(canvas.theme.box_shadow)
  2231. color.setAlphaF(opacity)
  2232. self.setColor(color)
  2233. def draw(self, painter):
  2234. if self.m_fakeParent:
  2235. self.m_fakeParent.repaintLines()
  2236. QGraphicsDropShadowEffect.draw(self, painter)