Cleanup
[carla.git] / source / frontend / patchcanvas / utils.py
bloba246765db5e4a703a99822f68c1d950920511fbb
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 qCritical, QPointF, QTimer
12 elif qt_config == 6:
13 from PyQt6.QtCore import qCritical, QPointF, QTimer
15 # ------------------------------------------------------------------------------------------------------------
16 # Imports (Custom)
18 from . import bool2str, canvas, CanvasBoxType
19 from .canvasfadeanimation import CanvasFadeAnimation
21 # ------------------------------------------------------------------------------------------------------------
23 def CanvasGetNewGroupPos(horizontal):
24 if canvas.debug:
25 print("PatchCanvas::CanvasGetNewGroupPos(%s)" % bool2str(horizontal))
27 new_pos = QPointF(canvas.initial_pos)
28 items = canvas.scene.items()
30 #break_loop = False
31 while True:
32 break_for = False
33 for i, item in enumerate(items):
34 if item and item.type() == CanvasBoxType:
35 if item.sceneBoundingRect().adjusted(-5, -5, 5, 5).contains(new_pos):
36 itemRect = item.boundingRect()
37 if horizontal:
38 new_pos += QPointF(itemRect.width() + 50, 0)
39 else:
40 itemHeight = itemRect.height()
41 if itemHeight < 30:
42 new_pos += QPointF(0, itemHeight + 50)
43 else:
44 new_pos.setY(item.scenePos().y() + itemHeight + 20)
45 break_for = True
46 break
47 else:
48 if not break_for:
49 break
50 #break_loop = True
52 return new_pos
54 def CanvasGetFullPortName(group_id, port_id):
55 if canvas.debug:
56 print("PatchCanvas::CanvasGetFullPortName(%i, %i)" % (group_id, port_id))
58 for port in canvas.port_list:
59 if port.group_id == group_id and port.port_id == port_id:
60 group_id = port.group_id
61 for group in canvas.group_list:
62 if group.group_id == group_id:
63 return group.group_name + ":" + port.port_name
64 break
66 qCritical("PatchCanvas::CanvasGetFullPortName(%i, %i) - unable to find port" % (group_id, port_id))
67 return ""
69 def CanvasGetPortConnectionList(group_id, port_id):
70 if canvas.debug:
71 print("PatchCanvas::CanvasGetPortConnectionList(%i, %i)" % (group_id, port_id))
73 conn_list = []
75 for connection in canvas.connection_list:
76 if connection.group_out_id == group_id and connection.port_out_id == port_id:
77 conn_list.append((connection.connection_id, connection.group_in_id, connection.port_in_id))
78 elif connection.group_in_id == group_id and connection.port_in_id == port_id:
79 conn_list.append((connection.connection_id, connection.group_out_id, connection.port_out_id))
81 return conn_list
83 def CanvasCallback(action, value1, value2, value_str):
84 if canvas.debug:
85 print("PatchCanvas::CanvasCallback(%i, %i, %i, %s)" % (action, value1, value2, value_str.encode()))
87 canvas.callback(action, value1, value2, value_str)
89 def CanvasItemFX(item, show, destroy):
90 if canvas.debug:
91 print("PatchCanvas::CanvasItemFX(%s, %s, %s)" % (item, bool2str(show), bool2str(destroy)))
93 # Check if the item already has an animation
94 for animation in canvas.animation_list:
95 if animation.item() == item:
96 animation.forceStop()
97 canvas.animation_list.remove(animation)
98 del animation
99 break
101 animation = CanvasFadeAnimation(item, show)
102 animation.setDuration(750 if show else 500)
104 if show:
105 animation.finished.connect(canvas.qobject.AnimationFinishedShow)
106 else:
107 if destroy:
108 animation.finished.connect(canvas.qobject.AnimationFinishedDestroy)
109 else:
110 animation.finished.connect(canvas.qobject.AnimationFinishedHide)
112 canvas.animation_list.append(animation)
114 animation.start()
116 def CanvasRemoveItemFX(item):
117 if canvas.debug:
118 print("PatchCanvas::CanvasRemoveItemFX(%s)" % item)
120 if item.type() == CanvasBoxType:
121 item.removeIconFromScene()
123 canvas.scene.removeItem(item)
124 del item
126 QTimer.singleShot(0, canvas.scene.update)
128 # ------------------------------------------------------------------------------------------------------------