SXXXXXXX_Radian/radian/core/main_controller.py
2025-11-12 13:36:07 +01:00

127 lines
4.9 KiB
Python

# radian/core/main_controller.py
from pathlib import Path
from radian.gui.main_window import MainWindow
from radian.utils import logger
from radian.config.logging_config import LOGGING_CONFIG
from radian.components.component_manager import ComponentManager
# Get a logger for this specific module
log = logger.get_logger(__name__)
class MainController:
"""
The main controller of the RADIAN application.
It orchestrates the application's lifecycle, including initializing the GUI,
setting up the logging system, loading components, and handling shutdown.
"""
def __init__(self):
self.app: MainWindow | None = None
self.component_manager: ComponentManager | None = None
def run(self):
# 1. Initialize the main window (the GUI)
self.app = MainWindow()
# 2. Set up the centralized logging system
self._setup_logging()
# 3. Initialize and run the component manager
self._load_components()
# 4. Populate the GUI with the loaded components
self._populate_gui_with_components()
# 5. Set up a graceful shutdown procedure
self.app.protocol("WM_DELETE_WINDOW", self._on_shutdown)
log.info("RADIAN application started.")
log.info("RADIAN application started.")
log.info("Welcome to the Radar Data Integration & Analysis Network.")
# --- TEST: Log i componenti caricati ---
if self.component_manager:
loaded_names = self.component_manager.get_all_components().keys()
if loaded_names:
log.info(f"Available components: {list(loaded_names)}")
else:
log.warning("No components were loaded. Check config/components.yaml and paths.")
# 5. Start the Tkinter main event loop
self.app.mainloop()
def _load_components(self):
"""Initializes the ComponentManager and loads all plugins."""
log.info("Initializing Component Manager...")
# Assume config file is in 'config/components.yaml' relative to the project root
# This needs a robust way to find the project root. For now, let's assume...
config_path = Path(__file__).parent.parent / "config" / "components.yaml"
self.component_manager = ComponentManager(config_path.resolve())
self.component_manager.load_components()
def _setup_logging(self):
"""Initializes the basic logger and adds the Tkinter handler."""
# Set up the queue, processor, and file/console handlers
logger.setup_basic_logging(
root_tk_instance_for_processor=self.app,
logging_config_dict=LOGGING_CONFIG
)
# Add the handler that writes to our GUI log widget
logger.add_tkinter_handler(
gui_log_widget=self.app.log_text,
root_tk_instance_for_gui_handler=self.app,
logging_config_dict=LOGGING_CONFIG
)
log.info("Logging system configured and GUI handler attached.")
def _on_shutdown(self):
"""Handles the application shutdown sequence."""
log.info("Shutdown sequence initiated...")
# --- Future shutdown logic for components would go here ---
# Properly shut down the logging system
logger.shutdown_logging_system()
# Destroy the main window
if self.app:
self.app.destroy()
def _populate_gui_with_components(self):
"""Gets components from the manager and adds them to the rich GUI list."""
if not self.component_manager or not self.app:
return
all_components = self.component_manager.get_all_components()
for comp_id, component in all_components.items():
self.app.add_component_to_list(
component_id=comp_id,
name=component.get_name(),
icon_path=component.get_icon_path(),
callback=self._on_component_selected # Passiamo il metodo come callback
)
def _on_component_selected(self, component_id: str): # Ora riceve l'id direttamente
"""Called when a user clicks on a component widget in the list."""
if not self.app or not self.component_manager:
return
component = self.component_manager.get_component(component_id)
if not component:
return
log.info(f"Component '{component.get_name()}' selected.")
# Deseleziona gli altri (feedback visivo)
# (Implementazione più avanzata in futuro)
component_ui_frame = component.get_config_ui(self.app.content_frame)
if component_ui_frame:
self.app.display_component_ui(component_ui_frame)
else:
self.app.clear_content_area()
log.info(f"Component '{component.get_name()}' has no configuration UI.")