chore: make Settings singleton

This commit is contained in:
283375 2023-09-21 11:43:41 +08:00
parent 7a0e476a1d
commit 5abf56ff83
Signed by: 283375
SSH Key Fingerprint: SHA256:UcX0qg6ZOSDOeieKPGokA5h7soykG61nz2uxuQgVLSk
2 changed files with 28 additions and 2 deletions

View File

@ -1,6 +1,8 @@
import sys
from PySide6.QtCore import QFileInfo, QSettings
from PySide6.QtCore import QFileInfo, QSettings, Signal
from .singleton import QObjectSingleton
__all__ = [
"DATABASE_URL",
@ -29,7 +31,9 @@ ANDREAL_FOLDER = "Andreal/AndrealFolder"
ANDREAL_EXECUTABLE = "Andreal/AndrealExecutable"
class Settings(QSettings):
class Settings(QSettings, metaclass=QObjectSingleton):
updated = Signal(str)
def __init__(self, parent=None):
super().__init__(
QFileInfo(sys.argv[0]).dir().absoluteFilePath("arcaea_offline.ini"),
@ -37,6 +41,10 @@ class Settings(QSettings):
parent,
)
def setValue(self, key: str, value) -> None:
super().setValue(key, value)
self.updated.emit(key)
def _strItem(self, key: str) -> str | None:
return self.value(key, None, str)

View File

@ -0,0 +1,18 @@
from typing import Generic, TypeVar
from PySide6.QtCore import QObject
T = TypeVar("T")
class Singleton(type, Generic[T]):
_instance = None
def __call__(cls, *args, **kwargs) -> T:
if cls._instance is None:
cls._instance = super().__call__(*args, **kwargs)
return cls._instance
class QObjectSingleton(type(QObject), Singleton):
pass