Skip to content

Commit

Permalink
feat(animation): implement AnimationManager for widget animations
Browse files Browse the repository at this point in the history
Adds AnimationManager class to handle animations for widgets, supporting fadeInOut effect. The manager ensures only allowed animation types are processed and logs errors for unsupported types. This enhancement provides a structured way to manage animations across the application.
  • Loading branch information
amnweb committed Jan 12, 2025
1 parent 9890a7c commit 5481329
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/core/utils/widgets/animation_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from PyQt6.QtWidgets import QGraphicsOpacityEffect
from PyQt6.QtCore import QTimer
import logging
class AnimationManager:
_instances = {}
ALLOWED_ANIMATIONS = ['fadeInOut']

@classmethod
def animate(cls, widget, animation_type: str, duration: int = 200):
if animation_type not in cls.ALLOWED_ANIMATIONS:
logging.error(f"Animation type '{animation_type}' not supported. Allowed types: {cls.ALLOWED_ANIMATIONS}")
return
key = f"{animation_type}_{duration}"
if key not in cls._instances:
cls._instances[key] = cls(animation_type, duration)
cls._instances[key]._animate(widget)

def __init__(self, animation_type: str, duration: int = 200):
self.animation_type = animation_type
self.duration = duration
self._opacity_effect = None
self._animation_timer = None

def _animate(self, widget):
animation_method = getattr(self, self.animation_type, None)
if animation_method:
animation_method(widget)

def fadeInOut(self, widget):
if hasattr(widget, '_opacity_effect') and widget._opacity_effect is not None:
widget._opacity_effect.setOpacity(1.0)
if hasattr(widget, '_animation_timer') and widget._animation_timer.isActive():
widget._animation_timer.stop()

widget._opacity_effect = QGraphicsOpacityEffect()
widget.setGraphicsEffect(widget._opacity_effect)
widget._opacity_effect.setOpacity(0.5)

widget._animation_timer = QTimer()
step = 0
steps = 20
increment = 0.5 / steps

def animate():
nonlocal step
new_opacity = widget._opacity_effect.opacity() + increment
if new_opacity >= 1.0:
new_opacity = 1.0
widget._opacity_effect.setOpacity(new_opacity)
widget._animation_timer.stop()
widget._opacity_effect = None
return
widget._opacity_effect.setOpacity(new_opacity)
step += 1

widget._animation_timer.timeout.connect(animate)
widget._animation_timer.start(self.duration // steps)

0 comments on commit 5481329

Please sign in to comment.