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.

2830 lines
100KB

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