from __future__ import annotations

import time

from PySide6.QtCore import QObject, Slot, QThreadPool
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QWidget, QMainWindow, QApplication, QPushButton


class Main(QObject):
    """Classe de gestion de threads dans Pyside."""
    area: QWidget = None
    window: QMainWindow | QWidget = None
    refresh_button: QPushButton = None
    thread_pool = QThreadPool()
    counter: int = 0

    def __init__(self):
        super().__init__()
        self.window = QUiLoader().load("base-window.ui")
        self.area = self.window.centralWidget()
        self.refresh_button = self.window.refresh_button
        # Répondre à un événement sur le bouton
        self.refresh_button.clicked.connect(self.on_refresh_button_click)

    @Slot()
    def on_refresh_button_click(self):
        """Rafraîchir le graphique."""
        self.refresh_button.setDisabled(True)
        self.thread_pool.start(self.counter_updater)

    def counter_updater(self):
        """Thread."""
        while True:
            self.counter += 1
            self.refresh_button.setText(f"{self.counter}")
            time.sleep(1.0)


if __name__ == '__main__':
    application = QApplication()
    main = Main()
    main.window.show()
    application.exec()
