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.

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