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 qCritical
, QPointF
, QTimer
13 from PyQt6
.QtCore
import qCritical
, QPointF
, QTimer
15 # ------------------------------------------------------------------------------------------------------------
18 from . import bool2str
, canvas
, CanvasBoxType
19 from .canvasfadeanimation
import CanvasFadeAnimation
21 # ------------------------------------------------------------------------------------------------------------
23 def CanvasGetNewGroupPos(horizontal
):
25 print("PatchCanvas::CanvasGetNewGroupPos(%s)" % bool2str(horizontal
))
27 new_pos
= QPointF(canvas
.initial_pos
)
28 items
= canvas
.scene
.items()
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()
38 new_pos
+= QPointF(itemRect
.width() + 50, 0)
40 itemHeight
= itemRect
.height()
42 new_pos
+= QPointF(0, itemHeight
+ 50)
44 new_pos
.setY(item
.scenePos().y() + itemHeight
+ 20)
54 def CanvasGetFullPortName(group_id
, port_id
):
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
66 qCritical("PatchCanvas::CanvasGetFullPortName(%i, %i) - unable to find port" % (group_id
, port_id
))
69 def CanvasGetPortConnectionList(group_id
, port_id
):
71 print("PatchCanvas::CanvasGetPortConnectionList(%i, %i)" % (group_id
, port_id
))
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
))
83 def CanvasCallback(action
, value1
, value2
, value_str
):
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
):
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
:
97 canvas
.animation_list
.remove(animation
)
101 animation
= CanvasFadeAnimation(item
, show
)
102 animation
.setDuration(750 if show
else 500)
105 animation
.finished
.connect(canvas
.qobject
.AnimationFinishedShow
)
108 animation
.finished
.connect(canvas
.qobject
.AnimationFinishedDestroy
)
110 animation
.finished
.connect(canvas
.qobject
.AnimationFinishedHide
)
112 canvas
.animation_list
.append(animation
)
116 def CanvasRemoveItemFX(item
):
118 print("PatchCanvas::CanvasRemoveItemFX(%s)" % item
)
120 if item
.type() == CanvasBoxType
:
121 item
.removeIconFromScene()
123 canvas
.scene
.removeItem(item
)
126 QTimer
.singleShot(0, canvas
.scene
.update
)
128 # ------------------------------------------------------------------------------------------------------------