""" Standalone dashboard runner. Usato da dash_tipologie/run.py, dash_em_pivot/run.py, dash_diritti/run.py. """ import sys from pathlib import Path # Assicura che MyICR_Suite/ sia in sys.path _myicr_root = Path(__file__).resolve().parent.parent.parent if str(_myicr_root) not in sys.path: sys.path.insert(0, str(_myicr_root)) import yaml from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox def _load_theme() -> dict: try: cfg_path = _myicr_root / 'app' / 'config' / 'config.yaml' with open(cfg_path, encoding='utf-8') as f: return yaml.safe_load(f).get('theme', {}) except Exception: return {} class _FakeMainWindow: """Stub minimale di MainWindow per le dashboard standalone.""" def __init__(self, status_bar, theme: dict): self._status_bar = status_bar self.theme = theme def show_status_message(self, message: str, is_temporary: bool = False, duration_ms: int = 4000): if is_temporary: self._status_bar.showMessage(message, duration_ms) else: self._status_bar.showMessage(message) class DashWindow(QMainWindow): """QMainWindow che ospita una dashboard standalone.""" def __init__(self, DashClass, filter_data: dict, title: str, theme: dict, fullscreen_map: dict = None): super().__init__() self.setWindowTitle(title) self._fullscreen_map = fullscreen_map or {} self._theme = theme self._fs_windows = [] fake_mw = _FakeMainWindow(self.statusBar(), theme) dash = DashClass(main_window=fake_mw, filter_data=filter_data) if hasattr(dash, 'status_updated'): dash.status_updated.connect(self._on_status) if hasattr(dash, 'open_new_tab_requested'): dash.open_new_tab_requested.connect(self._open_fullscreen) self.setCentralWidget(dash) self.showMaximized() def _on_status(self, msg: str, is_temp: bool, duration_ms: int): if is_temp: self.statusBar().showMessage(msg, duration_ms) else: self.statusBar().showMessage(msg) def _open_fullscreen(self, page_type: str, title: str, _singleton: bool, data: dict): FSClass = self._fullscreen_map.get(page_type) if FSClass is None or 'dataframe' not in data: return class _FakeMW2: theme = self._theme win = QMainWindow() win.setWindowTitle(title) page = FSClass(main_window=_FakeMW2(), data=data['dataframe'], theme=self._theme) win.setCentralWidget(page) win.showMaximized() self._fs_windows.append(win) win.show() def run_dashboard(DashClass, filter_loader, title: str, fullscreen_map: dict = None): """ Avvia una dashboard standalone. Parameters ---------- DashClass : classe della dashboard (es. DashboardTipologiePage) filter_loader : callable() → dict (es. FilterPanel.get_filter_data) title : titolo finestra fullscreen_map : {'fullscreen_tipologie': FullScreenTablePage, ...} """ app = QApplication(sys.argv) app.setApplicationName(title) theme = _load_theme() try: filter_data = filter_loader() except Exception as e: QMessageBox.critical(None, 'Errore avvio', f'Impossibile caricare i dati di configurazione:\n{e}') sys.exit(1) win = DashWindow(DashClass, filter_data, title, theme, fullscreen_map) sys.exit(app.exec())