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.

158 lines
5.9KB

  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)
  41. self.setParentItem(parent)
  42. self.item1 = item1
  43. self.item2 = item2
  44. self.m_locked = False
  45. self.m_lineSelected = False
  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. line = QLineF(rect1.right(),
  75. rect1.top() + float(canvas.theme.port_height)/2,
  76. rect2.left(),
  77. rect2.top() + float(canvas.theme.port_height)/2)
  78. self.setLine(line)
  79. self.m_lineSelected = False
  80. self.updateLineGradient()
  81. def type(self):
  82. return CanvasLineType
  83. def updateLineGradient(self):
  84. pos_top = self.boundingRect().top()
  85. pos_bot = self.boundingRect().bottom()
  86. if self.item2.scenePos().y() >= self.item1.scenePos().y():
  87. pos1 = 0
  88. pos2 = 1
  89. else:
  90. pos1 = 1
  91. pos2 = 0
  92. port_type1 = self.item1.getPortType()
  93. port_type2 = self.item2.getPortType()
  94. port_gradient = QLinearGradient(0, pos_top, 0, pos_bot)
  95. if port_type1 == PORT_TYPE_AUDIO_JACK:
  96. port_gradient.setColorAt(pos1, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  97. elif port_type1 == PORT_TYPE_MIDI_JACK:
  98. port_gradient.setColorAt(pos1, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  99. elif port_type1 == PORT_TYPE_MIDI_ALSA:
  100. port_gradient.setColorAt(pos1, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  101. elif port_type1 == PORT_TYPE_PARAMETER:
  102. port_gradient.setColorAt(pos1, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  103. if port_type2 == PORT_TYPE_AUDIO_JACK:
  104. port_gradient.setColorAt(pos2, canvas.theme.line_audio_jack_sel if self.m_lineSelected else canvas.theme.line_audio_jack)
  105. elif port_type2 == PORT_TYPE_MIDI_JACK:
  106. port_gradient.setColorAt(pos2, canvas.theme.line_midi_jack_sel if self.m_lineSelected else canvas.theme.line_midi_jack)
  107. elif port_type2 == PORT_TYPE_MIDI_ALSA:
  108. port_gradient.setColorAt(pos2, canvas.theme.line_midi_alsa_sel if self.m_lineSelected else canvas.theme.line_midi_alsa)
  109. elif port_type2 == PORT_TYPE_PARAMETER:
  110. port_gradient.setColorAt(pos2, canvas.theme.line_parameter_sel if self.m_lineSelected else canvas.theme.line_parameter)
  111. self.setPen(QPen(port_gradient, 2.00001, Qt.SolidLine, Qt.RoundCap))
  112. def paint(self, painter, option, widget):
  113. painter.save()
  114. painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
  115. pen = self.pen()
  116. cosm_pen = QPen(pen)
  117. cosm_pen.setCosmetic(True)
  118. cosm_pen.setWidthF(1.00001)
  119. QGraphicsLineItem.paint(self, painter, option, widget)
  120. painter.setPen(cosm_pen)
  121. painter.setBrush(Qt.NoBrush)
  122. painter.setOpacity(0.2)
  123. painter.drawLine(self.line())
  124. painter.restore()
  125. # ------------------------------------------------------------------------------------------------------------