Source code for cgl.apps.alchemy.data.filedialogcontextmanager
from __future__ import annotations
import logging
try:
from PySide6.QtCore import Signal, QObject
except ImportError:
from PySide6.QtCore import Signal, QObject
from cgl.core.path.object import PathObject
[docs]
class FPContextMgr(QObject):
__instance = None
__path_object = None
po_changed = Signal()
[docs]
@classmethod
def get_instance(cls) -> FPContextMgr:
if cls.__instance is None:
cls.__instance = cls()
return cls.__instance
def __init__(self):
if self.__instance is not None:
raise Exception("This class is a singleton, use get_instance() instead.")
else:
self.__instance = self
super().__init__()
[docs]
@classmethod
def update_po(cls, po):
print(f"FPD updating po {po.get_path()}")
mgr = cls.get_instance()
mgr.__path_object = po
mgr.po_changed.emit()
[docs]
@classmethod
def clear_po(cls):
cls.update_po(None)
[docs]
@classmethod
def get_current_po(cls, filename="") -> PathObject:
obj = cls.get_instance().__path_object
if filename:
obj = obj.copy(filename=filename)
# THis is a great way to debug any issues you're having with pathObject changes.
# import inspect
# caller = inspect.stack()[1]
# path = caller.filename.replace("\\", "/")
# logging.warning(f"{caller.function:20} -- {obj} {path}:{caller.lineno}")
return obj
[docs]
@classmethod
def list_watchers(cls):
for x in cls.get_instance().po_changed.recievers():
yield x
[docs]
@classmethod
def register_listener(cls, listener):
if listener is None:
raise Exception("listener can never be none")
logging.debug(f"connecting {listener}")
cls.get_instance().po_changed.connect(listener)