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.

2633 lines
92KB

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