from PySide6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel, QApplication) from PySide6.QtCore import Qt, Signal, QTimer from PySide6.QtGui import QPainter, QPixmap from .value_adjuster import ValueAdjuster from .switch_button import SwitchButton import sys import resources.resources_rc from utils.image_paths import ImagePaths class PlanWidget(QWidget): # 自动派单切换信号 # True: 自动派单 False: 手动派单 auto_dispatch_signal = Signal(bool) # 计划表单最终的确认的修改的方量 # 对应的任务名, 最终的方量 final_modify_volume_signal = Signal(str, float) def __init__(self, parent=None): super().__init__(parent) # 加载背景图 self.bg_pixmap = QPixmap(ImagePaths.TASK_PLAN_BACKGROUND) self.setFixedSize(self.bg_pixmap.size()) # 存储可修改控件的字典 self.controls = { "plan_no": None, # 计划单号 "volume": None, # 计划方量(ValueAdjuster) "ratio": None, # 计划配比 "status": None, # 下发状态 "auto_dispatch": None # 自动派单(SwitchButton) } # ================================================== # 用于计划表单的修改方量的最终确认 # 1、确认最终修改的方量的计时器(超时代表着确认了最终的方量) self.volume_timer = QTimer(self) self.volume_timer.setInterval(2500) # 2.5秒延迟(根据需求调整) self.volume_timer.setSingleShot(True) # 单次触发:避免重复执行 self.volume_timer.timeout.connect(self._sync_final_volume) # 2、该计划表单对应的任务名 self.plan_table_task_name = None # 主垂直布局 main_layout = QVBoxLayout(self) main_layout.setContentsMargins(13, 5, 6, 15) main_layout.setSpacing(0) # 第一行:计划单号 row1_layout = QHBoxLayout() row1_layout.setSpacing(0) status_icon1 = QLabel() status_icon1.setPixmap(QPixmap(ImagePaths.TASK_RECT4)) status_icon1.setFixedSize(6, 6) row1_layout.addWidget(status_icon1) row1_layout.addSpacing(4) label1 = QLabel("计划单号") label1.setStyleSheet("font-size: 18px; color: #03f5ff;") label1.setFixedSize(73, 20) row1_layout.addWidget(label1, alignment=Qt.AlignLeft) value1 = QLabel("PD0001") value1.setStyleSheet("font-size: 18px; color: white;") row1_layout.addWidget(value1) main_layout.addLayout(row1_layout) self.controls["plan_no"] = value1 # 第二行:计划方量 row2_layout = QHBoxLayout() status_icon2 = QLabel() status_icon2.setPixmap(QPixmap(ImagePaths.TASK_RECT1)) status_icon2.setFixedSize(6, 6) row2_layout.addWidget(status_icon2) row2_layout.addSpacing(4) label2 = QLabel("计划方量") label2.setStyleSheet("font-size: 18px; color: #03f5ff;") row2_layout.addWidget(label2) self.fangliang_adjuster = ValueAdjuster() # 导入的方量调整控件 self.fangliang_adjuster.minus_btn.clicked.connect(self._reset_volume_timer) self.fangliang_adjuster.plus_btn.clicked.connect(self._reset_volume_timer) row2_layout.addWidget(self.fangliang_adjuster) main_layout.addLayout(row2_layout) self.controls["volume"] = self.fangliang_adjuster # 第三行:计划配比 row3_layout = QHBoxLayout() status_icon3 = QLabel() status_icon3.setFixedSize(6, 6) status_icon3.setPixmap(QPixmap(ImagePaths.TASK_RECT2)) row3_layout.addWidget(status_icon3) row3_layout.addSpacing(4) label3 = QLabel("配合比") label3.setStyleSheet("font-size: 18px; color: #03f5ff;") row3_layout.addWidget(label3) value3 = QLabel("C55P12") value3.setStyleSheet("font-size: 18px; color: white;") row3_layout.addWidget(value3) main_layout.addLayout(row3_layout) # 存入字典 self.controls["ratio"] = value3 # 第四行:下发状态 row4_layout = QHBoxLayout() status_icon4 = QLabel() status_icon4.setFixedSize(6, 6) status_icon4.setPixmap(QPixmap(ImagePaths.TASK_RECT3)) row4_layout.addWidget(status_icon4) row4_layout.addSpacing(4) label4 = QLabel("下发状态") label4.setStyleSheet("font-size: 18px; color: #03f5ff;") row4_layout.addWidget(label4) value4 = QLabel("计划中") value4.setStyleSheet("font-size: 18px; color: white;") row4_layout.addWidget(value4) main_layout.addLayout(row4_layout) self.controls["status"] = value4 # 第五行:自动派单 row5_layout = QHBoxLayout() status_icon5 = QLabel() status_icon5.setPixmap(QPixmap(ImagePaths.TASK_RECT5)) status_icon5.setFixedSize(6, 6) row5_layout.addWidget(status_icon5) row5_layout.addSpacing(4) label5 = QLabel("自动派单") label5.setStyleSheet("font-size: 18px; color: #03f5ff;") row5_layout.addWidget(label5) self.switch = SwitchButton() self.switch.switched.connect(self.auto_dispatch_signal) self.switch.setChecked(True) row5_layout.addWidget(self.switch, alignment=Qt.AlignLeft) main_layout.addLayout(row5_layout) self.controls["auto_dispatch"] = self.switch def _reset_volume_timer(self): """每次调整方量时,重置定时器(倒计时重新开始)""" if self.volume_timer.isActive(): # 如果定时器正在运行,先停止 self.volume_timer.stop() self.volume_timer.start() # 重启定时器 def _sync_final_volume(self): """同步 确认的最终的修改方量""" final_volume = self.fangliang_adjuster.get_value() # print("最终确认的方量为:", final_volume) self.final_modify_volume_signal.emit(self.plan_table_task_name, final_volume) def paintEvent(self, event): """绘制背景图片""" painter = QPainter(self) painter.drawPixmap(self.rect(), self.bg_pixmap) super().paintEvent(event) # ------------------- 对外修改接口 ------------------- def set_plan_no(self, new_no:str): """修改计划单号""" if self.controls["plan_no"]: self.controls["plan_no"].setText(new_no) def set_plan_volume(self, new_volume:float): """修改计划方量""" if self.controls["volume"]: self.controls["volume"].set_value(new_volume) def set_plan_ratio(self, new_ratio:str): """修改计划配比""" if self.controls["ratio"]: self.controls["ratio"].setText(new_ratio) def set_status(self, new_status:str): """修改下发状态""" if self.controls["status"]: self.controls["status"].setText(new_status) def set_auto_dispatch(self, is_checked:bool): """修改自动派单开关状态, true: 自动派单, False: 手动派单""" if self.controls["auto_dispatch"]: self.controls["auto_dispatch"].setChecked(is_checked) def set_modify_volume_status(self, status:bool): """设置 计划表单中修改方量控件的状态, 是否能够修改方量 status: True, 能够修改方量 False, 不能修改方量 """ self.fangliang_adjuster.setEnabled(status) def set_plan_table_task_name(self, task_name:str): """设置 计划表单对应的选中任务名""" self.plan_table_task_name = task_name if __name__ == "__main__": app = QApplication(sys.argv) widget = PlanWidget() widget.show() # 测试接口 widget.set_plan_no("PD0002") # 计划单号 widget.set_plan_ratio("C60P15") # 计划配比 widget.set_plan_volume(5) # 计划方量 widget.set_status("已下发") # 下发状态 widget.set_auto_dispatch(False) # 自动派单 sys.exit(app.exec())