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.

165 lines
6.2KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # PatchBay Canvas engine using QGraphicsView/Scene
  4. # Copyright (C) 2010-2019 Filipe Coelho <falktx@falktx.com>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or 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 doc/GPL.txt file.
  17. # ------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. from PyQt5.QtCore import Qt, QPointF
  20. from PyQt5.QtGui import QColor, QLinearGradient, QPainter, QPainterPath, QPen
  21. from PyQt5.QtWidgets import QGraphicsPathItem
  22. # ------------------------------------------------------------------------------------------------------------
  23. # Imports (Custom)
  24. from . import (
  25. canvas,
  26. options,
  27. CanvasBezierLineType,
  28. ACTION_PORTS_DISCONNECT,
  29. EYECANDY_FULL,
  30. PORT_MODE_OUTPUT,
  31. PORT_TYPE_AUDIO_JACK,
  32. PORT_TYPE_MIDI_ALSA,
  33. PORT_TYPE_MIDI_JACK,
  34. PORT_TYPE_PARAMETER,
  35. )
  36. from .canvasportglow import CanvasPortGlow
  37. # ------------------------------------------------------------------------------------------------------------
  38. class CanvasBezierLine(QGraphicsPathItem):
  39. def __init__(self, item1, item2, parent):
  40. QGraphicsPathItem.__init__(self)
  41. self.setParentItem(parent)
  42. self.item1 = item1
  43. self.item2 = item2
  44. self.m_locked = False
  45. self.m_lineSelected = False
  46. self.setBrush(QColor(0, 0, 0, 0))
  47. self.setGraphicsEffect(None)
  48. self.updateLinePos()
  49. def isLocked(self):
  50. return self.m_locked
  51. def setLocked(self, yesno):
  52. self.m_locked = yesno
  53. def isLineSelected(self):
  54. return self.m_lineSelected
  55. def updateLineSelected(self):
  56. if self.m_locked:
  57. return
  58. yesno = self.item1.isSelected() or self.item2.isSelected()
  59. if yesno != self.m_lineSelected and options.eyecandy == EYECANDY_FULL:
  60. if yesno:
  61. self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
  62. else:
  63. self.setGraphicsEffect(None)
  64. self.m_lineSelected = yesno
  65. self.updateLineGradient()
  66. def triggerDisconnect(self):
  67. for connection in canvas.connection_list:
  68. if (connection.port_out_id == self.item1.getPortId() and connection.port_in_id == self.item2.getPortId()):
  69. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  70. break
  71. def updateLinePos(self):
  72. if self.item1.getPortMode() == PORT_MODE_OUTPUT:
  73. rect1 = self.item1.sceneBoundingRect()
  74. rect2 = self.item2.sceneBoundingRect()
  75. item1_x = rect1.right()
  76. item2_x = rect2.left()
  77. item1_y = rect1.top() + float(canvas.theme.port_height)/2
  78. item2_y = rect2.top() + float(canvas.theme.port_height)/2
  79. item1_new_x = item1_x + abs(item1_x - item2_x) / 2
  80. item2_new_x = item2_x - abs(item1_x - item2_x) / 2
  81. path = QPainterPath(QPointF(item1_x, item1_y))
  82. path.cubicTo(item1_new_x, item1_y, item2_new_x, item2_y, item2_x, item2_y)
  83. self.setPath(path)
  84. self.m_lineSelected = False
  85. self.updateLineGradient()
  86. def type(self):
  87. return CanvasBezierLineType
  88. def updateLineGradient(self):
  89. pos_top = self.boundingRect().top()
  90. pos_bot = self.boundingRect().bottom()
  91. if self.item2.scenePos().y() >= self.item1.scenePos().y():
  92. pos1 = 0
  93. pos2 = 1
  94. else:
  95. pos1 = 1
  96. pos2 = 0
  97. port_type1 = self.item1.getPortType()
  98. port_type2 = self.item2.getPortType()
  99. port_gradient = QLinearGradient(0, pos_top, 0, pos_bot)
  100. if port_type1 == PORT_TYPE_AUDIO_JACK:
  101. port_gradient.setColorAt(pos1, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  102. elif port_type1 == PORT_TYPE_MIDI_JACK:
  103. port_gradient.setColorAt(pos1, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  104. elif port_type1 == PORT_TYPE_MIDI_ALSA:
  105. port_gradient.setColorAt(pos1, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  106. elif port_type1 == PORT_TYPE_PARAMETER:
  107. port_gradient.setColorAt(pos1, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  108. if port_type2 == PORT_TYPE_AUDIO_JACK:
  109. port_gradient.setColorAt(pos2, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  110. elif port_type2 == PORT_TYPE_MIDI_JACK:
  111. port_gradient.setColorAt(pos2, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  112. elif port_type2 == PORT_TYPE_MIDI_ALSA:
  113. port_gradient.setColorAt(pos2, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  114. elif port_type2 == PORT_TYPE_PARAMETER:
  115. port_gradient.setColorAt(pos2, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  116. self.setPen(QPen(port_gradient, 2.00001, Qt.SolidLine, Qt.FlatCap))
  117. def paint(self, painter, option, widget):
  118. painter.save()
  119. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  120. pen = self.pen()
  121. cosm_pen = QPen(pen)
  122. cosm_pen.setCosmetic(True)
  123. cosm_pen.setWidthF(1.00001)
  124. QGraphicsPathItem.paint(self, painter, option, widget)
  125. painter.setPen(cosm_pen)
  126. painter.setBrush(Qt.NoBrush)
  127. painter.setOpacity(0.2)
  128. painter.drawPath(self.path())
  129. painter.restore()
  130. # ------------------------------------------------------------------------------------------------------------