62 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from pathlib import Path
 | 
						||
import importlib.util
 | 
						||
 | 
						||
from PySide6.QtWidgets import QStackedWidget, QWidget, QLabel
 | 
						||
from PySide6.QtCore import Qt
 | 
						||
 | 
						||
from codes.common import clibs
 | 
						||
from codes.common.signal_bus import signal_bus
 | 
						||
 | 
						||
 | 
						||
class SStackedWidget(QStackedWidget):
 | 
						||
    def __init__(self, parent=None):
 | 
						||
        super().__init__(parent)
 | 
						||
 | 
						||
        self.predos()
 | 
						||
        self.init_ui()
 | 
						||
        self.setup_slot()
 | 
						||
 | 
						||
    def predos(self):
 | 
						||
        self.page_list = {}
 | 
						||
 | 
						||
    def init_ui(self):
 | 
						||
        # stacked widget  1x: 10为一级按钮页,其余为二级按钮页,2-9同理 | 0x. 日志/设置/关于等页面
 | 
						||
        self.load_pages()
 | 
						||
        w = self.page_list.get("w01_setting")
 | 
						||
        self.setCurrentWidget(w)
 | 
						||
 | 
						||
    def setup_slot(self):
 | 
						||
        signal_bus.init_stacked_page.connect(self.set_current_page)
 | 
						||
        signal_bus.qa_stacked_page_switch.connect(self.set_current_page)
 | 
						||
        signal_bus.list_widget_click.connect(self.set_current_page)
 | 
						||
 | 
						||
    def set_current_page(self, page_id: str):
 | 
						||
        w = self.page_list.get(page_id)
 | 
						||
        self.setCurrentWidget(w)
 | 
						||
        signal_bus.current_stacked_page.emit(page_id)
 | 
						||
 | 
						||
    def load_pages(self):
 | 
						||
        def to_camel(snake: str) -> str:
 | 
						||
            # w01_setting -> W01Setting
 | 
						||
            return "".join(word.capitalize() for word in snake.split('_'))
 | 
						||
 | 
						||
        def instantiate(pyFile: Path, className: str) -> QWidget | None:
 | 
						||
            try:
 | 
						||
                spec = importlib.util.spec_from_file_location(pyFile.stem, pyFile)
 | 
						||
                module = importlib.util.module_from_spec(spec)
 | 
						||
                spec.loader.exec_module(module)
 | 
						||
                cls = getattr(module, className)
 | 
						||
                if issubclass(cls, QWidget):
 | 
						||
                    return cls()
 | 
						||
            except Exception as e:
 | 
						||
                print(f"加载 {pyFile} 失败: {e}")
 | 
						||
 | 
						||
        pages_dir = clibs.base_path / "codes/ui/stacked_pages/"
 | 
						||
        for py_file in pages_dir.glob("w*.py"):
 | 
						||
            page_id = py_file.stem          # w01_setting
 | 
						||
            class_name = to_camel(page_id)  # W01Setting
 | 
						||
            widget = instantiate(py_file, class_name)
 | 
						||
            if widget:
 | 
						||
                widget.setObjectName(page_id)    # 用于 findChild / 切换
 | 
						||
                self.addWidget(widget)
 | 
						||
                self.page_list[page_id] = widget |