Skip to content

Commit

Permalink
Merge pull request #245 from ccordoba12/start-stop-spin
Browse files Browse the repository at this point in the history
PR: Add API to start/stop animations to the Spin class
  • Loading branch information
dalthviz authored Dec 8, 2023
2 parents 6f2adb4 + 0db95cc commit c759f98
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 25 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/windows-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,30 @@ 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

```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
Expand Down
38 changes: 26 additions & 12 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
15 changes: 12 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 22 additions & 5 deletions qtawesome/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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
)

0 comments on commit c759f98

Please sign in to comment.