Cleanup
[carla.git] / source / frontend / patchcanvas / canvasbezierline.py
blob4ab5a927a0034e125f31c40b456c750760ecd4cf
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, QPointF
12 from PyQt5.QtGui import QColor, QLinearGradient, QPainter, QPainterPath, QPen
13 from PyQt5.QtWidgets import QGraphicsPathItem
14 elif qt_config == 6:
15 from PyQt6.QtCore import Qt, QPointF
16 from PyQt6.QtGui import QColor, QLinearGradient, QPainter, QPainterPath, QPen
17 from PyQt6.QtWidgets import QGraphicsPathItem
19 # ------------------------------------------------------------------------------------------------------------
20 # Imports (Custom)
22 from . import (
23 canvas,
24 options,
25 CanvasBezierLineType,
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 CanvasBezierLine(QGraphicsPathItem):
40 def __init__(self, item1, item2, parent):
41 QGraphicsPathItem.__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.setBrush(QColor(0, 0, 0, 0))
51 self.setGraphicsEffect(None)
52 self.updateLinePos()
54 def isLocked(self):
55 return self.m_locked
57 def setLocked(self, yesno):
58 self.m_locked = yesno
60 def isLineSelected(self):
61 return self.m_lineSelected
63 def updateLineSelected(self):
64 if self.m_locked:
65 return
67 yesno = self.item1.isSelected() or self.item2.isSelected()
68 if yesno != self.m_lineSelected and options.eyecandy == EYECANDY_FULL:
69 if yesno:
70 self.setGraphicsEffect(CanvasPortGlow(self.item1.getPortType(), self.toGraphicsObject()))
71 else:
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, "")
81 break
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)
97 self.setPath(path)
99 self.m_lineSelected = False
100 self.updateLineGradient()
102 def type(self):
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():
109 pos1 = 0
110 pos2 = 1
111 else:
112 pos1 = 1
113 pos2 = 0
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):
140 painter.save()
141 painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
143 pen = self.pen()
144 cosm_pen = QPen(pen)
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())
155 painter.restore()
157 # ------------------------------------------------------------------------------------------------------------