From 025e59b0a3cf9de383476082336e30509ff4bdf1 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Mon, 6 Nov 2023 11:09:39 -0500 Subject: [PATCH] Add API to start/stop animations to the Spin class This also adds a kwarg to decide if the animation should be automatically started (in some situations that's not desired). --- qtawesome/animation.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/qtawesome/animation.py b/qtawesome/animation.py index 711cdc86..0d8754af 100644 --- a/qtawesome/animation.py +++ b/qtawesome/animation.py @@ -3,9 +3,12 @@ class Spin: - def __init__(self, parent_widget, interval=10, step=1): + def __init__(self, parent_widget, interval=10, step=1, autostart=True): self.parent_widget = parent_widget - self.interval, self.step = interval, step + self.interval = interval + self.step = step + self.autostart = autostart + self.info = {} def _update(self): @@ -25,7 +28,8 @@ def setup(self, icon_painter, painter, rect): timer = QTimer(self.parent_widget) timer.timeout.connect(self._update) self.info[self.parent_widget] = [timer, 0, self.step] - timer.start(self.interval) + if self.autostart: + timer.start(self.interval) else: timer, angle, self.step = self.info[self.parent_widget] x_center = rect.width() * 0.5 @@ -34,8 +38,21 @@ def setup(self, icon_painter, painter, rect): painter.rotate(angle) painter.translate(-x_center, -y_center) + def start(self): + timer: QTimer = self.info[self.parent_widget][0] + timer.start(self.interval) + + def stop(self): + timer: QTimer = self.info[self.parent_widget][0] + timer.stop() + class Pulse(Spin): - def __init__(self, parent_widget): - super().__init__(parent_widget, interval=300, step=45) + def __init__(self, parent_widget, autostart=True): + super().__init__( + parent_widget, + interval=300, + step=45, + autostart=autostart + )