Source code for magicclass.gui.utils

from __future__ import annotations
from typing import Callable, TYPE_CHECKING, TypeVar
from magicgui.widgets import FunctionGui
from qtpy.QtCore import Qt

if TYPE_CHECKING:
    from ._base import BaseGui
    from .menu_gui import ContextMenuGui


[docs]def get_parameters(fgui: FunctionGui): return {k: v.default for k, v in fgui.__signature__.parameters.items()}
[docs]def define_callback(self: BaseGui, callback: Callable): """Define a callback function from a method.""" *_, clsname, funcname = callback.__qualname__.split(".") mro = self.__class__.__mro__ for base in mro: if base.__name__ == clsname: def _callback(): with self.macro.blocked(): getattr(base, funcname)(self) return None break else: def _callback(): # search for parent instances that have the same name. current_self = self while not ( hasattr(current_self, funcname) and current_self.__class__.__name__ == clsname ): current_self = current_self.__magicclass_parent__ with self.macro.blocked(): getattr(current_self, funcname)() return None return _callback
[docs]def set_context_menu(contextmenu: ContextMenuGui, parent: BaseGui) -> None: parent.native.setContextMenuPolicy(Qt.CustomContextMenu) @parent.native.customContextMenuRequested.connect def rightClickContextMenu(point): contextmenu.native.exec_(parent.native.mapToGlobal(point)) return None
_C = TypeVar("_C", bound=type)
[docs]def copy_class(cls: _C) -> _C: return type(cls.__name__, cls.__bases__, dict(cls.__dict__))
[docs]class MagicClassConstructionError(Exception): """ This exception will be raised when class definition is not a valid magic-class. """