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.

157 lines
5.8KB

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