Collection of tools useful for audio production
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.

2642 lines
91KB

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