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
, QLineF
12 from PyQt5
.QtGui
import QLinearGradient
, QPainter
, QPen
13 from PyQt5
.QtWidgets
import QGraphicsLineItem
15 from PyQt6
.QtCore
import Qt
, QLineF
16 from PyQt6
.QtGui
import QLinearGradient
, QPainter
, QPen
17 from PyQt6
.QtWidgets
import QGraphicsLineItem
19 # ------------------------------------------------------------------------------------------------------------
26 ACTION_PORTS_DISCONNECT
,
35 from .canvasportglow
import CanvasPortGlow
37 # ------------------------------------------------------------------------------------------------------------
39 class CanvasLine(QGraphicsLineItem
):
40 def __init__(self
, item1
, item2
, parent
):
41 QGraphicsLineItem
.__init
__(self
)
42 self
.setParentItem(parent
)
48 self
.m_lineSelected
= False
50 self
.setGraphicsEffect(None)
56 def setLocked(self
, yesno
):
59 def isLineSelected(self
):
60 return self
.m_lineSelected
62 def updateLineSelected(self
):
66 yesno
= self
.item1
.isSelected() or self
.item2
.isSelected()
67 if yesno
!= self
.m_lineSelected
and options
.eyecandy
== EYECANDY_FULL
:
69 self
.setGraphicsEffect(CanvasPortGlow(self
.item1
.getPortType(), self
.toGraphicsObject()))
71 self
.setGraphicsEffect(None)
73 self
.m_lineSelected
= yesno
74 self
.updateLineGradient()
76 def triggerDisconnect(self
):
77 for connection
in canvas
.connection_list
:
78 if (connection
.port_out_id
== self
.item1
.getPortId() and connection
.port_in_id
== self
.item2
.getPortId()):
79 canvas
.callback(ACTION_PORTS_DISCONNECT
, connection
.connection_id
, 0, "")
82 def updateLinePos(self
):
83 if self
.item1
.getPortMode() == PORT_MODE_OUTPUT
:
84 rect1
= self
.item1
.sceneBoundingRect()
85 rect2
= self
.item2
.sceneBoundingRect()
86 line
= QLineF(rect1
.right(),
87 rect1
.top() + float(canvas
.theme
.port_height
)/2,
89 rect2
.top() + float(canvas
.theme
.port_height
)/2)
92 self
.m_lineSelected
= False
93 self
.updateLineGradient()
98 def updateLineGradient(self
):
99 pos_top
= self
.boundingRect().top()
100 pos_bot
= self
.boundingRect().bottom()
101 if self
.item2
.scenePos().y() >= self
.item1
.scenePos().y():
108 port_type1
= self
.item1
.getPortType()
109 port_type2
= self
.item2
.getPortType()
110 port_gradient
= QLinearGradient(0, pos_top
, 0, pos_bot
)
112 if port_type1
== PORT_TYPE_AUDIO_JACK
:
113 port_gradient
.setColorAt(pos1
, canvas
.theme
.line_audio_jack_sel
if self
.m_lineSelected
else canvas
.theme
.line_audio_jack
)
114 elif port_type1
== PORT_TYPE_MIDI_JACK
:
115 port_gradient
.setColorAt(pos1
, canvas
.theme
.line_midi_jack_sel
if self
.m_lineSelected
else canvas
.theme
.line_midi_jack
)
116 elif port_type1
== PORT_TYPE_MIDI_ALSA
:
117 port_gradient
.setColorAt(pos1
, canvas
.theme
.line_midi_alsa_sel
if self
.m_lineSelected
else canvas
.theme
.line_midi_alsa
)
118 elif port_type1
== PORT_TYPE_PARAMETER
:
119 port_gradient
.setColorAt(pos1
, canvas
.theme
.line_parameter_sel
if self
.m_lineSelected
else canvas
.theme
.line_parameter
)
121 if port_type2
== PORT_TYPE_AUDIO_JACK
:
122 port_gradient
.setColorAt(pos2
, canvas
.theme
.line_audio_jack_sel
if self
.m_lineSelected
else canvas
.theme
.line_audio_jack
)
123 elif port_type2
== PORT_TYPE_MIDI_JACK
:
124 port_gradient
.setColorAt(pos2
, canvas
.theme
.line_midi_jack_sel
if self
.m_lineSelected
else canvas
.theme
.line_midi_jack
)
125 elif port_type2
== PORT_TYPE_MIDI_ALSA
:
126 port_gradient
.setColorAt(pos2
, canvas
.theme
.line_midi_alsa_sel
if self
.m_lineSelected
else canvas
.theme
.line_midi_alsa
)
127 elif port_type2
== PORT_TYPE_PARAMETER
:
128 port_gradient
.setColorAt(pos2
, canvas
.theme
.line_parameter_sel
if self
.m_lineSelected
else canvas
.theme
.line_parameter
)
130 self
.setPen(QPen(port_gradient
, 2.00001, Qt
.SolidLine
, Qt
.RoundCap
))
132 def paint(self
, painter
, option
, widget
):
134 painter
.setRenderHint(QPainter
.Antialiasing
, bool(options
.antialiasing
))
138 cosm_pen
.setCosmetic(True)
139 cosm_pen
.setWidthF(1.00001)
141 QGraphicsLineItem
.paint(self
, painter
, option
, widget
)
143 painter
.setPen(cosm_pen
)
144 painter
.setBrush(Qt
.NoBrush
)
145 painter
.setOpacity(0.2)
146 painter
.drawLine(self
.line())
150 # ------------------------------------------------------------------------------------------------------------