From 29bdc28bb698cd55171df548bd876a78bb9e363a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Weber?= Date: Thu, 5 Dec 2024 16:01:41 +0100 Subject: [PATCH 1/2] look for an entry called group_type to load the corresponding group parameter --- src/pymodaq_gui/utils/widgets/tree_toml.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pymodaq_gui/utils/widgets/tree_toml.py b/src/pymodaq_gui/utils/widgets/tree_toml.py index fdff68c5..76bc10a1 100644 --- a/src/pymodaq_gui/utils/widgets/tree_toml.py +++ b/src/pymodaq_gui/utils/widgets/tree_toml.py @@ -82,8 +82,9 @@ def dict_to_param(cls, config: dict, capitalize=True) -> Parameter: params = [] for key in config: if isinstance(config[key], dict): + ptype = config[key].get('group_type', 'group') params.append({'title': f'{key.capitalize() if capitalize else key}:', - 'name': key, 'type': 'group', + 'name': key, 'type': ptype, 'children': cls.dict_to_param(config[key], capitalize=capitalize), 'expanded': 'user' in key.lower() or 'general' in key.lower()}) else: From d28d509cbc9b25641c32d6af8adbe81e25f4668a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Weber?= Date: Thu, 5 Dec 2024 16:02:19 +0100 Subject: [PATCH 2/2] add a minimal working example of a config using a custom scalable group --- src/pymodaq_gui/examples/custom_group_toml.py | 89 +++++++++++++++++++ .../examples/custom_toml_group.toml | 9 ++ tests/data/custom_toml_group.toml | 9 ++ tests/utils/config_test.py | 2 +- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/pymodaq_gui/examples/custom_group_toml.py create mode 100644 src/pymodaq_gui/examples/custom_toml_group.toml create mode 100644 tests/data/custom_toml_group.toml diff --git a/src/pymodaq_gui/examples/custom_group_toml.py b/src/pymodaq_gui/examples/custom_group_toml.py new file mode 100644 index 00000000..821f9df8 --- /dev/null +++ b/src/pymodaq_gui/examples/custom_group_toml.py @@ -0,0 +1,89 @@ +from pathlib import Path +import datetime + +import toml + +from pymodaq_utils import config as config_mod +from pymodaq_gui.utils.widgets.tree_toml import TreeFromToml +from pymodaq_gui.parameter.pymodaq_ptypes import registerParameterType, GroupParameter + +config_path = Path('./custom_toml_group.toml') + + +class CustomConfig(config_mod.BaseConfig): + """Main class to deal with configuration values for this plugin""" + config_template_path = config_path + config_name = f"custom_settings" + + +child_template = [ + {'title': 'Do it?:', 'name': 'do_it', 'type': 'bool', 'value': True}, + {'title': 'Choose:', 'name': 'choice', 'type': 'list', 'value': 'ok', + 'limits': ['ok', 'nonok']}, +] + + +class ScalableCustomGroup(GroupParameter): + """ + | + + ================ ============= + **Attributes** **Type** + *opts* dictionnary + ================ ============= + + See Also + -------- + hardware.DAQ_Move_Stage_type + """ + + def __init__(self, **opts): + opts['type'] = 'mycustomgroupparameter' + opts['addText'] = "Add" + opts['addList'] = [str(ind) for ind in range(10)] + super().__init__(**opts) + + def addNew(self, typ): + """ + Add a child. + + =============== =========== + **Parameters** **Type** + *typ* string + =============== =========== + """ + name_prefix = 'template' + + child_indexes = [int(par.name()[len(name_prefix) + 1:]) for par in self.children()[1:]] + + if child_indexes == []: + newindex = 0 + else: + newindex = max(child_indexes) + 1 + + children = [] + for ind_child in range(int(typ)): + newindex += ind_child + children.append({'title': 'Template {:02.0f}'.format(newindex), + 'name': f'{name_prefix}{newindex:02.0f}', + 'type': 'group', + 'removable': True, 'children': child_template}) + + self.addChildren(children) + + +registerParameterType('mycustomgroupparameter', ScalableCustomGroup, override=True) + + +if __name__ == '__main__': + from pymodaq_gui.utils.utils import mkQApp + + app = mkQApp('Dashboard') + + config = CustomConfig() + + tree_toml = TreeFromToml(config) + tree_toml.show_dialog() + + app.exec() + diff --git a/src/pymodaq_gui/examples/custom_toml_group.toml b/src/pymodaq_gui/examples/custom_toml_group.toml new file mode 100644 index 00000000..c827865a --- /dev/null +++ b/src/pymodaq_gui/examples/custom_toml_group.toml @@ -0,0 +1,9 @@ +title = 'this is the configuration file of the plugin XXX' + +[astandardgroup] +afloat = 12.3 +aint = 8 + +[acustomgroup] +group_type = 'mycustomgroupparameter' + diff --git a/tests/data/custom_toml_group.toml b/tests/data/custom_toml_group.toml new file mode 100644 index 00000000..c827865a --- /dev/null +++ b/tests/data/custom_toml_group.toml @@ -0,0 +1,9 @@ +title = 'this is the configuration file of the plugin XXX' + +[astandardgroup] +afloat = 12.3 +aint = 8 + +[acustomgroup] +group_type = 'mycustomgroupparameter' + diff --git a/tests/utils/config_test.py b/tests/utils/config_test.py index 3c345eba..5c38e234 100644 --- a/tests/utils/config_test.py +++ b/tests/utils/config_test.py @@ -9,6 +9,7 @@ from pymodaq_gui.config import ConfigSaverLoader, get_set_roi_path + class CustomConfig(config_mod.BaseConfig): """Main class to deal with configuration values for this plugin""" config_template_path = None @@ -57,4 +58,3 @@ def test_modified(self, qtbot): saver_loader.load_config(settings) assert settings['aparent', 'anotherparam', 'max'] == value_before -