#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: SingletonInterface.py
 @DateTime: 2025/4/7 23:12
 @SoftWare: PyCharm
 @Author  ：xsh
"""
from PyQt5 import Qt
import sys
import os
class SingletonInterface:
    Locker: Qt.QMutex = Qt.QMutex()
    def __new__(cls, *args, **kwargs):
        if not cls.hasInstance():
            try:
                cls.setInstance(super().__new__(cls, *args, **kwargs))
            except Exception as e:
                cls.setInstance(super().__new__(cls))
        return getattr(cls, '_instance')

    @classmethod
    def hasInstance(cls) -> bool:
        return hasattr(cls, '_instance') and getattr(cls, '_instance') is not None

    @classmethod
    def getInstance(cls):
        if cls.hasInstance():
            return getattr(cls, '_instance')
        return None

    @classmethod
    def setInstance(cls, value):
        if value is not None:
            setattr(cls, "_instance", value)

    @classmethod
    def GetInstance(cls, *args, **kwargs):
        if not cls.hasInstance():
            if cls.Locker.tryLock(1000):

            # Locker = Qt.QMutexLocker(cls.Locker)
                if not cls.hasInstance():
                    if len(args) <= 0:
                        if len(kwargs) <= 0:
                            cls.setInstance(cls())
                        else:
                            cls.setInstance(cls(**kwargs))
                    else:
                        if len(kwargs) <= 0:
                            cls.setInstance(cls(*args))
                        else:
                            cls.setInstance(cls(*args, **kwargs))
                cls.Locker.unlock()
        # print(cls.__name__, cls.getInstance(), cls.Locker)
        return cls.getInstance()

