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.

2650 lines
92KB

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