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.

164 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, parent)
  41. self.item1 = item1
  42. self.item2 = item2
  43. self.m_locked = False
  44. self.m_lineSelected = False
  45. self.setBrush(QColor(0, 0, 0, 0))
  46. self.setGraphicsEffect(None)
  47. self.updateLinePos()
  48. def isLocked(self):
  49. return self.m_locked
  50. def setLocked(self, yesno):
  51. self.m_locked = yesno
  52. def isLineSelected(self):
  53. return self.m_lineSelected
  54. def updateLineSelected(self):
  55. if self.m_locked:
  56. return
  57. yesno = self.item1.isSelected() or self.item2.isSelected()
  58. if yesno != self.m_lineSelected and options.eyecandy == EYECANDY_FULL:
  59. if yesno:
  60. self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
  61. else:
  62. self.setGraphicsEffect(None)
  63. self.m_lineSelected = yesno
  64. self.updateLineGradient()
  65. def triggerDisconnect(self):
  66. for connection in canvas.connection_list:
  67. if (connection.port_out_id == self.item1.getPortId() and connection.port_in_id == self.item2.getPortId()):
  68. canvas.callback(ACTION_PORTS_DISCONNECT, connection.connection_id, 0, "")
  69. break
  70. def updateLinePos(self):
  71. if self.item1.getPortMode() == PORT_MODE_OUTPUT:
  72. rect1 = self.item1.sceneBoundingRect()
  73. rect2 = self.item2.sceneBoundingRect()
  74. item1_x = rect1.right()
  75. item2_x = rect2.left()
  76. item1_y = rect1.top() + float(canvas.theme.port_height)/2
  77. item2_y = rect2.top() + float(canvas.theme.port_height)/2
  78. item1_new_x = item1_x + abs(item1_x - item2_x) / 2
  79. item2_new_x = item2_x - abs(item1_x - item2_x) / 2
  80. path = QPainterPath(QPointF(item1_x, item1_y))
  81. path.cubicTo(item1_new_x, item1_y, item2_new_x, item2_y, item2_x, item2_y)
  82. self.setPath(path)
  83. self.m_lineSelected = False
  84. self.updateLineGradient()
  85. def type(self):
  86. return CanvasBezierLineType
  87. def updateLineGradient(self):
  88. pos_top = self.boundingRect().top()
  89. pos_bot = self.boundingRect().bottom()
  90. if self.item2.scenePos().y() >= self.item1.scenePos().y():
  91. pos1 = 0
  92. pos2 = 1
  93. else:
  94. pos1 = 1
  95. pos2 = 0
  96. port_type1 = self.item1.getPortType()
  97. port_type2 = self.item2.getPortType()
  98. port_gradient = QLinearGradient(0, pos_top, 0, pos_bot)
  99. if port_type1 == PORT_TYPE_AUDIO_JACK:
  100. port_gradient.setColorAt(pos1, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  101. elif port_type1 == PORT_TYPE_MIDI_JACK:
  102. port_gradient.setColorAt(pos1, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  103. elif port_type1 == PORT_TYPE_MIDI_ALSA:
  104. port_gradient.setColorAt(pos1, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  105. elif port_type1 == PORT_TYPE_PARAMETER:
  106. port_gradient.setColorAt(pos1, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  107. if port_type2 == PORT_TYPE_AUDIO_JACK:
  108. port_gradient.setColorAt(pos2, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  109. elif port_type2 == PORT_TYPE_MIDI_JACK:
  110. port_gradient.setColorAt(pos2, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  111. elif port_type2 == PORT_TYPE_MIDI_ALSA:
  112. port_gradient.setColorAt(pos2, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  113. elif port_type2 == PORT_TYPE_PARAMETER:
  114. port_gradient.setColorAt(pos2, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  115. self.setPen(QPen(port_gradient, 2.00001, Qt.SolidLine, Qt.FlatCap))
  116. def paint(self, painter, option, widget):
  117. painter.save()
  118. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  119. pen = self.pen()
  120. cosm_pen = QPen(pen)
  121. cosm_pen.setCosmetic(True)
  122. cosm_pen.setWidthF(1.00001)
  123. QGraphicsPathItem.paint(self, painter, option, widget)
  124. painter.setPen(cosm_pen)
  125. painter.setBrush(Qt.NoBrush)
  126. painter.setOpacity(0.2)
  127. painter.drawPath(self.path())
  128. painter.restore()
  129. # ------------------------------------------------------------------------------------------------------------