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. first_value = False
  993. min_x = pos.x()
  994. min_y = pos.y()
  995. max_x = pos.x() + rect.width()
  996. max_y = pos.y() + rect.height()
  997. else:
  998. min_x = min(min_x, pos.x())
  999. min_y = min(min_y, pos.y())
  1000. max_x = max(max_x, pos.x() + rect.width())
  1001. max_y = max(max_y, pos.y() + rect.height())
  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,
  1088. y+lineHinting,
  1089. abs(pos.x() - self.m_rubberband_orig_point.x()),
  1090. abs(pos.y() - self.m_rubberband_orig_point.y()))
  1091. return event.accept()
  1092. if self.m_mid_button_down and self.m_ctrl_down:
  1093. trail = QPolygonF([event.scenePos(), event.lastScenePos(), event.scenePos()])
  1094. items = self.items(trail)
  1095. for item in items:
  1096. if item and item.type() in [CanvasLineType, CanvasBezierLineType]:
  1097. item.triggerDisconnect()
  1098. QGraphicsScene.mouseMoveEvent(self, event)
  1099. def mouseReleaseEvent(self, event):
  1100. if self.m_rubberband_selection:
  1101. items_list = self.items()
  1102. if len(items_list) > 0:
  1103. for item in items_list:
  1104. if item and item.isVisible() and item.type() == CanvasBoxType:
  1105. item_rect = item.sceneBoundingRect()
  1106. item_top_left = QPointF(item_rect.x(), item_rect.y())
  1107. item_bottom_right = QPointF(item_rect.x() + item_rect.width(), item_rect.y() + item_rect.height())
  1108. if self.m_rubberband.contains(item_top_left) and self.m_rubberband.contains(item_bottom_right):
  1109. item.setSelected(True)
  1110. self.m_rubberband.hide()
  1111. self.m_rubberband.setRect(0, 0, 0, 0)
  1112. self.m_rubberband_selection = False
  1113. else:
  1114. items_list = self.selectedItems()
  1115. for item in items_list:
  1116. if item and item.isVisible() and item.type() == CanvasBoxType:
  1117. item.checkItemPos()
  1118. self.sceneGroupMoved.emit(item.getGroupId(), item.getSplittedMode(), item.scenePos())
  1119. if len(items_list) > 1:
  1120. canvas.scene.update()
  1121. self.m_mouse_down_init = False
  1122. self.m_mouse_rubberband = False
  1123. if event.button() == Qt.MidButton:
  1124. self.m_mid_button_down = False
  1125. return event.accept()
  1126. QGraphicsScene.mouseReleaseEvent(self, event)
  1127. def wheelEvent(self, event):
  1128. if not self.m_view:
  1129. return event.ignore()
  1130. if self.m_ctrl_down:
  1131. factor = 1.41 ** (event.delta() / 240.0)
  1132. self.m_view.scale(factor, factor)
  1133. self.fixScaleFactor()
  1134. return event.accept()
  1135. QGraphicsScene.wheelEvent(self, event)
  1136. def contextMenuEvent(self, event):
  1137. if len(self.selectedItems()) == 0:
  1138. event.accept()
  1139. canvas.callback(ACTION_BG_RIGHT_CLICK, 0, 0, "")
  1140. else:
  1141. QGraphicsScene.contextMenuEvent(self, event)
  1142. # ------------------------------------------------------------------------------
  1143. # canvasfadeanimation.cpp
  1144. class CanvasFadeAnimation(QAbstractAnimation):
  1145. def __init__(self, item, show):
  1146. QAbstractAnimation.__init__(self)
  1147. self.m_show = show
  1148. self.m_duration = 0
  1149. self.m_item = item
  1150. def item(self):
  1151. return self.m_item
  1152. def forceStop(self):
  1153. self.blockSignals(True)
  1154. self.stop()
  1155. def setDuration(self, time):
  1156. if self.m_item.opacity() == 0 and not self.m_show:
  1157. self._duration = 0
  1158. else:
  1159. self.m_item.show()
  1160. self.m_duration = time
  1161. def duration(self):
  1162. return self.m_duration
  1163. def updateCurrentTime(self, time):
  1164. if self.m_duration == 0:
  1165. return
  1166. if self.m_show:
  1167. value = float(time) / self.m_duration
  1168. else:
  1169. value = 1.0 - (float(time) / self.m_duration)
  1170. try:
  1171. self.m_item.setOpacity(value)
  1172. except:
  1173. print("Error: failed to animate canvas item, already destroyed?")
  1174. self.forceStop()
  1175. canvas.animation_list.remove(self)
  1176. return
  1177. if self.m_item.type() == CanvasBoxType:
  1178. self.m_item.setShadowOpacity(value)
  1179. def updateDirection(self, direction):
  1180. pass
  1181. def updateState(self, oldState, newState):
  1182. pass
  1183. # ------------------------------------------------------------------------------
  1184. # canvasline.cpp
  1185. class CanvasLine(QGraphicsLineItem):
  1186. def __init__(self, item1, item2, parent):
  1187. QGraphicsLineItem.__init__(self, parent)
  1188. self.item1 = item1
  1189. self.item2 = item2
  1190. self.m_locked = False
  1191. self.m_lineSelected = False
  1192. self.setGraphicsEffect(None)
  1193. self.updateLinePos()
  1194. def isLocked(self):
  1195. return self.m_locked
  1196. def setLocked(self, yesno):
  1197. self.m_locked = yesno
  1198. def isLineSelected(self):
  1199. return self.m_lineSelected
  1200. def setLineSelected(self, yesno):
  1201. if self.m_locked:
  1202. return
  1203. if options.eyecandy == EYECANDY_FULL:
  1204. if yesno:
  1205. self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
  1206. else:
  1207. self.setGraphicsEffect(None)
  1208. self.m_lineSelected = yesno
  1209. self.updateLineGradient()
  1210. def triggerDisconnect(self):
  1211. for connection in canvas.connection_list:
  1212. if (connection.port_out_id == self.item1.getPortId() and connection.port_in_id == self.item2.getPortId()):
  1213. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  1214. break
  1215. def updateLinePos(self):
  1216. if self.item1.getPortMode() == PORT_MODE_OUTPUT:
  1217. rect1 = self.item1.sceneBoundingRect()
  1218. rect2 = self.item2.sceneBoundingRect()
  1219. line = QLineF(rect1.right(),
  1220. rect1.top() + float(canvas.theme.port_height)/2,
  1221. rect2.left(),
  1222. rect2.top() + float(canvas.theme.port_height)/2)
  1223. self.setLine(line)
  1224. self.m_lineSelected = False
  1225. self.updateLineGradient()
  1226. def type(self):
  1227. return CanvasLineType
  1228. def updateLineGradient(self):
  1229. pos_top = self.boundingRect().top()
  1230. pos_bot = self.boundingRect().bottom()
  1231. if self.item2.scenePos().y() >= self.item1.scenePos().y():
  1232. pos1 = 0
  1233. pos2 = 1
  1234. else:
  1235. pos1 = 1
  1236. pos2 = 0
  1237. port_type1 = self.item1.getPortType()
  1238. port_type2 = self.item2.getPortType()
  1239. port_gradient = QLinearGradient(0, pos_top, 0, pos_bot)
  1240. if port_type1 == PORT_TYPE_AUDIO_JACK:
  1241. port_gradient.setColorAt(pos1, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1242. elif port_type1 == PORT_TYPE_MIDI_JACK:
  1243. port_gradient.setColorAt(pos1, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1244. elif port_type1 == PORT_TYPE_MIDI_ALSA:
  1245. port_gradient.setColorAt(pos1, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1246. elif port_type1 == PORT_TYPE_PARAMETER:
  1247. port_gradient.setColorAt(pos1, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1248. if port_type2 == PORT_TYPE_AUDIO_JACK:
  1249. port_gradient.setColorAt(pos2, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1250. elif port_type2 == PORT_TYPE_MIDI_JACK:
  1251. port_gradient.setColorAt(pos2, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1252. elif port_type2 == PORT_TYPE_MIDI_ALSA:
  1253. port_gradient.setColorAt(pos2, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1254. elif port_type2 == PORT_TYPE_PARAMETER:
  1255. port_gradient.setColorAt(pos2, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1256. self.setPen(QPen(port_gradient, 2))
  1257. def paint(self, painter, option, widget):
  1258. painter.save()
  1259. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1260. QGraphicsLineItem.paint(self, painter, option, widget)
  1261. painter.restore()
  1262. # ------------------------------------------------------------------------------
  1263. # canvasbezierline.cpp
  1264. class CanvasBezierLine(QGraphicsPathItem):
  1265. def __init__(self, item1, item2, parent):
  1266. QGraphicsPathItem.__init__(self, parent)
  1267. self.item1 = item1
  1268. self.item2 = item2
  1269. self.m_locked = False
  1270. self.m_lineSelected = False
  1271. self.setBrush(QColor(0, 0, 0, 0))
  1272. self.setGraphicsEffect(None)
  1273. self.updateLinePos()
  1274. def isLocked(self):
  1275. return self.m_locked
  1276. def setLocked(self, yesno):
  1277. self.m_locked = yesno
  1278. def isLineSelected(self):
  1279. return self.m_lineSelected
  1280. def setLineSelected(self, yesno):
  1281. if self.m_locked:
  1282. return
  1283. if options.eyecandy == EYECANDY_FULL:
  1284. if yesno:
  1285. self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
  1286. else:
  1287. self.setGraphicsEffect(None)
  1288. self.m_lineSelected = yesno
  1289. self.updateLineGradient()
  1290. def triggerDisconnect(self):
  1291. for connection in canvas.connection_list:
  1292. if (connection.port_out_id == self.item1.getPortId() and connection.port_in_id == self.item2.getPortId()):
  1293. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  1294. break
  1295. def updateLinePos(self):
  1296. if self.item1.getPortMode() == PORT_MODE_OUTPUT:
  1297. rect1 = self.item1.sceneBoundingRect()
  1298. rect2 = self.item2.sceneBoundingRect()
  1299. item1_x = rect1.right()
  1300. item2_x = rect2.left()
  1301. item1_y = rect1.top() + float(canvas.theme.port_height)/2
  1302. item2_y = rect2.top() + float(canvas.theme.port_height)/2
  1303. item1_new_x = item1_x + abs(item1_x - item2_x) / 2
  1304. item2_new_x = item2_x - abs(item1_x - item2_x) / 2
  1305. path = QPainterPath(QPointF(item1_x, item1_y))
  1306. path.cubicTo(item1_new_x, item1_y, item2_new_x, item2_y, item2_x, item2_y)
  1307. self.setPath(path)
  1308. self.m_lineSelected = False
  1309. self.updateLineGradient()
  1310. def type(self):
  1311. return CanvasBezierLineType
  1312. def updateLineGradient(self):
  1313. pos_top = self.boundingRect().top()
  1314. pos_bot = self.boundingRect().bottom()
  1315. if self.item2.scenePos().y() >= self.item1.scenePos().y():
  1316. pos1 = 0
  1317. pos2 = 1
  1318. else:
  1319. pos1 = 1
  1320. pos2 = 0
  1321. port_type1 = self.item1.getPortType()
  1322. port_type2 = self.item2.getPortType()
  1323. port_gradient = QLinearGradient(0, pos_top, 0, pos_bot)
  1324. if port_type1 == PORT_TYPE_AUDIO_JACK:
  1325. port_gradient.setColorAt(pos1, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1326. elif port_type1 == PORT_TYPE_MIDI_JACK:
  1327. port_gradient.setColorAt(pos1, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1328. elif port_type1 == PORT_TYPE_MIDI_ALSA:
  1329. port_gradient.setColorAt(pos1, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1330. elif port_type1 == PORT_TYPE_PARAMETER:
  1331. port_gradient.setColorAt(pos1, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1332. if port_type2 == PORT_TYPE_AUDIO_JACK:
  1333. port_gradient.setColorAt(pos2, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1334. elif port_type2 == PORT_TYPE_MIDI_JACK:
  1335. port_gradient.setColorAt(pos2, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1336. elif port_type2 == PORT_TYPE_MIDI_ALSA:
  1337. port_gradient.setColorAt(pos2, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1338. elif port_type2 == PORT_TYPE_PARAMETER:
  1339. port_gradient.setColorAt(pos2, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1340. self.setPen(QPen(port_gradient, 2))
  1341. def paint(self, painter, option, widget):
  1342. painter.save()
  1343. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1344. QGraphicsPathItem.paint(self, painter, option, widget)
  1345. painter.restore()
  1346. # ------------------------------------------------------------------------------
  1347. # canvaslivemov.cpp
  1348. class CanvasLineMov(QGraphicsLineItem):
  1349. def __init__(self, port_mode, port_type, parent):
  1350. QGraphicsLineItem.__init__(self, parent)
  1351. self.m_port_mode = port_mode
  1352. self.m_port_type = port_type
  1353. # Port position doesn't change while moving around line
  1354. self.p_lineX = self.scenePos().x()
  1355. self.p_lineY = self.scenePos().y()
  1356. self.p_width = self.parentItem().getPortWidth()
  1357. if port_type == PORT_TYPE_AUDIO_JACK:
  1358. pen = QPen(canvas.theme.line_audio_jack, 2)
  1359. elif port_type == PORT_TYPE_MIDI_JACK:
  1360. pen = QPen(canvas.theme.line_midi_jack, 2)
  1361. elif port_type == PORT_TYPE_MIDI_ALSA:
  1362. pen = QPen(canvas.theme.line_midi_alsa, 2)
  1363. elif port_type == PORT_TYPE_PARAMETER:
  1364. pen = QPen(canvas.theme.line_parameter, 2)
  1365. else:
  1366. qWarning("PatchCanvas::CanvasLineMov(%s, %s, %s) - invalid port type" % (port_mode2str(port_mode), port_type2str(port_type), parent))
  1367. pen = QPen(Qt.black)
  1368. self.setPen(pen)
  1369. def updateLinePos(self, scenePos):
  1370. item_pos = [0, 0]
  1371. if self.m_port_mode == PORT_MODE_INPUT:
  1372. item_pos[0] = 0
  1373. item_pos[1] = float(canvas.theme.port_height)/2
  1374. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1375. item_pos[0] = self.p_width + 12
  1376. item_pos[1] = float(canvas.theme.port_height)/2
  1377. else:
  1378. return
  1379. line = QLineF(item_pos[0], item_pos[1], scenePos.x() - self.p_lineX, scenePos.y() - self.p_lineY)
  1380. self.setLine(line)
  1381. def type(self):
  1382. return CanvasLineMovType
  1383. def paint(self, painter, option, widget):
  1384. painter.save()
  1385. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1386. QGraphicsLineItem.paint(self, painter, option, widget)
  1387. painter.restore()
  1388. # ------------------------------------------------------------------------------
  1389. # canvasbezierlinemov.cpp
  1390. class CanvasBezierLineMov(QGraphicsPathItem):
  1391. def __init__(self, port_mode, port_type, parent):
  1392. QGraphicsPathItem.__init__(self, parent)
  1393. self.m_port_mode = port_mode
  1394. self.m_port_type = port_type
  1395. # Port position doesn't change while moving around line
  1396. self.p_itemX = self.scenePos().x()
  1397. self.p_itemY = self.scenePos().y()
  1398. self.p_width = self.parentItem().getPortWidth()
  1399. if port_type == PORT_TYPE_AUDIO_JACK:
  1400. pen = QPen(canvas.theme.line_audio_jack, 2)
  1401. elif port_type == PORT_TYPE_MIDI_JACK:
  1402. pen = QPen(canvas.theme.line_midi_jack, 2)
  1403. elif port_type == PORT_TYPE_MIDI_ALSA:
  1404. pen = QPen(canvas.theme.line_midi_alsa, 2)
  1405. elif port_type == PORT_TYPE_PARAMETER:
  1406. pen = QPen(canvas.theme.line_parameter, 2)
  1407. else:
  1408. qWarning("PatchCanvas::CanvasBezierLineMov(%s, %s, %s) - invalid port type" % (port_mode2str(port_mode), port_type2str(port_type), parent))
  1409. pen = QPen(Qt.black)
  1410. self.setBrush(QColor(0, 0, 0, 0))
  1411. self.setPen(pen)
  1412. def updateLinePos(self, scenePos):
  1413. if self.m_port_mode == PORT_MODE_INPUT:
  1414. old_x = 0
  1415. old_y = float(canvas.theme.port_height)/2
  1416. mid_x = abs(scenePos.x() - self.p_itemX) / 2
  1417. new_x = old_x - mid_x
  1418. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1419. old_x = self.p_width + 12
  1420. old_y = float(canvas.theme.port_height)/2
  1421. mid_x = abs(scenePos.x() - (self.p_itemX + old_x)) / 2
  1422. new_x = old_x + mid_x
  1423. else:
  1424. return
  1425. final_x = scenePos.x() - self.p_itemX
  1426. final_y = scenePos.y() - self.p_itemY
  1427. path = QPainterPath(QPointF(old_x, old_y))
  1428. path.cubicTo(new_x, old_y, new_x, final_y, final_x, final_y)
  1429. self.setPath(path)
  1430. def type(self):
  1431. return CanvasBezierLineMovType
  1432. def paint(self, painter, option, widget):
  1433. painter.save()
  1434. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1435. QGraphicsPathItem.paint(self, painter, option, widget)
  1436. painter.restore()
  1437. # ------------------------------------------------------------------------------
  1438. # canvasport.cpp
  1439. class CanvasPort(QGraphicsItem):
  1440. def __init__(self, group_id, port_id, port_name, port_mode, port_type, is_alternate, parent):
  1441. QGraphicsItem.__init__(self, parent)
  1442. # Save Variables, useful for later
  1443. self.m_group_id = group_id
  1444. self.m_port_id = port_id
  1445. self.m_port_mode = port_mode
  1446. self.m_port_type = port_type
  1447. self.m_port_name = port_name
  1448. self.m_is_alternate = is_alternate
  1449. # Base Variables
  1450. self.m_port_width = 15
  1451. self.m_port_height = canvas.theme.port_height
  1452. self.m_port_font = QFont()
  1453. self.m_port_font.setFamily(canvas.theme.port_font_name)
  1454. self.m_port_font.setPixelSize(canvas.theme.port_font_size)
  1455. self.m_port_font.setWeight(canvas.theme.port_font_state)
  1456. self.m_line_mov = None
  1457. self.m_hover_item = None
  1458. self.m_last_selected_state = False
  1459. self.m_mouse_down = False
  1460. self.m_cursor_moving = False
  1461. self.setFlags(QGraphicsItem.ItemIsSelectable)
  1462. if options.auto_select_items:
  1463. self.setAcceptHoverEvents(True)
  1464. def getGroupId(self):
  1465. return self.m_group_id
  1466. def getPortId(self):
  1467. return self.m_port_id
  1468. def getPortMode(self):
  1469. return self.m_port_mode
  1470. def getPortType(self):
  1471. return self.m_port_type
  1472. def getPortName(self):
  1473. return self.m_port_name
  1474. def getFullPortName(self):
  1475. return self.parentItem().getGroupName() + ":" + self.m_port_name
  1476. def getPortWidth(self):
  1477. return self.m_port_width
  1478. def getPortHeight(self):
  1479. return self.m_port_height
  1480. def setPortMode(self, port_mode):
  1481. self.m_port_mode = port_mode
  1482. self.update()
  1483. def setPortType(self, port_type):
  1484. self.m_port_type = port_type
  1485. self.update()
  1486. def setPortName(self, port_name):
  1487. if QFontMetrics(self.m_port_font).width(port_name) < QFontMetrics(self.m_port_font).width(self.m_port_name):
  1488. QTimer.singleShot(0, canvas.scene.update)
  1489. self.m_port_name = port_name
  1490. self.update()
  1491. def setPortWidth(self, port_width):
  1492. if port_width < self.m_port_width:
  1493. QTimer.singleShot(0, canvas.scene.update)
  1494. self.m_port_width = port_width
  1495. self.update()
  1496. def type(self):
  1497. return CanvasPortType
  1498. def hoverEnterEvent(self, event):
  1499. if options.auto_select_items:
  1500. self.setSelected(True)
  1501. QGraphicsItem.hoverEnterEvent(self, event)
  1502. def hoverLeaveEvent(self, event):
  1503. if options.auto_select_items:
  1504. self.setSelected(False)
  1505. QGraphicsItem.hoverLeaveEvent(self, event)
  1506. def mousePressEvent(self, event):
  1507. self.m_hover_item = None
  1508. self.m_mouse_down = bool(event.button() == Qt.LeftButton)
  1509. self.m_cursor_moving = False
  1510. QGraphicsItem.mousePressEvent(self, event)
  1511. def mouseMoveEvent(self, event):
  1512. if self.m_mouse_down:
  1513. if not self.m_cursor_moving:
  1514. self.setCursor(QCursor(Qt.CrossCursor))
  1515. self.m_cursor_moving = True
  1516. for connection in canvas.connection_list:
  1517. if ((connection.group_out_id == self.m_group_id and connection.port_out_id == self.m_port_id) or
  1518. (connection.group_in_id == self.m_group_id and connection.port_in_id == self.m_port_id)):
  1519. connection.widget.setLocked(True)
  1520. if not self.m_line_mov:
  1521. if options.use_bezier_lines:
  1522. self.m_line_mov = CanvasBezierLineMov(self.m_port_mode, self.m_port_type, self)
  1523. else:
  1524. self.m_line_mov = CanvasLineMov(self.m_port_mode, self.m_port_type, self)
  1525. canvas.last_z_value += 1
  1526. self.m_line_mov.setZValue(canvas.last_z_value)
  1527. canvas.last_z_value += 1
  1528. self.parentItem().setZValue(canvas.last_z_value)
  1529. item = None
  1530. items = canvas.scene.items(event.scenePos(), Qt.ContainsItemShape, Qt.AscendingOrder)
  1531. for i in range(len(items)):
  1532. if items[i].type() == CanvasPortType:
  1533. if items[i] != self:
  1534. if not item:
  1535. item = items[i]
  1536. elif items[i].parentItem().zValue() > item.parentItem().zValue():
  1537. item = items[i]
  1538. if self.m_hover_item and self.m_hover_item != item:
  1539. self.m_hover_item.setSelected(False)
  1540. if item:
  1541. if item.getPortMode() != self.m_port_mode and item.getPortType() == self.m_port_type:
  1542. item.setSelected(True)
  1543. self.m_hover_item = item
  1544. else:
  1545. self.m_hover_item = None
  1546. else:
  1547. self.m_hover_item = None
  1548. self.m_line_mov.updateLinePos(event.scenePos())
  1549. return event.accept()
  1550. QGraphicsItem.mouseMoveEvent(self, event)
  1551. def mouseReleaseEvent(self, event):
  1552. if self.m_mouse_down:
  1553. if self.m_line_mov is not None:
  1554. item = self.m_line_mov
  1555. self.m_line_mov = None
  1556. canvas.scene.removeItem(item)
  1557. del item
  1558. for connection in canvas.connection_list:
  1559. if ((connection.group_out_id == self.m_group_id and connection.port_out_id == self.m_port_id) or
  1560. (connection.group_in_id == self.m_group_id and connection.port_in_id == self.m_port_id)):
  1561. connection.widget.setLocked(False)
  1562. if self.m_hover_item:
  1563. # TODO: a better way to check already existing connection
  1564. for connection in canvas.connection_list:
  1565. hover_group_id = self.m_hover_item.getGroupId()
  1566. hover_port_id = self.m_hover_item.getPortId()
  1567. if (
  1568. (connection.group_out_id == self.m_group_id and connection.port_out_id == self.m_port_id and
  1569. connection.group_in_id == hover_group_id and connection.port_in_id == hover_port_id) or
  1570. (connection.group_out_id == hover_group_id and connection.port_out_id == hover_port_id and
  1571. connection.group_in_id == self.m_group_id and connection.port_in_id == self.m_port_id)
  1572. ):
  1573. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  1574. break
  1575. else:
  1576. if self.m_port_mode == PORT_MODE_OUTPUT:
  1577. conn = "%i:%i:%i:%i" % (self.m_group_id, self.m_port_id, self.m_hover_item.getGroupId(), self.m_hover_item.getPortId())
  1578. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  1579. else:
  1580. conn = "%i:%i:%i:%i" % (self.m_hover_item.getGroupId(), self.m_hover_item.getPortId(), self.m_group_id, self.m_port_id)
  1581. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  1582. canvas.scene.clearSelection()
  1583. if self.m_cursor_moving:
  1584. self.setCursor(QCursor(Qt.ArrowCursor))
  1585. self.m_hover_item = None
  1586. self.m_mouse_down = False
  1587. self.m_cursor_moving = False
  1588. QGraphicsItem.mouseReleaseEvent(self, event)
  1589. def contextMenuEvent(self, event):
  1590. canvas.scene.clearSelection()
  1591. self.setSelected(True)
  1592. menu = QMenu()
  1593. discMenu = QMenu("Disconnect", menu)
  1594. conn_list = CanvasGetPortConnectionList(self.m_group_id, self.m_port_id)
  1595. if len(conn_list) > 0:
  1596. for conn_id, group_id, port_id in conn_list:
  1597. act_x_disc = discMenu.addAction(CanvasGetFullPortName(group_id, port_id))
  1598. act_x_disc.setData(conn_id)
  1599. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  1600. else:
  1601. act_x_disc = discMenu.addAction("No connections")
  1602. act_x_disc.setEnabled(False)
  1603. menu.addMenu(discMenu)
  1604. act_x_disc_all = menu.addAction("Disconnect &All")
  1605. act_x_sep_1 = menu.addSeparator()
  1606. act_x_info = menu.addAction("Get &Info")
  1607. act_x_rename = menu.addAction("&Rename")
  1608. if not features.port_info:
  1609. act_x_info.setVisible(False)
  1610. if not features.port_rename:
  1611. act_x_rename.setVisible(False)
  1612. if not (features.port_info and features.port_rename):
  1613. act_x_sep_1.setVisible(False)
  1614. act_selected = menu.exec_(event.screenPos())
  1615. if act_selected == act_x_disc_all:
  1616. for conn_id, group_id, port_id in conn_list:
  1617. canvas.callback(ACTION_PORTS_DISCONNECT, conn_id, 0, "")
  1618. elif act_selected == act_x_info:
  1619. canvas.callback(ACTION_PORT_INFO, self.m_group_id, self.m_port_id, "")
  1620. elif act_selected == act_x_rename:
  1621. canvas.callback(ACTION_PORT_RENAME, self.m_group_id, self.m_port_id, "")
  1622. event.accept()
  1623. def boundingRect(self):
  1624. return QRectF(0, 0, self.m_port_width + 12, self.m_port_height)
  1625. def paint(self, painter, option, widget):
  1626. painter.save()
  1627. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  1628. # FIXME: would be more correct to take line width from Pen, loaded to painter,
  1629. # but this needs some code rearrangement
  1630. lineHinting = canvas.theme.port_audio_jack_pen.widthF() / 2
  1631. poly_locx = [0, 0, 0, 0, 0]
  1632. poly_corner_xhinting = (float(canvas.theme.port_height)/2) % floor(float(canvas.theme.port_height)/2)
  1633. if poly_corner_xhinting == 0:
  1634. poly_corner_xhinting = 0.5 * (1 - 7 / (float(canvas.theme.port_height)/2))
  1635. if self.m_port_mode == PORT_MODE_INPUT:
  1636. text_pos = QPointF(3, canvas.theme.port_text_ypos)
  1637. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  1638. poly_locx[0] = lineHinting
  1639. poly_locx[1] = self.m_port_width + 5 - lineHinting
  1640. poly_locx[2] = self.m_port_width + 12 - poly_corner_xhinting
  1641. poly_locx[3] = self.m_port_width + 5 - lineHinting
  1642. poly_locx[4] = lineHinting
  1643. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  1644. poly_locx[0] = lineHinting
  1645. poly_locx[1] = self.m_port_width + 5 - lineHinting
  1646. poly_locx[2] = self.m_port_width + 5 - lineHinting
  1647. poly_locx[3] = self.m_port_width + 5 - lineHinting
  1648. poly_locx[4] = lineHinting
  1649. else:
  1650. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  1651. return
  1652. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1653. text_pos = QPointF(9, canvas.theme.port_text_ypos)
  1654. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  1655. poly_locx[0] = self.m_port_width + 12 - lineHinting
  1656. poly_locx[1] = 7 + lineHinting
  1657. poly_locx[2] = 0 + poly_corner_xhinting
  1658. poly_locx[3] = 7 + lineHinting
  1659. poly_locx[4] = self.m_port_width + 12 - lineHinting
  1660. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  1661. poly_locx[0] = self.m_port_width + 12 - lineHinting
  1662. poly_locx[1] = 5 + lineHinting
  1663. poly_locx[2] = 5 + lineHinting
  1664. poly_locx[3] = 5 + lineHinting
  1665. poly_locx[4] = self.m_port_width + 12 - lineHinting
  1666. else:
  1667. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  1668. return
  1669. else:
  1670. qCritical("PatchCanvas::CanvasPort.paint() - invalid port mode '%s'" % port_mode2str(self.m_port_mode))
  1671. return
  1672. if self.m_port_type == PORT_TYPE_AUDIO_JACK:
  1673. poly_color = canvas.theme.port_audio_jack_bg_sel if self.isSelected() else canvas.theme.port_audio_jack_bg
  1674. poly_pen = canvas.theme.port_audio_jack_pen_sel if self.isSelected() else canvas.theme.port_audio_jack_pen
  1675. text_pen = canvas.theme.port_audio_jack_text_sel if self.isSelected() else canvas.theme.port_audio_jack_text
  1676. conn_pen = canvas.theme.port_audio_jack_pen_sel
  1677. elif self.m_port_type == PORT_TYPE_MIDI_JACK:
  1678. poly_color = canvas.theme.port_midi_jack_bg_sel if self.isSelected() else canvas.theme.port_midi_jack_bg
  1679. poly_pen = canvas.theme.port_midi_jack_pen_sel if self.isSelected() else canvas.theme.port_midi_jack_pen
  1680. text_pen = canvas.theme.port_midi_jack_text_sel if self.isSelected() else canvas.theme.port_midi_jack_text
  1681. conn_pen = canvas.theme.port_midi_jack_pen_sel
  1682. elif self.m_port_type == PORT_TYPE_MIDI_ALSA:
  1683. poly_color = canvas.theme.port_midi_alsa_bg_sel if self.isSelected() else canvas.theme.port_midi_alsa_bg
  1684. poly_pen = canvas.theme.port_midi_alsa_pen_sel if self.isSelected() else canvas.theme.port_midi_alsa_pen
  1685. text_pen = canvas.theme.port_midi_alsa_text_sel if self.isSelected() else canvas.theme.port_midi_alsa_text
  1686. conn_pen = canvas.theme.port_midi_alsa_pen_sel
  1687. elif self.m_port_type == PORT_TYPE_PARAMETER:
  1688. poly_color = canvas.theme.port_parameter_bg_sel if self.isSelected() else canvas.theme.port_parameter_bg
  1689. poly_pen = canvas.theme.port_parameter_pen_sel if self.isSelected() else canvas.theme.port_parameter_pen
  1690. text_pen = canvas.theme.port_parameter_text_sel if self.isSelected() else canvas.theme.port_parameter_text
  1691. conn_pen = canvas.theme.port_parameter_pen_sel
  1692. else:
  1693. qCritical("PatchCanvas::CanvasPort.paint() - invalid port type '%s'" % port_type2str(self.m_port_type))
  1694. return
  1695. if self.m_is_alternate:
  1696. poly_color = poly_color.darker(180)
  1697. #poly_pen.setColor(poly_pen.color().darker(110))
  1698. #text_pen.setColor(text_pen.color()) #.darker(150))
  1699. #conn_pen.setColor(conn_pen.color()) #.darker(150))
  1700. polygon = QPolygonF()
  1701. polygon += QPointF(poly_locx[0], lineHinting)
  1702. polygon += QPointF(poly_locx[1], lineHinting)
  1703. polygon += QPointF(poly_locx[2], float(canvas.theme.port_height)/2)
  1704. polygon += QPointF(poly_locx[3], canvas.theme.port_height - lineHinting)
  1705. polygon += QPointF(poly_locx[4], canvas.theme.port_height - lineHinting)
  1706. polygon += QPointF(poly_locx[0], lineHinting)
  1707. if canvas.theme.port_bg_pixmap:
  1708. portRect = polygon.boundingRect()
  1709. portPos = portRect.topLeft()
  1710. painter.drawTiledPixmap(portRect, canvas.theme.port_bg_pixmap, portPos)
  1711. else:
  1712. painter.setBrush(poly_color) #.lighter(200))
  1713. painter.setPen(poly_pen)
  1714. painter.drawPolygon(polygon)
  1715. painter.setPen(text_pen)
  1716. painter.setFont(self.m_port_font)
  1717. painter.drawText(text_pos, self.m_port_name)
  1718. if self.isSelected() != self.m_last_selected_state:
  1719. for connection in canvas.connection_list:
  1720. if ((connection.group_out_id == self.m_group_id and connection.port_out_id == self.m_port_id) or
  1721. (connection.group_in_id == self.m_group_id and connection.port_in_id == self.m_port_id)):
  1722. connection.widget.setLineSelected(self.isSelected())
  1723. if canvas.theme.idx == Theme.THEME_OOSTUDIO and canvas.theme.port_bg_pixmap:
  1724. painter.setPen(Qt.NoPen)
  1725. painter.setBrush(conn_pen.brush())
  1726. if self.m_port_mode == PORT_MODE_INPUT:
  1727. connRect = QRectF(portRect.topLeft(), QSizeF(2, portRect.height()))
  1728. else:
  1729. connRect = QRectF(QPointF(portRect.right()-2, portRect.top()), QSizeF(2, portRect.height()))
  1730. painter.drawRect(connRect)
  1731. self.m_last_selected_state = self.isSelected()
  1732. painter.restore()
  1733. # ------------------------------------------------------------------------------
  1734. # canvasbox.cpp
  1735. class cb_line_t(object):
  1736. __slots__ = [
  1737. 'line',
  1738. 'connection_id'
  1739. ]
  1740. class CanvasBox(QGraphicsItem):
  1741. def __init__(self, group_id, group_name, icon, parent=None):
  1742. QGraphicsItem.__init__(self, parent)
  1743. # Save Variables, useful for later
  1744. self.m_group_id = group_id
  1745. self.m_group_name = group_name
  1746. # plugin Id, < 0 if invalid
  1747. self.m_plugin_id = -1
  1748. self.m_plugin_ui = False
  1749. # Base Variables
  1750. self.p_width = 50
  1751. self.p_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing + 1
  1752. self.m_last_pos = QPointF()
  1753. self.m_splitted = False
  1754. self.m_splitted_mode = PORT_MODE_NULL
  1755. self.m_cursor_moving = False
  1756. self.m_forced_split = False
  1757. self.m_mouse_down = False
  1758. self.m_port_list_ids = []
  1759. self.m_connection_lines = []
  1760. # Set Font
  1761. self.m_font_name = QFont()
  1762. self.m_font_name.setFamily(canvas.theme.box_font_name)
  1763. self.m_font_name.setPixelSize(canvas.theme.box_font_size)
  1764. self.m_font_name.setWeight(canvas.theme.box_font_state)
  1765. self.m_font_port = QFont()
  1766. self.m_font_port.setFamily(canvas.theme.port_font_name)
  1767. self.m_font_port.setPixelSize(canvas.theme.port_font_size)
  1768. self.m_font_port.setWeight(canvas.theme.port_font_state)
  1769. # Icon
  1770. if canvas.theme.box_use_icon:
  1771. self.icon_svg = CanvasIcon(icon, self.m_group_name, self)
  1772. else:
  1773. self.icon_svg = None
  1774. # Shadow
  1775. if options.eyecandy:
  1776. self.shadow = CanvasBoxShadow(self.toGraphicsObject())
  1777. self.shadow.setFakeParent(self)
  1778. self.setGraphicsEffect(self.shadow)
  1779. else:
  1780. self.shadow = None
  1781. # Final touches
  1782. self.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
  1783. # Wait for at least 1 port
  1784. if options.auto_hide_groups:
  1785. self.setVisible(False)
  1786. self.setFlag(QGraphicsItem.ItemIsFocusable, True)
  1787. if options.auto_select_items:
  1788. self.setAcceptHoverEvents(True)
  1789. self.updatePositions()
  1790. canvas.scene.addItem(self)
  1791. def getGroupId(self):
  1792. return self.m_group_id
  1793. def getGroupName(self):
  1794. return self.m_group_name
  1795. def isSplitted(self):
  1796. return self.m_splitted
  1797. def getSplittedMode(self):
  1798. return self.m_splitted_mode
  1799. def getPortCount(self):
  1800. return len(self.m_port_list_ids)
  1801. def getPortList(self):
  1802. return self.m_port_list_ids
  1803. def setAsPlugin(self, plugin_id, hasUi):
  1804. self.m_plugin_id = plugin_id
  1805. self.m_plugin_ui = hasUi
  1806. def setIcon(self, icon):
  1807. if self.icon_svg is not None:
  1808. self.icon_svg.setIcon(icon, self.m_group_name)
  1809. def setSplit(self, split, mode=PORT_MODE_NULL):
  1810. self.m_splitted = split
  1811. self.m_splitted_mode = mode
  1812. def setGroupName(self, group_name):
  1813. self.m_group_name = group_name
  1814. self.updatePositions()
  1815. def setShadowOpacity(self, opacity):
  1816. if self.shadow:
  1817. self.shadow.setOpacity(opacity)
  1818. def addPortFromGroup(self, port_id, port_mode, port_type, port_name, is_alternate):
  1819. if len(self.m_port_list_ids) == 0:
  1820. if options.auto_hide_groups:
  1821. if options.eyecandy == EYECANDY_FULL:
  1822. CanvasItemFX(self, True, False)
  1823. self.setVisible(True)
  1824. new_widget = CanvasPort(self.m_group_id, port_id, port_name, port_mode, port_type, is_alternate, self)
  1825. port_dict = port_dict_t()
  1826. port_dict.group_id = self.m_group_id
  1827. port_dict.port_id = port_id
  1828. port_dict.port_name = port_name
  1829. port_dict.port_mode = port_mode
  1830. port_dict.port_type = port_type
  1831. port_dict.is_alternate = is_alternate
  1832. port_dict.widget = new_widget
  1833. self.m_port_list_ids.append(port_id)
  1834. return new_widget
  1835. def removePortFromGroup(self, port_id):
  1836. if port_id in self.m_port_list_ids:
  1837. self.m_port_list_ids.remove(port_id)
  1838. else:
  1839. qCritical("PatchCanvas::CanvasBox.removePort(%i) - unable to find port to remove" % port_id)
  1840. return
  1841. if len(self.m_port_list_ids) > 0:
  1842. self.updatePositions()
  1843. elif self.isVisible():
  1844. if options.auto_hide_groups:
  1845. if options.eyecandy == EYECANDY_FULL:
  1846. CanvasItemFX(self, False, False)
  1847. else:
  1848. self.setVisible(False)
  1849. def addLineFromGroup(self, line, connection_id):
  1850. new_cbline = cb_line_t()
  1851. new_cbline.line = line
  1852. new_cbline.connection_id = connection_id
  1853. self.m_connection_lines.append(new_cbline)
  1854. def removeLineFromGroup(self, connection_id):
  1855. for connection in self.m_connection_lines:
  1856. if connection.connection_id == connection_id:
  1857. self.m_connection_lines.remove(connection)
  1858. return
  1859. qCritical("PatchCanvas::CanvasBox.removeLineFromGroup(%i) - unable to find line to remove" % connection_id)
  1860. def checkItemPos(self):
  1861. if not canvas.size_rect.isNull():
  1862. pos = self.scenePos()
  1863. if not (canvas.size_rect.contains(pos) and canvas.size_rect.contains(pos + QPointF(self.p_width, self.p_height))):
  1864. if pos.x() < canvas.size_rect.x():
  1865. self.setPos(canvas.size_rect.x(), pos.y())
  1866. elif pos.x() + self.p_width > canvas.size_rect.width():
  1867. self.setPos(canvas.size_rect.width() - self.p_width, pos.y())
  1868. pos = self.scenePos()
  1869. if pos.y() < canvas.size_rect.y():
  1870. self.setPos(pos.x(), canvas.size_rect.y())
  1871. elif pos.y() + self.p_height > canvas.size_rect.height():
  1872. self.setPos(pos.x(), canvas.size_rect.height() - self.p_height)
  1873. def removeIconFromScene(self):
  1874. if self.icon_svg is None:
  1875. return
  1876. item = self.icon_svg
  1877. self.icon_svg = None
  1878. canvas.scene.removeItem(item)
  1879. del item
  1880. def updatePositions(self):
  1881. self.prepareGeometryChange()
  1882. # Check Text Name size
  1883. app_name_size = QFontMetrics(self.m_font_name).width(self.m_group_name) + 30
  1884. self.p_width = max( 50, app_name_size )
  1885. # Get Port List
  1886. port_list = []
  1887. for port in canvas.port_list:
  1888. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  1889. port_list.append(port)
  1890. if len(port_list) == 0:
  1891. self.p_height = canvas.theme.box_header_height
  1892. else:
  1893. max_in_width = max_out_width = 0
  1894. port_spacing = canvas.theme.port_height + canvas.theme.port_spacing
  1895. # Get Max Box Width, vertical ports re-positioning
  1896. port_types = [PORT_TYPE_AUDIO_JACK, PORT_TYPE_MIDI_JACK, PORT_TYPE_MIDI_ALSA, PORT_TYPE_PARAMETER]
  1897. last_in_type = last_out_type = PORT_TYPE_NULL
  1898. last_in_pos = last_out_pos = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1899. for port_type in port_types:
  1900. for port in port_list:
  1901. if port.port_type != port_type:
  1902. continue
  1903. size = QFontMetrics(self.m_font_port).width(port.port_name)
  1904. if port.port_mode == PORT_MODE_INPUT:
  1905. max_in_width = max( max_in_width, size )
  1906. if port.port_type != last_in_type:
  1907. if last_in_type != PORT_TYPE_NULL:
  1908. last_in_pos += canvas.theme.port_spacingT
  1909. last_in_type = port.port_type
  1910. port.widget.setY(last_in_pos)
  1911. last_in_pos += port_spacing
  1912. elif port.port_mode == PORT_MODE_OUTPUT:
  1913. max_out_width = max( max_out_width, size )
  1914. if port.port_type != last_out_type:
  1915. if last_out_type != PORT_TYPE_NULL:
  1916. last_out_pos += canvas.theme.port_spacingT
  1917. last_out_type = port.port_type
  1918. port.widget.setY(last_out_pos)
  1919. last_out_pos += port_spacing
  1920. self.p_width = max(self.p_width, 30 + max_in_width + max_out_width)
  1921. # Horizontal ports re-positioning
  1922. inX = 1 + canvas.theme.port_offset
  1923. outX = self.p_width - max_out_width - canvas.theme.port_offset - 13
  1924. for port_type in port_types:
  1925. for port in port_list:
  1926. if port.port_mode == PORT_MODE_INPUT:
  1927. port.widget.setX(inX)
  1928. port.widget.setPortWidth(max_in_width)
  1929. elif port.port_mode == PORT_MODE_OUTPUT:
  1930. port.widget.setX(outX)
  1931. port.widget.setPortWidth(max_out_width)
  1932. self.p_height = max(last_in_pos, last_out_pos)
  1933. self.p_height += max(canvas.theme.port_spacing, canvas.theme.port_spacingT) - canvas.theme.port_spacing
  1934. self.p_height += canvas.theme.box_pen.widthF()
  1935. self.repaintLines(True)
  1936. self.update()
  1937. def repaintLines(self, forced=False):
  1938. if self.pos() != self.m_last_pos or forced:
  1939. for connection in self.m_connection_lines:
  1940. connection.line.updateLinePos()
  1941. self.m_last_pos = self.pos()
  1942. def resetLinesZValue(self):
  1943. for connection in canvas.connection_list:
  1944. if connection.port_out_id in self.m_port_list_ids and connection.port_in_id in self.m_port_list_ids:
  1945. z_value = canvas.last_z_value
  1946. else:
  1947. z_value = canvas.last_z_value - 1
  1948. connection.widget.setZValue(z_value)
  1949. def type(self):
  1950. return CanvasBoxType
  1951. def contextMenuEvent(self, event):
  1952. menu = QMenu()
  1953. discMenu = QMenu("Disconnect", menu)
  1954. conn_list = []
  1955. conn_list_ids = []
  1956. for port_id in self.m_port_list_ids:
  1957. tmp_conn_list = CanvasGetPortConnectionList(self.m_group_id, port_id)
  1958. for conn_id, group_id, port_id in tmp_conn_list:
  1959. if conn_id not in conn_list_ids:
  1960. conn_list.append((conn_id, group_id, port_id))
  1961. conn_list_ids.append(conn_id)
  1962. if len(conn_list) > 0:
  1963. for conn_id, group_id, port_id in conn_list:
  1964. act_x_disc = discMenu.addAction(CanvasGetFullPortName(group_id, port_id))
  1965. act_x_disc.setData(conn_id)
  1966. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  1967. else:
  1968. act_x_disc = discMenu.addAction("No connections")
  1969. act_x_disc.setEnabled(False)
  1970. menu.addMenu(discMenu)
  1971. act_x_disc_all = menu.addAction("Disconnect &All")
  1972. act_x_sep1 = menu.addSeparator()
  1973. act_x_info = menu.addAction("Info")
  1974. act_x_rename = menu.addAction("Rename")
  1975. act_x_sep2 = menu.addSeparator()
  1976. act_x_split_join = menu.addAction("Join" if self.m_splitted else "Split")
  1977. if not features.group_info:
  1978. act_x_info.setVisible(False)
  1979. if not features.group_rename:
  1980. act_x_rename.setVisible(False)
  1981. if not (features.group_info and features.group_rename):
  1982. act_x_sep1.setVisible(False)
  1983. if self.m_plugin_id >= 0 and self.m_plugin_id <= MAX_PLUGIN_ID_ALLOWED:
  1984. menu.addSeparator()
  1985. act_p_edit = menu.addAction("Edit")
  1986. act_p_ui = menu.addAction("Show Custom UI")
  1987. menu.addSeparator()
  1988. act_p_clone = menu.addAction("Clone")
  1989. act_p_rename = menu.addAction("Rename...")
  1990. act_p_replace = menu.addAction("Replace...")
  1991. act_p_remove = menu.addAction("Remove")
  1992. if not self.m_plugin_ui:
  1993. act_p_ui.setVisible(False)
  1994. else:
  1995. act_p_edit = act_p_ui = None
  1996. act_p_clone = act_p_rename = None
  1997. act_p_replace = act_p_remove = None
  1998. haveIns = haveOuts = False
  1999. for port in canvas.port_list:
  2000. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  2001. if port.port_mode == PORT_MODE_INPUT:
  2002. haveIns = True
  2003. elif port.port_mode == PORT_MODE_OUTPUT:
  2004. haveOuts = True
  2005. if not (self.m_splitted or bool(haveIns and haveOuts)):
  2006. act_x_sep2.setVisible(False)
  2007. act_x_split_join.setVisible(False)
  2008. act_selected = menu.exec_(event.screenPos())
  2009. if act_selected is None:
  2010. pass
  2011. elif act_selected == act_x_disc_all:
  2012. for conn_id in conn_list_ids:
  2013. canvas.callback(ACTION_PORTS_DISCONNECT, conn_id, 0, "")
  2014. elif act_selected == act_x_info:
  2015. canvas.callback(ACTION_GROUP_INFO, self.m_group_id, 0, "")
  2016. elif act_selected == act_x_rename:
  2017. canvas.callback(ACTION_GROUP_RENAME, self.m_group_id, 0, "")
  2018. elif act_selected == act_x_split_join:
  2019. if self.m_splitted:
  2020. canvas.callback(ACTION_GROUP_JOIN, self.m_group_id, 0, "")
  2021. else:
  2022. canvas.callback(ACTION_GROUP_SPLIT, self.m_group_id, 0, "")
  2023. elif act_selected == act_p_edit:
  2024. canvas.callback(ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  2025. elif act_selected == act_p_ui:
  2026. canvas.callback(ACTION_PLUGIN_SHOW_UI, self.m_plugin_id, 0, "")
  2027. elif act_selected == act_p_clone:
  2028. canvas.callback(ACTION_PLUGIN_CLONE, self.m_plugin_id, 0, "")
  2029. elif act_selected == act_p_rename:
  2030. canvas.callback(ACTION_PLUGIN_RENAME, self.m_plugin_id, 0, "")
  2031. elif act_selected == act_p_replace:
  2032. canvas.callback(ACTION_PLUGIN_REPLACE, self.m_plugin_id, 0, "")
  2033. elif act_selected == act_p_remove:
  2034. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  2035. event.accept()
  2036. def keyPressEvent(self, event):
  2037. if self.m_plugin_id >= 0 and event.key() == Qt.Key_Delete:
  2038. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  2039. return event.accept()
  2040. QGraphicsItem.keyPressEvent(self, event)
  2041. def hoverEnterEvent(self, event):
  2042. if options.auto_select_items:
  2043. if len(canvas.scene.selectedItems()) > 0:
  2044. canvas.scene.clearSelection()
  2045. self.setSelected(True)
  2046. QGraphicsItem.hoverEnterEvent(self, event)
  2047. def mouseDoubleClickEvent(self, event):
  2048. if self.m_plugin_id >= 0:
  2049. canvas.callback(ACTION_PLUGIN_SHOW_UI if self.m_plugin_ui else ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  2050. return event.accept()
  2051. QGraphicsItem.mouseDoubleClickEvent(self, event)
  2052. def mousePressEvent(self, event):
  2053. canvas.last_z_value += 1
  2054. self.setZValue(canvas.last_z_value)
  2055. self.resetLinesZValue()
  2056. self.m_cursor_moving = False
  2057. if event.button() == Qt.RightButton:
  2058. canvas.scene.clearSelection()
  2059. self.setSelected(True)
  2060. self.m_mouse_down = False
  2061. return event.accept()
  2062. elif event.button() == Qt.LeftButton:
  2063. if self.sceneBoundingRect().contains(event.scenePos()):
  2064. self.m_mouse_down = True
  2065. else:
  2066. # Fix a weird Qt behaviour with right-click mouseMove
  2067. self.m_mouse_down = False
  2068. return event.ignore()
  2069. else:
  2070. self.m_mouse_down = False
  2071. QGraphicsItem.mousePressEvent(self, event)
  2072. def mouseMoveEvent(self, event):
  2073. if self.m_mouse_down:
  2074. if not self.m_cursor_moving:
  2075. self.setCursor(QCursor(Qt.SizeAllCursor))
  2076. self.m_cursor_moving = True
  2077. self.repaintLines()
  2078. QGraphicsItem.mouseMoveEvent(self, event)
  2079. def mouseReleaseEvent(self, event):
  2080. if self.m_cursor_moving:
  2081. self.setCursor(QCursor(Qt.ArrowCursor))
  2082. self.setX(round(self.x()))
  2083. self.setY(round(self.y()))
  2084. self.m_mouse_down = False
  2085. self.m_cursor_moving = False
  2086. QGraphicsItem.mouseReleaseEvent(self, event)
  2087. def boundingRect(self):
  2088. return QRectF(0, 0, self.p_width, self.p_height)
  2089. def paint(self, painter, option, widget):
  2090. painter.save()
  2091. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  2092. rect = QRectF(0, 0, self.p_width, self.p_height)
  2093. # Draw rectangle
  2094. pen = canvas.theme.box_pen_sel if self.isSelected() else canvas.theme.box_pen
  2095. painter.setPen(pen)
  2096. lineHinting = pen.widthF() / 2
  2097. if canvas.theme.box_bg_type == Theme.THEME_BG_GRADIENT:
  2098. box_gradient = QLinearGradient(0, 0, 0, self.p_height)
  2099. box_gradient.setColorAt(0, canvas.theme.box_bg_1)
  2100. box_gradient.setColorAt(1, canvas.theme.box_bg_2)
  2101. painter.setBrush(box_gradient)
  2102. else:
  2103. painter.setBrush(canvas.theme.box_bg_1)
  2104. rect.adjust(lineHinting, lineHinting, -lineHinting, -lineHinting)
  2105. painter.drawRect(rect)
  2106. # Draw pixmap header
  2107. rect.setHeight(canvas.theme.box_header_height)
  2108. if canvas.theme.box_header_pixmap:
  2109. painter.setPen(Qt.NoPen)
  2110. painter.setBrush(canvas.theme.box_bg_2)
  2111. # outline
  2112. rect.adjust(lineHinting, lineHinting, -lineHinting, -lineHinting)
  2113. painter.drawRect(rect)
  2114. rect.adjust(1, 1, -1, 0)
  2115. painter.drawTiledPixmap(rect, canvas.theme.box_header_pixmap, rect.topLeft())
  2116. # Draw text
  2117. painter.setFont(self.m_font_name)
  2118. if self.isSelected():
  2119. painter.setPen(canvas.theme.box_text_sel)
  2120. else:
  2121. painter.setPen(canvas.theme.box_text)
  2122. if canvas.theme.box_use_icon:
  2123. textPos = QPointF(25, canvas.theme.box_text_ypos)
  2124. else:
  2125. appNameSize = QFontMetrics(self.m_font_name).width(self.m_group_name)
  2126. rem = self.p_width - appNameSize
  2127. textPos = QPointF(rem/2, canvas.theme.box_text_ypos)
  2128. painter.drawText(textPos, self.m_group_name)
  2129. self.repaintLines()
  2130. painter.restore()
  2131. # ------------------------------------------------------------------------------
  2132. # canvasicon.cpp
  2133. class CanvasIcon(QGraphicsSvgItem):
  2134. def __init__(self, icon, name, parent):
  2135. QGraphicsSvgItem.__init__(self, parent)
  2136. self.m_renderer = None
  2137. self.p_size = QRectF(0, 0, 0, 0)
  2138. self.m_colorFX = QGraphicsColorizeEffect(self)
  2139. self.m_colorFX.setColor(canvas.theme.box_text.color())
  2140. self.setGraphicsEffect(self.m_colorFX)
  2141. self.setIcon(icon, name)
  2142. def setIcon(self, icon, name):
  2143. name = name.lower()
  2144. icon_path = ""
  2145. if icon == ICON_APPLICATION:
  2146. self.p_size = QRectF(3, 2, 19, 18)
  2147. if "audacious" in name:
  2148. icon_path = ":/scalable/pb_audacious.svg"
  2149. self.p_size = QRectF(5, 4, 16, 16)
  2150. elif "clementine" in name:
  2151. icon_path = ":/scalable/pb_clementine.svg"
  2152. self.p_size = QRectF(5, 4, 16, 16)
  2153. elif "distrho" in name:
  2154. icon_path = ":/scalable/pb_distrho.svg"
  2155. self.p_size = QRectF(5, 4, 14, 14)
  2156. elif "jamin" in name:
  2157. icon_path = ":/scalable/pb_jamin.svg"
  2158. self.p_size = QRectF(5, 3, 16, 16)
  2159. elif "mplayer" in name:
  2160. icon_path = ":/scalable/pb_mplayer.svg"
  2161. self.p_size = QRectF(5, 4, 16, 16)
  2162. elif "vlc" in name:
  2163. icon_path = ":/scalable/pb_vlc.svg"
  2164. self.p_size = QRectF(5, 3, 16, 16)
  2165. else:
  2166. icon_path = ":/scalable/pb_generic.svg"
  2167. self.p_size = QRectF(4, 3, 16, 16)
  2168. elif icon == ICON_HARDWARE:
  2169. icon_path = ":/scalable/pb_hardware.svg"
  2170. self.p_size = QRectF(5, 2, 16, 16)
  2171. elif icon == ICON_DISTRHO:
  2172. icon_path = ":/scalable/pb_distrho.svg"
  2173. self.p_size = QRectF(5, 4, 14, 14)
  2174. elif icon == ICON_FILE:
  2175. icon_path = ":/scalable/pb_file.svg"
  2176. self.p_size = QRectF(5, 4, 12, 14)
  2177. elif icon == ICON_PLUGIN:
  2178. icon_path = ":/scalable/pb_plugin.svg"
  2179. self.p_size = QRectF(5, 4, 14, 14)
  2180. elif icon == ICON_LADISH_ROOM:
  2181. # TODO - make a unique ladish-room icon
  2182. icon_path = ":/scalable/pb_hardware.svg"
  2183. self.p_size = QRectF(5, 2, 16, 16)
  2184. else:
  2185. self.p_size = QRectF(0, 0, 0, 0)
  2186. qCritical("PatchCanvas::CanvasIcon.setIcon(%s, %s) - unsupported icon requested" % (icon2str(icon), name.encode()))
  2187. return
  2188. self.m_renderer = QSvgRenderer(icon_path, canvas.scene)
  2189. self.setSharedRenderer(self.m_renderer)
  2190. self.update()
  2191. def type(self):
  2192. return CanvasIconType
  2193. def boundingRect(self):
  2194. return self.p_size
  2195. def paint(self, painter, option, widget):
  2196. if not self.m_renderer:
  2197. return QGraphicsSvgItem.paint(self, painter, option, widget)
  2198. painter.save()
  2199. painter.setRenderHint(QPainter.Antialiasing, False)
  2200. painter.setRenderHint(QPainter.TextAntialiasing, False)
  2201. self.m_renderer.render(painter, self.p_size)
  2202. painter.restore()
  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)