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.

2730 lines
95KB

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