jiateng_ws/widgets/unloading_dialog_widget.py

112 lines
4.7 KiB
Python
Raw Normal View History

from ui.unloading_dialog_ui import UnloadingDialogUI
from apis.tary_api import TaryApi
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QMessageBox, QDialog
import logging
class UnloadingDialog(UnloadingDialogUI):
# 定义一个信号,用于向主窗口传递托盘号
tray_code_signal = Signal(str, str, str, str)
def __init__(self, parent=None):
"""初始化下料对话框"""
super().__init__()
self.parent = parent
# 初始化API
self.tary_api = TaryApi()
# 彻底禁用对话框的回车键关闭功能
self.setModal(True)
# 禁用所有按钮的默认行为
self.confirm_button.setAutoDefault(False)
self.confirm_button.setDefault(False)
self.cancel_button.setAutoDefault(False)
self.cancel_button.setDefault(False)
# 设置对话框特性按下Escape键才能关闭
self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
# 绑定事件
self.setup_connections()
def setup_connections(self):
"""设置事件连接"""
# 托盘号输入框回车事件触发查询
self.tray_input.returnPressed.connect(self.handle_tray_return_pressed)
self.tray_input.editingFinished.connect(self.on_tray_query)
# 确认按钮点击事件
self.confirm_button.clicked.connect(self.accept)
# 取消按钮点击事件
self.cancel_button.clicked.connect(self.reject)
def handle_tray_return_pressed(self):
"""处理托盘输入框的回车事件"""
# 阻止事件传播
logging.info("托盘输入框回车事件触发")
self.on_tray_query()
# 阻止事件继续传播
return True
def on_tray_query(self):
"""查询托盘信息"""
try:
tray_code = self.tray_input.text().strip()
if not tray_code:
return
logging.info(f"查询托盘号: {tray_code}")
# 调用API获取托盘信息
response = self.tary_api.get_tary_info(tray_code)
logging.info(f"托盘信息响应: {response}")
logging.info(f"response.success={response.get('success')}, response.data存在={response.get('data') is not None}")
if response.get("success", False) and response.get("data"):
tray_data = response.get("data", {})
logging.info(f"托盘数据: {tray_data}")
# 显示托盘相关信息
axis_type = str(tray_data.get("axis_type", "--"))
material = str(tray_data.get("material", "--"))
weight = str(tray_data.get("weight", "--"))
logging.info(f"显示托盘信息: 轴型={axis_type}, 托盘料={material}, 重量={weight}")
# 设置显示值
self.axis_value.setText(axis_type)
self.pallet_material_value.setText(material)
self.quantity_value.setText("") # 数量为空
self.weight_value.setText(f"{weight} kg")
# 发送托盘号到主窗口
from widgets.main_window import MainWindow
main_window = self.parent
if main_window and isinstance(main_window, MainWindow):
# 检查托盘号是否已存在
existed = False
for i in range(main_window.tray_edit.count()):
if main_window.tray_edit.itemText(i) == tray_code:
existed = True
break
# 如果不存在,则添加
if not existed:
logging.info(f"添加托盘号到主窗口: {tray_code}")
main_window.tray_edit.addItem(tray_code)
# 设置当前选中的托盘号
main_window.tray_edit.setCurrentText(tray_code)
logging.info(f"设置主窗口当前托盘号: {tray_code}")
else:
# 获取托盘信息失败
error_msg = response.get("message", "获取托盘信息失败")
logging.warning(f"查询失败: {error_msg}")
QMessageBox.warning(self, "查询失败", error_msg)
except Exception as e:
logging.error(f"查询托盘信息异常: {str(e)}")
QMessageBox.critical(self, "查询异常", f"查询托盘信息时发生异常: {str(e)}")