Cleanup
[carla.git] / source / frontend / patchcanvas / canvasline.py
blob9a1b3bec6a02ebe9e3e15dc14d6dc8bd6e7677a1
1 #!/usr/bin/env python3
2 # SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <falktx@falktx.com>
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # ------------------------------------------------------------------------------------------------------------
6 # Imports (Global)
8 from qt_compat import qt_config
10 if qt_config == 5:
11 from PyQt5.QtCore import Qt, QLineF
12 from PyQt5.QtGui import QLinearGradient, QPainter, QPen
13 from PyQt5.QtWidgets import QGraphicsLineItem
14 elif qt_config == 6:
15 from PyQt6.QtCore import Qt, QLineF
16 from PyQt6.QtGui import QLinearGradient, QPainter, QPen
17 from PyQt6.QtWidgets import QGraphicsLineItem
19 # ------------------------------------------------------------------------------------------------------------
20 # Imports (Custom)
22 from . import (
23 canvas,
24 options,
25 CanvasLineType,
26 ACTION_PORTS_DISCONNECT,
27 EYECANDY_FULL,
28 PORT_MODE_OUTPUT,
29 PORT_TYPE_AUDIO_JACK,
30 PORT_TYPE_MIDI_ALSA,
31 PORT_TYPE_MIDI_JACK,
32 PORT_TYPE_PARAMETER,
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)
44 self.item1 = item1
45 self.item2 = item2
47 self.m_locked = False
48 self.m_lineSelected = False
50 self.setGraphicsEffect(None)
51 self.updateLinePos()
53 def isLocked(self):
54 return self.m_locked
56 def setLocked(self, yesno):
57 self.m_locked = yesno
59 def isLineSelected(self):
60 return self.m_lineSelected
62 def updateLineSelected(self):
63 if self.m_locked:
64 return
66 yesno = self.item1.isSelected() or self.item2.isSelected()
67 if yesno != self.m_lineSelected and options.eyecandy == EYECANDY_FULL:
68 if yesno:
69 self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
70 else:
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, "")
80 break
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,
88 rect2.left(),
89 rect2.top() + float(canvas.theme.port_height)/2)
90 self.setLine(line)
92 self.m_lineSelected = False
93 self.updateLineGradient()
95 def type(self):
96 return CanvasLineType
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():
102 pos1 = 0
103 pos2 = 1
104 else:
105 pos1 = 1
106 pos2 = 0
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):
133 painter.save()
134 painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
136 pen = self.pen()
137 cosm_pen = QPen(pen)
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())
148 painter.restore()
150 # ------------------------------------------------------------------------------------------------------------