-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(animation): implement AnimationManager for widget animations
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
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |