File indexing completed on 2025-01-19 03:59:52

0001 from PyQt5.QtGui import QIcon
0002 from PyQt5.QtCore import Qt, QTimer
0003 from PyQt5.QtCore import pyqtSignal as Signal
0004 from PyQt5.QtCore import pyqtSlot as Slot
0005 from PyQt5.QtWidgets import *
0006 
0007 
0008 class TimelineWidget(QWidget):
0009     frame_changed = Signal(int)
0010 
0011     def __init__(self):
0012         super().__init__()
0013         layout_slider = QHBoxLayout()
0014         layout_slider.setContentsMargins(0, 0, 0, 0)
0015         self.setLayout(layout_slider)
0016         self.button_play = QPushButton()
0017         self.button_play.setCheckable(True)
0018         self.button_play.toggled.connect(self.play_toggle)
0019         layout_slider.addWidget(self.button_play)
0020         self.slider = QSlider(Qt.Horizontal)
0021         layout_slider.addWidget(self.slider)
0022         self.slider_spin = QSpinBox()
0023         layout_slider.addWidget(self.slider_spin)
0024         self.slider.valueChanged.connect(self.slider_spin.setValue)
0025         self.slider_spin.valueChanged.connect(self.slider.setValue)
0026         self.slider.valueChanged.connect(self.frame_changed)
0027 
0028         self.framerate = 60
0029         self.timer = QTimer(self)
0030         self.timer.timeout.connect(self._next_frame)
0031         self.stop()
0032 
0033         self._min = 0
0034         self._max = 99
0035 
0036     def set_min_max(self, min, max):
0037         self._min = min
0038         self._max = max
0039         self.slider.setMinimum(min)
0040         self.slider.setMaximum(max)
0041         self.slider_spin.setMinimum(min)
0042         self.slider_spin.setMaximum(max)
0043 
0044     @Slot(int)
0045     def set_frame(self, frame):
0046         self.slider.setValue(frame)
0047 
0048     @property
0049     def frame(self):
0050         return self.slider.value()
0051 
0052     def _next_frame(self):
0053         nf = self.frame + 1
0054         if nf > self._max:
0055             nf = self._min
0056         self.slider.setValue(nf)
0057 
0058     @Slot()
0059     def stop(self):
0060         self.timer.stop()
0061         self.button_play.setIcon(QIcon.fromTheme("media-playback-start"))
0062         self.button_play.setText("Play")
0063         self.button_play.setChecked(False)
0064 
0065     @Slot()
0066     def play(self):
0067         if not self.isEnabled():
0068             self.stop()
0069             return
0070 
0071         self.button_play.setIcon(QIcon.fromTheme("media-playback-stop"))
0072         self.button_play.setText("Stop")
0073 
0074         self.timer.start(1000/self.fps)
0075 
0076     @Slot(bool)
0077     def play_toggle(self, play):
0078         if play:
0079             self.play()
0080         else:
0081             self.stop()