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.

2760 lines
96KB

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