import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QCheckBox, QSlider
from PyQt6.QtCore import Qt
from gcode_viewer import GCodeViewerWidget

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("G-code 预览")
        self.viewer = GCodeViewerWidget()

        # 加载文件（请替换为你的 G-code 路径）
        self.viewer.load_gcode("/home/lhye200/.octoprint/uploads/20260508141659_085359c9908947bebcaa0fe7490641e8.gcode")

        # 进度滑块（0~100%）
        self.slider = QSlider(Qt.Orientation.Horizontal)
        self.slider.setRange(0, 100)
        self.slider.setValue(100)
        self.slider.valueChanged.connect(
            lambda v: self.viewer.update_processes(v / 100.0))

        # 类型开关
        self.chk_support = QCheckBox("显示支撑")
        self.chk_support.setChecked(True)
        self.chk_support.toggled.connect(
            lambda v: self.viewer.update_switch('SUPPORT', v))

        self.chk_infill = QCheckBox("显示填充")
        self.chk_infill.setChecked(True)
        self.chk_infill.toggled.connect(
            lambda v: self.viewer.update_switch('FILL', v))

        self.chk_perimeter = QCheckBox("显示外壳")
        self.chk_perimeter.setChecked(True)
        self.chk_perimeter.toggled.connect(
            lambda v: self.viewer.update_switch('WALL-OUTER', v))
        self.chk_perimeter.toggled.connect(
            lambda v: self.viewer.update_switch('WALL-INNER', v))

        central = QWidget()
        layout = QVBoxLayout(central)
        layout.addWidget(self.viewer, 1)
        layout.addWidget(self.slider)
        layout.addWidget(self.chk_support)
        layout.addWidget(self.chk_infill)
        layout.addWidget(self.chk_perimeter)
        self.setCentralWidget(central)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec())