python-lottie  0.6.11+deved4e6c7
A framework to work with lottie files and telegram animated stickers (tgs)
timeline_widget.py
Go to the documentation of this file.
1 from PyQt5.QtGui import QIcon
2 from PyQt5.QtCore import Qt, QTimer
3 from PyQt5.QtCore import pyqtSignal as Signal
4 from PyQt5.QtCore import pyqtSlot as Slot
5 from PyQt5.QtWidgets import *
6 
7 
8 class TimelineWidget(QWidget):
9  frame_changed = Signal(int)
10 
11  def __init__(self):
12  super().__init__()
13  layout_slider = QHBoxLayout()
14  layout_slider.setContentsMargins(0, 0, 0, 0)
15  self.setLayout(layout_slider)
16  self.button_playbutton_play = QPushButton()
17  self.button_playbutton_play.setCheckable(True)
18  self.button_playbutton_play.toggled.connect(self.play_toggleplay_toggle)
19  layout_slider.addWidget(self.button_playbutton_play)
20  self.sliderslider = QSlider(Qt.Horizontal)
21  layout_slider.addWidget(self.sliderslider)
22  self.slider_spinslider_spin = QSpinBox()
23  layout_slider.addWidget(self.slider_spinslider_spin)
24  self.sliderslider.valueChanged.connect(self.slider_spinslider_spin.setValue)
25  self.slider_spinslider_spin.valueChanged.connect(self.sliderslider.setValue)
26  self.sliderslider.valueChanged.connect(self.frame_changedframe_changed)
27 
28  self.framerateframerate = 60
29  self.timertimer = QTimer(self)
30  self.timertimer.timeout.connect(self._next_frame_next_frame)
31  self.stopstop()
32 
33  self._min_min = 0
34  self._max_max = 99
35 
36  def set_min_max(self, min, max):
37  self._min_min = min
38  self._max_max = max
39  self.sliderslider.setMinimum(min)
40  self.sliderslider.setMaximum(max)
41  self.slider_spinslider_spin.setMinimum(min)
42  self.slider_spinslider_spin.setMaximum(max)
43 
44  @Slot(int)
45  def set_frame(self, frame):
46  self.sliderslider.setValue(frame)
47 
48  @property
49  def frame(self):
50  return self.sliderslider.value()
51 
52  def _next_frame(self):
53  nf = self.frameframe + 1
54  if nf > self._max_max:
55  nf = self._min_min
56  self.sliderslider.setValue(nf)
57 
58  @Slot()
59  def stop(self):
60  self.timertimer.stop()
61  self.button_playbutton_play.setIcon(QIcon.fromTheme("media-playback-start"))
62  self.button_playbutton_play.setText("Play")
63  self.button_playbutton_play.setChecked(False)
64 
65  @Slot()
66  def play(self):
67  if not self.isEnabled():
68  self.stopstop()
69  return
70 
71  self.button_playbutton_play.setIcon(QIcon.fromTheme("media-playback-stop"))
72  self.button_playbutton_play.setText("Stop")
73 
74  self.timertimer.start(1000/self.fps)
75 
76  @Slot(bool)
77  def play_toggle(self, play):
78  if play:
79  self.playplay()
80  else:
81  self.stopstop()