Cleanup
[carla.git] / source / frontend / patchcanvas / canvasfadeanimation.py
blobed5425dad90deb3d009f6c711ac514639e1837c6
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 QAbstractAnimation
12 from PyQt5.QtWidgets import QGraphicsObject
13 elif qt_config == 6:
14 from PyQt6.QtCore import QAbstractAnimation
15 from PyQt6.QtWidgets import QGraphicsObject
17 # ------------------------------------------------------------------------------------------------------------
18 # Imports (Custom)
20 from . import canvas, CanvasBoxType
22 # ------------------------------------------------------------------------------------------------------------
24 class CanvasFadeAnimation(QAbstractAnimation):
25 def __init__(self, item, show):
26 QAbstractAnimation.__init__(self)
28 self.m_show = show
29 self.m_duration = 0
30 self.m_item = item
31 self.m_item_is_object = isinstance(item, QGraphicsObject)
33 def item(self):
34 return self.m_item
36 def forceStop(self):
37 self.blockSignals(True)
38 self.stop()
39 self.blockSignals(False)
41 def setDuration(self, time):
42 if self.m_item.opacity() == 0 and not self.m_show:
43 self.m_duration = 0
44 else:
45 if self.m_item_is_object:
46 self.m_item.blockSignals(True)
47 self.m_item.show()
48 self.m_item.blockSignals(False)
49 else:
50 self.m_item.show()
51 self.m_duration = time
53 def duration(self):
54 return self.m_duration
56 def updateCurrentTime(self, time):
57 if self.m_duration == 0:
58 return
60 if self.m_show:
61 value = float(time) / self.m_duration
62 else:
63 value = 1.0 - (float(time) / self.m_duration)
65 try:
66 self.m_item.setOpacity(value)
67 except RuntimeError:
68 print("CanvasFadeAnimation::updateCurrentTime() - failed to animate canvas item, already destroyed?")
69 self.forceStop()
70 canvas.animation_list.remove(self)
71 return
73 if self.m_item.type() == CanvasBoxType:
74 self.m_item.setShadowOpacity(value)
76 def updateDirection(self, direction):
77 pass
79 def updateState(self, oldState, newState):
80 pass
82 # ------------------------------------------------------------------------------------------------------------