diff --git a/.github/workflows/windows-tests.yml b/.github/workflows/windows-tests.yml index b319c6d8..d8a1c3e8 100644 --- a/.github/workflows/windows-tests.yml +++ b/.github/workflows/windows-tests.yml @@ -49,7 +49,10 @@ jobs: - name: Run tests shell: bash -l {0} run: | - python example.py + if [ "$QT_BINDING" = "pyside2" ]; then + python example.py + fi + pytest -x -vv --cov-report xml --cov=qtawesome qtawesome - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 diff --git a/README.md b/README.md index 3292cb18..8c411bb6 100644 --- a/README.md +++ b/README.md @@ -160,9 +160,12 @@ drawn_image_button = QtWidgets.QPushButton(drawn_image_icon, ```python # Spining icons spin_button = QtWidgets.QPushButton(' Spinning icon') -spin_icon = qta.icon('fa5s.spinner', color='red', - animation=qta.Spin(spin_button)) +animation = qta.Spin(spin_button) +spin_icon = qta.icon('fa5s.spinner', color='red', animation=animation) spin_button.setIcon(spin_icon) + +# Stop the animation when needed +animation.stop() ``` - Display Icon as a widget @@ -170,13 +173,17 @@ spin_button.setIcon(spin_icon) ```python # Spining icon widget spin_widget = qta.IconWidget() -spin_icon = qta.icon('mdi.loading', color='red', - animation=qta.Spin(spin_widget)) +animation = qta.Spin(spin_widget, autostart=False) +spin_icon = qta.icon('mdi.loading', color='red', animation=animation) spin_widget.setIcon(spin_icon) # Simple icon widget simple_widget = qta.IconWidget('mdi.web', color='blue', size=QtCore.QSize(16, 16)) + +# Start and stop the animation when needed +animation.start() +animation.stop() ``` - Screenshot diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 170c6acf..b5d82e85 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -172,28 +172,39 @@ Examples .. code:: python - # Spin icons + # -- Spin icons spin_button = QtWidgets.QPushButton(' Spinning icon') - spin_icon = qta.icon('fa5s.spinner', color='red', - animation=qta.Spin(spin_button)) + animation = qta.Spin(spin_button) + spin_icon = qta.icon('fa5s.spinner', color='red', animation=animation) spin_button.setIcon(spin_icon) - # Pulse icons + # Stop animation when needed + animation.stop() + + # -- Pulse icons pulse_button = QtWidgets.QPushButton(' Pulsing icon') - pulse_icon = qta.icon('fa5s.spinner', color='green', - animation=qta.Pulse(pulse_button)) + animation = qta.Pulse(pulse_button, autostart=False) + pulse_icon = qta.icon('fa5s.spinner', color='green', animation=animation) pulse_button.setIcon(pulse_icon) - # Stacked spin icons + # Start and stop the animation when needed + animation.start() + animation.stop() + + # -- Stacked spin icons stack_spin_button = QtWidgets.QPushButton('Stack spin') + animation = qta.Spin(stack_spin_button) options = [{'scale_factor': 0.4, - 'animation': qta.Spin(stack_spin_button)}, + 'animation': animation}, {'color': 'blue'}] stack_spin_icon = qta.icon('ei.asl', 'fa5.square', options=options) stack_spin_button.setIcon(stack_spin_icon) stack_spin_button.setIconSize(QtCore.QSize(32, 32)) + # Stop animation when needed + animation.stop() + - Apply font label rendering: .. code:: python @@ -206,13 +217,16 @@ Examples .. code:: python - # Spinning icon widget + # -- Spinning icon widget spin_widget = qta.IconWidget() - spin_icon = qta.icon('mdi.loading', color='red', - animation=qta.Spin(spin_widget)) + animation = qta.Spin(spin_widget) + spin_icon = qta.icon('mdi.loading', color='red', animation=animation) spin_widget.setIcon(spin_icon) - # simple widget + # Stop animation when needed + animation.stop() + + # -- Simple widget simple_widget = qta.IconWidget('mdi.web', color='blue', size=QtCore.QSize(16, 16)) diff --git a/example.py b/example.py index 9d4db7be..d3276720 100644 --- a/example.py +++ b/example.py @@ -122,16 +122,25 @@ def __init__(self): # Spin icons spin_button = QtWidgets.QPushButton(' Spinning icon') - spin_icon = qta.icon('fa5s.spinner', color='red', - animation=qta.Spin(spin_button)) + animation1 = qta.Spin(spin_button) + spin_icon = qta.icon('fa5s.spinner', color='red', animation=animation1) spin_button.setIcon(spin_icon) + timer1 = QtCore.QTimer() + timer1.singleShot(3000, animation1.stop) + # Pulse icons pulse_button = QtWidgets.QPushButton(' Pulsing icon') + animation2 = qta.Pulse(pulse_button, autostart=False) pulse_icon = qta.icon('fa5s.spinner', color='green', - animation=qta.Pulse(pulse_button)) + animation=animation2) pulse_button.setIcon(pulse_icon) + timer2 = QtCore.QTimer() + timer2.singleShot(1500, animation2.start) + timer3 = QtCore.QTimer() + timer3.singleShot(6000, animation2.stop) + # Stacked spin icons stack_spin_button = QtWidgets.QPushButton('Stack spin') options = [{'scale_factor': 0.4, 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 + )