2 # SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <falktx@falktx.com>
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # ------------------------------------------------------------------------------------------------------------
8 from qt_compat
import qt_config
11 from PyQt5
.QtCore
import Qt
, QPointF
12 from PyQt5
.QtGui
import QColor
, QLinearGradient
, QPainter
, QPainterPath
, QPen
13 from PyQt5
.QtWidgets
import QGraphicsPathItem
15 from PyQt6
.QtCore
import Qt
, QPointF
16 from PyQt6
.QtGui
import QColor
, QLinearGradient
, QPainter
, QPainterPath
, QPen
17 from PyQt6
.QtWidgets
import QGraphicsPathItem
19 # ------------------------------------------------------------------------------------------------------------
26 ACTION_PORTS_DISCONNECT
,
35 from .canvasportglow
import CanvasPortGlow
37 # ------------------------------------------------------------------------------------------------------------
39 class CanvasBezierLine(QGraphicsPathItem
):
40 def __init__(self
, item1
, item2
, parent
):
41 QGraphicsPathItem
.__init
__(self
)
42 self
.setParentItem(parent
)
48 self
.m_lineSelected
= False
50 self
.setBrush(QColor(0, 0, 0, 0))
51 self
.setGraphicsEffect(None)
57 def setLocked(self
, yesno
):
60 def isLineSelected(self
):
61 return self
.m_lineSelected
63 def updateLineSelected(self
):
67 yesno
= self
.item1
.isSelected() or self
.item2
.isSelected()
68 if yesno
!= self
.m_lineSelected
and options
.eyecandy
== EYECANDY_FULL
:
70 self
.setGraphicsEffect(CanvasPortGlow(self
.item1
.getPortType(), self
.toGraphicsObject()))
72 self
.setGraphicsEffect(None)
74 self
.m_lineSelected
= yesno
75 self
.updateLineGradient()
77 def triggerDisconnect(self
):
78 for connection
in canvas
.connection_list
:
79 if (connection
.port_out_id
== self
.item1
.getPortId() and connection
.port_in_id
== self
.item2
.getPortId()):
80 canvas
.callback(ACTION_PORTS_DISCONNECT
, connection
.connection_id
, 0, "")
83 def updateLinePos(self
):
84 if self
.item1
.getPortMode() == PORT_MODE_OUTPUT
:
85 rect1
= self
.item1
.sceneBoundingRect()
86 rect2
= self
.item2
.sceneBoundingRect()
88 item1_x
= rect1
.right()
89 item2_x
= rect2
.left()
90 item1_y
= rect1
.top() + float(canvas
.theme
.port_height
)/2
91 item2_y
= rect2
.top() + float(canvas
.theme
.port_height
)/2
92 item1_new_x
= item1_x
+ abs(item1_x
- item2_x
) / 2
93 item2_new_x
= item2_x
- abs(item1_x
- item2_x
) / 2
95 path
= QPainterPath(QPointF(item1_x
, item1_y
))
96 path
.cubicTo(item1_new_x
, item1_y
, item2_new_x
, item2_y
, item2_x
, item2_y
)
99 self
.m_lineSelected
= False
100 self
.updateLineGradient()
103 return CanvasBezierLineType
105 def updateLineGradient(self
):
106 pos_top
= self
.boundingRect().top()
107 pos_bot
= self
.boundingRect().bottom()
108 if self
.item2
.scenePos().y() >= self
.item1
.scenePos().y():
115 port_type1
= self
.item1
.getPortType()
116 port_type2
= self
.item2
.getPortType()
117 port_gradient
= QLinearGradient(0, pos_top
, 0, pos_bot
)
119 if port_type1
== PORT_TYPE_AUDIO_JACK
:
120 port_gradient
.setColorAt(pos1
, canvas
.theme
.line_audio_jack_sel
if self
.m_lineSelected
else canvas
.theme
.line_audio_jack
)
121 elif port_type1
== PORT_TYPE_MIDI_JACK
:
122 port_gradient
.setColorAt(pos1
, canvas
.theme
.line_midi_jack_sel
if self
.m_lineSelected
else canvas
.theme
.line_midi_jack
)
123 elif port_type1
== PORT_TYPE_MIDI_ALSA
:
124 port_gradient
.setColorAt(pos1
, canvas
.theme
.line_midi_alsa_sel
if self
.m_lineSelected
else canvas
.theme
.line_midi_alsa
)
125 elif port_type1
== PORT_TYPE_PARAMETER
:
126 port_gradient
.setColorAt(pos1
, canvas
.theme
.line_parameter_sel
if self
.m_lineSelected
else canvas
.theme
.line_parameter
)
128 if port_type2
== PORT_TYPE_AUDIO_JACK
:
129 port_gradient
.setColorAt(pos2
, canvas
.theme
.line_audio_jack_sel
if self
.m_lineSelected
else canvas
.theme
.line_audio_jack
)
130 elif port_type2
== PORT_TYPE_MIDI_JACK
:
131 port_gradient
.setColorAt(pos2
, canvas
.theme
.line_midi_jack_sel
if self
.m_lineSelected
else canvas
.theme
.line_midi_jack
)
132 elif port_type2
== PORT_TYPE_MIDI_ALSA
:
133 port_gradient
.setColorAt(pos2
, canvas
.theme
.line_midi_alsa_sel
if self
.m_lineSelected
else canvas
.theme
.line_midi_alsa
)
134 elif port_type2
== PORT_TYPE_PARAMETER
:
135 port_gradient
.setColorAt(pos2
, canvas
.theme
.line_parameter_sel
if self
.m_lineSelected
else canvas
.theme
.line_parameter
)
137 self
.setPen(QPen(port_gradient
, 2.00001, Qt
.SolidLine
, Qt
.FlatCap
))
139 def paint(self
, painter
, option
, widget
):
141 painter
.setRenderHint(QPainter
.Antialiasing
, bool(options
.antialiasing
))
145 cosm_pen
.setCosmetic(True)
146 cosm_pen
.setWidthF(1.00001)
148 QGraphicsPathItem
.paint(self
, painter
, option
, widget
)
150 painter
.setPen(cosm_pen
)
151 painter
.setBrush(Qt
.NoBrush
)
152 painter
.setOpacity(0.2)
153 painter
.drawPath(self
.path())
157 # ------------------------------------------------------------------------------------------------------------