import sys from PySide6.QtWidgets import ( QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, ) from PySide6.QtGui import QPixmap from PySide6.QtCore import Qt import resources.resources_rc from utils.image_paths import ImagePaths # 传送系统控件 (包括传送带 和 料斗) class ConveyorSystemWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("料斗与传送带界面") self._last_upper_hopper_weight = None # 上一次的上料斗重量(初始为None) self.setFixedSize(443, 190) self.init_ui() self._bind() def init_ui(self): # 主布局 main_layout = QVBoxLayout(self) main_layout.setSpacing(0) # 组件间间距 # main_layout.setContentsMargins(20, 20, 20, 20) main_layout.setContentsMargins(10, 120, 6, 3) # 添加料斗组件 self.hopper_widget = self.create_upper_hopper() # main_layout.addWidget(self.hopper_widget) self.hopper_widget.setParent(self) # 明确父对象为窗口,确保显示在窗口上 self.hopper_widget.move(190, 7) # 中间的过渡位置 self.hopper_widget.setHidden(True) # 添加传送带组件 self.conveyor_widget = self.create_conveyor() main_layout.addWidget(self.conveyor_widget, alignment=Qt.AlignLeft) # 添加传送带控制按钮 self.btn_layout = self.create_conveyor_buttons() main_layout.addLayout(self.btn_layout) def create_upper_hopper(self): """创建简化版上位料斗(移除了按钮、文字标签等元素)""" group = QWidget() layout = QVBoxLayout(group) layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) # 外框图片 outer_img = ImagePaths.HOPPER1 outer_pixmap = QPixmap(outer_img) if outer_pixmap.isNull(): print(f"警告:图片 {outer_img} 加载失败,请检查路径!") return group group.setFixedSize(outer_pixmap.size()) # 设置尺寸, 大小和外框一样 # 背景容器 upper_bg_widget = QWidget() upper_bg_widget.setFixedSize(outer_pixmap.width(), outer_pixmap.height()) upper_bg_widget.setStyleSheet( f"background-image: url({outer_img}); " "background-repeat: no-repeat; " "background-position: center;" ) layout.addWidget(upper_bg_widget, alignment=Qt.AlignCenter) # 内框图片 inner_img = ImagePaths.HOPPER2 inner_pixmap = QPixmap(inner_img) if not inner_pixmap.isNull(): self.upper_inner_label = QLabel(upper_bg_widget) self.upper_inner_label.setPixmap(inner_pixmap) self.upper_inner_label.setFixedSize(inner_pixmap.width(), inner_pixmap.height()) self.upper_inner_label.setScaledContents(False) self.upper_inner_label.setStyleSheet("background: none;") self.upper_inner_label.move(14, 9) self.upper_inner_label.setAlignment(Qt.AlignBottom) return group def _update_upper_inner_height(self, total_weight, current_weight: float): """根据当前重量占比, 更新upper_inner_label的高度, 实现动态进度的效果""" # 1、处理边界值(超过总重量按100%,低于0按0%) clamped_weight = max(0.0, min(current_weight, total_weight)) # 2、计算占比(0~1之间) weight_ratio = clamped_weight / (total_weight * 1.0) # 3、根据占比计算实际高度 inner_img_height = 100 # 内部的料斗阴影的高度为100px target_height = int(weight_ratio * inner_img_height) # print("target_height: ", target_height) # 4、设置标签高度(动态变化) self.upper_inner_label.setFixedHeight(target_height) # 5、计算标签位置(确保标签底部与父容器底部对齐) container_bottom = 117 # 容器的高固定为了 117px (背景图片"料斗1"的高) label_y = container_bottom - target_height - 8 # 标签顶部y坐标 (减去底部8px) self.upper_inner_label.move(14, label_y) # x固定,y动态计算 # print("label_y", label_y) # 6、强制刷新UI,确保立即显示变化 self.upper_inner_label.update() def setConveyorHopperWeight(self, weight:int): if weight != self._last_upper_hopper_weight: # 1、更新传送带中的 上料斗内部进度显示 # 假设上料斗装满之后,总的重量为 5100kg (褚工说设置为 6000kg 11/6) total_weight = 6000 self._update_upper_inner_height(total_weight, weight) # 2、将self._last_upper_hopper_weight设置为当前重量 self._last_upper_hopper_weight = weight def create_conveyor(self): """创建传送带组件(包含左右齿轮,group容器背景为传送带图片)""" group = QWidget() group.setObjectName("conveyorGroup") # 1. 加载传送带图片,设置group的尺寸和背景 conveyor_path = ImagePaths.CONVEYOR # 需要替换为实际的路径 conveyor_pix = QPixmap(conveyor_path) if conveyor_pix.isNull(): print("警告:传送带图片加载失败!请检查图片路径是否正确!") else: # group的尺寸 = 传送带图片尺寸(保证背景图完整显示) group.setFixedSize(conveyor_pix.size()) group.setStyleSheet(f""" #conveyorGroup {{ background-image: url({conveyor_path}); background-repeat: no-repeat; background-position: center; }} """) # 2. 给group设置布局(用于放置左右齿轮) layout = QHBoxLayout(group) layout.setSpacing(0) # 齿轮与容器边缘无间距 # 内边距(根据实际图片调整) layout.setContentsMargins(3, 3, 2, 4) # 3. 左侧齿轮(直接放在group的布局里,层级在背景之上) left_gear = QLabel(group) left_gear_pix = QPixmap(ImagePaths.CONVEYOR_GEAR) if left_gear_pix.isNull(): print("警告:左侧齿轮图片加载失败!") else: left_gear.setPixmap(left_gear_pix) left_gear.setFixedSize(left_gear_pix.size()) # 左对齐,让齿轮靠在传送带背景的左端 layout.addWidget(left_gear, alignment=Qt.AlignLeft | Qt.AlignVCenter) # 4. 右侧齿轮(通过addStretch推到最右边) right_gear = QLabel(group) right_gear_pix = QPixmap(ImagePaths.CONVEYOR_GEAR) if right_gear_pix.isNull(): print("警告:右侧齿轮图片加载失败!") else: right_gear.setPixmap(right_gear_pix) right_gear.setFixedSize(right_gear_pix.size()) # 右对齐,让齿轮靠在传送带背景的右端 layout.addWidget(right_gear, alignment=Qt.AlignRight | Qt.AlignVCenter) return group def create_conveyor_buttons(self): """创建传送带控制按钮(左右箭头按钮)""" layout = QHBoxLayout() layout.setSpacing(0) # 两个按钮之间的间距 # 左侧按钮 self.left_btn = QPushButton() self.left_btn.setFixedSize(25, 25) self.left_btn.setCursor(Qt.PointingHandCursor) self.left_btn.setStyleSheet( f""" QPushButton {{ background-image: url({ImagePaths.CONVEYOR_ARROW_LEFT1}); background-repeat: no-repeat; background-position: center; border: none; }} QPushButton:hover {{ background-image: url({ImagePaths.CONVEYOR_ARROW_LEFT2}); }} QPushButton:pressed {{ background-image: url({ImagePaths.CONVEYOR_ARROW_LEFT2}); }} """ ) # 右侧按钮 self.right_btn = QPushButton() self.right_btn.setFixedSize(25, 25) self.right_btn.setCursor(Qt.PointingHandCursor) self.right_btn.setStyleSheet( f""" QPushButton {{ background-image: url({ImagePaths.CONVEYOR_ARROW_RIGHT1}); background-repeat: no-repeat; background-position: center; border: none; }} QPushButton:hover {{ background-image: url({ImagePaths.CONVEYOR_ARROW_RIGHT2}); }} QPushButton:pressed {{ background-image: url({ImagePaths.CONVEYOR_ARROW_RIGHT2}); }} """ ) # addStretch调整按钮的位置 layout.addStretch(1) layout.addWidget(self.left_btn, alignment=Qt.AlignLeft) layout.addStretch(3) layout.addWidget(self.right_btn, alignment=Qt.AlignLeft) layout.addStretch(4) return layout def _bind(self): # self.left_btn.clicked.connect(self.moveHopperBelowMixer) # self.right_btn.clicked.connect(self.moveHopperToTransition) pass # 移动料斗到搅拌机下方 (传送带中间位置) def moveHopperBelowMixer(self): self.hopper_widget.move(34, 7) # 搅拌机下方坐标 # 移动料斗到中间过渡的位置 def moveHopperToTransition(self): """将料斗移动到中间过渡位置(用于位置切换过程)""" self.hopper_widget.move(190, 7) # 中间过渡坐标 # 隐藏料斗 (用于传送带中 料斗向右移动完成之后) def hideHopper(self): self.hopper_widget.setHidden(True) # 显示料斗 (用于传送带中 料斗开始向左移动时) def showHopper(self): self.hopper_widget.setHidden(False) if __name__ == "__main__": app = QApplication(sys.argv) window = ConveyorSystemWidget() window.show() sys.exit(app.exec())