2023-04-05 10:00:01 +12:00
|
|
|
from PyQt5.QtCore import QEvent, QObject, Qt
|
|
|
|
from PyQt5.QtGui import QShowEvent, QMovie
|
|
|
|
from PyQt5.QtWidgets import QLabel
|
2023-04-03 03:50:01 +12:00
|
|
|
|
|
|
|
|
2023-04-05 10:00:01 +12:00
|
|
|
class LoadingWidget(QLabel):
|
|
|
|
def __init__(self, autostart=False, parent=None):
|
|
|
|
super(LoadingWidget, self).__init__(parent=parent)
|
|
|
|
self.setObjectName(type(self).__name__)
|
|
|
|
self.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
|
2023-09-05 08:27:26 +12:00
|
|
|
self.movie = QMovie(":/images/loader.webp", parent=self)
|
|
|
|
# The animation's exact size is 94x94
|
|
|
|
self.setFixedSize(96, 96)
|
2023-04-05 10:00:01 +12:00
|
|
|
self.setMovie(self.movie)
|
|
|
|
if self.parent() is not None:
|
|
|
|
self.parent().installEventFilter(self)
|
|
|
|
if autostart:
|
|
|
|
self.movie.start()
|
2023-04-03 03:50:01 +12:00
|
|
|
|
2023-04-05 10:00:01 +12:00
|
|
|
def __center_on_parent(self):
|
|
|
|
rect = self.rect()
|
|
|
|
rect.moveCenter(self.parent().contentsRect().center())
|
|
|
|
self.setGeometry(rect)
|
2023-04-03 03:50:01 +12:00
|
|
|
|
2023-04-05 10:00:01 +12:00
|
|
|
def event(self, e: QEvent) -> bool:
|
|
|
|
if e.type() == QEvent.ParentAboutToChange:
|
|
|
|
if self.parent() is not None:
|
|
|
|
self.parent().removeEventFilter(self)
|
|
|
|
if e.type() == QEvent.ParentChange:
|
|
|
|
if self.parent() is not None:
|
|
|
|
self.parent().installEventFilter(self)
|
|
|
|
return super().event(e)
|
2023-04-03 03:50:01 +12:00
|
|
|
|
2023-04-05 10:00:01 +12:00
|
|
|
def showEvent(self, a0: QShowEvent) -> None:
|
2023-09-05 06:05:40 +12:00
|
|
|
if a0.spontaneous():
|
|
|
|
return super().showEvent(a0)
|
2023-04-05 10:00:01 +12:00
|
|
|
self.__center_on_parent()
|
2023-09-05 06:05:40 +12:00
|
|
|
super().showEvent(a0)
|
2023-04-05 10:00:01 +12:00
|
|
|
|
|
|
|
def eventFilter(self, a0: QObject, a1: QEvent) -> bool:
|
|
|
|
if a0 is self.parent() and a1.type() == QEvent.Resize:
|
|
|
|
self.__center_on_parent()
|
|
|
|
return a0.event(a1)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.setVisible(True)
|
|
|
|
self.movie.start()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.setVisible(False)
|
|
|
|
self.movie.stop()
|