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.

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