From 51904efa90444a191a2b75054ad503726f20c62c Mon Sep 17 00:00:00 2001 From: loathingKernel <142770+loathingKernel@users.noreply.github.com> Date: Wed, 20 Dec 2023 00:21:21 +0200 Subject: [PATCH] GameWidget: Request pixmap in paintEvent. Requesting the pixmap in `paintEvent` only loads the pixmap when the widget first becomes visible, making loading the library smoother. Also avoid spawning multiple singleshot QTimers and use QObject's internal timer, with CoarseTimer precision. --- .../tabs/games/game_widgets/game_widget.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/rare/components/tabs/games/game_widgets/game_widget.py b/rare/components/tabs/games/game_widgets/game_widget.py index 3390a3ac..e5cf3d24 100644 --- a/rare/components/tabs/games/game_widgets/game_widget.py +++ b/rare/components/tabs/games/game_widgets/game_widget.py @@ -3,7 +3,7 @@ import random from logging import getLogger from PyQt5.QtCore import pyqtSignal, Qt, pyqtSlot, QObject, QEvent, QTimer -from PyQt5.QtGui import QMouseEvent, QShowEvent +from PyQt5.QtGui import QMouseEvent, QShowEvent, QPaintEvent from PyQt5.QtWidgets import QMessageBox, QAction from rare.models.game import RareGame @@ -106,11 +106,21 @@ class GameWidget(LibraryWidget): # lk: attributes as `GameWidgetUi` class __slots__ = "ui" + def paintEvent(self, a0: QPaintEvent) -> None: + if not self.visibleRegion().isNull() and self.rgame.pixmap.isNull(): + self.startTimer(random.randrange(42, 2361, 129), Qt.CoarseTimer) + # self.startTimer(random.randrange(42, 2361, 363), Qt.VeryCoarseTimer) + # self.rgame.load_pixmap() + # QTimer.singleShot(random.randrange(42, 2361, 7), Qt.VeryCoarseTimer, self.rgame.load_pixmap) + super().paintEvent(a0) + + def timerEvent(self, a0): + self.killTimer(a0.timerId()) + self.rgame.load_pixmap() + def showEvent(self, a0: QShowEvent) -> None: if a0.spontaneous(): return super().showEvent(a0) - if self.rgame.pixmap.isNull(): - QTimer.singleShot(random.randrange(42, 361, 7), self.rgame.load_pixmap) super().showEvent(a0) @pyqtSlot()