增加重量更新显示,hopper_controller 提供了料斗模块的数据接口

This commit is contained in:
2025-11-06 10:55:29 +08:00
parent 84382b0cc0
commit d2603cec4d
16 changed files with 278 additions and 94 deletions

View File

@ -74,12 +74,47 @@ class ConveyorSystemWidget(QWidget):
inner_img = ImagePaths.HOPPER2
inner_pixmap = QPixmap(inner_img)
if not inner_pixmap.isNull():
upper_inner_label = QLabel(upper_bg_widget)
upper_inner_label.setPixmap(inner_pixmap)
upper_inner_label.setFixedSize(inner_pixmap.width(), inner_pixmap.height())
upper_inner_label.move(14, 9) # 保持原位置
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:float):
# 1、更新传送带中的 上料斗内部进度显示
# 假设上料斗装满之后,总的重量为 5100kg
total_weight = 5100
self._update_upper_inner_height(total_weight, weight)
def create_conveyor(self):
"""创建传送带组件包含左右齿轮group容器背景为传送带图片"""

View File

@ -89,9 +89,9 @@ class HopperWidget(QWidget):
# 背景容器(上位)
self.upper_bg_widget = QWidget()
self.upper_bg_widget.setFixedSize(outer_width, outer_height)
self.upper_bg_widget.setStyleSheet(f"background-image: url({outer_img}); background-repeat: no-repeat; background-position: center;")
self.upper_bg_widget.setStyleSheet(f"background-image: url({outer_img});background-repeat: no-repeat; background-position: center;")
# self.upper_bg_widget.setStyleSheet(f"background-color:red; background-repeat: no-repeat; background-position: center;")
layout.addWidget(self.upper_bg_widget, alignment=Qt.AlignCenter)
# 内框图片(上位)
inner_img = ImagePaths.HOPPER2
@ -99,8 +99,11 @@ class HopperWidget(QWidget):
if not inner_pixmap.isNull():
self.upper_inner_label = QLabel(self.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.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)
# 状态图片(上位,绿色)
status_img = ImagePaths.HOPPER_STATUS_GREEN
@ -162,17 +165,6 @@ class HopperWidget(QWidget):
self.upper_arch_btn.clicked.connect(self.onUpperArchBreaking)
self.lower_arch_btn.clicked.connect(self.onLowerArchBreaking)
self.upper_open_btn.clicked.connect(self.onUpperClampOpen)
self.lower_open_btn.clicked.connect(self.onLowerClampOpen)
@Slot()
def onUpperClampOpen(self):
self.upper_clamp_widget.testAnimation(target_angle=60, duration=6) # 测试6秒打开60度
@Slot()
def onLowerClampOpen(self):
self.lower_clamp_widget.testAnimation(target_angle=25, duration=6) # 测试6秒打开30度
@Slot()
def onUpperArchBreaking(self):
if self.upper_arch_breaking_status == False: # 不破拱状态
@ -251,7 +243,10 @@ class HopperWidget(QWidget):
self.lower_inner_label = QLabel(self.lower_bg_widget)
self.lower_inner_label.setPixmap(inner_pixmap)
self.lower_inner_label.setFixedSize(inner_pixmap.width(), inner_pixmap.height())
self.lower_inner_label.setScaledContents(False) # 禁用图片缩放
self.lower_inner_label.setStyleSheet("background: none;")
self.lower_inner_label.move(14, 9)
self.lower_inner_label.setAlignment(Qt.AlignBottom)
# 状态图片(下位)
status_img = ImagePaths.HOPPER_STATUS_GREEN
@ -313,10 +308,42 @@ class HopperWidget(QWidget):
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 = self.upper_bg_widget.y() + self.upper_bg_widget.height()
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 setUpperHopperWeight(self, weight:float):
# 1、更新上料斗重量标签显示最新重量
self.upper_weight_label.setText(f"{weight}kg")
# 2、更新上料斗内部进度显示
# 假设上料斗装满之后,总的重量为 5100kg
total_weight = 5100
self._update_upper_inner_height(total_weight, weight)
# 上料斗方量设置
def setUpperHopperVolume(self, volume: float):
"""Args:
@ -334,8 +361,36 @@ class HopperWidget(QWidget):
# 下料斗重量设置
def setLowerHopperWeight(self, weight:float):
# 1、更新下料斗显示标签显示的重量
self.lower_weight_label.setText(f"{weight}kg")
# 2、更新下料斗的进度显示
# 假设下料斗装满之后 总重量为 5100kg
total_weight = 5100
self._update_lower_inner_height(total_weight, weight)
def _update_lower_inner_height(self, total_weight, current_weight: float):
# 1、处理边界值
clamped_weight = max(0.0, min(current_weight, total_weight))
# 2、计算占比
weight_ratio = clamped_weight / (total_weight * 1.0)
# 3、根据占比计算当前的实际高度
inner_img_height = 100 # 内部料斗阴影的高度为100px
target_height = int(weight_ratio * inner_img_height)
# 4、设置内部阴影标签的高度
self.lower_inner_label.setFixedHeight(target_height)
# 5、计算标签位置
container_bottom = self.lower_bg_widget.y() + self.lower_bg_widget.height()
label_y = container_bottom - target_height - 8
self.lower_inner_label.move(14, label_y)
# 6、强制刷新UI确保立即显示变化
self.lower_inner_label.update()
# 下料斗开合角度设置 (包括 夹爪和标签)
def setLowerHopperOpeningAngle(self, angle: float):
"""Args:
@ -347,7 +402,7 @@ class HopperWidget(QWidget):
# ------------------------------
# 设置上料斗状态0=绿1=黄2=红)
# ------------------------------
def setUpperArchStatus(self, status: int):
def setUpperHopperStatus(self, status: int):
"""
设置上料斗状态图片
Args:
@ -369,17 +424,12 @@ class HopperWidget(QWidget):
# 加载并缩放图片
status_pixmap = QPixmap(img_path)
if not status_pixmap.isNull():
status_pixmap = status_pixmap.scaled(
22, 22,
Qt.KeepAspectRatio,
Qt.SmoothTransformation
)
self.upper_status_label.setPixmap(status_pixmap)
# ------------------------------
# 设置下料斗状态0=绿1=黄2=红)
# ------------------------------
def setLowerArchStatus(self, status: int):
def setLowerHopperStatus(self, status: int):
"""
设置下料斗状态图片
Args:
@ -398,11 +448,6 @@ class HopperWidget(QWidget):
status_pixmap = QPixmap(img_path)
if not status_pixmap.isNull():
status_pixmap = status_pixmap.scaled(
22, 22,
Qt.KeepAspectRatio,
Qt.SmoothTransformation
)
self.lower_status_label.setPixmap(status_pixmap)
# 隐藏上料斗 (用于上料斗移动)
@ -428,7 +473,7 @@ if __name__ == "__main__":
window.setLowerHopperWeight(2000)
window.setUpperHopperVolume(3.0)
window.setLowerHopperOpeningAngle(45)
window.setUpperArchStatus(2)
window.setLowerArchStatus(1)
window.setUpperHopperStatus(2)
window.setLowerHopperStatus(1)
window.show()
sys.exit(app.exec())