Skip to content

Commit

Permalink
Merge pull request #889 from ynput/enhancement/attr-defs-better-arg-n…
Browse files Browse the repository at this point in the history
…ames

Attribute definitions: Positive argument names
  • Loading branch information
iLLiCiTiT authored Oct 8, 2024
2 parents 6651a61 + f27a57b commit 1489d98
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
85 changes: 67 additions & 18 deletions client/ayon_core/lib/attribute_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uuid
import json
import copy
import warnings
from abc import ABCMeta, abstractmethod

import clique
Expand Down Expand Up @@ -90,6 +91,30 @@ def __call__(cls, *args, **kwargs):
return obj


def _convert_reversed_attr(
main_value, depr_value, main_label, depr_label, default
):
if main_value is not None and depr_value is not None:
if main_value == depr_value:
print(
f"Got invalid '{main_label}' and '{depr_label}' arguments."
f" Using '{main_label}' value."
)
elif depr_value is not None:
warnings.warn(
(
"DEPRECATION WARNING: Using deprecated argument"
f" '{depr_label}' please use '{main_label}' instead."
),
DeprecationWarning,
stacklevel=4,
)
main_value = not depr_value
elif main_value is None:
main_value = default
return main_value


class AbstractAttrDef(metaclass=AbstractAttrDefMeta):
"""Abstraction of attribute definition.
Expand All @@ -106,12 +131,14 @@ class AbstractAttrDef(metaclass=AbstractAttrDefMeta):
Args:
key (str): Under which key will be attribute value stored.
default (Any): Default value of an attribute.
label (str): Attribute label.
tooltip (str): Attribute tooltip.
is_label_horizontal (bool): UI specific argument. Specify if label is
next to value input or ahead.
hidden (bool): Will be item hidden (for UI purposes).
disabled (bool): Item will be visible but disabled (for UI purposes).
label (Optional[str]): Attribute label.
tooltip (Optional[str]): Attribute tooltip.
is_label_horizontal (Optional[bool]): UI specific argument. Specify
if label is next to value input or ahead.
visible (Optional[bool]): Item is shown to user (for UI purposes).
enabled (Optional[bool]): Item is enabled (for UI purposes).
hidden (Optional[bool]): DEPRECATED: Use 'visible' instead.
disabled (Optional[bool]): DEPRECATED: Use 'enabled' instead.
"""

type_attributes = []
Expand All @@ -125,22 +152,28 @@ def __init__(
label=None,
tooltip=None,
is_label_horizontal=None,
hidden=False,
disabled=False
visible=None,
enabled=None,
hidden=None,
disabled=None,
):
if is_label_horizontal is None:
is_label_horizontal = True

if hidden is None:
hidden = False
enabled = _convert_reversed_attr(
enabled, disabled, "enabled", "disabled", True
)
visible = _convert_reversed_attr(
visible, hidden, "visible", "hidden", True
)

self.key = key
self.label = label
self.tooltip = tooltip
self.default = default
self.is_label_horizontal = is_label_horizontal
self.hidden = hidden
self.disabled = disabled
self.visible = visible
self.enabled = enabled
self._id = uuid.uuid4().hex

self.__init__class__ = AbstractAttrDef
Expand All @@ -149,14 +182,30 @@ def __init__(
def id(self):
return self._id

@property
def hidden(self):
return not self.visible

@hidden.setter
def hidden(self, value):
self.visible = not value

@property
def disabled(self):
return not self.enabled

@disabled.setter
def disabled(self, value):
self.enabled = not value

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return (
self.key == other.key
and self.hidden == other.hidden
and self.default == other.default
and self.disabled == other.disabled
and self.visible == other.visible
and self.enabled == other.enabled
)

def __ne__(self, other):
Expand Down Expand Up @@ -198,8 +247,8 @@ def serialize(self):
"tooltip": self.tooltip,
"default": self.default,
"is_label_horizontal": self.is_label_horizontal,
"hidden": self.hidden,
"disabled": self.disabled
"visible": self.visible,
"enabled": self.enabled
}
for attr in self.type_attributes:
data[attr] = getattr(self, attr)
Expand Down Expand Up @@ -279,8 +328,8 @@ class HiddenDef(AbstractAttrDef):

def __init__(self, key, default=None, **kwargs):
kwargs["default"] = default
kwargs["hidden"] = True
super(HiddenDef, self).__init__(key, **kwargs)
kwargs["visible"] = False
super().__init__(key, **kwargs)

def convert_value(self, value):
return value
Expand Down
6 changes: 3 additions & 3 deletions client/ayon_core/tools/attribute_defs/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

def create_widget_for_attr_def(attr_def, parent=None):
widget = _create_widget_for_attr_def(attr_def, parent)
if attr_def.hidden:
if not attr_def.visible:
widget.setVisible(False)

if attr_def.disabled:
if not attr_def.enabled:
widget.setEnabled(False)
return widget

Expand Down Expand Up @@ -135,7 +135,7 @@ def add_attr_defs(self, attr_defs):
widget = create_widget_for_attr_def(attr_def, self)
self._widgets.append(widget)

if attr_def.hidden:
if not attr_def.visible:
continue

expand_cols = 2
Expand Down
4 changes: 2 additions & 2 deletions client/ayon_core/tools/publisher/widgets/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ def set_current_instances(self, instances):
self._attr_def_id_to_instances[attr_def.id] = attr_instances
self._attr_def_id_to_attr_def[attr_def.id] = attr_def

if attr_def.hidden:
if not attr_def.visible:
continue

expand_cols = 2
Expand Down Expand Up @@ -1585,7 +1585,7 @@ def set_current_instances(self, instances, context_selected):
widget = create_widget_for_attr_def(
attr_def, content_widget
)
hidden_widget = attr_def.hidden
hidden_widget = not attr_def.visible
# Hide unknown values of publish plugins
# - The keys in most of cases does not represent what would
# label represent
Expand Down

0 comments on commit 1489d98

Please sign in to comment.