jiateng_ws/widgets/loading_dialog_widget.py

211 lines
9.1 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.loading_dialog_ui import LoadingDialogUI
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
from apis.gc_api import GcApi
class LoadingDialog(LoadingDialogUI):
# 定义一个信号,用于向主窗口传递托盘号
tray_code_signal = Signal(str, str, str, str)
# 定义一个信号,用于向主窗口传递订单号
order_code_signal = Signal(str)
def __init__(self, parent=None,user_id=None,user_name=None,corp_id=None):
"""初始化加载对话框"""
super().__init__()
self.parent = parent
self.user_id = user_id
self.user_name = user_name
self.corp_id = corp_id
# 彻底禁用对话框的回车键关闭功能
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.order_input.returnPressed.connect(self.handle_order_return_pressed)
# 确认按钮点击事件
self.confirm_button.clicked.connect(self.accept)
# 取消按钮点击事件
self.cancel_button.clicked.connect(self.reject)
def handle_order_return_pressed(self):
"""处理订单输入框的回车事件"""
logging.info("订单输入框回车事件触发")
# 获取当前订单号值
order_code = self.order_input.text().strip()
if not order_code:
QMessageBox.warning(self, "提示", "请输入订单号")
return
# 发送订单号到主窗口
from widgets.main_window import MainWindow
main_window = self.parent
if main_window and isinstance(main_window, MainWindow):
logging.info(f"发送订单号到主窗口: {order_code}")
self.order_code_signal.emit(order_code)
#判断是否是接口,如果不是接口直接添加如果是则走接口
# 如果开启接口模式,则需要调用接口同步到业务库
order_info = None
self.on_order_query(order_code)
# 阻止事件继续传播
return True
def on_order_query(self,order_code):
"""查询订单信息,同时生成托盘号,并且回显到页面上"""
try:
if AppMode.is_api():
# 调用接口
gc_api = GcApi()
# 防止response为None导致异常
# 获取工程号信息,并且初始化数据
order_response = gc_api.get_order_info(order_code,self.corp_id)
# 生成托盘号
if(order_response.get("status",False)):
# 将接口数据保存到数据库,用于后续的关联使用
from dao.inspection_dao import InspectionDAO
inspection_dao = InspectionDAO()
order_info = order_response.get("data", {})[0]
# 设置轴数
order_info['user_id'] = self.user_id
order_info['user_name'] = self.user_name
order_info['data_corp'] = self.corp_id
inspection_dao.save_order_info(order_code,order_info)
self.axis_value.setText(order_info['zx_name'])
self.quantity_value.setText(str(order_info['sl']))
self.weight_value.setText(str(order_info['sl']))
xpack_response = gc_api.get_xpack(order_code,self.corp_id)
if(xpack_response.get("status",False)):
xpack = xpack_response['xpack']
self.tray_input.setText(xpack)
self.pallet_tier_value.setText('3')
# 发送托盘号到主窗
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) == xpack:
existed = True
break
# 如果不存在,则添加
if not existed:
logging.info(f"添加托盘号到主窗口: {xpack}")
main_window.tray_edit.addItem(xpack)
# 设置当前选中的托盘号
main_window.tray_edit.setCurrentText(xpack)
logging.info(f"设置主窗口当前托盘号: {xpack}")
return order_info
except Exception as e:
logging.error(f"查询订单信息异常: {str(e)}")
return None
def keyPressEvent(self, event):
"""重写键盘事件处理,防止回车关闭对话框"""
# 如果按下回车键
if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
logging.info(f"捕获到回车键事件,当前焦点部件: {self.focusWidget()}")
# 如果焦点在托盘输入框上,触发查询
# 注释掉此处的on_tray_query调用因为已经通过returnPressed信号处理了
if self.focusWidget() == self.tray_input:
# self.on_tray_query() - 通过returnPressed信号已经处理这里不需要再调用
event.accept() # 消费掉这个事件
return
# 如果焦点在确认按钮上,则允许默认行为(确认)
if self.focusWidget() == self.confirm_button:
return super().keyPressEvent(event)
# 其他情况下,阻止回车事件传播
event.accept() # 消费掉这个事件
return
# 其他键位事件交给父类处理
super().keyPressEvent(event)
def accept(self):
"""重写接受方法,确保在确认前所有数据都已处理"""
logging.info("确认按钮被点击或回车触发确认")
# 检查托盘层数是否有值
tier_value = self.pallet_tier_value.text().strip()
if not tier_value:
QMessageBox.warning(self, "提示", "请输入托盘层数")
return
# 获取包装号
pallet_code = self.tray_input.text().strip()
if not pallet_code:
QMessageBox.warning(self, "提示", "请输入托盘号")
return
try:
# 保存托盘档案信息
from utils.pallet_type_manager import PalletTypeManager
pallet_manager = PalletTypeManager.get_instance()
success = pallet_manager.save_pallet_archives(
pallet_code=pallet_code,
tier=int(tier_value),
user_id=self.user_id,
user_name=self.user_name
)
if not success:
QMessageBox.warning(self, "提示", "保存托盘档案失败")
return
logging.info(f"已保存托盘档案:托盘号={pallet_code}, 层数={tier_value}")
except Exception as e:
logging.error(f"保存托盘档案时发生错误: {str(e)}")
QMessageBox.warning(self, "错误", f"保存托盘档案失败: {str(e)}")
return
# 确保主窗口启动了监听
from widgets.main_window import MainWindow
main_window = self.parent
if main_window and isinstance(main_window, MainWindow):
# 确保主窗口已启动监听
try:
if not hasattr(main_window, 'modbus_monitor') or not main_window.modbus_monitor.is_running():
main_window.setup_modbus_monitor()
logging.info("已在LoadingDialog确认时启动Modbus监控")
# 启动串口监听
main_window.serial_manager.auto_open_configured_ports()
# 启动键盘监听器
main_window.serial_manager.start_keyboard_listener()
logging.info("已在LoadingDialog确认时启动键盘监听器")
except Exception as e:
logging.error(f"LoadingDialog确认时启动监听失败: {str(e)}")
# 调用父类的accept方法关闭对话框
super().accept()
def reject(self):
"""重写拒绝方法"""
logging.info("取消按钮被点击或ESC触发取消")
# 调用父类的reject方法关闭对话框
super().reject()