mirror of
https://github.com/283375/arcaea-offline-pyside-ui.git
synced 2025-04-20 17:50:17 +00:00
chore: make Settings
singleton
This commit is contained in:
parent
7a0e476a1d
commit
5abf56ff83
@ -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)
|
||||
|
||||
|
18
ui/extends/shared/singleton.py
Normal file
18
ui/extends/shared/singleton.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user