Merge pull request #1891 from pypeclub/feature/conditional_dict_enum_position

Settings: Conditional dict enum positioning
This commit is contained in:
Jakub Trllo 2021-08-02 09:46:32 +02:00 committed by GitHub
commit bd02a61684
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 14 deletions

View file

@ -144,6 +144,13 @@ class DictConditionalEntity(ItemEntity):
self.enum_entity = None
# GUI attributes
self.enum_is_horizontal = self.schema_data.get(
"enum_is_horizontal", False
)
# `enum_on_right` can be used only if
self.enum_on_right = self.schema_data.get("enum_on_right", False)
self.highlight_content = self.schema_data.get(
"highlight_content", False
)
@ -185,13 +192,13 @@ class DictConditionalEntity(ItemEntity):
children_def_keys = []
for children_def in self.enum_children:
if not isinstance(children_def, dict):
raise EntitySchemaError((
raise EntitySchemaError(self, (
"Children definition under key 'enum_children' must"
" be a dictionary."
))
if "key" not in children_def:
raise EntitySchemaError((
raise EntitySchemaError(self, (
"Children definition under key 'enum_children' miss"
" 'key' definition."
))
@ -286,7 +293,7 @@ class DictConditionalEntity(ItemEntity):
"multiselection": False,
"enum_items": enum_items,
"key": enum_key,
"label": self.enum_label or enum_key
"label": self.enum_label
}
enum_entity = self.create_schema_object(enum_schema, self)

View file

@ -204,6 +204,8 @@
- it is possible to add darker background with `"highlight_content"` (Default: `False`)
- darker background has limits of maximum applies after 3-4 nested highlighted items there is not difference in the color
- output is dictionary `{the "key": children values}`
- for UI porposes was added `enum_is_horizontal` which will make combobox appear next to children inputs instead of on top of them (Default: `False`)
- this has extended ability of `enum_on_right` which will move combobox to right side next to children widgets (Default: `False`)
```
# Example
{

View file

@ -9,6 +9,31 @@
"label": "Color input",
"type": "color"
},
{
"type": "dict-conditional",
"key": "overriden_value",
"label": "Overriden value",
"enum_key": "overriden",
"enum_is_horizontal": true,
"enum_children": [
{
"key": "overriden",
"label": "Override value",
"children": [
{
"type": "number",
"key": "value",
"label": "value"
}
]
},
{
"key": "inherit",
"label": "Inherit value",
"children": []
}
]
},
{
"type": "dict-conditional",
"use_label_wrap": true,

View file

@ -24,6 +24,7 @@ class DictConditionalWidget(BaseWidget):
self.body_widget = None
self.content_widget = None
self.content_layout = None
self.enum_layout = None
label = None
if self.entity.is_dynamic_item:
@ -40,8 +41,36 @@ class DictConditionalWidget(BaseWidget):
self._enum_key_by_wrapper_id = {}
self._added_wrapper_ids = set()
self.content_layout.setColumnStretch(0, 0)
self.content_layout.setColumnStretch(1, 1)
enum_layout = QtWidgets.QGridLayout()
enum_layout.setContentsMargins(0, 0, 0, 0)
enum_layout.setColumnStretch(0, 0)
enum_layout.setColumnStretch(1, 1)
all_children_layout = QtWidgets.QVBoxLayout()
all_children_layout.setContentsMargins(0, 0, 0, 0)
if self.entity.enum_is_horizontal:
if self.entity.enum_on_right:
self.content_layout.addLayout(all_children_layout, 0, 0)
self.content_layout.addLayout(enum_layout, 0, 1)
# Stretch combobox to minimum and expand value
self.content_layout.setColumnStretch(0, 1)
self.content_layout.setColumnStretch(1, 0)
else:
self.content_layout.addLayout(enum_layout, 0, 0)
self.content_layout.addLayout(all_children_layout, 0, 1)
# Stretch combobox to minimum and expand value
self.content_layout.setColumnStretch(0, 0)
self.content_layout.setColumnStretch(1, 1)
else:
# Expand content
self.content_layout.setColumnStretch(0, 1)
self.content_layout.addLayout(enum_layout, 0, 0)
self.content_layout.addLayout(all_children_layout, 1, 0)
self.enum_layout = enum_layout
self.all_children_layout = all_children_layout
# Add enum entity to layout mapping
enum_entity = self.entity.enum_entity
@ -58,6 +87,8 @@ class DictConditionalWidget(BaseWidget):
content_layout.setContentsMargins(0, 0, 0, 0)
content_layout.setSpacing(5)
all_children_layout.addWidget(content_widget)
self._content_by_enum_value[enum_key] = {
"widget": content_widget,
"layout": content_layout
@ -80,9 +111,6 @@ class DictConditionalWidget(BaseWidget):
for item_key, children in self.entity.children.items():
content_widget = self._content_by_enum_value[item_key]["widget"]
row = self.content_layout.rowCount()
self.content_layout.addWidget(content_widget, row, 0, 1, 2)
for child_obj in children:
self.input_fields.append(
self.create_ui_for_entity(
@ -191,12 +219,25 @@ class DictConditionalWidget(BaseWidget):
else:
map_id = widget.entity.id
content_widget = self.content_widget
content_layout = self.content_layout
if map_id != self.entity.enum_entity.id:
enum_value = self._enum_key_by_wrapper_id[map_id]
content_widget = self._content_by_enum_value[enum_value]["widget"]
content_layout = self._content_by_enum_value[enum_value]["layout"]
is_enum_item = map_id == self.entity.enum_entity.id
if is_enum_item:
content_widget = self.content_widget
content_layout = self.enum_layout
if not label:
content_layout.addWidget(widget, 0, 0, 1, 2)
return
label_widget = GridLabelWidget(label, widget)
label_widget.input_field = widget
widget.label_widget = label_widget
content_layout.addWidget(label_widget, 0, 0, 1, 1)
content_layout.addWidget(widget, 0, 1, 1, 1)
return
enum_value = self._enum_key_by_wrapper_id[map_id]
content_widget = self._content_by_enum_value[enum_value]["widget"]
content_layout = self._content_by_enum_value[enum_value]["layout"]
wrapper = self._parent_widget_by_entity_id[map_id]
if wrapper is not content_widget: