jiateng_ws/widgets/unloading_dialog_widget.py

191 lines
8.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
from utils.app_mode import AppMode
from utils.pallet_type_manager import PalletTypeManager
class UnloadingDialog(UnloadingDialogUI):
# 定义一个信号,用于向主窗口传递托盘号
tray_code_signal = Signal(str, str, str, str)
def __init__(self, parent=None, user_id=None):
"""初始化下料对话框"""
super().__init__()
self.parent = parent
self.user_id = user_id
self.current_tier = None # 添加一个变量来保存当前层数
# 初始化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.init_pallet_types()
# 绑定事件
self.setup_connections()
def init_pallet_types(self):
"""初始化托盘类型下拉列表"""
# 从数据库获取托盘类型列表
pallet_type_manager = PalletTypeManager.get_instance()
pallet_types = pallet_type_manager.get_pallet_types()
# 清空现有选项
self.pallet_type_input.clear()
# 添加新选项
for pallet_id, pallet_name in pallet_types.items():
self.pallet_type_input.addItem(pallet_name, userData=pallet_id)
logging.info(f"从数据库加载{len(pallet_types)}个托盘类型")
def get_current_pallet_type_id(self):
"""获取当前选中的托盘类型ID"""
current_index = self.pallet_type_input.currentIndex()
if current_index >= 0:
pallet_id = self.pallet_type_input.itemData(current_index)
logging.info(f"获取当前选中的托盘类型ID: {pallet_id}")
return pallet_id
logging.warning("未获取到托盘类型ID")
return None
def get_current_tier(self):
"""获取当前托盘的层数"""
return self.current_tier if self.current_tier else None
def setup_connections(self):
"""设置事件连接"""
# 托盘号输入框回车事件触发查询
self.tray_input.returnPressed.connect(self.handle_tray_return_pressed)
# 移除editingFinished事件避免重复触发查询
# 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()
pallet_type = self.pallet_type_input.currentText().strip()
if not tray_code:
return
logging.info(f"查询托盘号: {tray_code}")
pallet_info = {}
if AppMode.is_api():
self.tary_api = TaryApi()
# 调用API获取托盘信息,并保存到本地
response = self.tary_api.get_tary_info(tray_code)
if response.get("success", False) and response.get("data"):
tray_data = response.get("data", {})
logging.info(f"托盘数据: {tray_data}")
pallet_type_manager = PalletTypeManager.get_instance()
pallet_type_manager.save_pallet_info(tray_code, tray_data,self.user_id)
else:
# 不是接口,就查询数据库,如果查到了托盘号就回显,查不到就新增
pallet_type_manager = PalletTypeManager.get_instance()
pallet_info = pallet_type_manager.get_pallet_info_by_pallet_id(tray_code)
if not pallet_info:
# 获取页面上的数据,保存到数据库中
pallet_info = {
"cc": pallet_type,
"cs": "",
}
pallet_type_manager.save_pallet_info(tray_code, pallet_info,self.user_id)
#回显数据到页面
pallet_info = pallet_type_manager.get_pallet_detail(tray_code)
if pallet_info:
logging.info(f"托盘数据: {pallet_info}")
# 显示托盘相关信息
axis_type = str(pallet_info.get("zx_name", "--"))
material = str(pallet_info.get("cz", "--"))
weight = str(pallet_info.get("weight", "--"))
amount = str(pallet_info.get("amount", "--"))
self.current_tier = pallet_info.get("tier") # 保存层数
logging.info(f"显示托盘信息: 轴型={axis_type}, 材质={material}, 重量={weight}, 层数={self.current_tier}")
# 设置显示值
self.axis_value.setText(axis_type)
self.material_tier_value.setPlaceholderText(material)
self.material_tier_value.setText("") # 清空文本,让用户输入
self.quantity_value.setText(amount)
self.weight_value.setText(f"{weight} kg")
self.pallet_type_input.setCurrentText(pallet_info.get("pallet_name", "--"))
# 发送托盘号到主窗口
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)}")
def keyPressEvent(self, event):
"""重写键盘事件处理,防止回车关闭对话框"""
# 如果按下回车键
if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
logging.info(f"捕获到回车键事件,当前焦点部件: {self.focusWidget()}")
# 如果焦点在托盘输入框上,触发查询
if self.focusWidget() == self.tray_input:
# 不需要重复调用on_tray_query因为returnPressed信号已经处理了
event.accept() # 消费掉这个事件
return
# 如果焦点在确认按钮上,则允许默认行为(确认)
if self.focusWidget() == self.confirm_button:
return super().keyPressEvent(event)
# 其他情况下,阻止回车事件传播
event.accept() # 消费掉这个事件
return
# 其他键位事件交给父类处理
super().keyPressEvent(event)