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.

2969 lines
105KB

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