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.

2849 lines
100KB

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