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.

2597 lines
91KB

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