forked from ArduPilot/MethodicConfigurator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_model_vehicle_components_display.py
More file actions
160 lines (127 loc) · 5.67 KB
/
Copy pathdata_model_vehicle_components_display.py
File metadata and controls
160 lines (127 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Data model for vehicle component display logic.
This file is part of ArduPilot Methodic Configurator. https://github.com/ArduPilot/MethodicConfigurator
SPDX-FileCopyrightText: 2024-2026 Amilcar do Carmo Lucas <amilcar.lucas@iav.de>
SPDX-License-Identifier: GPL-3.0-or-later
"""
from typing import Union
from ardupilot_methodic_configurator import _
from ardupilot_methodic_configurator.data_model_vehicle_components_base import (
ComponentDataModelBase,
ComponentPath,
)
class ComponentDataModelDisplay(ComponentDataModelBase):
"""
Business logic for determining component display rules and configuration.
This class separates the data processing logic from UI concerns,
improving testability and maintainability.
"""
def should_display_in_simple_mode( # pylint: disable=too-many-branches
self, key: str, value: dict, path: list[str], complexity_mode: str
) -> bool:
"""
Determine if a component should be displayed in simple mode.
In simple mode, only show components that have at least one non-optional parameter.
Args:
key (str): The component key
value (dict): The component value
path (list): The path to the component
complexity_mode (str): Current complexity mode ("simple" or "normal")
Returns:
bool: True if the component should be displayed, False otherwise
"""
# If not in simple mode, always display the component
if complexity_mode != "simple":
return True
# Top-level components need special handling
if not path and key in self.get_all_components():
# Check if this component has any non-optional parameters
ret = False
for sub_key, sub_value in value.items():
if isinstance(sub_value, dict):
# For nested dictionaries, recursively check
if self.should_display_in_simple_mode(sub_key, sub_value, [*path, key], complexity_mode):
ret = True
break
else:
# For leaf nodes, check if they are optional
current_path = (*path, key, sub_key)
_, is_optional = self.schema.get_component_property_description(current_path)
if not is_optional:
ret = True
break
return ret
# For non-top-level components or leaf nodes
if isinstance(value, dict):
# Check if this component has any non-optional parameters
for sub_key, sub_value in value.items():
current_path = (*path, key, sub_key)
if isinstance(sub_value, dict):
if self.should_display_in_simple_mode(sub_key, sub_value, [*path, key], complexity_mode):
return True
else:
_, is_optional = self.schema.get_component_property_description(current_path)
if not is_optional:
return True
return False
def should_display_leaf_in_simple_mode(self, path: ComponentPath, complexity_mode: str) -> bool:
"""
Determine if a leaf component should be displayed in simple mode.
Args:
path: The component path
complexity_mode: Current complexity mode ("simple" or "normal")
Returns:
bool: True if the component should be displayed, False otherwise
"""
if complexity_mode != "simple":
return True
_, is_optional = self.schema.get_component_property_description(path)
return not is_optional
def prepare_non_leaf_widget_config(self, key: str, value: dict, path: list[str]) -> dict:
"""
Prepare configuration for non-leaf widget creation. Pure function for easy testing.
Args:
key: Component key
value: Component value dictionary
path: Path to the component
Returns:
Dictionary with widget configuration data
"""
is_toplevel = len(path) == 0 # More explicit than checking parent type
current_path = (*path, key)
description, is_optional = self.schema.get_component_property_description(current_path)
description = _(description) if description else ""
# Enhance tooltip for optional fields
if is_optional and description:
description += _("\nThis is optional and can be left blank")
return {
"key": key,
"value": value,
"path": current_path,
"description": description,
"is_optional": is_optional,
"is_toplevel": is_toplevel,
}
def prepare_leaf_widget_config(self, key: str, value: Union[str, float], path: list[str]) -> dict:
"""
Prepare configuration for leaf widget creation. Pure function for easy testing.
Args:
key: Component key
value: Component value
path: Path to the component
Returns:
Dictionary with widget configuration data
"""
component_path = (*path, key)
description, is_optional = self.schema.get_component_property_description(component_path)
description = _(description) if description else ""
# Enhance tooltip for optional fields
if is_optional and description:
description += _("\nThis is optional and can be left blank")
return {
"key": key,
"value": value,
"path": component_path,
"description": description,
"is_optional": is_optional,
}