sistemata visualizzazione degli enum

This commit is contained in:
VALLONGOL 2025-12-22 11:25:02 +01:00
parent fd478c324f
commit 33a3741fc2

View File

@ -311,6 +311,14 @@ class MonitorApp(tk.Frame):
except Exception:
return None
def _get_enum_for_field(self, field_name):
"""Return enum class for field_name from ENUM_MAP, or None."""
try:
import Grifo_E_1553lib.data_types.enum_map as em
return em.ENUM_MAP.get(field_name)
except Exception:
return None
def show_message_form(self, label: str, editable: bool = True):
"""Build an editable form for message `label` (A messages).
@ -333,6 +341,10 @@ class MonitorApp(tk.Frame):
mdb = self._get_message_db()
if mdb is None:
try:
self.logger.warning(f"show_message_form: MessageDB not available for {label}")
except Exception:
pass
return
try:
msg_wrapper = mdb.getMessage(label)
@ -340,11 +352,25 @@ class MonitorApp(tk.Frame):
allm = mdb.getAllMessages()
msg_wrapper = allm.get(label)
if not msg_wrapper:
try:
self.logger.warning(f"show_message_form: No wrapper found for {label}")
except Exception:
pass
return
msg = getattr(msg_wrapper, 'message', None)
if msg is None:
try:
self.logger.warning(f"show_message_form: Wrapper for {label} has no message")
except Exception:
pass
return
# Log successful retrieval
try:
self.logger.info(f"Building form for {label}, message type: {type(msg)}")
except Exception:
pass
# clear previous form only when we know we're going to build a new one
for w in self.detail_form_container.winfo_children():
try:
@ -353,6 +379,7 @@ class MonitorApp(tk.Frame):
pass
self.form_widgets = {}
self.current_form_label = label
self.current_msg_wrapper = msg_wrapper # Store wrapper for updates
# header with Apply button at top
hdr = tk.Frame(self.detail_form_container)
@ -371,6 +398,13 @@ class MonitorApp(tk.Frame):
frm.pack(fill=tk.BOTH, expand=True, padx=6, pady=6)
self._build_form_fields(frm, msg, prefix='', editable=editable)
# IMPORTANT: Update values after building widgets
try:
self._update_form_values(msg_wrapper)
self.logger.info(f"Form values updated for {label}, {len(self.form_widgets)} widgets")
except Exception as e:
self.logger.error(f"Failed to update form values: {e}")
# show form container
try:
self.detail_form_container.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
@ -379,6 +413,7 @@ class MonitorApp(tk.Frame):
except Exception as e:
try:
self.log.insert(tk.END, f"Error building form: {e}\n")
self.logger.error(f"Error building form for {label}: {e}")
except Exception:
pass
@ -416,9 +451,9 @@ class MonitorApp(tk.Frame):
if hasattr(val, 'str') and _is_struct_like(getattr(val, 'str')):
subfrm = ttk.LabelFrame(parent, text=name)
subfrm.pack(fill=tk.X, padx=6, pady=4)
# recurse into the .str sub-structure so fields become e.g. settings.spare
# recurse into the .str sub-structure - prefix must include .str for correct value resolution
try:
self._build_form_fields(subfrm, getattr(val, 'str'), prefix=full, max_depth=max_depth, _depth=_depth+1)
self._build_form_fields(subfrm, getattr(val, 'str'), prefix=f"{full}.str", max_depth=max_depth, _depth=_depth+1)
except Exception:
pass
continue
@ -434,9 +469,15 @@ class MonitorApp(tk.Frame):
row_fr.pack(fill=tk.X, padx=2, pady=1)
lbl = tk.Label(row_fr, text=name, width=28, anchor=tk.W)
lbl.pack(side=tk.LEFT)
# enum choices?
# enum choices? Check ENUM_MAP using field name
enum_items = None
try:
# Try ENUM_MAP first using field name
enum_cls = self._get_enum_for_field(name)
if enum_cls:
enum_items = [(m.name, m.value) for m in enum_cls]
else:
# Fallback to class-based lookup
enum_items = self._get_enum_items(val.__class__.__name__)
except Exception:
enum_items = None
@ -1447,6 +1488,10 @@ class MonitorApp(tk.Frame):
msg = getattr(msg_wrapper, 'message', None)
if msg is None:
return
# DEBUG: Log first 3 fields to see what's happening
debug_count = 0
for full, widget in list(self.form_widgets.items()):
try:
# resolve the value from msg
@ -1462,8 +1507,21 @@ class MonitorApp(tk.Frame):
cur = cur[int(idx)]
else:
cur = getattr(cur, p)
val = getattr(cur, 'raw', getattr(cur, 'value', cur))
except Exception:
# Extract value - try raw, then value, then the object itself
raw_val = getattr(cur, 'raw', None)
value_val = getattr(cur, 'value', None)
val = raw_val if raw_val is not None else (value_val if value_val is not None else cur)
# DEBUG: Log first few fields
if debug_count < 3:
self.logger.info(f"Field '{full}': cur={type(cur).__name__}, raw={raw_val}, value={value_val}, final_val={val}")
debug_count += 1
except Exception as e:
if debug_count < 3:
self.logger.warning(f"Field '{full}': Exception during value extraction: {e}")
debug_count += 1
val = None
if widget[0] == 'combobox':