2019-05-02 07:31:02 +10:00
|
|
|
from typing import Any, Dict, List, Mapping, Set, Tuple, Union
|
2019-04-30 13:51:37 +10:00
|
|
|
|
|
|
|
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, pyqtSlot
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
PyqtType = Union[str, type]
|
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
|
|
|
|
class _ListItemMeta(type(QObject)): # type: ignore
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __new__(mcs, name: str, bases: Tuple[type], attrs: Dict[str, Any]):
|
2019-05-02 07:31:02 +10:00
|
|
|
def to_pyqt_type(type_) -> PyqtType:
|
|
|
|
"Return an appropriate pyqtProperty type from an annotation."
|
2019-04-30 13:51:37 +10:00
|
|
|
try:
|
2019-05-02 07:31:02 +10:00
|
|
|
if issubclass(type_, (bool, int, float, str, type(None))):
|
2019-04-30 13:51:37 +10:00
|
|
|
return type_
|
|
|
|
if issubclass(type_, Mapping):
|
|
|
|
return "QVariantMap"
|
|
|
|
return "QVariant"
|
|
|
|
except TypeError: # e.g. None passed
|
|
|
|
return to_pyqt_type(type(type_))
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# These special attributes must not be processed like properties
|
|
|
|
special = {"_main_key", "_required_init_values", "_constant"}
|
|
|
|
|
|
|
|
# These properties won't be settable and will not have a notify signal
|
|
|
|
constant: Set[str] = set(attrs.get("_constant") or set())
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# {property_name: (its_pyqt_type, its_default_value)}
|
|
|
|
props: Dict[str, Tuple[PyqtType, Any]] = {
|
2019-04-30 13:51:37 +10:00
|
|
|
name: (to_pyqt_type(attrs.get("__annotations__", {}).get(name)),
|
|
|
|
value)
|
|
|
|
for name, value in attrs.items()
|
|
|
|
|
|
|
|
if not (name.startswith("__") or callable(value) or
|
|
|
|
name in special)
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# Signals for the pyqtProperty notify arguments
|
|
|
|
signals: Dict[str, pyqtSignal] = {
|
2019-04-30 13:51:37 +10:00
|
|
|
f"{name}Changed": pyqtSignal(type_)
|
|
|
|
for name, (type_, _) in props.items() if name not in constant
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# pyqtProperty() won't take None, so we make dicts of extra kwargs
|
|
|
|
# to pass for each property
|
2019-04-30 13:51:37 +10:00
|
|
|
pyqt_props_kwargs: Dict[str, Dict[str, Any]] = {
|
|
|
|
name: {"constant": True} if name in constant else
|
2019-05-02 07:31:02 +10:00
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
{"notify": signals[f"{name}Changed"],
|
2019-05-02 07:31:02 +10:00
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
"fset": lambda self, value, n=name: (
|
|
|
|
setattr(self, f"_{n}", value) or # type: ignore
|
|
|
|
getattr(self, f"{n}Changed").emit(value),
|
2019-05-02 07:31:02 +10:00
|
|
|
)}
|
2019-04-30 13:51:37 +10:00
|
|
|
for name in props
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# The final pyqtProperty objects
|
|
|
|
pyqt_props: Dict[str, pyqtProperty] = {
|
2019-04-30 13:51:37 +10:00
|
|
|
name: pyqtProperty(
|
|
|
|
type_,
|
|
|
|
fget=lambda self, n=name: getattr(self, f"_{n}"),
|
|
|
|
**pyqt_props_kwargs.get(name, {}),
|
|
|
|
)
|
|
|
|
for name, (type_, _) in props.items()
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs = {
|
2019-05-02 07:31:02 +10:00
|
|
|
**attrs, # Original class attributes
|
|
|
|
**signals,
|
|
|
|
**pyqt_props,
|
|
|
|
|
|
|
|
# Set the internal _properties as slots for memory savings
|
2019-04-30 13:51:37 +10:00
|
|
|
"__slots__": tuple({f"_{prop}" for prop in props} & {"_main_key"}),
|
2019-05-02 07:31:02 +10:00
|
|
|
|
|
|
|
"_props": props,
|
|
|
|
|
|
|
|
# The main key is either the attribute _main_key,
|
|
|
|
# or the first defined property
|
2019-04-30 13:51:37 +10:00
|
|
|
"_main_key": attrs.get("_main_key") or
|
|
|
|
list(props.keys())[0] if props else None,
|
|
|
|
|
|
|
|
"_required_init_values": attrs.get("_required_init_values") or (),
|
|
|
|
"_constant": constant,
|
|
|
|
}
|
|
|
|
return type.__new__(mcs, name, bases, attrs)
|
|
|
|
|
|
|
|
|
|
|
|
class ListItem(QObject, metaclass=_ListItemMeta):
|
|
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
|
|
super().__init__()
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
method: str = "%s.__init__()" % type(self).__name__
|
|
|
|
already_set: Set[str] = set()
|
|
|
|
|
|
|
|
required: Set[str] = set(self._required_init_values)
|
|
|
|
required_num: int = len(required) + 1 # + 1 = self
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
args_num: int = len(self._props) + 1
|
|
|
|
from_to: str = str(args_num) if required_num == args_num else \
|
|
|
|
f"from {required_num} to {args_num}"
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# Check that not too many positional arguments were passed
|
2019-04-30 13:51:37 +10:00
|
|
|
if len(args) > len(self._props):
|
|
|
|
raise TypeError(
|
|
|
|
f"{method} takes {from_to} positional arguments but "
|
|
|
|
f"{len(args) + 1} were given"
|
|
|
|
)
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# Set properties from provided positional arguments
|
2019-04-30 13:51:37 +10:00
|
|
|
for prop, value in zip(self._props, args):
|
|
|
|
setattr(self, f"_{prop}", value)
|
|
|
|
already_set.add(prop)
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# Set properties from provided keyword arguments
|
2019-04-30 13:51:37 +10:00
|
|
|
for prop, value in kwargs.items():
|
|
|
|
if prop in already_set:
|
|
|
|
raise TypeError(f"{method} got multiple values for "
|
|
|
|
f"argument {prop!r}")
|
|
|
|
if prop not in self._props:
|
|
|
|
raise TypeError(f"{method} got an unexpected keyword "
|
|
|
|
f"argument {prop!r}")
|
|
|
|
setattr(self, f"_{prop}", value)
|
|
|
|
already_set.add(prop)
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# Check for required init arguments not provided
|
|
|
|
missing: Set[str] = required - already_set
|
2019-04-30 13:51:37 +10:00
|
|
|
if missing:
|
|
|
|
raise TypeError("%s missing %d required argument: %s" % (
|
|
|
|
method, len(missing), ", ".join((repr(m) for m in missing))))
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
# Set default values for properties not provided in arguments
|
2019-04-30 13:51:37 +10:00
|
|
|
for prop in set(self._props) - already_set:
|
|
|
|
setattr(self, f"_{prop}", self._props[prop][1])
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2019-05-02 07:31:02 +10:00
|
|
|
prop_strings = (
|
|
|
|
"\033[%dm%s\033[0m=%r" % (
|
|
|
|
1 if p == self.mainKey else 0, # 1 = term bold
|
|
|
|
p,
|
|
|
|
getattr(self, p)
|
|
|
|
) for p in self._props
|
2019-04-30 13:51:37 +10:00
|
|
|
)
|
2019-05-02 07:31:02 +10:00
|
|
|
return "%s(%s)" % (type(self).__name__, ", ".join(prop_strings))
|
2019-04-30 13:51:37 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(result=str)
|
|
|
|
def repr(self) -> str:
|
2019-05-02 07:31:02 +10:00
|
|
|
return self.__repr__()
|
2019-04-30 13:51:37 +10:00
|
|
|
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
@pyqtProperty("QStringList", constant=True)
|
2019-04-30 13:51:37 +10:00
|
|
|
def roles(self) -> List[str]:
|
|
|
|
return list(self._props.keys())
|
|
|
|
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
@pyqtProperty(str, constant=True)
|
2019-04-30 13:51:37 +10:00
|
|
|
def mainKey(self) -> str:
|
|
|
|
return self._main_key
|