import sys
import time
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QVBoxLayout, QPushButton, QLabel, QProgressBar
from PyQt5.QtCore import Qt, QThread, pyqtSignal, pyqtSlot

from UI.SearchAddressWidget import CustomProgressBar

class LoadDataThread(QThread):
    progress = pyqtSignal(int)

    def run(self):
        for i in range(101):
            time.sleep(0.01)  # 模拟数据加载过程
            self.progress.emit(i)

class LoadingDataWidget(QDialog):
    # loadDataSignal = pyqtSignal(int, int)
    def __init__(self, refreshType = None):
        super().__init__()
        self.refreshType = refreshType#判断是单次刷新还是整体加载数据
        self.initUI() 
        # self.loadDataSignal.connect(self.loadData)

    def initUI(self):
        vbox = QVBoxLayout()
        
        self.label = QLabel("加载数据中，请稍后...", self)
        self.progressBar = CustomProgressBar(self)
        
        vbox.addWidget(self.label)
        vbox.addWidget(self.progressBar)

        if self.refreshType:
            self.thread = LoadDataThread()
            self.thread.progress.connect(self.singleLoadData)
            self.thread.start()
        
        self.setLayout(vbox)
        self.setWindowTitle('数据查询')
        self.setWindowFlags(Qt.Window | Qt.WindowTitleHint | Qt.CustomizeWindowHint)
        self.setModal(True)

    # def loadSignalEmit(self, maxValue:int, value:int):
    #     # print(maxValue, value)
    #     self.loadDataSignal.emit(maxValue, value)

    @pyqtSlot(int, int)
    def loadData(self, maxValue:int, value:int):
        self.progressBar.setMaximum(maxValue)
        self.progressBar.setValue(value)
        if maxValue == value:
            self.accept()

    def singleLoadData(self, value):
        value *= 4
        self.loadData(100, value)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = LoadingDataWidget()
    sys.exit(app.exec_())
