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.

2897 lines
102KB

  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 setPortValue(group_id, port_id, value):
  692. if canvas.debug:
  693. qDebug("PatchCanvas::setPortValue(%i, %i, %f)" % (group_id, port_id, value))
  694. for port in canvas.port_list:
  695. if port.group_id == group_id and port.port_id == port_id:
  696. port.widget.setPortValue(value)
  697. return
  698. qCritical("PatchCanvas::setPortValue(%i, %i, %f) - Unable to find port" % (group_id, port_id, value))
  699. def connectPorts(connection_id, group_out_id, port_out_id, group_in_id, port_in_id):
  700. if canvas.debug:
  701. qDebug("PatchCanvas::connectPorts(%i, %i, %i, %i, %i)" % (connection_id, group_out_id, port_out_id, group_in_id, port_in_id))
  702. port_out = None
  703. port_in = None
  704. port_out_parent = None
  705. port_in_parent = None
  706. for port in canvas.port_list:
  707. if port.group_id == group_out_id and port.port_id == port_out_id:
  708. port_out = port.widget
  709. port_out_parent = port_out.parentItem()
  710. elif port.group_id == group_in_id and port.port_id == port_in_id:
  711. port_in = port.widget
  712. port_in_parent = port_in.parentItem()
  713. # FIXME
  714. if not (port_out and port_in):
  715. 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))
  716. return
  717. connection_dict = connection_dict_t()
  718. connection_dict.connection_id = connection_id
  719. connection_dict.group_in_id = group_in_id
  720. connection_dict.port_in_id = port_in_id
  721. connection_dict.group_out_id = group_out_id
  722. connection_dict.port_out_id = port_out_id
  723. if options.use_bezier_lines:
  724. connection_dict.widget = CanvasBezierLine(port_out, port_in, None)
  725. else:
  726. connection_dict.widget = CanvasLine(port_out, port_in, None)
  727. canvas.scene.addItem(connection_dict.widget)
  728. port_out_parent.addLineFromGroup(connection_dict.widget, connection_id)
  729. port_in_parent.addLineFromGroup(connection_dict.widget, connection_id)
  730. canvas.last_z_value += 1
  731. port_out_parent.setZValue(canvas.last_z_value)
  732. port_in_parent.setZValue(canvas.last_z_value)
  733. canvas.last_z_value += 1
  734. connection_dict.widget.setZValue(canvas.last_z_value)
  735. canvas.connection_list.append(connection_dict)
  736. if options.eyecandy == EYECANDY_FULL:
  737. item = connection_dict.widget
  738. CanvasItemFX(item, True)
  739. QTimer.singleShot(0, canvas.scene.update)
  740. def disconnectPorts(connection_id):
  741. if canvas.debug:
  742. qDebug("PatchCanvas::disconnectPorts(%i)" % connection_id)
  743. port_1_id = port_2_id = 0
  744. line = None
  745. item1 = None
  746. item2 = None
  747. for connection in canvas.connection_list:
  748. if connection.connection_id == connection_id:
  749. port_1_id = connection.port_out_id
  750. port_2_id = connection.port_in_id
  751. line = connection.widget
  752. canvas.connection_list.remove(connection)
  753. break
  754. if not line:
  755. qCritical("PatchCanvas::disconnectPorts(%i) - unable to find connection ports" % connection_id)
  756. return
  757. for port in canvas.port_list:
  758. if port.port_id == port_1_id:
  759. item1 = port.widget
  760. break
  761. if not item1:
  762. qCritical("PatchCanvas::disconnectPorts(%i) - unable to find output port" % connection_id)
  763. return
  764. for port in canvas.port_list:
  765. if port.port_id == port_2_id:
  766. item2 = port.widget
  767. break
  768. if not item2:
  769. qCritical("PatchCanvas::disconnectPorts(%i) - unable to find input port" % connection_id)
  770. return
  771. item1.parentItem().removeLineFromGroup(connection_id)
  772. item2.parentItem().removeLineFromGroup(connection_id)
  773. if options.eyecandy == EYECANDY_FULL:
  774. CanvasItemFX(line, False, True)
  775. else:
  776. line.deleteFromScene()
  777. QTimer.singleShot(0, canvas.scene.update)
  778. def arrange():
  779. if canvas.debug:
  780. qDebug("PatchCanvas::arrange()")
  781. def updateZValues():
  782. if canvas.debug:
  783. qDebug("PatchCanvas::updateZValues()")
  784. for group in canvas.group_list:
  785. group.widgets[0].resetLinesZValue()
  786. if group.split and group.widgets[1]:
  787. group.widgets[1].resetLinesZValue()
  788. def handlePluginRemoved(plugin_id):
  789. if canvas.debug:
  790. qDebug("PatchCanvas::handlePluginRemoved(%i)" % plugin_id)
  791. for group in canvas.group_list:
  792. if group.plugin_id < plugin_id:
  793. continue
  794. group.plugin_id -= 1
  795. group.widgets[0].m_plugin_id -= 1
  796. if group.split and group.widgets[1]:
  797. group.widgets[1].m_plugin_id -= 1
  798. # Extra Internal functions
  799. def CanvasGetGroupName(group_id):
  800. if canvas.debug:
  801. qDebug("PatchCanvas::CanvasGetGroupName(%i)" % group_id)
  802. for group in canvas.group_list:
  803. if group.group_id == group_id:
  804. return group.group_name
  805. qCritical("PatchCanvas::CanvasGetGroupName(%i) - unable to find group" % group_id)
  806. return ""
  807. def CanvasGetGroupPortCount(group_id):
  808. if canvas.debug:
  809. qDebug("PatchCanvas::CanvasGetGroupPortCount(%i)" % group_id)
  810. port_count = 0
  811. for port in canvas.port_list:
  812. if port.group_id == group_id:
  813. port_count += 1
  814. return port_count
  815. def CanvasGetNewGroupPos(horizontal=False):
  816. if canvas.debug:
  817. qDebug("PatchCanvas::CanvasGetNewGroupPos(%s)" % bool2str(horizontal))
  818. new_pos = QPointF(canvas.initial_pos.x(), canvas.initial_pos.y())
  819. items = canvas.scene.items()
  820. break_loop = False
  821. while not break_loop:
  822. break_for = False
  823. for i in range(len(items)):
  824. item = items[i]
  825. if item and item.type() == CanvasBoxType:
  826. if item.sceneBoundingRect().contains(new_pos):
  827. if horizontal:
  828. new_pos += QPointF(item.boundingRect().width() + 15, 0)
  829. else:
  830. new_pos += QPointF(0, item.boundingRect().height() + 15)
  831. break_for = True
  832. break
  833. if i >= len(items) - 1 and not break_for:
  834. break_loop = True
  835. return new_pos
  836. def CanvasGetFullPortName(group_id, port_id):
  837. if canvas.debug:
  838. qDebug("PatchCanvas::CanvasGetFullPortName(%i, %i)" % (group_id, port_id))
  839. for port in canvas.port_list:
  840. if port.group_id == group_id and port.port_id == port_id:
  841. group_id = port.group_id
  842. for group in canvas.group_list:
  843. if group.group_id == group_id:
  844. return group.group_name + ":" + port.port_name
  845. break
  846. qCritical("PatchCanvas::CanvasGetFullPortName(%i, %i) - unable to find port" % (group_id, port_id))
  847. return ""
  848. def CanvasGetPortConnectionList(group_id, port_id):
  849. if canvas.debug:
  850. qDebug("PatchCanvas::CanvasGetPortConnectionList(%i, %i)" % (group_id, port_id))
  851. port_con_list = []
  852. for connection in canvas.connection_list:
  853. if connection.port_out_id == port_id or connection.port_in_id == port_id:
  854. port_con_list.append(connection.connection_id)
  855. return port_con_list
  856. def CanvasGetConnectedPort(connection_id, port_id):
  857. if canvas.debug:
  858. qDebug("PatchCanvas::CanvasGetConnectedPort(%i, %i)" % (connection_id, port_id))
  859. for connection in canvas.connection_list:
  860. if connection.connection_id == connection_id:
  861. if connection.port_out_id == port_id:
  862. return connection.port_in_id
  863. else:
  864. return connection.port_out_id
  865. qCritical("PatchCanvas::CanvasGetConnectedPort(%i, %i) - unable to find connection" % (connection_id, port_id))
  866. return 0
  867. def CanvasRemoveAnimation(f_animation):
  868. if canvas.debug:
  869. qDebug("PatchCanvas::CanvasRemoveAnimation(%s)" % f_animation)
  870. for animation in canvas.animation_list:
  871. if animation.animation == f_animation:
  872. canvas.animation_list.remove(animation)
  873. del animation.animation
  874. break
  875. def CanvasCallback(action, value1, value2, value_str):
  876. if canvas.debug:
  877. qDebug("PatchCanvas::CanvasCallback(%i, %i, %i, %s)" % (action, value1, value2, value_str.encode()))
  878. canvas.callback(action, value1, value2, value_str)
  879. def CanvasItemFX(item, show, destroy=False):
  880. if canvas.debug:
  881. qDebug("PatchCanvas::CanvasItemFX(%s, %s, %s)" % (item, bool2str(show), bool2str(destroy)))
  882. # Check if item already has an animationItemFX
  883. for animation in canvas.animation_list:
  884. if animation.item == item:
  885. animation.animation.stop()
  886. canvas.animation_list.remove(animation)
  887. del animation.animation
  888. break
  889. animation = CanvasFadeAnimation(item, show)
  890. animation.setDuration(750 if show else 500)
  891. animation_dict = animation_dict_t()
  892. animation_dict.animation = animation
  893. animation_dict.item = item
  894. canvas.animation_list.append(animation_dict)
  895. if show:
  896. animation.finished.connect(canvas.qobject.AnimationIdle)
  897. else:
  898. if destroy:
  899. animation.finished.connect(canvas.qobject.AnimationDestroy)
  900. else:
  901. animation.finished.connect(canvas.qobject.AnimationHide)
  902. animation.start()
  903. def CanvasRemoveItemFX(item):
  904. if canvas.debug:
  905. qDebug("PatchCanvas::CanvasRemoveItemFX(%s)" % item)
  906. if item.type() == CanvasBoxType:
  907. item.removeIconFromScene()
  908. canvas.scene.removeItem(item)
  909. elif item.type() == CanvasPortType:
  910. canvas.scene.removeItem(item)
  911. elif item.type() in (CanvasLineType, CanvasBezierLineType):
  912. pass #item.deleteFromScene()
  913. # Force deletion of item if needed
  914. if item.type() in (CanvasBoxType, CanvasPortType):
  915. del item
  916. # ------------------------------------------------------------------------------
  917. # patchscene.cpp
  918. class PatchScene(QGraphicsScene):
  919. scaleChanged = pyqtSignal(float)
  920. sceneGroupMoved = pyqtSignal(int, int, QPointF)
  921. pluginSelected = pyqtSignal(list)
  922. def __init__(self, parent, view):
  923. QGraphicsScene.__init__(self, parent)
  924. self.m_ctrl_down = False
  925. self.m_mouse_down_init = False
  926. self.m_mouse_rubberband = False
  927. self.addRubberBand()
  928. self.m_view = view
  929. if not self.m_view:
  930. qFatal("PatchCanvas::PatchScene() - invalid view")
  931. self.selectionChanged.connect(self.slot_selectionChanged)
  932. def addRubberBand(self):
  933. self.m_rubberband = self.addRect(QRectF(0, 0, 0, 0))
  934. self.m_rubberband.setZValue(-1)
  935. self.m_rubberband.hide()
  936. self.m_rubberband_selection = False
  937. self.m_rubberband_orig_point = QPointF(0, 0)
  938. def clear(self):
  939. QGraphicsScene.clear(self)
  940. # Re-add rubberband, that just got deleted
  941. self.addRubberBand()
  942. def fixScaleFactor(self):
  943. scale = self.m_view.transform().m11()
  944. if scale > 3.0:
  945. self.m_view.resetTransform()
  946. self.m_view.scale(3.0, 3.0)
  947. elif scale < 0.2:
  948. self.m_view.resetTransform()
  949. self.m_view.scale(0.2, 0.2)
  950. self.scaleChanged.emit(self.m_view.transform().m11())
  951. def updateTheme(self):
  952. self.setBackgroundBrush(canvas.theme.canvas_bg)
  953. self.m_rubberband.setPen(canvas.theme.rubberband_pen)
  954. self.m_rubberband.setBrush(canvas.theme.rubberband_brush)
  955. def zoom_fit(self):
  956. min_x = min_y = max_x = max_y = None
  957. first_value = True
  958. items_list = self.items()
  959. if len(items_list) > 0:
  960. for item in items_list:
  961. if item and item.isVisible() and item.type() == CanvasBoxType:
  962. pos = item.scenePos()
  963. rect = item.boundingRect()
  964. if first_value:
  965. min_x = pos.x()
  966. elif pos.x() < min_x:
  967. min_x = pos.x()
  968. if first_value:
  969. min_y = pos.y()
  970. elif pos.y() < min_y:
  971. min_y = pos.y()
  972. if first_value:
  973. max_x = pos.x() + rect.width()
  974. elif pos.x() + rect.width() > max_x:
  975. max_x = pos.x() + rect.width()
  976. if first_value:
  977. max_y = pos.y() + rect.height()
  978. elif pos.y() + rect.height() > max_y:
  979. max_y = pos.y() + rect.height()
  980. first_value = False
  981. if not first_value:
  982. self.m_view.fitInView(min_x, min_y, abs(max_x - min_x), abs(max_y - min_y), Qt.KeepAspectRatio)
  983. self.fixScaleFactor()
  984. def zoom_in(self):
  985. if self.m_view.transform().m11() < 3.0:
  986. self.m_view.scale(1.2, 1.2)
  987. self.scaleChanged.emit(self.m_view.transform().m11())
  988. def zoom_out(self):
  989. if self.m_view.transform().m11() > 0.2:
  990. self.m_view.scale(0.8, 0.8)
  991. self.scaleChanged.emit(self.m_view.transform().m11())
  992. def zoom_reset(self):
  993. self.m_view.resetTransform()
  994. self.scaleChanged.emit(1.0)
  995. @pyqtSlot()
  996. def slot_selectionChanged(self):
  997. items_list = self.selectedItems()
  998. if len(items_list) == 0:
  999. self.pluginSelected.emit([])
  1000. return
  1001. plugin_list = []
  1002. for item in items_list:
  1003. if item and item.isVisible():
  1004. group_item = None
  1005. if item.type() == CanvasBoxType:
  1006. group_item = item
  1007. elif item.type() == CanvasPortType:
  1008. group_item = item.parentItem()
  1009. #elif item.type() in (CanvasLineType, CanvasBezierLineType, CanvasLineMovType, CanvasBezierLineMovType):
  1010. #plugin_list = []
  1011. #break
  1012. if group_item is not None and group_item.m_plugin_id >= 0:
  1013. plugin_list.append(group_item.m_plugin_id)
  1014. self.pluginSelected.emit(plugin_list)
  1015. def keyPressEvent(self, event):
  1016. if not self.m_view:
  1017. return event.ignore()
  1018. if event.key() == Qt.Key_Control:
  1019. self.m_ctrl_down = True
  1020. elif event.key() == Qt.Key_Home:
  1021. self.zoom_fit()
  1022. return event.accept()
  1023. elif self.m_ctrl_down:
  1024. if event.key() == Qt.Key_Plus:
  1025. self.zoom_in()
  1026. return event.accept()
  1027. elif event.key() == Qt.Key_Minus:
  1028. self.zoom_out()
  1029. return event.accept()
  1030. elif event.key() == Qt.Key_1:
  1031. self.zoom_reset()
  1032. return event.accept()
  1033. QGraphicsScene.keyPressEvent(self, event)
  1034. def keyReleaseEvent(self, event):
  1035. if event.key() == Qt.Key_Control:
  1036. self.m_ctrl_down = False
  1037. QGraphicsScene.keyReleaseEvent(self, event)
  1038. def mousePressEvent(self, event):
  1039. self.m_mouse_down_init = bool(event.button() == Qt.LeftButton)
  1040. self.m_mouse_rubberband = False
  1041. QGraphicsScene.mousePressEvent(self, event)
  1042. def mouseMoveEvent(self, event):
  1043. if self.m_mouse_down_init:
  1044. self.m_mouse_down_init = False
  1045. self.m_mouse_rubberband = bool(len(self.selectedItems()) == 0)
  1046. if self.m_mouse_rubberband:
  1047. if not self.m_rubberband_selection:
  1048. self.m_rubberband.show()
  1049. self.m_rubberband_selection = True
  1050. self.m_rubberband_orig_point = event.scenePos()
  1051. pos = event.scenePos()
  1052. if pos.x() > self.m_rubberband_orig_point.x():
  1053. x = self.m_rubberband_orig_point.x()
  1054. else:
  1055. x = pos.x()
  1056. if pos.y() > self.m_rubberband_orig_point.y():
  1057. y = self.m_rubberband_orig_point.y()
  1058. else:
  1059. y = pos.y()
  1060. self.m_rubberband.setRect(x, y, abs(pos.x() - self.m_rubberband_orig_point.x()), abs(pos.y() - self.m_rubberband_orig_point.y()))
  1061. return event.accept()
  1062. QGraphicsScene.mouseMoveEvent(self, event)
  1063. def mouseReleaseEvent(self, event):
  1064. if self.m_rubberband_selection:
  1065. items_list = self.items()
  1066. if len(items_list) > 0:
  1067. for item in items_list:
  1068. if item and item.isVisible() and item.type() == CanvasBoxType:
  1069. item_rect = item.sceneBoundingRect()
  1070. item_top_left = QPointF(item_rect.x(), item_rect.y())
  1071. item_bottom_right = QPointF(item_rect.x() + item_rect.width(), item_rect.y() + item_rect.height())
  1072. if self.m_rubberband.contains(item_top_left) and self.m_rubberband.contains(item_bottom_right):
  1073. item.setSelected(True)
  1074. self.m_rubberband.hide()
  1075. self.m_rubberband.setRect(0, 0, 0, 0)
  1076. self.m_rubberband_selection = False
  1077. else:
  1078. items_list = self.selectedItems()
  1079. for item in items_list:
  1080. if item and item.isVisible() and item.type() == CanvasBoxType:
  1081. item.checkItemPos()
  1082. self.sceneGroupMoved.emit(item.getGroupId(), item.getSplittedMode(), item.scenePos())
  1083. if len(items_list) > 1:
  1084. canvas.scene.update()
  1085. self.m_mouse_down_init = False
  1086. self.m_mouse_rubberband = False
  1087. QGraphicsScene.mouseReleaseEvent(self, event)
  1088. def wheelEvent(self, event):
  1089. if not self.m_view:
  1090. return event.ignore()
  1091. if self.m_ctrl_down:
  1092. factor = 1.41 ** (event.delta() / 240.0)
  1093. self.m_view.scale(factor, factor)
  1094. self.fixScaleFactor()
  1095. return event.accept()
  1096. QGraphicsScene.wheelEvent(self, event)
  1097. # ------------------------------------------------------------------------------
  1098. # canvasfadeanimation.cpp
  1099. class CanvasFadeAnimation(QAbstractAnimation):
  1100. def __init__(self, item, show, parent=None):
  1101. QAbstractAnimation.__init__(self, parent)
  1102. self.m_show = show
  1103. self.m_duration = 0
  1104. self.m_item = item
  1105. def item(self):
  1106. return self.m_item
  1107. def setDuration(self, time):
  1108. if self.m_item.opacity() == 0 and not self.m_show:
  1109. self._duration = 0
  1110. else:
  1111. self.m_item.show()
  1112. self.m_duration = time
  1113. def duration(self):
  1114. return self.m_duration
  1115. def updateCurrentTime(self, time):
  1116. if self.m_duration == 0:
  1117. return
  1118. if self.m_show:
  1119. value = float(time) / self.m_duration
  1120. else:
  1121. value = 1.0 - (float(time) / self.m_duration)
  1122. self.m_item.setOpacity(value)
  1123. if self.m_item.type() == CanvasBoxType:
  1124. self.m_item.setShadowOpacity(value)
  1125. def updateDirection(self, direction):
  1126. pass
  1127. def updateState(self, oldState, newState):
  1128. pass
  1129. # ------------------------------------------------------------------------------
  1130. # canvasline.cpp
  1131. class CanvasLine(QGraphicsLineItem):
  1132. def __init__(self, item1, item2, parent):
  1133. QGraphicsLineItem.__init__(self, parent)
  1134. self.item1 = item1
  1135. self.item2 = item2
  1136. self.m_locked = False
  1137. self.m_lineSelected = False
  1138. self.setGraphicsEffect(None)
  1139. self.updateLinePos()
  1140. def deleteFromScene(self):
  1141. canvas.scene.removeItem(self)
  1142. del self
  1143. def isLocked(self):
  1144. return self.m_locked
  1145. def setLocked(self, yesno):
  1146. self.m_locked = yesno
  1147. def isLineSelected(self):
  1148. return self.m_lineSelected
  1149. def setLineSelected(self, yesno):
  1150. if self.m_locked:
  1151. return
  1152. if options.eyecandy == EYECANDY_FULL:
  1153. if yesno:
  1154. self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
  1155. else:
  1156. self.setGraphicsEffect(None)
  1157. self.m_lineSelected = yesno
  1158. self.updateLineGradient()
  1159. def updateLinePos(self):
  1160. if self.item1.getPortMode() == PORT_MODE_OUTPUT:
  1161. line = QLineF(self.item1.scenePos().x() + self.item1.getPortWidth() + 12,
  1162. self.item1.scenePos().y() + float(canvas.theme.port_height)/2,
  1163. self.item2.scenePos().x(),
  1164. self.item2.scenePos().y() + float(canvas.theme.port_height)/2)
  1165. self.setLine(line)
  1166. self.m_lineSelected = False
  1167. self.updateLineGradient()
  1168. def type(self):
  1169. return CanvasLineType
  1170. def updateLineGradient(self):
  1171. pos_top = self.boundingRect().top()
  1172. pos_bot = self.boundingRect().bottom()
  1173. if self.item2.scenePos().y() >= self.item1.scenePos().y():
  1174. pos1 = 0
  1175. pos2 = 1
  1176. else:
  1177. pos1 = 1
  1178. pos2 = 0
  1179. port_type1 = self.item1.getPortType()
  1180. port_type2 = self.item2.getPortType()
  1181. port_gradient = QLinearGradient(0, pos_top, 0, pos_bot)
  1182. if port_type1 == PORT_TYPE_AUDIO_JACK:
  1183. port_gradient.setColorAt(pos1, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1184. elif port_type1 == PORT_TYPE_MIDI_JACK:
  1185. port_gradient.setColorAt(pos1, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1186. elif port_type1 == PORT_TYPE_MIDI_ALSA:
  1187. port_gradient.setColorAt(pos1, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1188. elif port_type1 == PORT_TYPE_PARAMETER:
  1189. port_gradient.setColorAt(pos1, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1190. if port_type2 == PORT_TYPE_AUDIO_JACK:
  1191. port_gradient.setColorAt(pos2, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1192. elif port_type2 == PORT_TYPE_MIDI_JACK:
  1193. port_gradient.setColorAt(pos2, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1194. elif port_type2 == PORT_TYPE_MIDI_ALSA:
  1195. port_gradient.setColorAt(pos2, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1196. elif port_type2 == PORT_TYPE_PARAMETER:
  1197. port_gradient.setColorAt(pos2, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1198. self.setPen(QPen(port_gradient, 2))
  1199. def paint(self, painter, option, widget):
  1200. painter.save()
  1201. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1202. QGraphicsLineItem.paint(self, painter, option, widget)
  1203. painter.restore()
  1204. # ------------------------------------------------------------------------------
  1205. # canvasbezierline.cpp
  1206. class CanvasBezierLine(QGraphicsPathItem):
  1207. def __init__(self, item1, item2, parent):
  1208. QGraphicsPathItem.__init__(self, parent)
  1209. self.item1 = item1
  1210. self.item2 = item2
  1211. self.m_locked = False
  1212. self.m_lineSelected = False
  1213. self.setBrush(QColor(0, 0, 0, 0))
  1214. self.setGraphicsEffect(None)
  1215. self.updateLinePos()
  1216. def deleteFromScene(self):
  1217. canvas.scene.removeItem(self)
  1218. del self
  1219. def isLocked(self):
  1220. return self.m_locked
  1221. def setLocked(self, yesno):
  1222. self.m_locked = yesno
  1223. def isLineSelected(self):
  1224. return self.m_lineSelected
  1225. def setLineSelected(self, yesno):
  1226. if self.m_locked:
  1227. return
  1228. if options.eyecandy == EYECANDY_FULL:
  1229. if yesno:
  1230. self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
  1231. else:
  1232. self.setGraphicsEffect(None)
  1233. self.m_lineSelected = yesno
  1234. self.updateLineGradient()
  1235. def updateLinePos(self):
  1236. if self.item1.getPortMode() == PORT_MODE_OUTPUT:
  1237. item1_x = self.item1.scenePos().x() + self.item1.getPortWidth() + 12
  1238. item1_y = self.item1.scenePos().y() + float(canvas.theme.port_height)/2
  1239. item2_x = self.item2.scenePos().x()
  1240. item2_y = self.item2.scenePos().y() + float(canvas.theme.port_height)/2
  1241. item1_mid_x = abs(item1_x - item2_x) / 2
  1242. item1_new_x = item1_x + item1_mid_x
  1243. item2_mid_x = abs(item1_x - item2_x) / 2
  1244. item2_new_x = item2_x - item2_mid_x
  1245. path = QPainterPath(QPointF(item1_x, item1_y))
  1246. path.cubicTo(item1_new_x, item1_y, item2_new_x, item2_y, item2_x, item2_y)
  1247. self.setPath(path)
  1248. self.m_lineSelected = False
  1249. self.updateLineGradient()
  1250. def type(self):
  1251. return CanvasBezierLineType
  1252. def updateLineGradient(self):
  1253. pos_top = self.boundingRect().top()
  1254. pos_bot = self.boundingRect().bottom()
  1255. if self.item2.scenePos().y() >= self.item1.scenePos().y():
  1256. pos1 = 0
  1257. pos2 = 1
  1258. else:
  1259. pos1 = 1
  1260. pos2 = 0
  1261. port_type1 = self.item1.getPortType()
  1262. port_type2 = self.item2.getPortType()
  1263. port_gradient = QLinearGradient(0, pos_top, 0, pos_bot)
  1264. if port_type1 == PORT_TYPE_AUDIO_JACK:
  1265. port_gradient.setColorAt(pos1, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1266. elif port_type1 == PORT_TYPE_MIDI_JACK:
  1267. port_gradient.setColorAt(pos1, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1268. elif port_type1 == PORT_TYPE_MIDI_ALSA:
  1269. port_gradient.setColorAt(pos1, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1270. elif port_type1 == PORT_TYPE_PARAMETER:
  1271. port_gradient.setColorAt(pos1, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1272. if port_type2 == PORT_TYPE_AUDIO_JACK:
  1273. port_gradient.setColorAt(pos2, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  1274. elif port_type2 == PORT_TYPE_MIDI_JACK:
  1275. port_gradient.setColorAt(pos2, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  1276. elif port_type2 == PORT_TYPE_MIDI_ALSA:
  1277. port_gradient.setColorAt(pos2, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  1278. elif port_type2 == PORT_TYPE_PARAMETER:
  1279. port_gradient.setColorAt(pos2, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  1280. self.setPen(QPen(port_gradient, 2))
  1281. def paint(self, painter, option, widget):
  1282. painter.save()
  1283. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1284. QGraphicsPathItem.paint(self, painter, option, widget)
  1285. painter.restore()
  1286. # ------------------------------------------------------------------------------
  1287. # canvaslivemov.cpp
  1288. class CanvasLineMov(QGraphicsLineItem):
  1289. def __init__(self, port_mode, port_type, parent):
  1290. QGraphicsLineItem.__init__(self, parent)
  1291. self.m_port_mode = port_mode
  1292. self.m_port_type = port_type
  1293. # Port position doesn't change while moving around line
  1294. self.p_lineX = self.scenePos().x()
  1295. self.p_lineY = self.scenePos().y()
  1296. self.p_width = self.parentItem().getPortWidth()
  1297. if port_type == PORT_TYPE_AUDIO_JACK:
  1298. pen = QPen(canvas.theme.line_audio_jack, 2)
  1299. elif port_type == PORT_TYPE_MIDI_JACK:
  1300. pen = QPen(canvas.theme.line_midi_jack, 2)
  1301. elif port_type == PORT_TYPE_MIDI_ALSA:
  1302. pen = QPen(canvas.theme.line_midi_alsa, 2)
  1303. elif port_type == PORT_TYPE_PARAMETER:
  1304. pen = QPen(canvas.theme.line_parameter, 2)
  1305. else:
  1306. qWarning("PatchCanvas::CanvasLineMov(%s, %s, %s) - invalid port type" % (port_mode2str(port_mode), port_type2str(port_type), parent))
  1307. pen = QPen(Qt.black)
  1308. self.setPen(pen)
  1309. def deleteFromScene(self):
  1310. canvas.scene.removeItem(self)
  1311. del self
  1312. def updateLinePos(self, scenePos):
  1313. item_pos = [0, 0]
  1314. if self.m_port_mode == PORT_MODE_INPUT:
  1315. item_pos[0] = 0
  1316. item_pos[1] = float(canvas.theme.port_height)/2
  1317. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1318. item_pos[0] = self.p_width + 12
  1319. item_pos[1] = float(canvas.theme.port_height)/2
  1320. else:
  1321. return
  1322. line = QLineF(item_pos[0], item_pos[1], scenePos.x() - self.p_lineX, scenePos.y() - self.p_lineY)
  1323. self.setLine(line)
  1324. def type(self):
  1325. return CanvasLineMovType
  1326. def paint(self, painter, option, widget):
  1327. painter.save()
  1328. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1329. QGraphicsLineItem.paint(self, painter, option, widget)
  1330. painter.restore()
  1331. # ------------------------------------------------------------------------------
  1332. # canvasbezierlinemov.cpp
  1333. class CanvasBezierLineMov(QGraphicsPathItem):
  1334. def __init__(self, port_mode, port_type, parent):
  1335. QGraphicsPathItem.__init__(self, parent)
  1336. self.m_port_mode = port_mode
  1337. self.m_port_type = port_type
  1338. # Port position doesn't change while moving around line
  1339. self.p_itemX = self.scenePos().x()
  1340. self.p_itemY = self.scenePos().y()
  1341. self.p_width = self.parentItem().getPortWidth()
  1342. if port_type == PORT_TYPE_AUDIO_JACK:
  1343. pen = QPen(canvas.theme.line_audio_jack, 2)
  1344. elif port_type == PORT_TYPE_MIDI_JACK:
  1345. pen = QPen(canvas.theme.line_midi_jack, 2)
  1346. elif port_type == PORT_TYPE_MIDI_ALSA:
  1347. pen = QPen(canvas.theme.line_midi_alsa, 2)
  1348. elif port_type == PORT_TYPE_PARAMETER:
  1349. pen = QPen(canvas.theme.line_parameter, 2)
  1350. else:
  1351. qWarning("PatchCanvas::CanvasBezierLineMov(%s, %s, %s) - invalid port type" % (port_mode2str(port_mode), port_type2str(port_type), parent))
  1352. pen = QPen(Qt.black)
  1353. self.setBrush(QColor(0, 0, 0, 0))
  1354. self.setPen(pen)
  1355. def deleteFromScene(self):
  1356. canvas.scene.removeItem(self)
  1357. del self
  1358. def updateLinePos(self, scenePos):
  1359. if self.m_port_mode == PORT_MODE_INPUT:
  1360. old_x = 0
  1361. old_y = float(canvas.theme.port_height)/2
  1362. mid_x = abs(scenePos.x() - self.p_itemX) / 2
  1363. new_x = old_x - mid_x
  1364. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1365. old_x = self.p_width + 12
  1366. old_y = float(canvas.theme.port_height)/2
  1367. mid_x = abs(scenePos.x() - (self.p_itemX + old_x)) / 2
  1368. new_x = old_x + mid_x
  1369. else:
  1370. return
  1371. final_x = scenePos.x() - self.p_itemX
  1372. final_y = scenePos.y() - self.p_itemY
  1373. path = QPainterPath(QPointF(old_x, old_y))
  1374. path.cubicTo(new_x, old_y, new_x, final_y, final_x, final_y)
  1375. self.setPath(path)
  1376. def type(self):
  1377. return CanvasBezierLineMovType
  1378. def paint(self, painter, option, widget):
  1379. painter.save()
  1380. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  1381. QGraphicsPathItem.paint(self, painter, option, widget)
  1382. painter.restore()
  1383. # ------------------------------------------------------------------------------
  1384. # canvasport.cpp
  1385. class CanvasPort(QGraphicsItem):
  1386. def __init__(self, group_id, port_id, port_name, port_mode, port_type, is_alternate, parent):
  1387. QGraphicsItem.__init__(self, parent)
  1388. # Save Variables, useful for later
  1389. self.m_group_id = group_id
  1390. self.m_port_id = port_id
  1391. self.m_port_mode = port_mode
  1392. self.m_port_type = port_type
  1393. self.m_port_name = port_name
  1394. self.m_is_alternate = is_alternate
  1395. # Base Variables
  1396. self.m_port_width = 15
  1397. self.m_port_height = canvas.theme.port_height
  1398. self.m_port_font = QFont() # FIXME
  1399. self.m_port_font.setFamily(canvas.theme.port_font_name)
  1400. self.m_port_font.setPointSize(canvas.theme.port_font_size)
  1401. self.m_port_font.setWeight(canvas.theme.port_font_state)
  1402. self.m_port_value = 1.0
  1403. self.m_line_mov = None
  1404. self.m_hover_item = None
  1405. self.m_last_selected_state = False
  1406. self.m_mouse_down = False
  1407. self.m_cursor_moving = False
  1408. self.setFlags(QGraphicsItem.ItemIsSelectable)
  1409. def getGroupId(self):
  1410. return self.m_group_id
  1411. def getPortId(self):
  1412. return self.m_port_id
  1413. def getPortMode(self):
  1414. return self.m_port_mode
  1415. def getPortType(self):
  1416. return self.m_port_type
  1417. def getPortName(self):
  1418. return self.m_port_name
  1419. def getFullPortName(self):
  1420. return self.parentItem().getGroupName() + ":" + self.m_port_name
  1421. def getPortWidth(self):
  1422. return self.m_port_width
  1423. def getPortHeight(self):
  1424. return self.m_port_height
  1425. def setPortMode(self, port_mode):
  1426. self.m_port_mode = port_mode
  1427. self.update()
  1428. def setPortType(self, port_type):
  1429. self.m_port_type = port_type
  1430. self.update()
  1431. def setPortValue(self, value):
  1432. if self.m_port_value == value:
  1433. return
  1434. self.m_port_value = value
  1435. self.update()
  1436. def setPortName(self, port_name):
  1437. if QFontMetrics(self.m_port_font).width(port_name) < QFontMetrics(self.m_port_font).width(self.m_port_name):
  1438. QTimer.singleShot(0, canvas.scene.update)
  1439. self.m_port_name = port_name
  1440. self.update()
  1441. def setPortWidth(self, port_width):
  1442. if port_width < self.m_port_width:
  1443. QTimer.singleShot(0, canvas.scene.update)
  1444. self.m_port_width = port_width
  1445. self.update()
  1446. def type(self):
  1447. return CanvasPortType
  1448. def mousePressEvent(self, event):
  1449. self.m_hover_item = None
  1450. self.m_mouse_down = bool(event.button() == Qt.LeftButton)
  1451. self.m_cursor_moving = False
  1452. QGraphicsItem.mousePressEvent(self, event)
  1453. def mouseMoveEvent(self, event):
  1454. if self.m_mouse_down:
  1455. if not self.m_cursor_moving:
  1456. self.setCursor(QCursor(Qt.CrossCursor))
  1457. self.m_cursor_moving = True
  1458. for connection in canvas.connection_list:
  1459. if connection.port_out_id == self.m_port_id or connection.port_in_id == self.m_port_id:
  1460. connection.widget.setLocked(True)
  1461. if not self.m_line_mov:
  1462. if options.use_bezier_lines:
  1463. self.m_line_mov = CanvasBezierLineMov(self.m_port_mode, self.m_port_type, self)
  1464. else:
  1465. self.m_line_mov = CanvasLineMov(self.m_port_mode, self.m_port_type, self)
  1466. canvas.last_z_value += 1
  1467. self.m_line_mov.setZValue(canvas.last_z_value)
  1468. canvas.last_z_value += 1
  1469. self.parentItem().setZValue(canvas.last_z_value)
  1470. item = None
  1471. items = canvas.scene.items(event.scenePos(), Qt.ContainsItemShape, Qt.AscendingOrder)
  1472. for i in range(len(items)):
  1473. if items[i].type() == CanvasPortType:
  1474. if items[i] != self:
  1475. if not item:
  1476. item = items[i]
  1477. elif items[i].parentItem().zValue() > item.parentItem().zValue():
  1478. item = items[i]
  1479. if self.m_hover_item and self.m_hover_item != item:
  1480. self.m_hover_item.setSelected(False)
  1481. if item:
  1482. if item.getPortMode() != self.m_port_mode and item.getPortType() == self.m_port_type:
  1483. item.setSelected(True)
  1484. self.m_hover_item = item
  1485. else:
  1486. self.m_hover_item = None
  1487. else:
  1488. self.m_hover_item = None
  1489. self.m_line_mov.updateLinePos(event.scenePos())
  1490. return event.accept()
  1491. QGraphicsItem.mouseMoveEvent(self, event)
  1492. def mouseReleaseEvent(self, event):
  1493. if self.m_mouse_down:
  1494. if self.m_line_mov:
  1495. self.m_line_mov.deleteFromScene()
  1496. self.m_line_mov = None
  1497. for connection in canvas.connection_list:
  1498. if connection.port_out_id == self.m_port_id or connection.port_in_id == self.m_port_id:
  1499. connection.widget.setLocked(False)
  1500. if self.m_hover_item:
  1501. # TODO: a better way to check already existing connection
  1502. for connection in canvas.connection_list:
  1503. if ( (connection.port_out_id == self.m_port_id and connection.port_in_id == self.m_hover_item.getPortId()) or
  1504. (connection.port_out_id == self.m_hover_item.getPortId() and connection.port_in_id == self.m_port_id) ):
  1505. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  1506. break
  1507. else:
  1508. if self.m_port_mode == PORT_MODE_OUTPUT:
  1509. conn = "%i:%i:%i:%i" % (self.m_group_id, self.m_port_id, self.m_hover_item.getGroupId(), self.m_hover_item.getPortId())
  1510. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  1511. else:
  1512. conn = "%i:%i:%i:%i" % (self.m_hover_item.getGroupId(), self.m_hover_item.getPortId(), self.m_group_id, self.m_port_id)
  1513. canvas.callback(ACTION_PORTS_CONNECT, 0, 0, conn)
  1514. canvas.scene.clearSelection()
  1515. if self.m_cursor_moving:
  1516. self.setCursor(QCursor(Qt.ArrowCursor))
  1517. self.m_hover_item = None
  1518. self.m_mouse_down = False
  1519. self.m_cursor_moving = False
  1520. QGraphicsItem.mouseReleaseEvent(self, event)
  1521. def contextMenuEvent(self, event):
  1522. canvas.scene.clearSelection()
  1523. self.setSelected(True)
  1524. menu = QMenu()
  1525. discMenu = QMenu("Disconnect", menu)
  1526. port_con_list = CanvasGetPortConnectionList(self.m_group_id, self.m_port_id)
  1527. if len(port_con_list) > 0:
  1528. for port_id in port_con_list:
  1529. port_con_id = CanvasGetConnectedPort(port_id, self.m_port_id)
  1530. act_x_disc = discMenu.addAction(CanvasGetFullPortName(self.m_group_id, port_con_id))
  1531. act_x_disc.setData(port_id)
  1532. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  1533. else:
  1534. act_x_disc = discMenu.addAction("No connections")
  1535. act_x_disc.setEnabled(False)
  1536. menu.addMenu(discMenu)
  1537. act_x_disc_all = menu.addAction("Disconnect &All")
  1538. act_x_sep_1 = menu.addSeparator()
  1539. act_x_info = menu.addAction("Get &Info")
  1540. act_x_rename = menu.addAction("&Rename")
  1541. if not features.port_info:
  1542. act_x_info.setVisible(False)
  1543. if not features.port_rename:
  1544. act_x_rename.setVisible(False)
  1545. if not (features.port_info and features.port_rename):
  1546. act_x_sep_1.setVisible(False)
  1547. act_selected = menu.exec_(event.screenPos())
  1548. if act_selected == act_x_disc_all:
  1549. for port_id in port_con_list:
  1550. canvas.callback(ACTION_PORTS_DISCONNECT, port_id, 0, "")
  1551. elif act_selected == act_x_info:
  1552. canvas.callback(ACTION_PORT_INFO, self.m_port_id, 0, "")
  1553. elif act_selected == act_x_rename:
  1554. new_name_try = QInputDialog.getText(None, "Rename Port", "New name:", QLineEdit.Normal, self.m_port_name)
  1555. if new_name_try[1] and new_name_try[0]: # 1 - bool ok, 0 - return text
  1556. canvas.callback(ACTION_PORT_RENAME, self.m_port_id, 0, new_name_try[0])
  1557. event.accept()
  1558. def boundingRect(self):
  1559. return QRectF(0, 0, self.m_port_width + 12, self.m_port_height)
  1560. def paint(self, painter, option, widget):
  1561. painter.save()
  1562. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing == ANTIALIASING_FULL))
  1563. poly_locx = [0, 0, 0, 0, 0]
  1564. if self.m_port_mode == PORT_MODE_INPUT:
  1565. text_pos = QPointF(3, canvas.theme.port_text_ypos)
  1566. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  1567. poly_locx[0] = 0
  1568. poly_locx[1] = self.m_port_width + 5
  1569. poly_locx[2] = self.m_port_width + 12
  1570. poly_locx[3] = self.m_port_width + 5
  1571. poly_locx[4] = 0
  1572. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  1573. poly_locx[0] = 0
  1574. poly_locx[1] = self.m_port_width + 5
  1575. poly_locx[2] = self.m_port_width + 5
  1576. poly_locx[3] = self.m_port_width + 5
  1577. poly_locx[4] = 0
  1578. else:
  1579. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  1580. return
  1581. elif self.m_port_mode == PORT_MODE_OUTPUT:
  1582. text_pos = QPointF(9, canvas.theme.port_text_ypos)
  1583. if canvas.theme.port_mode == Theme.THEME_PORT_POLYGON:
  1584. poly_locx[0] = self.m_port_width + 12
  1585. poly_locx[1] = 7
  1586. poly_locx[2] = 0
  1587. poly_locx[3] = 7
  1588. poly_locx[4] = self.m_port_width + 12
  1589. elif canvas.theme.port_mode == Theme.THEME_PORT_SQUARE:
  1590. poly_locx[0] = self.m_port_width + 12
  1591. poly_locx[1] = 5
  1592. poly_locx[2] = 5
  1593. poly_locx[3] = 5
  1594. poly_locx[4] = self.m_port_width + 12
  1595. else:
  1596. qCritical("PatchCanvas::CanvasPort.paint() - invalid theme port mode '%s'" % canvas.theme.port_mode)
  1597. return
  1598. else:
  1599. qCritical("PatchCanvas::CanvasPort.paint() - invalid port mode '%s'" % port_mode2str(self.m_port_mode))
  1600. return
  1601. if self.m_port_type == PORT_TYPE_AUDIO_JACK:
  1602. poly_color = canvas.theme.port_audio_jack_bg_sel if self.isSelected() else canvas.theme.port_audio_jack_bg
  1603. poly_pen = canvas.theme.port_audio_jack_pen_sel if self.isSelected() else canvas.theme.port_audio_jack_pen
  1604. text_pen = canvas.theme.port_audio_jack_text_sel if self.isSelected() else canvas.theme.port_audio_jack_text
  1605. conn_pen = canvas.theme.port_audio_jack_pen_sel
  1606. elif self.m_port_type == PORT_TYPE_MIDI_JACK:
  1607. poly_color = canvas.theme.port_midi_jack_bg_sel if self.isSelected() else canvas.theme.port_midi_jack_bg
  1608. poly_pen = canvas.theme.port_midi_jack_pen_sel if self.isSelected() else canvas.theme.port_midi_jack_pen
  1609. text_pen = canvas.theme.port_midi_jack_text_sel if self.isSelected() else canvas.theme.port_midi_jack_text
  1610. conn_pen = canvas.theme.port_midi_jack_pen_sel
  1611. elif self.m_port_type == PORT_TYPE_MIDI_ALSA:
  1612. poly_color = canvas.theme.port_midi_alsa_bg_sel if self.isSelected() else canvas.theme.port_midi_alsa_bg
  1613. poly_pen = canvas.theme.port_midi_alsa_pen_sel if self.isSelected() else canvas.theme.port_midi_alsa_pen
  1614. text_pen = canvas.theme.port_midi_alsa_text_sel if self.isSelected() else canvas.theme.port_midi_alsa_text
  1615. conn_pen = canvas.theme.port_midi_alsa_pen_sel
  1616. elif self.m_port_type == PORT_TYPE_PARAMETER:
  1617. poly_color = canvas.theme.port_parameter_bg_sel if self.isSelected() else canvas.theme.port_parameter_bg
  1618. poly_pen = canvas.theme.port_parameter_pen_sel if self.isSelected() else canvas.theme.port_parameter_pen
  1619. text_pen = canvas.theme.port_parameter_text_sel if self.isSelected() else canvas.theme.port_parameter_text
  1620. conn_pen = canvas.theme.port_parameter_pen_sel
  1621. else:
  1622. qCritical("PatchCanvas::CanvasPort.paint() - invalid port type '%s'" % port_type2str(self.m_port_type))
  1623. return
  1624. if self.m_is_alternate:
  1625. poly_color = poly_color.darker(180)
  1626. #poly_pen.setColor(poly_pen.color().darker(110))
  1627. #text_pen.setColor(text_pen.color()) #.darker(150))
  1628. #conn_pen.setColor(conn_pen.color()) #.darker(150))
  1629. polygon = QPolygonF()
  1630. polygon += QPointF(poly_locx[0], 0)
  1631. polygon += QPointF(poly_locx[1], 0)
  1632. polygon += QPointF(poly_locx[2], float(canvas.theme.port_height)/2)
  1633. polygon += QPointF(poly_locx[3], canvas.theme.port_height)
  1634. polygon += QPointF(poly_locx[4], canvas.theme.port_height)
  1635. if self.m_port_value == 1.0 or canvas.theme.port_bg_pixmap:
  1636. # normal paint
  1637. if canvas.theme.port_bg_pixmap:
  1638. portRect = polygon.boundingRect()
  1639. portPos = portRect.topLeft()
  1640. painter.drawTiledPixmap(portRect, canvas.theme.port_bg_pixmap, portPos)
  1641. else:
  1642. painter.setBrush(poly_color) #.lighter(200))
  1643. painter.setPen(poly_pen)
  1644. painter.drawPolygon(polygon)
  1645. else:
  1646. # incomplete paint
  1647. painter.setPen(poly_pen)
  1648. painter.drawPolygon(polygon)
  1649. sub = QPolygonF()
  1650. if self.m_port_mode == PORT_MODE_INPUT:
  1651. sub += QPointF(poly_locx[0], 0)
  1652. sub += QPointF(poly_locx[2]*self.m_port_value, 0)
  1653. sub += QPointF(poly_locx[2]*self.m_port_value, canvas.theme.port_height)
  1654. sub += QPointF(poly_locx[0], canvas.theme.port_height)
  1655. else:
  1656. sub += QPointF(poly_locx[2], 0)
  1657. sub += QPointF(poly_locx[0]*self.m_port_value, 0)
  1658. sub += QPointF(poly_locx[0]*self.m_port_value, canvas.theme.port_height)
  1659. sub += QPointF(poly_locx[2], canvas.theme.port_height)
  1660. painter.setBrush(poly_color)
  1661. painter.setPen(poly_pen)
  1662. painter.drawPolygon(polygon.intersected(sub))
  1663. painter.setPen(text_pen)
  1664. painter.setFont(self.m_port_font)
  1665. painter.drawText(text_pos, self.m_port_name)
  1666. if self.isSelected() != self.m_last_selected_state:
  1667. for connection in canvas.connection_list:
  1668. if connection.port_out_id == self.m_port_id or connection.port_in_id == self.m_port_id:
  1669. connection.widget.setLineSelected(self.isSelected())
  1670. if canvas.theme.idx == Theme.THEME_OOSTUDIO and canvas.theme.port_bg_pixmap:
  1671. painter.setPen(Qt.NoPen)
  1672. painter.setBrush(conn_pen.brush())
  1673. if self.m_port_mode == PORT_MODE_INPUT:
  1674. connRect = QRectF(portRect.topLeft(), QSizeF(2, portRect.height()))
  1675. else:
  1676. connRect = QRectF(QPointF(portRect.right()-2, portRect.top()), QSizeF(2, portRect.height()))
  1677. painter.drawRect(connRect)
  1678. self.m_last_selected_state = self.isSelected()
  1679. painter.restore()
  1680. # ------------------------------------------------------------------------------
  1681. # canvasbox.cpp
  1682. class cb_line_t(object):
  1683. __slots__ = [
  1684. 'line',
  1685. 'connection_id'
  1686. ]
  1687. class CanvasBox(QGraphicsItem):
  1688. def __init__(self, group_id, group_name, icon, parent=None):
  1689. QGraphicsItem.__init__(self, parent)
  1690. # Save Variables, useful for later
  1691. self.m_group_id = group_id
  1692. self.m_group_name = group_name
  1693. # plugin Id, < 0 if invalid
  1694. self.m_plugin_id = -1
  1695. self.m_plugin_ui = False
  1696. # Base Variables
  1697. self.p_width = 50
  1698. self.p_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing + 1
  1699. self.m_last_pos = QPointF()
  1700. self.m_splitted = False
  1701. self.m_splitted_mode = PORT_MODE_NULL
  1702. self.m_cursor_moving = False
  1703. self.m_forced_split = False
  1704. self.m_mouse_down = False
  1705. self.m_port_list_ids = []
  1706. self.m_connection_lines = []
  1707. # Set Font
  1708. self.m_font_name = QFont() # FIXME
  1709. self.m_font_name.setFamily(canvas.theme.box_font_name)
  1710. self.m_font_name.setPointSize(canvas.theme.box_font_size)
  1711. self.m_font_name.setWeight(canvas.theme.box_font_state)
  1712. self.m_font_port = QFont() # FIXME
  1713. self.m_font_port.setFamily(canvas.theme.port_font_name)
  1714. self.m_font_port.setPointSize(canvas.theme.port_font_size)
  1715. self.m_font_port.setWeight(canvas.theme.port_font_state)
  1716. # Icon
  1717. if canvas.theme.box_use_icon:
  1718. self.icon_svg = CanvasIcon(icon, self.m_group_name, self)
  1719. else:
  1720. self.icon_svg = None
  1721. # Shadow
  1722. if options.eyecandy:
  1723. self.shadow = CanvasBoxShadow(self.toGraphicsObject())
  1724. self.shadow.setFakeParent(self)
  1725. self.setGraphicsEffect(self.shadow)
  1726. else:
  1727. self.shadow = None
  1728. # Final touches
  1729. self.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
  1730. # Wait for at least 1 port
  1731. if options.auto_hide_groups:
  1732. self.setVisible(False)
  1733. self.setFlag(QGraphicsItem.ItemIsFocusable, True)
  1734. self.updatePositions()
  1735. canvas.scene.addItem(self)
  1736. def getGroupId(self):
  1737. return self.m_group_id
  1738. def getGroupName(self):
  1739. return self.m_group_name
  1740. def isSplitted(self):
  1741. return self.m_splitted
  1742. def getSplittedMode(self):
  1743. return self.m_splitted_mode
  1744. def getPortCount(self):
  1745. return len(self.m_port_list_ids)
  1746. def getPortList(self):
  1747. return self.m_port_list_ids
  1748. def setAsPlugin(self, plugin_id, hasUi):
  1749. self.m_plugin_id = plugin_id
  1750. self.m_plugin_ui = hasUi
  1751. def setIcon(self, icon):
  1752. if self.icon_svg:
  1753. self.icon_svg.setIcon(icon, self.m_group_name)
  1754. def setSplit(self, split, mode=PORT_MODE_NULL):
  1755. self.m_splitted = split
  1756. self.m_splitted_mode = mode
  1757. def setGroupName(self, group_name):
  1758. self.m_group_name = group_name
  1759. self.updatePositions()
  1760. def setShadowOpacity(self, opacity):
  1761. if self.shadow:
  1762. self.shadow.setOpacity(opacity)
  1763. def addPortFromGroup(self, port_id, port_mode, port_type, port_name, is_alternate):
  1764. if len(self.m_port_list_ids) == 0:
  1765. if options.auto_hide_groups:
  1766. if options.eyecandy == EYECANDY_FULL:
  1767. CanvasItemFX(self, True)
  1768. self.setVisible(True)
  1769. new_widget = CanvasPort(self.m_group_id, port_id, port_name, port_mode, port_type, is_alternate, self)
  1770. port_dict = port_dict_t()
  1771. port_dict.group_id = self.m_group_id
  1772. port_dict.port_id = port_id
  1773. port_dict.port_name = port_name
  1774. port_dict.port_mode = port_mode
  1775. port_dict.port_type = port_type
  1776. port_dict.is_alternate = is_alternate
  1777. port_dict.widget = new_widget
  1778. self.m_port_list_ids.append(port_id)
  1779. return new_widget
  1780. def removePortFromGroup(self, port_id):
  1781. if port_id in self.m_port_list_ids:
  1782. self.m_port_list_ids.remove(port_id)
  1783. else:
  1784. qCritical("PatchCanvas::CanvasBox.removePort(%i) - unable to find port to remove" % port_id)
  1785. return
  1786. if len(self.m_port_list_ids) > 0:
  1787. self.updatePositions()
  1788. elif self.isVisible():
  1789. if options.auto_hide_groups:
  1790. if options.eyecandy == EYECANDY_FULL:
  1791. CanvasItemFX(self, False)
  1792. else:
  1793. self.setVisible(False)
  1794. def addLineFromGroup(self, line, connection_id):
  1795. new_cbline = cb_line_t()
  1796. new_cbline.line = line
  1797. new_cbline.connection_id = connection_id
  1798. self.m_connection_lines.append(new_cbline)
  1799. def removeLineFromGroup(self, connection_id):
  1800. for connection in self.m_connection_lines:
  1801. if connection.connection_id == connection_id:
  1802. self.m_connection_lines.remove(connection)
  1803. return
  1804. qCritical("PatchCanvas::CanvasBox.removeLineFromGroup(%i) - unable to find line to remove" % connection_id)
  1805. def checkItemPos(self):
  1806. if not canvas.size_rect.isNull():
  1807. pos = self.scenePos()
  1808. if not (canvas.size_rect.contains(pos) and canvas.size_rect.contains(pos + QPointF(self.p_width, self.p_height))):
  1809. if pos.x() < canvas.size_rect.x():
  1810. self.setPos(canvas.size_rect.x(), pos.y())
  1811. elif pos.x() + self.p_width > canvas.size_rect.width():
  1812. self.setPos(canvas.size_rect.width() - self.p_width, pos.y())
  1813. pos = self.scenePos()
  1814. if pos.y() < canvas.size_rect.y():
  1815. self.setPos(pos.x(), canvas.size_rect.y())
  1816. elif pos.y() + self.p_height > canvas.size_rect.height():
  1817. self.setPos(pos.x(), canvas.size_rect.height() - self.p_height)
  1818. def removeIconFromScene(self):
  1819. if self.icon_svg:
  1820. canvas.scene.removeItem(self.icon_svg)
  1821. def updatePositions(self):
  1822. self.prepareGeometryChange()
  1823. max_in_width = 0
  1824. max_in_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1825. max_out_width = 0
  1826. max_out_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1827. port_spacing = canvas.theme.port_height + canvas.theme.port_spacing
  1828. have_audio_jack_in = have_midi_jack_in = have_midi_alsa_in = have_parameter_in = False
  1829. have_audio_jack_out = have_midi_jack_out = have_midi_alsa_out = have_parameter_out = False
  1830. # reset box size
  1831. self.p_width = 50
  1832. self.p_height = canvas.theme.box_header_height + canvas.theme.box_header_spacing + 1
  1833. # Check Text Name size
  1834. app_name_size = QFontMetrics(self.m_font_name).width(self.m_group_name) + 30
  1835. if app_name_size > self.p_width:
  1836. self.p_width = app_name_size
  1837. # Get Port List
  1838. port_list = []
  1839. for port in canvas.port_list:
  1840. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  1841. port_list.append(port)
  1842. # Get Max Box Width/Height
  1843. for port in port_list:
  1844. if port.port_mode == PORT_MODE_INPUT:
  1845. max_in_height += port_spacing
  1846. size = QFontMetrics(self.m_font_port).width(port.port_name)
  1847. if size > max_in_width:
  1848. max_in_width = size
  1849. if port.port_type == PORT_TYPE_AUDIO_JACK and not have_audio_jack_in:
  1850. have_audio_jack_in = True
  1851. max_in_height += canvas.theme.port_spacingT
  1852. elif port.port_type == PORT_TYPE_MIDI_JACK and not have_midi_jack_in:
  1853. have_midi_jack_in = True
  1854. max_in_height += canvas.theme.port_spacingT
  1855. elif port.port_type == PORT_TYPE_MIDI_ALSA and not have_midi_alsa_in:
  1856. have_midi_alsa_in = True
  1857. max_in_height += canvas.theme.port_spacingT
  1858. elif port.port_type == PORT_TYPE_PARAMETER and not have_parameter_in:
  1859. have_parameter_in = True
  1860. max_in_height += canvas.theme.port_spacingT
  1861. elif port.port_mode == PORT_MODE_OUTPUT:
  1862. max_out_height += port_spacing
  1863. size = QFontMetrics(self.m_font_port).width(port.port_name)
  1864. if size > max_out_width:
  1865. max_out_width = size
  1866. if port.port_type == PORT_TYPE_AUDIO_JACK and not have_audio_jack_out:
  1867. have_audio_jack_out = True
  1868. max_out_height += canvas.theme.port_spacingT
  1869. elif port.port_type == PORT_TYPE_MIDI_JACK and not have_midi_jack_out:
  1870. have_midi_jack_out = True
  1871. max_out_height += canvas.theme.port_spacingT
  1872. elif port.port_type == PORT_TYPE_MIDI_ALSA and not have_midi_alsa_out:
  1873. have_midi_alsa_out = True
  1874. max_out_height += canvas.theme.port_spacingT
  1875. elif port.port_type == PORT_TYPE_PARAMETER and not have_parameter_out:
  1876. have_parameter_out = True
  1877. max_out_height += canvas.theme.port_spacingT
  1878. if canvas.theme.port_spacingT == 0:
  1879. max_in_height += 2
  1880. max_out_height += 2
  1881. final_width = 30 + max_in_width + max_out_width
  1882. if final_width > self.p_width:
  1883. self.p_width = final_width
  1884. if max_in_height > self.p_height:
  1885. self.p_height = max_in_height
  1886. if max_out_height > self.p_height:
  1887. self.p_height = max_out_height
  1888. # Remove bottom space
  1889. self.p_height -= canvas.theme.port_spacingT
  1890. if canvas.theme.box_header_spacing > 0:
  1891. if len(port_list) == 0:
  1892. self.p_height -= canvas.theme.box_header_spacing
  1893. else:
  1894. self.p_height -= canvas.theme.box_header_spacing/2
  1895. last_in_pos = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1896. last_out_pos = canvas.theme.box_header_height + canvas.theme.box_header_spacing
  1897. last_in_type = PORT_TYPE_NULL
  1898. last_out_type = PORT_TYPE_NULL
  1899. # Re-position ports, AUDIO_JACK
  1900. for port in port_list:
  1901. if port.port_type != PORT_TYPE_AUDIO_JACK:
  1902. continue
  1903. if port.port_mode == PORT_MODE_INPUT:
  1904. if last_in_type != PORT_TYPE_NULL and port.port_type != last_in_type:
  1905. last_in_pos += canvas.theme.port_spacingT
  1906. port.widget.setPos(QPointF(1 + canvas.theme.port_offset, last_in_pos))
  1907. port.widget.setPortWidth(max_in_width)
  1908. last_in_pos += port_spacing
  1909. last_in_type = port.port_type
  1910. elif port.port_mode == PORT_MODE_OUTPUT:
  1911. if last_out_type != PORT_TYPE_NULL and port.port_type != last_out_type:
  1912. last_out_pos += canvas.theme.port_spacingT
  1913. port.widget.setPos(QPointF(self.p_width - max_out_width - canvas.theme.port_offset - 13, last_out_pos))
  1914. port.widget.setPortWidth(max_out_width)
  1915. last_out_pos += port_spacing
  1916. last_out_type = port.port_type
  1917. # Re-position ports, MIDI_JACK
  1918. for port in port_list:
  1919. if port.port_type != PORT_TYPE_MIDI_JACK:
  1920. continue
  1921. if port.port_mode == PORT_MODE_INPUT:
  1922. if last_in_type != PORT_TYPE_NULL and port.port_type != last_in_type:
  1923. last_in_pos += canvas.theme.port_spacingT
  1924. port.widget.setPos(QPointF(1 + canvas.theme.port_offset, last_in_pos))
  1925. port.widget.setPortWidth(max_in_width)
  1926. last_in_pos += port_spacing
  1927. last_in_type = port.port_type
  1928. elif port.port_mode == PORT_MODE_OUTPUT:
  1929. if last_out_type != PORT_TYPE_NULL and port.port_type != last_out_type:
  1930. last_out_pos += canvas.theme.port_spacingT
  1931. port.widget.setPos(QPointF(self.p_width - max_out_width - canvas.theme.port_offset - 13, last_out_pos))
  1932. port.widget.setPortWidth(max_out_width)
  1933. last_out_pos += port_spacing
  1934. last_out_type = port.port_type
  1935. # Re-position ports, MIDI_ALSA
  1936. for port in port_list:
  1937. if port.port_type != PORT_TYPE_MIDI_ALSA:
  1938. continue
  1939. if port.port_mode == PORT_MODE_INPUT:
  1940. if last_in_type != PORT_TYPE_NULL and port.port_type != last_in_type:
  1941. last_in_pos += canvas.theme.port_spacingT
  1942. port.widget.setPos(QPointF(1 + canvas.theme.port_offset, last_in_pos))
  1943. port.widget.setPortWidth(max_in_width)
  1944. last_in_pos += port_spacing
  1945. last_in_type = port.port_type
  1946. elif port.port_mode == PORT_MODE_OUTPUT:
  1947. if last_out_type != PORT_TYPE_NULL and port.port_type != last_out_type:
  1948. last_out_pos += canvas.theme.port_spacingT
  1949. port.widget.setPos(QPointF(self.p_width - max_out_width - canvas.theme.port_offset - 13, last_out_pos))
  1950. port.widget.setPortWidth(max_out_width)
  1951. last_out_pos += port_spacing
  1952. last_out_type = port.port_type
  1953. # Re-position ports, PARAMETER
  1954. for port in port_list:
  1955. if port.port_type != PORT_TYPE_PARAMETER:
  1956. continue
  1957. if port.port_mode == PORT_MODE_INPUT:
  1958. if last_in_type != PORT_TYPE_NULL and port.port_type != last_in_type:
  1959. last_in_pos += canvas.theme.port_spacingT
  1960. port.widget.setPos(QPointF(1 + canvas.theme.port_offset, last_in_pos))
  1961. port.widget.setPortWidth(max_in_width)
  1962. last_in_pos += port_spacing
  1963. last_in_type = port.port_type
  1964. elif port.port_mode == PORT_MODE_OUTPUT:
  1965. if last_out_type != PORT_TYPE_NULL and port.port_type != last_out_type:
  1966. last_out_pos += canvas.theme.port_spacingT
  1967. port.widget.setPos(QPointF(self.p_width - max_out_width - canvas.theme.port_offset - 13, last_out_pos))
  1968. port.widget.setPortWidth(max_out_width)
  1969. last_out_pos += port_spacing
  1970. last_out_type = port.port_type
  1971. self.repaintLines(True)
  1972. self.update()
  1973. def repaintLines(self, forced=False):
  1974. if self.pos() != self.m_last_pos or forced:
  1975. for connection in self.m_connection_lines:
  1976. connection.line.updateLinePos()
  1977. self.m_last_pos = self.pos()
  1978. def resetLinesZValue(self):
  1979. for connection in canvas.connection_list:
  1980. if connection.port_out_id in self.m_port_list_ids and connection.port_in_id in self.m_port_list_ids:
  1981. z_value = canvas.last_z_value
  1982. else:
  1983. z_value = canvas.last_z_value - 1
  1984. connection.widget.setZValue(z_value)
  1985. def type(self):
  1986. return CanvasBoxType
  1987. def contextMenuEvent(self, event):
  1988. menu = QMenu()
  1989. discMenu = QMenu("Disconnect", menu)
  1990. port_con_list = []
  1991. port_con_list_ids = []
  1992. for port_id in self.m_port_list_ids:
  1993. tmp_port_con_list = CanvasGetPortConnectionList(self.m_group_id, port_id)
  1994. for port_con_id in tmp_port_con_list:
  1995. if port_con_id not in port_con_list:
  1996. port_con_list.append(port_con_id)
  1997. port_con_list_ids.append(port_id)
  1998. if len(port_con_list) > 0:
  1999. for i in range(len(port_con_list)):
  2000. port_con_id = CanvasGetConnectedPort(port_con_list[i], port_con_list_ids[i])
  2001. act_x_disc = discMenu.addAction(CanvasGetFullPortName(self.m_group_id, port_con_id))
  2002. act_x_disc.setData(port_con_list[i])
  2003. act_x_disc.triggered.connect(canvas.qobject.PortContextMenuDisconnect)
  2004. else:
  2005. act_x_disc = discMenu.addAction("No connections")
  2006. act_x_disc.setEnabled(False)
  2007. menu.addMenu(discMenu)
  2008. act_x_disc_all = menu.addAction("Disconnect &All")
  2009. act_x_sep1 = menu.addSeparator()
  2010. act_x_info = menu.addAction("&Info")
  2011. act_x_rename = menu.addAction("&Rename")
  2012. act_x_sep2 = menu.addSeparator()
  2013. act_x_split_join = menu.addAction("Join" if self.m_splitted else "Split")
  2014. if not features.group_info:
  2015. act_x_info.setVisible(False)
  2016. if not features.group_rename:
  2017. act_x_rename.setVisible(False)
  2018. if not (features.group_info and features.group_rename):
  2019. act_x_sep1.setVisible(False)
  2020. if self.m_plugin_id >= 0:
  2021. menu.addSeparator()
  2022. act_p_edit = menu.addAction("&Edit")
  2023. act_p_ui = menu.addAction("&Show Custom UI")
  2024. menu.addSeparator()
  2025. act_p_clone = menu.addAction("&Clone")
  2026. act_p_rename = menu.addAction("&Rename...")
  2027. act_p_remove = menu.addAction("Re&move")
  2028. if not self.m_plugin_ui:
  2029. act_p_ui.setVisible(False)
  2030. else:
  2031. act_p_edit = act_p_ui = None
  2032. act_p_clone = act_p_rename = act_p_remove = None
  2033. haveIns = haveOuts = False
  2034. for port in canvas.port_list:
  2035. if port.group_id == self.m_group_id and port.port_id in self.m_port_list_ids:
  2036. if port.port_mode == PORT_MODE_INPUT:
  2037. haveIns = True
  2038. elif port.port_mode == PORT_MODE_OUTPUT:
  2039. haveOuts = True
  2040. if not (self.m_splitted or bool(haveIns and haveOuts)):
  2041. act_x_sep2.setVisible(False)
  2042. act_x_split_join.setVisible(False)
  2043. act_selected = menu.exec_(event.screenPos())
  2044. if act_selected is None:
  2045. pass
  2046. elif act_selected == act_x_disc_all:
  2047. for port_id in port_con_list:
  2048. canvas.callback(ACTION_PORTS_DISCONNECT, port_id, 0, "")
  2049. elif act_selected == act_x_info:
  2050. canvas.callback(ACTION_GROUP_INFO, self.m_group_id, 0, "")
  2051. elif act_selected == act_x_rename:
  2052. new_name_try = QInputDialog.getText(None, "Rename Group", "New name:", QLineEdit.Normal, self.m_group_name)
  2053. if new_name_try[1] and new_name_try[0]: # 1 - bool ok, 0 - return text
  2054. canvas.callback(ACTION_GROUP_RENAME, self.m_group_id, 0, new_name_try[0])
  2055. elif act_selected == act_x_split_join:
  2056. if self.m_splitted:
  2057. canvas.callback(ACTION_GROUP_JOIN, self.m_group_id, 0, "")
  2058. else:
  2059. canvas.callback(ACTION_GROUP_SPLIT, self.m_group_id, 0, "")
  2060. elif act_selected == act_p_edit:
  2061. canvas.callback(ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  2062. elif act_selected == act_p_ui:
  2063. canvas.callback(ACTION_PLUGIN_SHOW_UI, self.m_plugin_id, 0, "")
  2064. elif act_selected == act_p_clone:
  2065. canvas.callback(ACTION_PLUGIN_CLONE, self.m_plugin_id, 0, "")
  2066. elif act_selected == act_p_rename:
  2067. new_name_try = QInputDialog.getText(None, "Rename Plugin", "New name:", QLineEdit.Normal, self.m_group_name)
  2068. if new_name_try[1] and new_name_try[0]: # 1 - bool ok, 0 - return text
  2069. canvas.callback(ACTION_PLUGIN_RENAME, self.m_plugin_id, 0, new_name_try[0])
  2070. elif act_selected == act_p_remove:
  2071. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  2072. event.accept()
  2073. def keyPressEvent(self, event):
  2074. if self.m_plugin_id >= 0 and event.key() == Qt.Key_Delete:
  2075. canvas.callback(ACTION_PLUGIN_REMOVE, self.m_plugin_id, 0, "")
  2076. return event.accept()
  2077. QGraphicsItem.keyPressEvent(self, event)
  2078. def mouseDoubleClickEvent(self, event):
  2079. if self.m_plugin_id >= 0:
  2080. canvas.callback(ACTION_PLUGIN_SHOW_UI if self.m_plugin_ui else ACTION_PLUGIN_EDIT, self.m_plugin_id, 0, "")
  2081. return event.accept()
  2082. QGraphicsItem.mouseDoubleClickEvent(self, event)
  2083. def mousePressEvent(self, event):
  2084. canvas.last_z_value += 1
  2085. self.setZValue(canvas.last_z_value)
  2086. self.resetLinesZValue()
  2087. self.m_cursor_moving = False
  2088. if event.button() == Qt.RightButton:
  2089. canvas.scene.clearSelection()
  2090. self.setSelected(True)
  2091. self.m_mouse_down = False
  2092. return event.accept()
  2093. elif event.button() == Qt.LeftButton:
  2094. if self.sceneBoundingRect().contains(event.scenePos()):
  2095. self.m_mouse_down = True
  2096. else:
  2097. # Fix a weird Qt behaviour with right-click mouseMove
  2098. self.m_mouse_down = False
  2099. return event.ignore()
  2100. else:
  2101. self.m_mouse_down = False
  2102. QGraphicsItem.mousePressEvent(self, event)
  2103. def mouseMoveEvent(self, event):
  2104. if self.m_mouse_down:
  2105. if not self.m_cursor_moving:
  2106. self.setCursor(QCursor(Qt.SizeAllCursor))
  2107. self.m_cursor_moving = True
  2108. self.repaintLines()
  2109. QGraphicsItem.mouseMoveEvent(self, event)
  2110. def mouseReleaseEvent(self, event):
  2111. if self.m_cursor_moving:
  2112. self.setCursor(QCursor(Qt.ArrowCursor))
  2113. self.m_mouse_down = False
  2114. self.m_cursor_moving = False
  2115. QGraphicsItem.mouseReleaseEvent(self, event)
  2116. def boundingRect(self):
  2117. return QRectF(0, 0, self.p_width, self.p_height)
  2118. def paint(self, painter, option, widget):
  2119. painter.save()
  2120. painter.setRenderHint(QPainter.Antialiasing, False)
  2121. # Draw rectangle
  2122. if self.isSelected():
  2123. painter.setPen(canvas.theme.box_pen_sel)
  2124. else:
  2125. painter.setPen(canvas.theme.box_pen)
  2126. if canvas.theme.box_bg_type == Theme.THEME_BG_GRADIENT:
  2127. box_gradient = QLinearGradient(0, 0, 0, self.p_height)
  2128. box_gradient.setColorAt(0, canvas.theme.box_bg_1)
  2129. box_gradient.setColorAt(1, canvas.theme.box_bg_2)
  2130. painter.setBrush(box_gradient)
  2131. else:
  2132. painter.setBrush(canvas.theme.box_bg_1)
  2133. painter.drawRect(0, 0, self.p_width, self.p_height)
  2134. # Draw pixmap header
  2135. if canvas.theme.box_header_pixmap:
  2136. painter.setPen(Qt.NoPen)
  2137. painter.setBrush(canvas.theme.box_bg_2)
  2138. painter.drawRect(1, 1, self.p_width-2, canvas.theme.box_header_height)
  2139. headerPos = QPointF(1, 1)
  2140. headerRect = QRectF(2, 2, self.p_width-4, canvas.theme.box_header_height-3)
  2141. painter.drawTiledPixmap(headerRect, canvas.theme.box_header_pixmap, headerPos)
  2142. # Draw text
  2143. painter.setFont(self.m_font_name)
  2144. if self.isSelected():
  2145. painter.setPen(canvas.theme.box_text_sel)
  2146. else:
  2147. painter.setPen(canvas.theme.box_text)
  2148. if canvas.theme.box_use_icon:
  2149. textPos = QPointF(25, canvas.theme.box_text_ypos)
  2150. else:
  2151. appNameSize = QFontMetrics(self.m_font_name).width(self.m_group_name)
  2152. rem = self.p_width - appNameSize
  2153. textPos = QPointF(rem/2, canvas.theme.box_text_ypos)
  2154. painter.drawText(textPos, self.m_group_name)
  2155. self.repaintLines()
  2156. painter.restore()
  2157. # ------------------------------------------------------------------------------
  2158. # canvasicon.cpp
  2159. class CanvasIcon(QGraphicsSvgItem):
  2160. def __init__(self, icon, name, parent):
  2161. QGraphicsSvgItem.__init__(self, parent)
  2162. self.m_renderer = None
  2163. self.p_size = QRectF(0, 0, 0, 0)
  2164. self.m_colorFX = QGraphicsColorizeEffect(self)
  2165. self.m_colorFX.setColor(canvas.theme.box_text.color())
  2166. self.setGraphicsEffect(self.m_colorFX)
  2167. self.setIcon(icon, name)
  2168. def setIcon(self, icon, name):
  2169. name = name.lower()
  2170. icon_path = ""
  2171. if icon == ICON_APPLICATION:
  2172. self.p_size = QRectF(3, 2, 19, 18)
  2173. if "audacious" in name:
  2174. icon_path = ":/scalable/pb_audacious.svg"
  2175. self.p_size = QRectF(5, 4, 16, 16)
  2176. elif "clementine" in name:
  2177. icon_path = ":/scalable/pb_clementine.svg"
  2178. self.p_size = QRectF(5, 4, 16, 16)
  2179. elif "distrho" in name:
  2180. icon_path = ":/scalable/pb_distrho.svg"
  2181. self.p_size = QRectF(5, 4, 14, 14)
  2182. elif "jamin" in name:
  2183. icon_path = ":/scalable/pb_jamin.svg"
  2184. self.p_size = QRectF(5, 3, 16, 16)
  2185. elif "mplayer" in name:
  2186. icon_path = ":/scalable/pb_mplayer.svg"
  2187. self.p_size = QRectF(5, 4, 16, 16)
  2188. elif "vlc" in name:
  2189. icon_path = ":/scalable/pb_vlc.svg"
  2190. self.p_size = QRectF(5, 3, 16, 16)
  2191. else:
  2192. icon_path = ":/scalable/pb_generic.svg"
  2193. self.p_size = QRectF(4, 3, 16, 16)
  2194. elif icon == ICON_HARDWARE:
  2195. icon_path = ":/scalable/pb_hardware.svg"
  2196. self.p_size = QRectF(5, 2, 16, 16)
  2197. elif icon == ICON_DISTRHO:
  2198. icon_path = ":/scalable/pb_distrho.svg"
  2199. self.p_size = QRectF(5, 4, 14, 14)
  2200. elif icon == ICON_FILE:
  2201. icon_path = ":/scalable/pb_file.svg"
  2202. self.p_size = QRectF(5, 4, 12, 14)
  2203. elif icon == ICON_PLUGIN:
  2204. icon_path = ":/scalable/pb_plugin.svg"
  2205. self.p_size = QRectF(5, 4, 14, 14)
  2206. elif icon == ICON_LADISH_ROOM:
  2207. # TODO - make a unique ladish-room icon
  2208. icon_path = ":/scalable/pb_hardware.svg"
  2209. self.p_size = QRectF(5, 2, 16, 16)
  2210. else:
  2211. self.p_size = QRectF(0, 0, 0, 0)
  2212. qCritical("PatchCanvas::CanvasIcon.setIcon(%s, %s) - unsupported icon requested" % (icon2str(icon), name.encode()))
  2213. return
  2214. self.m_renderer = QSvgRenderer(icon_path, canvas.scene)
  2215. self.setSharedRenderer(self.m_renderer)
  2216. self.update()
  2217. def type(self):
  2218. return CanvasIconType
  2219. def boundingRect(self):
  2220. return self.p_size
  2221. def paint(self, painter, option, widget):
  2222. if self.m_renderer:
  2223. painter.save()
  2224. painter.setRenderHint(QPainter.Antialiasing, False)
  2225. painter.setRenderHint(QPainter.TextAntialiasing, False)
  2226. self.m_renderer.render(painter, self.p_size)
  2227. painter.restore()
  2228. else:
  2229. QGraphicsSvgItem.paint(self, painter, option, widget)
  2230. # ------------------------------------------------------------------------------
  2231. # canvasportglow.cpp
  2232. class CanvasPortGlow(QGraphicsDropShadowEffect):
  2233. def __init__(self, port_type, parent):
  2234. QGraphicsDropShadowEffect.__init__(self, parent)
  2235. self.setBlurRadius(12)
  2236. self.setOffset(0, 0)
  2237. if port_type == PORT_TYPE_AUDIO_JACK:
  2238. self.setColor(canvas.theme.line_audio_jack_glow)
  2239. elif port_type == PORT_TYPE_MIDI_JACK:
  2240. self.setColor(canvas.theme.line_midi_jack_glow)
  2241. elif port_type == PORT_TYPE_MIDI_ALSA:
  2242. self.setColor(canvas.theme.line_midi_alsa_glow)
  2243. elif port_type == PORT_TYPE_PARAMETER:
  2244. self.setColor(canvas.theme.line_parameter_glow)
  2245. # ------------------------------------------------------------------------------
  2246. # canvasboxshadow.cpp
  2247. class CanvasBoxShadow(QGraphicsDropShadowEffect):
  2248. def __init__(self, parent):
  2249. QGraphicsDropShadowEffect.__init__(self, parent)
  2250. self.m_fakeParent = None
  2251. self.setBlurRadius(20)
  2252. self.setColor(canvas.theme.box_shadow)
  2253. self.setOffset(0, 0)
  2254. def setFakeParent(self, fakeParent):
  2255. self.m_fakeParent = fakeParent
  2256. def setOpacity(self, opacity):
  2257. color = QColor(canvas.theme.box_shadow)
  2258. color.setAlphaF(opacity)
  2259. self.setColor(color)
  2260. def draw(self, painter):
  2261. if self.m_fakeParent:
  2262. self.m_fakeParent.repaintLines()
  2263. QGraphicsDropShadowEffect.draw(self, painter)