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.

2757 lines
96KB

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