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.

2820 lines
99KB

  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 # port_id, N, N
  61. ACTION_PORT_RENAME = 5 # port_id, N, 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.port_out_id == self.m_port_id or connection.port_in_id == self.m_port_id:
  1424. connection.widget.setLocked(True)
  1425. if not self.m_line_mov:
  1426. if options.use_bezier_lines:
  1427. self.m_line_mov = CanvasBezierLineMov(self.m_port_mode, self.m_port_type, self)
  1428. else:
  1429. self.m_line_mov = CanvasLineMov(self.m_port_mode, self.m_port_type, self)
  1430. canvas.last_z_value += 1
  1431. self.m_line_mov.setZValue(canvas.last_z_value)
  1432. canvas.last_z_value += 1
  1433. self.parentItem().setZValue(canvas.last_z_value)
  1434. item = None
  1435. items = canvas.scene.items(event.scenePos(), Qt.ContainsItemShape, Qt.AscendingOrder)
  1436. for i in range(len(items)):
  1437. if items[i].type() == CanvasPortType:
  1438. if items[i] != self:
  1439. if not item:
  1440. item = items[i]
  1441. elif items[i].parentItem().zValue() > item.parentItem().zValue():
  1442. item = items[i]
  1443. if self.m_hover_item and self.m_hover_item != item:
  1444. self.m_hover_item.setSelected(False)
  1445. if item:
  1446. if item.getPortMode() != self.m_port_mode and item.getPortType() == self.m_port_type:
  1447. item.setSelected(True)
  1448. self.m_hover_item = item
  1449. else:
  1450. self.m_hover_item = None
  1451. else:
  1452. self.m_hover_item = None
  1453. self.m_line_mov.updateLinePos(event.scenePos())
  1454. return event.accept()
  1455. QGraphicsItem.mouseMoveEvent(self, event)
  1456. def mouseReleaseEvent(self, event):
  1457. if self.m_mouse_down:
  1458. if self.m_line_mov:
  1459. self.m_line_mov.deleteFromScene()
  1460. self.m_line_mov = None
  1461. for connection in canvas.connection_list:
  1462. if connection.port_out_id == self.m_port_id or connection.port_in_id == self.m_port_id:
  1463. connection.widget.setLocked(False)
  1464. if self.m_hover_item:
  1465. # TODO: a better way to check already existing connection
  1466. for connection in canvas.connection_list:
  1467. if ( (connection.port_out_id == self.m_port_id and connection.port_in_id == self.m_hover_item.getPortId()) or
  1468. (connection.port_out_id == self.m_hover_item.getPortId() and connection.port_in_id == self.m_port_id) ):
  1469. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  1470. break
  1471. else:
  1472. if self.m_port_mode == PORT_MODE_OUTPUT:
  1473. conn = "%i:%i:%i:%i" % (self.m_group_id, self.m_port_id, self.m_hover_item.getGroupId(), self.m_hover_item.getPortId())
  1474. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  1475. else:
  1476. conn = "%i:%i:%i:%i" % (self.m_hover_item.getGroupId(), self.m_hover_item.getPortId(), self.m_group_id, self.m_port_id)
  1477. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  1478. canvas.scene.clearSelection()
  1479. if self.m_cursor_moving:
  1480. self.setCursor(QCursor(Qt.ArrowCursor))
  1481. self.m_hover_item = None
  1482. self.m_mouse_down = False
  1483. self.m_cursor_moving = False
  1484. QGraphicsItem.mouseReleaseEvent(self, event)
  1485. def contextMenuEvent(self, event):
  1486. canvas.scene.clearSelection()
  1487. self.setSelected(True)
  1488. menu = QMenu()
  1489. discMenu = QMenu("Disconnect", menu)
  1490. conn_list = CanvasGetPortConnectionList(self.m_group_id, self.m_port_id)
  1491. if len(conn_list) > 0:
  1492. for conn_id, group_id, port_id in conn_list:
  1493. act_x_disc = discMenu.addAction(CanvasGetFullPortName(group_id, port_id))
  1494. act_x_disc.setData(conn_id)
  1495. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  1496. else:
  1497. act_x_disc = discMenu.addAction("No connections")
  1498. act_x_disc.setEnabled(False)
  1499. menu.addMenu(discMenu)
  1500. act_x_disc_all = menu.addAction("Disconnect &All")
  1501. act_x_sep_1 = menu.addSeparator()
  1502. act_x_info = menu.addAction("Get &Info")
  1503. act_x_rename = menu.addAction("&Rename")
  1504. if not features.port_info:
  1505. act_x_info.setVisible(False)
  1506. if not features.port_rename:
  1507. act_x_rename.setVisible(False)
  1508. if not (features.port_info and features.port_rename):
  1509. act_x_sep_1.setVisible(False)
  1510. act_selected = menu.exec_(event.screenPos())
  1511. if act_selected == act_x_disc_all:
  1512. for conn_id, group_id, port_id in conn_list:
  1513. canvas.callback(ACTION_PORTS_DISCONNECT, conn_id, 0, "")
  1514. elif act_selected == act_x_info:
  1515. canvas.callback(ACTION_PORT_INFO, self.m_port_id, 0, "")
  1516. elif act_selected == act_x_rename:
  1517. new_name_try = QInputDialog.getText(None, "Rename Port", "New name:", QLineEdit.Normal, self.m_port_name)
  1518. if new_name_try[1] and new_name_try[0]: # 1 - bool ok, 0 - return text
  1519. canvas.callback(ACTION_PORT_RENAME, self.m_port_id, 0, new_name_try[0])
  1520. event.accept()
  1521. def boundingRect(self):
  1522. return QRectF(0, 0, self.m_port_width + 12, self.m_port_height)
  1523. def paint(self, painter, option, widget):
  1524. painter.save()
  1525. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  1526. poly_locx = [0, 0, 0, 0, 0]
  1527. if self.m_port_mode == PORT_MODE_INPUT:
  1528. text_pos = QPointF(3, canvas.theme.port_text_ypos)
  1529. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  1530. poly_locx[0] = 0
  1531. poly_locx[1] = self.m_port_width + 5
  1532. poly_locx[2] = self.m_port_width + 12
  1533. poly_locx[3] = self.m_port_width + 5
  1534. poly_locx[4] = 0
  1535. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  1536. poly_locx[0] = 0
  1537. poly_locx[1] = self.m_port_width + 5
  1538. poly_locx[2] = self.m_port_width + 5
  1539. poly_locx[3] = self.m_port_width + 5
  1540. poly_locx[4] = 0
  1541. else:
  1542. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  1543. return
  1544. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1545. text_pos = QPointF(9, canvas.theme.port_text_ypos)
  1546. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  1547. poly_locx[0] = self.m_port_width + 12
  1548. poly_locx[1] = 7
  1549. poly_locx[2] = 0
  1550. poly_locx[3] = 7
  1551. poly_locx[4] = self.m_port_width + 12
  1552. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  1553. poly_locx[0] = self.m_port_width + 12
  1554. poly_locx[1] = 5
  1555. poly_locx[2] = 5
  1556. poly_locx[3] = 5
  1557. poly_locx[4] = self.m_port_width + 12
  1558. else:
  1559. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  1560. return
  1561. else:
  1562. qCritical("PatchCanvas::CanvasPort.paint() - invalid port mode '%s'" % port_mode2str(self.m_port_mode))
  1563. return
  1564. if self.m_port_type == PORT_TYPE_AUDIO_JACK:
  1565. poly_color = canvas.theme.port_audio_jack_bg_sel if self.isSelected() else canvas.theme.port_audio_jack_bg
  1566. poly_pen = canvas.theme.port_audio_jack_pen_sel if self.isSelected() else canvas.theme.port_audio_jack_pen
  1567. text_pen = canvas.theme.port_audio_jack_text_sel if self.isSelected() else canvas.theme.port_audio_jack_text
  1568. conn_pen = canvas.theme.port_audio_jack_pen_sel
  1569. elif self.m_port_type == PORT_TYPE_MIDI_JACK:
  1570. poly_color = canvas.theme.port_midi_jack_bg_sel if self.isSelected() else canvas.theme.port_midi_jack_bg
  1571. poly_pen = canvas.theme.port_midi_jack_pen_sel if self.isSelected() else canvas.theme.port_midi_jack_pen
  1572. text_pen = canvas.theme.port_midi_jack_text_sel if self.isSelected() else canvas.theme.port_midi_jack_text
  1573. conn_pen = canvas.theme.port_midi_jack_pen_sel
  1574. elif self.m_port_type == PORT_TYPE_MIDI_ALSA:
  1575. poly_color = canvas.theme.port_midi_alsa_bg_sel if self.isSelected() else canvas.theme.port_midi_alsa_bg
  1576. poly_pen = canvas.theme.port_midi_alsa_pen_sel if self.isSelected() else canvas.theme.port_midi_alsa_pen
  1577. text_pen = canvas.theme.port_midi_alsa_text_sel if self.isSelected() else canvas.theme.port_midi_alsa_text
  1578. conn_pen = canvas.theme.port_midi_alsa_pen_sel
  1579. elif self.m_port_type == PORT_TYPE_PARAMETER:
  1580. poly_color = canvas.theme.port_parameter_bg_sel if self.isSelected() else canvas.theme.port_parameter_bg
  1581. poly_pen = canvas.theme.port_parameter_pen_sel if self.isSelected() else canvas.theme.port_parameter_pen
  1582. text_pen = canvas.theme.port_parameter_text_sel if self.isSelected() else canvas.theme.port_parameter_text
  1583. conn_pen = canvas.theme.port_parameter_pen_sel
  1584. else:
  1585. qCritical("PatchCanvas::CanvasPort.paint() - invalid port type '%s'" % port_type2str(self.m_port_type))
  1586. return
  1587. if self.m_is_alternate:
  1588. poly_color = poly_color.darker(180)
  1589. #poly_pen.setColor(poly_pen.color().darker(110))
  1590. #text_pen.setColor(text_pen.color()) #.darker(150))
  1591. #conn_pen.setColor(conn_pen.color()) #.darker(150))
  1592. polygon = QPolygonF()
  1593. polygon += QPointF(poly_locx[0], 0)
  1594. polygon += QPointF(poly_locx[1], 0)
  1595. polygon += QPointF(poly_locx[2], float(canvas.theme.port_height)/2)
  1596. polygon += QPointF(poly_locx[3], canvas.theme.port_height)
  1597. polygon += QPointF(poly_locx[4], canvas.theme.port_height)
  1598. if canvas.theme.port_bg_pixmap:
  1599. portRect = polygon.boundingRect()
  1600. portPos = portRect.topLeft()
  1601. painter.drawTiledPixmap(portRect, canvas.theme.port_bg_pixmap, portPos)
  1602. else:
  1603. painter.setBrush(poly_color) #.lighter(200))
  1604. painter.setPen(poly_pen)
  1605. painter.drawPolygon(polygon)
  1606. painter.setPen(text_pen)
  1607. painter.setFont(self.m_port_font)
  1608. painter.drawText(text_pos, self.m_port_name)
  1609. if self.isSelected() != self.m_last_selected_state:
  1610. for connection in canvas.connection_list:
  1611. if connection.port_out_id == self.m_port_id or connection.port_in_id == self.m_port_id:
  1612. connection.widget.setLineSelected(self.isSelected())
  1613. if canvas.theme.idx == Theme.THEME_OOSTUDIO and canvas.theme.port_bg_pixmap:
  1614. painter.setPen(Qt.NoPen)
  1615. painter.setBrush(conn_pen.brush())
  1616. if self.m_port_mode == PORT_MODE_INPUT:
  1617. connRect = QRectF(portRect.topLeft(), QSizeF(2, portRect.height()))
  1618. else:
  1619. connRect = QRectF(QPointF(portRect.right()-2, portRect.top()), QSizeF(2, portRect.height()))
  1620. painter.drawRect(connRect)
  1621. self.m_last_selected_state = self.isSelected()
  1622. painter.restore()
  1623. # ------------------------------------------------------------------------------
  1624. # canvasbox.cpp
  1625. class cb_line_t(object):
  1626. __slots__ = [
  1627. 'line',
  1628. 'connection_id'
  1629. ]
  1630. class CanvasBox(QGraphicsItem):
  1631. def __init__(self, group_id, group_name, icon, parent=None):
  1632. QGraphicsItem.__init__(self, parent)
  1633. # Save Variables, useful for later
  1634. self.m_group_id = group_id
  1635. self.m_group_name = group_name
  1636. # plugin Id, < 0 if invalid
  1637. self.m_plugin_id = -1
  1638. self.m_plugin_ui = False
  1639. # Base Variables
  1640. self.p_width = 50
  1641. self.p_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing + 1
  1642. self.m_last_pos = QPointF()
  1643. self.m_splitted = False
  1644. self.m_splitted_mode = PORT_MODE_NULL
  1645. self.m_cursor_moving = False
  1646. self.m_forced_split = False
  1647. self.m_mouse_down = False
  1648. self.m_port_list_ids = []
  1649. self.m_connection_lines = []
  1650. # Set Font
  1651. self.m_font_name = QFont()
  1652. self.m_font_name.setFamily(canvas.theme.box_font_name)
  1653. self.m_font_name.setPixelSize(canvas.theme.box_font_size)
  1654. self.m_font_name.setWeight(canvas.theme.box_font_state)
  1655. self.m_font_port = QFont()
  1656. self.m_font_port.setFamily(canvas.theme.port_font_name)
  1657. self.m_font_port.setPixelSize(canvas.theme.port_font_size)
  1658. self.m_font_port.setWeight(canvas.theme.port_font_state)
  1659. # Icon
  1660. if canvas.theme.box_use_icon:
  1661. self.icon_svg = CanvasIcon(icon, self.m_group_name, self)
  1662. else:
  1663. self.icon_svg = None
  1664. # Shadow
  1665. if options.eyecandy:
  1666. self.shadow = CanvasBoxShadow(self.toGraphicsObject())
  1667. self.shadow.setFakeParent(self)
  1668. self.setGraphicsEffect(self.shadow)
  1669. else:
  1670. self.shadow = None
  1671. # Final touches
  1672. self.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
  1673. # Wait for at least 1 port
  1674. if options.auto_hide_groups:
  1675. self.setVisible(False)
  1676. self.setFlag(QGraphicsItem.ItemIsFocusable, True)
  1677. self.updatePositions()
  1678. canvas.scene.addItem(self)
  1679. def getGroupId(self):
  1680. return self.m_group_id
  1681. def getGroupName(self):
  1682. return self.m_group_name
  1683. def isSplitted(self):
  1684. return self.m_splitted
  1685. def getSplittedMode(self):
  1686. return self.m_splitted_mode
  1687. def getPortCount(self):
  1688. return len(self.m_port_list_ids)
  1689. def getPortList(self):
  1690. return self.m_port_list_ids
  1691. def setAsPlugin(self, plugin_id, hasUi):
  1692. self.m_plugin_id = plugin_id
  1693. self.m_plugin_ui = hasUi
  1694. def setIcon(self, icon):
  1695. if self.icon_svg:
  1696. self.icon_svg.setIcon(icon, self.m_group_name)
  1697. def setSplit(self, split, mode=PORT_MODE_NULL):
  1698. self.m_splitted = split
  1699. self.m_splitted_mode = mode
  1700. def setGroupName(self, group_name):
  1701. self.m_group_name = group_name
  1702. self.updatePositions()
  1703. def setShadowOpacity(self, opacity):
  1704. if self.shadow:
  1705. self.shadow.setOpacity(opacity)
  1706. def addPortFromGroup(self, port_id, port_mode, port_type, port_name, is_alternate):
  1707. if len(self.m_port_list_ids) == 0:
  1708. if options.auto_hide_groups:
  1709. if options.eyecandy == EYECANDY_FULL:
  1710. CanvasItemFX(self, True)
  1711. self.setVisible(True)
  1712. new_widget = CanvasPort(self.m_group_id, port_id, port_name, port_mode, port_type, is_alternate, self)
  1713. port_dict = port_dict_t()
  1714. port_dict.group_id = self.m_group_id
  1715. port_dict.port_id = port_id
  1716. port_dict.port_name = port_name
  1717. port_dict.port_mode = port_mode
  1718. port_dict.port_type = port_type
  1719. port_dict.is_alternate = is_alternate
  1720. port_dict.widget = new_widget
  1721. self.m_port_list_ids.append(port_id)
  1722. return new_widget
  1723. def removePortFromGroup(self, port_id):
  1724. if port_id in self.m_port_list_ids:
  1725. self.m_port_list_ids.remove(port_id)
  1726. else:
  1727. qCritical("PatchCanvas::CanvasBox.removePort(%i) - unable to find port to remove" % port_id)
  1728. return
  1729. if len(self.m_port_list_ids) > 0:
  1730. self.updatePositions()
  1731. elif self.isVisible():
  1732. if options.auto_hide_groups:
  1733. if options.eyecandy == EYECANDY_FULL:
  1734. CanvasItemFX(self, False)
  1735. else:
  1736. self.setVisible(False)
  1737. def addLineFromGroup(self, line, connection_id):
  1738. new_cbline = cb_line_t()
  1739. new_cbline.line = line
  1740. new_cbline.connection_id = connection_id
  1741. self.m_connection_lines.append(new_cbline)
  1742. def removeLineFromGroup(self, connection_id):
  1743. for connection in self.m_connection_lines:
  1744. if connection.connection_id == connection_id:
  1745. self.m_connection_lines.remove(connection)
  1746. return
  1747. qCritical("PatchCanvas::CanvasBox.removeLineFromGroup(%i) - unable to find line to remove" % connection_id)
  1748. def checkItemPos(self):
  1749. if not canvas.size_rect.isNull():
  1750. pos = self.scenePos()
  1751. if not (canvas.size_rect.contains(pos) and canvas.size_rect.contains(pos + QPointF(self.p_width, self.p_height))):
  1752. if pos.x() < canvas.size_rect.x():
  1753. self.setPos(canvas.size_rect.x(), pos.y())
  1754. elif pos.x() + self.p_width > canvas.size_rect.width():
  1755. self.setPos(canvas.size_rect.width() - self.p_width, pos.y())
  1756. pos = self.scenePos()
  1757. if pos.y() < canvas.size_rect.y():
  1758. self.setPos(pos.x(), canvas.size_rect.y())
  1759. elif pos.y() + self.p_height > canvas.size_rect.height():
  1760. self.setPos(pos.x(), canvas.size_rect.height() - self.p_height)
  1761. def removeIconFromScene(self):
  1762. if self.icon_svg:
  1763. canvas.scene.removeItem(self.icon_svg)
  1764. def updatePositions(self):
  1765. self.prepareGeometryChange()
  1766. max_in_width = 0
  1767. max_in_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1768. max_out_width = 0
  1769. max_out_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1770. port_spacing = canvas.theme.port_height + canvas.theme.port_spacing
  1771. have_audio_jack_in = have_midi_jack_in = have_midi_alsa_in = have_parameter_in = False
  1772. have_audio_jack_out = have_midi_jack_out = have_midi_alsa_out = have_parameter_out = False
  1773. # reset box size
  1774. self.p_width = 50
  1775. self.p_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing + 1
  1776. # Check Text Name size
  1777. app_name_size = QFontMetrics(self.m_font_name).width(self.m_group_name) + 30
  1778. if app_name_size > self.p_width:
  1779. self.p_width = app_name_size
  1780. # Get Port List
  1781. port_list = []
  1782. for port in canvas.port_list:
  1783. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  1784. port_list.append(port)
  1785. # Get Max Box Width/Height
  1786. for port in port_list:
  1787. if port.port_mode == PORT_MODE_INPUT:
  1788. max_in_height += port_spacing
  1789. size = QFontMetrics(self.m_font_port).width(port.port_name)
  1790. if size > max_in_width:
  1791. max_in_width = size
  1792. if port.port_type == PORT_TYPE_AUDIO_JACK and not have_audio_jack_in:
  1793. have_audio_jack_in = True
  1794. max_in_height += canvas.theme.port_spacingT
  1795. elif port.port_type == PORT_TYPE_MIDI_JACK and not have_midi_jack_in:
  1796. have_midi_jack_in = True
  1797. max_in_height += canvas.theme.port_spacingT
  1798. elif port.port_type == PORT_TYPE_MIDI_ALSA and not have_midi_alsa_in:
  1799. have_midi_alsa_in = True
  1800. max_in_height += canvas.theme.port_spacingT
  1801. elif port.port_type == PORT_TYPE_PARAMETER and not have_parameter_in:
  1802. have_parameter_in = True
  1803. max_in_height += canvas.theme.port_spacingT
  1804. elif port.port_mode == PORT_MODE_OUTPUT:
  1805. max_out_height += port_spacing
  1806. size = QFontMetrics(self.m_font_port).width(port.port_name)
  1807. if size > max_out_width:
  1808. max_out_width = size
  1809. if port.port_type == PORT_TYPE_AUDIO_JACK and not have_audio_jack_out:
  1810. have_audio_jack_out = True
  1811. max_out_height += canvas.theme.port_spacingT
  1812. elif port.port_type == PORT_TYPE_MIDI_JACK and not have_midi_jack_out:
  1813. have_midi_jack_out = True
  1814. max_out_height += canvas.theme.port_spacingT
  1815. elif port.port_type == PORT_TYPE_MIDI_ALSA and not have_midi_alsa_out:
  1816. have_midi_alsa_out = True
  1817. max_out_height += canvas.theme.port_spacingT
  1818. elif port.port_type == PORT_TYPE_PARAMETER and not have_parameter_out:
  1819. have_parameter_out = True
  1820. max_out_height += canvas.theme.port_spacingT
  1821. if canvas.theme.port_spacingT == 0:
  1822. max_in_height += 2
  1823. max_out_height += 2
  1824. final_width = 30 + max_in_width + max_out_width
  1825. if final_width > self.p_width:
  1826. self.p_width = final_width
  1827. if max_in_height > self.p_height:
  1828. self.p_height = max_in_height
  1829. if max_out_height > self.p_height:
  1830. self.p_height = max_out_height
  1831. # Remove bottom space
  1832. self.p_height -= canvas.theme.port_spacingT
  1833. if canvas.theme.box_header_spacing > 0:
  1834. if len(port_list) == 0:
  1835. self.p_height -= canvas.theme.box_header_spacing
  1836. else:
  1837. self.p_height -= canvas.theme.box_header_spacing/2
  1838. last_in_pos = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1839. last_out_pos = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1840. last_in_type = PORT_TYPE_NULL
  1841. last_out_type = PORT_TYPE_NULL
  1842. # Re-position ports, AUDIO_JACK
  1843. for port in port_list:
  1844. if port.port_type != PORT_TYPE_AUDIO_JACK:
  1845. continue
  1846. if port.port_mode == PORT_MODE_INPUT:
  1847. if last_in_type != PORT_TYPE_NULL and port.port_type != last_in_type:
  1848. last_in_pos += canvas.theme.port_spacingT
  1849. port.widget.setPos(QPointF(1 + canvas.theme.port_offset, last_in_pos))
  1850. port.widget.setPortWidth(max_in_width)
  1851. last_in_pos += port_spacing
  1852. last_in_type = port.port_type
  1853. elif port.port_mode == PORT_MODE_OUTPUT:
  1854. if last_out_type != PORT_TYPE_NULL and port.port_type != last_out_type:
  1855. last_out_pos += canvas.theme.port_spacingT
  1856. port.widget.setPos(QPointF(self.p_width - max_out_width - canvas.theme.port_offset - 13, last_out_pos))
  1857. port.widget.setPortWidth(max_out_width)
  1858. last_out_pos += port_spacing
  1859. last_out_type = port.port_type
  1860. # Re-position ports, MIDI_JACK
  1861. for port in port_list:
  1862. if port.port_type != PORT_TYPE_MIDI_JACK:
  1863. continue
  1864. if port.port_mode == PORT_MODE_INPUT:
  1865. if last_in_type != PORT_TYPE_NULL and port.port_type != last_in_type:
  1866. last_in_pos += canvas.theme.port_spacingT
  1867. port.widget.setPos(QPointF(1 + canvas.theme.port_offset, last_in_pos))
  1868. port.widget.setPortWidth(max_in_width)
  1869. last_in_pos += port_spacing
  1870. last_in_type = port.port_type
  1871. elif port.port_mode == PORT_MODE_OUTPUT:
  1872. if last_out_type != PORT_TYPE_NULL and port.port_type != last_out_type:
  1873. last_out_pos += canvas.theme.port_spacingT
  1874. port.widget.setPos(QPointF(self.p_width - max_out_width - canvas.theme.port_offset - 13, last_out_pos))
  1875. port.widget.setPortWidth(max_out_width)
  1876. last_out_pos += port_spacing
  1877. last_out_type = port.port_type
  1878. # Re-position ports, MIDI_ALSA
  1879. for port in port_list:
  1880. if port.port_type != PORT_TYPE_MIDI_ALSA:
  1881. continue
  1882. if port.port_mode == PORT_MODE_INPUT:
  1883. if last_in_type != PORT_TYPE_NULL and port.port_type != last_in_type:
  1884. last_in_pos += canvas.theme.port_spacingT
  1885. port.widget.setPos(QPointF(1 + canvas.theme.port_offset, last_in_pos))
  1886. port.widget.setPortWidth(max_in_width)
  1887. last_in_pos += port_spacing
  1888. last_in_type = port.port_type
  1889. elif port.port_mode == PORT_MODE_OUTPUT:
  1890. if last_out_type != PORT_TYPE_NULL and port.port_type != last_out_type:
  1891. last_out_pos += canvas.theme.port_spacingT
  1892. port.widget.setPos(QPointF(self.p_width - max_out_width - canvas.theme.port_offset - 13, last_out_pos))
  1893. port.widget.setPortWidth(max_out_width)
  1894. last_out_pos += port_spacing
  1895. last_out_type = port.port_type
  1896. # Re-position ports, PARAMETER
  1897. for port in port_list:
  1898. if port.port_type != PORT_TYPE_PARAMETER:
  1899. continue
  1900. if port.port_mode == PORT_MODE_INPUT:
  1901. if last_in_type != PORT_TYPE_NULL and port.port_type != last_in_type:
  1902. last_in_pos += canvas.theme.port_spacingT
  1903. port.widget.setPos(QPointF(1 + canvas.theme.port_offset, last_in_pos))
  1904. port.widget.setPortWidth(max_in_width)
  1905. last_in_pos += port_spacing
  1906. last_in_type = port.port_type
  1907. elif port.port_mode == PORT_MODE_OUTPUT:
  1908. if last_out_type != PORT_TYPE_NULL and port.port_type != last_out_type:
  1909. last_out_pos += canvas.theme.port_spacingT
  1910. port.widget.setPos(QPointF(self.p_width - max_out_width - canvas.theme.port_offset - 13, last_out_pos))
  1911. port.widget.setPortWidth(max_out_width)
  1912. last_out_pos += port_spacing
  1913. last_out_type = port.port_type
  1914. self.repaintLines(True)
  1915. self.update()
  1916. def repaintLines(self, forced=False):
  1917. if self.pos() != self.m_last_pos or forced:
  1918. for connection in self.m_connection_lines:
  1919. connection.line.updateLinePos()
  1920. self.m_last_pos = self.pos()
  1921. def resetLinesZValue(self):
  1922. for connection in canvas.connection_list:
  1923. if connection.port_out_id in self.m_port_list_ids and connection.port_in_id in self.m_port_list_ids:
  1924. z_value = canvas.last_z_value
  1925. else:
  1926. z_value = canvas.last_z_value - 1
  1927. connection.widget.setZValue(z_value)
  1928. def type(self):
  1929. return CanvasBoxType
  1930. def contextMenuEvent(self, event):
  1931. menu = QMenu()
  1932. discMenu = QMenu("Disconnect", menu)
  1933. conn_list = []
  1934. conn_list_ids = []
  1935. for port_id in self.m_port_list_ids:
  1936. tmp_conn_list = CanvasGetPortConnectionList(self.m_group_id, port_id)
  1937. for conn_id, group_id, port_id in tmp_conn_list:
  1938. if conn_id not in conn_list_ids:
  1939. conn_list.append((conn_id, group_id, port_id))
  1940. conn_list_ids.append(conn_id)
  1941. if len(conn_list) > 0:
  1942. for conn_id, group_id, port_id in conn_list:
  1943. act_x_disc = discMenu.addAction(CanvasGetFullPortName(group_id, port_id))
  1944. act_x_disc.setData(conn_id)
  1945. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  1946. else:
  1947. act_x_disc = discMenu.addAction("No connections")
  1948. act_x_disc.setEnabled(False)
  1949. menu.addMenu(discMenu)
  1950. act_x_disc_all = menu.addAction("Disconnect &All")
  1951. act_x_sep1 = menu.addSeparator()
  1952. act_x_info = menu.addAction("&Info")
  1953. act_x_rename = menu.addAction("&Rename")
  1954. act_x_sep2 = menu.addSeparator()
  1955. act_x_split_join = menu.addAction("Join" if self.m_splitted else "Split")
  1956. if not features.group_info:
  1957. act_x_info.setVisible(False)
  1958. if not features.group_rename:
  1959. act_x_rename.setVisible(False)
  1960. if not (features.group_info and features.group_rename):
  1961. act_x_sep1.setVisible(False)
  1962. if self.m_plugin_id >= 0:
  1963. menu.addSeparator()
  1964. act_p_edit = menu.addAction("&Edit")
  1965. act_p_ui = menu.addAction("&Show Custom UI")
  1966. menu.addSeparator()
  1967. act_p_clone = menu.addAction("&Clone")
  1968. act_p_rename = menu.addAction("&Rename...")
  1969. act_p_remove = menu.addAction("Re&move")
  1970. if not self.m_plugin_ui:
  1971. act_p_ui.setVisible(False)
  1972. else:
  1973. act_p_edit = act_p_ui = None
  1974. act_p_clone = act_p_rename = act_p_remove = None
  1975. haveIns = haveOuts = False
  1976. for port in canvas.port_list:
  1977. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  1978. if port.port_mode == PORT_MODE_INPUT:
  1979. haveIns = True
  1980. elif port.port_mode == PORT_MODE_OUTPUT:
  1981. haveOuts = True
  1982. if not (self.m_splitted or bool(haveIns and haveOuts)):
  1983. act_x_sep2.setVisible(False)
  1984. act_x_split_join.setVisible(False)
  1985. act_selected = menu.exec_(event.screenPos())
  1986. if act_selected is None:
  1987. pass
  1988. elif act_selected == act_x_disc_all:
  1989. for conn_id in conn_list_ids:
  1990. canvas.callback(ACTION_PORTS_DISCONNECT, conn_id, 0, "")
  1991. elif act_selected == act_x_info:
  1992. canvas.callback(ACTION_GROUP_INFO, self.m_group_id, 0, "")
  1993. elif act_selected == act_x_rename:
  1994. new_name_try = QInputDialog.getText(None, "Rename Group", "New name:", QLineEdit.Normal, self.m_group_name)
  1995. if new_name_try[1] and new_name_try[0]: # 1 - bool ok, 0 - return text
  1996. canvas.callback(ACTION_GROUP_RENAME, self.m_group_id, 0, new_name_try[0])
  1997. elif act_selected == act_x_split_join:
  1998. if self.m_splitted:
  1999. canvas.callback(ACTION_GROUP_JOIN, self.m_group_id, 0, "")
  2000. else:
  2001. canvas.callback(ACTION_GROUP_SPLIT, self.m_group_id, 0, "")
  2002. elif act_selected == act_p_edit:
  2003. canvas.callback(ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  2004. elif act_selected == act_p_ui:
  2005. canvas.callback(ACTION_PLUGIN_SHOW_UI, self.m_plugin_id, 0, "")
  2006. elif act_selected == act_p_clone:
  2007. canvas.callback(ACTION_PLUGIN_CLONE, self.m_plugin_id, 0, "")
  2008. elif act_selected == act_p_rename:
  2009. new_name_try = QInputDialog.getText(None, "Rename Plugin", "New name:", QLineEdit.Normal, self.m_group_name)
  2010. if new_name_try[1] and new_name_try[0]: # 1 - bool ok, 0 - return text
  2011. canvas.callback(ACTION_PLUGIN_RENAME, self.m_plugin_id, 0, new_name_try[0])
  2012. elif act_selected == act_p_remove:
  2013. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  2014. event.accept()
  2015. def keyPressEvent(self, event):
  2016. if self.m_plugin_id >= 0 and event.key() == Qt.Key_Delete:
  2017. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  2018. return event.accept()
  2019. QGraphicsItem.keyPressEvent(self, event)
  2020. def mouseDoubleClickEvent(self, event):
  2021. if self.m_plugin_id >= 0:
  2022. canvas.callback(ACTION_PLUGIN_SHOW_UI if self.m_plugin_ui else ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  2023. return event.accept()
  2024. QGraphicsItem.mouseDoubleClickEvent(self, event)
  2025. def mousePressEvent(self, event):
  2026. canvas.last_z_value += 1
  2027. self.setZValue(canvas.last_z_value)
  2028. self.resetLinesZValue()
  2029. self.m_cursor_moving = False
  2030. if event.button() == Qt.RightButton:
  2031. canvas.scene.clearSelection()
  2032. self.setSelected(True)
  2033. self.m_mouse_down = False
  2034. return event.accept()
  2035. elif event.button() == Qt.LeftButton:
  2036. if self.sceneBoundingRect().contains(event.scenePos()):
  2037. self.m_mouse_down = True
  2038. else:
  2039. # Fix a weird Qt behaviour with right-click mouseMove
  2040. self.m_mouse_down = False
  2041. return event.ignore()
  2042. else:
  2043. self.m_mouse_down = False
  2044. QGraphicsItem.mousePressEvent(self, event)
  2045. def mouseMoveEvent(self, event):
  2046. if self.m_mouse_down:
  2047. if not self.m_cursor_moving:
  2048. self.setCursor(QCursor(Qt.SizeAllCursor))
  2049. self.m_cursor_moving = True
  2050. self.repaintLines()
  2051. QGraphicsItem.mouseMoveEvent(self, event)
  2052. def mouseReleaseEvent(self, event):
  2053. if self.m_cursor_moving:
  2054. self.setCursor(QCursor(Qt.ArrowCursor))
  2055. self.m_mouse_down = False
  2056. self.m_cursor_moving = False
  2057. QGraphicsItem.mouseReleaseEvent(self, event)
  2058. def boundingRect(self):
  2059. return QRectF(0, 0, self.p_width, self.p_height)
  2060. def paint(self, painter, option, widget):
  2061. painter.save()
  2062. painter.setRenderHint(QPainter.Antialiasing, False)
  2063. # Draw rectangle
  2064. if self.isSelected():
  2065. painter.setPen(canvas.theme.box_pen_sel)
  2066. else:
  2067. painter.setPen(canvas.theme.box_pen)
  2068. if canvas.theme.box_bg_type == Theme.THEME_BG_GRADIENT:
  2069. box_gradient = QLinearGradient(0, 0, 0, self.p_height)
  2070. box_gradient.setColorAt(0, canvas.theme.box_bg_1)
  2071. box_gradient.setColorAt(1, canvas.theme.box_bg_2)
  2072. painter.setBrush(box_gradient)
  2073. else:
  2074. painter.setBrush(canvas.theme.box_bg_1)
  2075. painter.drawRect(0, 0, self.p_width, self.p_height)
  2076. # Draw pixmap header
  2077. if canvas.theme.box_header_pixmap:
  2078. painter.setPen(Qt.NoPen)
  2079. painter.setBrush(canvas.theme.box_bg_2)
  2080. painter.drawRect(1, 1, self.p_width-2, canvas.theme.box_header_height)
  2081. headerPos = QPointF(1, 1)
  2082. headerRect = QRectF(2, 2, self.p_width-4, canvas.theme.box_header_height-3)
  2083. painter.drawTiledPixmap(headerRect, canvas.theme.box_header_pixmap, headerPos)
  2084. # Draw text
  2085. painter.setFont(self.m_font_name)
  2086. if self.isSelected():
  2087. painter.setPen(canvas.theme.box_text_sel)
  2088. else:
  2089. painter.setPen(canvas.theme.box_text)
  2090. if canvas.theme.box_use_icon:
  2091. textPos = QPointF(25, canvas.theme.box_text_ypos)
  2092. else:
  2093. appNameSize = QFontMetrics(self.m_font_name).width(self.m_group_name)
  2094. rem = self.p_width - appNameSize
  2095. textPos = QPointF(rem/2, canvas.theme.box_text_ypos)
  2096. painter.drawText(textPos, self.m_group_name)
  2097. self.repaintLines()
  2098. painter.restore()
  2099. # ------------------------------------------------------------------------------
  2100. # canvasicon.cpp
  2101. class CanvasIcon(QGraphicsSvgItem):
  2102. def __init__(self, icon, name, parent):
  2103. QGraphicsSvgItem.__init__(self, parent)
  2104. self.m_renderer = None
  2105. self.p_size = QRectF(0, 0, 0, 0)
  2106. self.m_colorFX = QGraphicsColorizeEffect(self)
  2107. self.m_colorFX.setColor(canvas.theme.box_text.color())
  2108. self.setGraphicsEffect(self.m_colorFX)
  2109. self.setIcon(icon, name)
  2110. def setIcon(self, icon, name):
  2111. name = name.lower()
  2112. icon_path = ""
  2113. if icon == ICON_APPLICATION:
  2114. self.p_size = QRectF(3, 2, 19, 18)
  2115. if "audacious" in name:
  2116. icon_path = ":/scalable/pb_audacious.svg"
  2117. self.p_size = QRectF(5, 4, 16, 16)
  2118. elif "clementine" in name:
  2119. icon_path = ":/scalable/pb_clementine.svg"
  2120. self.p_size = QRectF(5, 4, 16, 16)
  2121. elif "distrho" in name:
  2122. icon_path = ":/scalable/pb_distrho.svg"
  2123. self.p_size = QRectF(5, 4, 14, 14)
  2124. elif "jamin" in name:
  2125. icon_path = ":/scalable/pb_jamin.svg"
  2126. self.p_size = QRectF(5, 3, 16, 16)
  2127. elif "mplayer" in name:
  2128. icon_path = ":/scalable/pb_mplayer.svg"
  2129. self.p_size = QRectF(5, 4, 16, 16)
  2130. elif "vlc" in name:
  2131. icon_path = ":/scalable/pb_vlc.svg"
  2132. self.p_size = QRectF(5, 3, 16, 16)
  2133. else:
  2134. icon_path = ":/scalable/pb_generic.svg"
  2135. self.p_size = QRectF(4, 3, 16, 16)
  2136. elif icon == ICON_HARDWARE:
  2137. icon_path = ":/scalable/pb_hardware.svg"
  2138. self.p_size = QRectF(5, 2, 16, 16)
  2139. elif icon == ICON_DISTRHO:
  2140. icon_path = ":/scalable/pb_distrho.svg"
  2141. self.p_size = QRectF(5, 4, 14, 14)
  2142. elif icon == ICON_FILE:
  2143. icon_path = ":/scalable/pb_file.svg"
  2144. self.p_size = QRectF(5, 4, 12, 14)
  2145. elif icon == ICON_PLUGIN:
  2146. icon_path = ":/scalable/pb_plugin.svg"
  2147. self.p_size = QRectF(5, 4, 14, 14)
  2148. elif icon == ICON_LADISH_ROOM:
  2149. # TODO - make a unique ladish-room icon
  2150. icon_path = ":/scalable/pb_hardware.svg"
  2151. self.p_size = QRectF(5, 2, 16, 16)
  2152. else:
  2153. self.p_size = QRectF(0, 0, 0, 0)
  2154. qCritical("PatchCanvas::CanvasIcon.setIcon(%s, %s) - unsupported icon requested" % (icon2str(icon), name.encode()))
  2155. return
  2156. self.m_renderer = QSvgRenderer(icon_path, canvas.scene)
  2157. self.setSharedRenderer(self.m_renderer)
  2158. self.update()
  2159. def type(self):
  2160. return CanvasIconType
  2161. def boundingRect(self):
  2162. return self.p_size
  2163. def paint(self, painter, option, widget):
  2164. if self.m_renderer:
  2165. painter.save()
  2166. painter.setRenderHint(QPainter.Antialiasing, False)
  2167. painter.setRenderHint(QPainter.TextAntialiasing, False)
  2168. self.m_renderer.render(painter, self.p_size)
  2169. painter.restore()
  2170. else:
  2171. QGraphicsSvgItem.paint(self, painter, option, widget)
  2172. # ------------------------------------------------------------------------------
  2173. # canvasportglow.cpp
  2174. class CanvasPortGlow(QGraphicsDropShadowEffect):
  2175. def __init__(self, port_type, parent):
  2176. QGraphicsDropShadowEffect.__init__(self, parent)
  2177. self.setBlurRadius(12)
  2178. self.setOffset(0, 0)
  2179. if port_type == PORT_TYPE_AUDIO_JACK:
  2180. self.setColor(canvas.theme.line_audio_jack_glow)
  2181. elif port_type == PORT_TYPE_MIDI_JACK:
  2182. self.setColor(canvas.theme.line_midi_jack_glow)
  2183. elif port_type == PORT_TYPE_MIDI_ALSA:
  2184. self.setColor(canvas.theme.line_midi_alsa_glow)
  2185. elif port_type == PORT_TYPE_PARAMETER:
  2186. self.setColor(canvas.theme.line_parameter_glow)
  2187. # ------------------------------------------------------------------------------
  2188. # canvasboxshadow.cpp
  2189. class CanvasBoxShadow(QGraphicsDropShadowEffect):
  2190. def __init__(self, parent):
  2191. QGraphicsDropShadowEffect.__init__(self, parent)
  2192. self.m_fakeParent = None
  2193. self.setBlurRadius(20)
  2194. self.setColor(canvas.theme.box_shadow)
  2195. self.setOffset(0, 0)
  2196. def setFakeParent(self, fakeParent):
  2197. self.m_fakeParent = fakeParent
  2198. def setOpacity(self, opacity):
  2199. color = QColor(canvas.theme.box_shadow)
  2200. color.setAlphaF(opacity)
  2201. self.setColor(color)
  2202. def draw(self, painter):
  2203. if self.m_fakeParent:
  2204. self.m_fakeParent.repaintLines()
  2205. QGraphicsDropShadowEffect.draw(self, painter)