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.

2853 lines
100KB

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