Skip to content

Commit 8c5c128

Browse files
committed
improve SimulationGUI and PreferencesGUI
1 parent 6230470 commit 8c5c128

File tree

3 files changed

+340
-158
lines changed

3 files changed

+340
-158
lines changed
0 Bytes
Binary file not shown.

devsimpy/PreferencesGUI.py

Lines changed: 141 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -807,9 +807,6 @@ def __init__(self, parent):
807807
self.selected_message_format = getattr(builtins, 'SELECTED_MESSAGE_FORMAT', 'DEVSStreaming')
808808
self.selected_broker = getattr(builtins, 'SELECTED_BROKER', 'Kafka')
809809

810-
# Load MQTT configuration from config file at startup
811-
self._load_mqtt_config_from_file()
812-
813810
self.InitUI()
814811

815812
def _load_mqtt_config_from_file(self):
@@ -836,11 +833,10 @@ def _load_mqtt_config_from_file(self):
836833
setattr(builtins, 'MQTT_USERNAME', username or None)
837834
setattr(builtins, 'MQTT_PASSWORD', password or None)
838835

839-
logger.info(f"Loaded MQTT config from file: {address}:{port}, username={'(set)' if username else 'None'}")
840-
else:
841-
logger.info(f"No BROKER_MQTT section found in config file")
836+
# Only log at debug level, not info
837+
logger.debug(f"Loaded MQTT config from file: {address}:{port}, username={'(set)' if username else 'None'}")
842838
except Exception as e:
843-
logger.warning(f"Could not load MQTT config from file: {e}")
839+
logger.debug(f"Could not load MQTT config from file: {e}")
844840

845841
def InitUI(self):
846842
""" Initialize the UI with modern layout
@@ -854,7 +850,7 @@ def InitUI(self):
854850
# ============================================================
855851
devsBox = wx.StaticBoxSizer(wx.VERTICAL, self, _('DEVS Kernel'))
856852

857-
devsGrid = wx.FlexGridSizer(4, 3, 10, 10)
853+
devsGrid = wx.FlexGridSizer(0, 3, 10, 10) # 0 rows means auto-calculate
858854
devsGrid.AddGrowableCol(1, 1)
859855

860856
# DEVS Package selection
@@ -898,58 +894,45 @@ def InitUI(self):
898894
devsGrid.Add(strategyInfoBtn, 0, wx.ALIGN_CENTER_VERTICAL)
899895

900896
# Message Format and Broker selection (for BrokerDEVS with nested strategies)
901-
self._init_broker_ui_elements()
897+
# Create broker UI elements (will be added to grid conditionally)
898+
self.msgFormatLabel = wx.StaticText(self, label=_("Message Format:"))
899+
self.msgFormatLabel.SetToolTip(_("Select the message standardization (DEVSStreaming, Custom, etc.)"))
900+
self.msgFormatLabel.Hide() # Hidden by default
901+
902+
self.cb_msg_format = wx.ComboBox(self, wx.NewIdRef(), "", choices=[], style=wx.CB_READONLY)
903+
self.cb_msg_format.SetToolTip(_("Message format for broker communication"))
904+
self.cb_msg_format.Bind(wx.EVT_COMBOBOX, self.onMessageFormatChange)
905+
self.cb_msg_format.Hide() # Hidden by default
906+
907+
# Broker selection
908+
self.brokerLabel = wx.StaticText(self, label=_("Broker:"))
909+
self.brokerLabel.SetToolTip(_("Select the message broker (Kafka, MQTT, etc.)"))
910+
self.brokerLabel.Hide() # Hidden by default
911+
912+
self.cb_broker = wx.ComboBox(self, wx.NewIdRef(), "", choices=[], style=wx.CB_READONLY)
913+
self.cb_broker.SetToolTip(_("Message broker for distributed simulation"))
914+
self.cb_broker.Bind(wx.EVT_COMBOBOX, self.onBrokerChange)
915+
self.cb_broker.Hide() # Hidden by default
916+
917+
# Broker info button
918+
self.brokerInfoBtn = wx.Button(self, wx.NewIdRef(), _("Config"))
919+
self.brokerInfoBtn.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_CDROM, wx.ART_BUTTON))
920+
self.brokerInfoBtn.SetToolTip(_("Configure broker settings"))
921+
self.brokerInfoBtn.Bind(wx.EVT_BUTTON, self.OnBrokerConfig)
922+
self.brokerInfoBtn.Hide() # Hidden by default
923+
924+
# Store reference to the grid for dynamic updates
925+
self.devsGrid = devsGrid
902926

903927
# Check if current strategy dict is nested (message format + broker)
904928
strategy_dict = getattr(builtins,
905929
f'{self.cb3.GetValue().upper()}_SIM_STRATEGY_DICT')
906930
self._has_broker_selection = isinstance(list(strategy_dict.values())[0] if strategy_dict else {}, dict)
907931

932+
# Add broker UI to grid only if package supports it
908933
if self._has_broker_selection:
909-
# Message Format selection
910-
self.msgFormatLabel = wx.StaticText(self, label=_("Message Format:"))
911-
self.msgFormatLabel.SetToolTip(_("Select the message standardization (DEVSStreaming, Custom, etc.)"))
912-
913-
msg_formats = list(strategy_dict.keys())
914-
# Use saved message format if it exists in the list
915-
default_msg_format = self.selected_message_format if self.selected_message_format in msg_formats else (msg_formats[0] if msg_formats else "")
916-
917-
self.cb_msg_format = wx.ComboBox(self, wx.NewIdRef(), default_msg_format,
918-
choices=msg_formats, style=wx.CB_READONLY)
919-
self.cb_msg_format.SetToolTip(_("Message format for broker communication"))
920-
self.cb_msg_format.Bind(wx.EVT_COMBOBOX, self.onMessageFormatChange)
921-
922-
devsGrid.Add(self.msgFormatLabel, 0, wx.ALIGN_CENTER_VERTICAL)
923-
devsGrid.Add(self.cb_msg_format, 1, wx.EXPAND)
924-
devsGrid.Add(wx.StaticText(self, label=""), 0) # Empty cell for button column
925-
926-
# Broker selection
927-
self.brokerLabel = wx.StaticText(self, label=_("Broker:"))
928-
self.brokerLabel.SetToolTip(_("Select the message broker (Kafka, MQTT, etc.)"))
929-
930-
if msg_formats and default_msg_format in strategy_dict:
931-
brokers = list(strategy_dict[default_msg_format].keys())
932-
# Use saved broker if it exists in the list
933-
default_broker = self.selected_broker if self.selected_broker in brokers else (brokers[0] if brokers else "")
934-
self.cb_broker = wx.ComboBox(self, wx.NewIdRef(), default_broker,
935-
choices=brokers, style=wx.CB_READONLY)
936-
else:
937-
self.cb_broker = wx.ComboBox(self, wx.NewIdRef(), "", choices=[], style=wx.CB_READONLY)
938-
939-
self.cb_broker.SetToolTip(_("Message broker for distributed simulation"))
940-
self.cb_broker.Bind(wx.EVT_COMBOBOX, self.onBrokerChange)
941-
942-
# Broker info button
943-
self.brokerInfoBtn = wx.Button(self, wx.NewIdRef(), _("Config"))
944-
self.brokerInfoBtn.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_CDROM, wx.ART_BUTTON))
945-
self.brokerInfoBtn.SetToolTip(_("Configure broker settings"))
946-
self.brokerInfoBtn.Bind(wx.EVT_BUTTON, self.OnBrokerConfig)
947-
948-
devsGrid.Add(self.brokerLabel, 0, wx.ALIGN_CENTER_VERTICAL)
949-
devsGrid.Add(self.cb_broker, 1, wx.EXPAND)
950-
devsGrid.Add(self.brokerInfoBtn, 0, wx.ALIGN_CENTER_VERTICAL)
951-
else:
952-
self._has_broker_selection = False
934+
self._populate_broker_ui_elements(strategy_dict)
935+
self._add_broker_rows_to_grid()
953936

954937
devsBox.Add(devsGrid, 0, wx.EXPAND|wx.ALL, 5)
955938
mainSizer.Add(devsBox, 0, wx.EXPAND|wx.ALL, 10)
@@ -1204,22 +1187,80 @@ def onCb3(self, evt):
12041187
self.default_devs_dir = val
12051188
self.sim_defaut_strategy = self.cb4.GetValue()
12061189

1207-
# Show/hide broker UI elements based on whether BrokerDEVS is selected
1208-
is_broker_devs = val.upper() == 'BROKERDEVS'
1209-
1210-
if self.msgFormatLabel:
1211-
self.msgFormatLabel.Show(is_broker_devs)
1212-
if self.cb_msg_format:
1213-
self.cb_msg_format.Show(is_broker_devs)
1214-
if self.brokerLabel:
1215-
self.brokerLabel.Show(is_broker_devs)
1216-
if self.cb_broker:
1217-
self.cb_broker.Show(is_broker_devs)
1218-
if self.brokerInfoBtn:
1219-
self.brokerInfoBtn.Show(is_broker_devs)
1220-
1221-
# Refresh layout
1190+
# Check if new package has broker selection (nested strategy dict)
1191+
strategy_dict = getattr(builtins, f'{val.upper()}_SIM_STRATEGY_DICT')
1192+
has_broker_selection = isinstance(list(strategy_dict.values())[0] if strategy_dict else {}, dict)
1193+
1194+
# Update broker UI based on package type
1195+
if has_broker_selection and not self._has_broker_selection:
1196+
# Transitioning TO BrokerDEVS - add rows
1197+
self._populate_broker_ui_elements(strategy_dict)
1198+
self._add_broker_rows_to_grid()
1199+
self._has_broker_selection = True
1200+
elif not has_broker_selection and self._has_broker_selection:
1201+
# Transitioning FROM BrokerDEVS - remove rows
1202+
self._remove_broker_rows_from_grid()
1203+
self._has_broker_selection = False
1204+
elif has_broker_selection and self._has_broker_selection:
1205+
# Already in broker mode - just update options
1206+
self._populate_broker_ui_elements(strategy_dict)
1207+
1208+
# Force complete layout refresh
1209+
self.devsGrid.Layout()
1210+
self.Layout()
12221211
self.GetParent().Layout()
1212+
self.GetParent().Refresh()
1213+
self.GetParent().Update()
1214+
1215+
def _add_broker_rows_to_grid(self):
1216+
""" Add broker UI rows to the grid
1217+
"""
1218+
# Show widgets before adding to grid
1219+
self.msgFormatLabel.Show()
1220+
self.cb_msg_format.Show()
1221+
self.brokerLabel.Show()
1222+
self.cb_broker.Show()
1223+
self.brokerInfoBtn.Show()
1224+
1225+
self.devsGrid.Add(self.msgFormatLabel, 0, wx.ALIGN_CENTER_VERTICAL)
1226+
self.devsGrid.Add(self.cb_msg_format, 1, wx.EXPAND)
1227+
self.devsGrid.Add(wx.StaticText(self, label=""), 0) # Empty cell for button column
1228+
1229+
self.devsGrid.Add(self.brokerLabel, 0, wx.ALIGN_CENTER_VERTICAL)
1230+
self.devsGrid.Add(self.cb_broker, 1, wx.EXPAND)
1231+
self.devsGrid.Add(self.brokerInfoBtn, 0, wx.ALIGN_CENTER_VERTICAL)
1232+
1233+
def _remove_broker_rows_from_grid(self):
1234+
""" Remove broker UI rows from the grid
1235+
"""
1236+
# Hide broker widgets
1237+
self.msgFormatLabel.Hide()
1238+
self.cb_msg_format.Hide()
1239+
self.brokerLabel.Hide()
1240+
self.cb_broker.Hide()
1241+
self.brokerInfoBtn.Hide()
1242+
1243+
# Remove broker rows by clearing and re-adding only non-broker items
1244+
self.devsGrid.Clear(False) # False to NOT delete child windows
1245+
1246+
# Re-add non-broker rows
1247+
devsLabel = wx.StaticText(self, label=_("Package:"))
1248+
self.devsGrid.Add(devsLabel, 0, wx.ALIGN_CENTER_VERTICAL)
1249+
self.devsGrid.Add(self.cb3, 1, wx.EXPAND)
1250+
self.devsGrid.Add(self.devs_doc_btn, 0, wx.ALIGN_CENTER_VERTICAL)
1251+
1252+
strategyLabel = wx.StaticText(self, label=_("Strategy:"))
1253+
self.devsGrid.Add(strategyLabel, 0, wx.ALIGN_CENTER_VERTICAL)
1254+
self.devsGrid.Add(self.cb4, 1, wx.EXPAND)
1255+
1256+
# Strategy info button - need to recreate since we cleared
1257+
strategyInfoBtn = wx.Button(self, wx.NewIdRef(), _("Info"))
1258+
strategyInfoBtn.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_BUTTON))
1259+
strategyInfoBtn.SetToolTip(_("Information about the selected strategy"))
1260+
strategyInfoBtn.Bind(wx.EVT_BUTTON, self.OnStrategyInfo)
1261+
self.devsGrid.Add(strategyInfoBtn, 0, wx.ALIGN_CENTER_VERTICAL)
1262+
1263+
12231264

12241265
def _init_broker_ui_elements(self):
12251266
""" Initialize broker UI elements as None (created dynamically if needed)
@@ -1230,6 +1271,34 @@ def _init_broker_ui_elements(self):
12301271
self.brokerLabel = None
12311272
self.brokerInfoBtn = None
12321273

1274+
def _populate_broker_ui_elements(self, strategy_dict):
1275+
""" Populate broker UI elements with options from the strategy dict
1276+
"""
1277+
# Get message formats
1278+
msg_formats = list(strategy_dict.keys())
1279+
default_msg_format = self.selected_message_format if self.selected_message_format in msg_formats else (msg_formats[0] if msg_formats else "")
1280+
1281+
# Update message format dropdown
1282+
self.cb_msg_format.Clear()
1283+
self.cb_msg_format.Set(msg_formats)
1284+
if default_msg_format:
1285+
self.cb_msg_format.SetValue(default_msg_format)
1286+
self.selected_message_format = default_msg_format
1287+
1288+
# Update broker list based on selected message format
1289+
if msg_formats and default_msg_format in strategy_dict:
1290+
brokers = list(strategy_dict[default_msg_format].keys())
1291+
default_broker = self.selected_broker if self.selected_broker in brokers else (brokers[0] if brokers else "")
1292+
else:
1293+
brokers = []
1294+
default_broker = ""
1295+
1296+
self.cb_broker.Clear()
1297+
self.cb_broker.Set(brokers)
1298+
if default_broker:
1299+
self.cb_broker.SetValue(default_broker)
1300+
self.selected_broker = default_broker
1301+
12331302
def onMessageFormatChange(self, evt):
12341303
""" Message format selection changed
12351304
"""
@@ -1261,6 +1330,10 @@ def onBrokerChange(self, evt):
12611330

12621331
# Update global config
12631332
builtins.SELECTED_BROKER = broker
1333+
1334+
# Load MQTT config if MQTT is selected
1335+
if broker and broker.upper() == 'MQTT':
1336+
self._load_mqtt_config_from_file()
12641337

12651338
def OnBrokerConfig(self, evt):
12661339
""" Open broker configuration dialog

0 commit comments

Comments
 (0)