You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1148 lines
30 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "valueMainUI.h"
#include <QJsonDocument>
#include <QJsonObject>
#include "lpConfig.h"
#include <QProcess>
#include "SystemTool.h"
#include <QFileInfo>
#include <QMessageBox>
#include <QSettings>
#include <QToolButton>
#include <QMenu>
#include <QDir>
#include <QFile>
#include "lpGlobalData.h"
#pragma execution_character_set("utf-8")
#define WINDOWICON ":/Resources/icon.png"
valueMainUI::valueMainUI(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
{//加载语言设置
QSettings languageSetting("hubdetect.ini", QSettings::IniFormat);
QString strLanguage = languageSetting.value("language", "Chinese").toString();
SetLanguage(strLanguage);
}
setupTrayIcon();//后台任务栏初始化
setWindowIcon(QIcon(WINDOWICON));
setWindowTitle(tr("识别定位一体检测系统(v1.0.3 21.11.09)"));
lpGlobalData::instance();
lpConfig::instance()->loadConfig();
//状态栏信息展示初始化 两个后台检测程序的通讯状态展示
m_pLabelA = new QLabel(this);
m_pLabelB = new QLabel(this);
m_pLabelPLC = new QLabel(this);
m_pLabelSystem = new QLabel(this);
QFont font;
font.setBold(true);
font.setPixelSize(16);
m_pLabelA->setFont(font);
m_pLabelB->setFont(font);
m_pLabelPLC->setFont(font);
m_pLabelSystem->setFont(font);
m_pLabelA->setText(tr("A侧连接状态"));
m_pLabelB->setText(tr("B侧连接状态"));
m_pLabelPLC->setText(tr("PLC连接状态"));
m_pLabelSystem->setText(tr("时间"));
m_pLabelA->setAlignment(Qt::AlignCenter);
m_pLabelB->setAlignment(Qt::AlignCenter);
m_pLabelPLC->setAlignment(Qt::AlignCenter);
m_pLabelSystem->setAlignment(Qt::AlignCenter);
ui.statusBar->addWidget(m_pLabelA, 50);
ui.statusBar->addWidget(m_pLabelB, 50);
ui.statusBar->addWidget(m_pLabelPLC, 50);
ui.statusBar->addPermanentWidget(m_pLabelSystem, 50);
m_pLabelA->setText("");
m_pLabelB->setText("");
//检测结果图展示 UI初始化
{
QGridLayout *pLayout_A = new QGridLayout(ui.widget_A);//A侧相机结果图展示UI
m_ImgViewer_A = new RoiImgViewer(ui.widget_A);
m_ImgViewer_A->setObjectName("m_ImgViewer_A");
connect(m_ImgViewer_A, SIGNAL(sgImageScale(qreal)), this, SLOT(onImageScale(qreal)));
pLayout_A->addWidget(m_ImgViewer_A);
ui.widget_A->setLayout(pLayout_A);
QGridLayout *pLayout_B = new QGridLayout(ui.widget_B);//B侧相机结果图展示UI
m_ImgViewer_B = new RoiImgViewer(ui.widget_A);
m_ImgViewer_B->setObjectName("m_ImgViewer_B");
connect(m_ImgViewer_B, SIGNAL(sgImageScale(qreal)), this, SLOT(onImageScale(qreal)));
pLayout_B->addWidget(m_ImgViewer_B);
ui.widget_B->setLayout(pLayout_B);
QString strPath = QApplication::applicationDirPath() + "/showImg.ini";//图像展示比例的参数
QSettings setting(strPath, QSettings::IniFormat);
double nScale_A = setting.value("ShowImg/ScaleA", 0.53).toDouble();
double nScale_B = setting.value("ShowImg/ScaleB", 0.53).toDouble();
m_ImgViewer_A->setInitScale(nScale_A);
m_ImgViewer_B->setInitScale(nScale_B);
}
//工具栏菜单
QMenu *pToolMenu = new QMenu(this);
pToolMenu->addAction(ui.actionConfig);//系统配置
pToolMenu->addAction(ui.actiondebug);//测试调试
pToolMenu->addAction(ui.action_communicate);//通讯测试页面
QToolButton* pbutton = new QToolButton(this);
pbutton->setMenu(pToolMenu);
pbutton->setIcon(QIcon(":/Resources/Setting.png"));
pbutton->setText(tr("工具"));
pbutton->setToolTip(tr("工具"));
pbutton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
pbutton->setPopupMode(QToolButton::InstantPopup);
font.setBold(false);
font.setPixelSize(18);
pbutton->setFont(font);
ui.toolBar->addWidget(pbutton);
connect(ui.actionsettingA, SIGNAL(triggered()), this, SLOT(onButtonClicked()));
connect(ui.actionsettingB, SIGNAL(triggered()), this, SLOT(onButtonClicked()));
connect(ui.actionConfig, SIGNAL(triggered()), this, SLOT(onButtonClicked()));
connect(ui.actiondebug, SIGNAL(triggered()), this, SLOT(onButtonClicked()));
connect(ui.action_communicate, SIGNAL(triggered()), this, SLOT(onButtonClicked()));
//子页面初始化
m_pConfigUI = new lpConfigUI();//系统配置
connect(m_pConfigUI, SIGNAL(sgUpdateConfig()), this, SLOT(onUpdateConfig()));
connect(m_pConfigUI, SIGNAL(sgLanguageChange(QString)), this, SLOT(onLanguageChange(QString)));
m_pDebugUI = new lpDebugUI();//调试测试
connect(m_pDebugUI, SIGNAL(sgButtonClicked(int)), this, SLOT(onDebugClicked(int)));
m_pTcpDebugUI = new QTCPDebugUI();//通讯测试页面
onShowUI(lpConfig::instance()->doubleStation);
{//PLC tcp 通讯 本检测系统作为服务端PLC作为客户端主动连接
m_pPlcDevice = new QPLCDevice();
m_pPlcDevice->onInitDevice();
m_pPlcDevice->onStartServer(lpConfig::instance()->plcServerPort);
connect(m_pPlcDevice, SIGNAL(sgTrigerCam(int)), this, SLOT(onPLCTrigerCam(int)));
connect(m_pPlcDevice, SIGNAL(sgSendAlgResult(int)), this, SLOT(onSendAlgResult(int)));
connect(m_pPlcDevice, SIGNAL(sgSendDataCallBack(QByteArray)), this, SLOT(onShowSendTcpData(QByteArray)));
connect(m_pPlcDevice, SIGNAL(sgRecvDataCallBack(QByteArray)), this, SLOT(onShowRecvTcpData(QByteArray)));
}
{
m_SysTimerID = startTimer(1000);//系统定时刷新定时器
m_timerStartID = startTimer(1000);//启动从机客户端
}
lpGlobalData::instance()->m_StationResult_1.stationID = 1;
lpGlobalData::instance()->m_StationResult_2.stationID = 2;
}
valueMainUI::~valueMainUI()
{
onStopProcess();//本系统退出时 杀死由本系统启动的A B检测程序
//内存释放
{
if (m_ImgViewer_A)
{
delete m_ImgViewer_A;
m_ImgViewer_A = nullptr;
}
if (m_ImgViewer_B)
{
delete m_ImgViewer_B;
m_ImgViewer_B = nullptr;
}
}
if (tcp_client_ptr_A)
{
delete tcp_client_ptr_A;
tcp_client_ptr_A = nullptr;
}
if (tcp_client_ptr_B)
{
delete tcp_client_ptr_B;
tcp_client_ptr_B = nullptr;
}
if (m_pPlcDevice)
{
delete m_pPlcDevice;
m_pPlcDevice = nullptr;
}
if (m_pDebugUI)
{
delete m_pDebugUI;
m_pDebugUI = nullptr;
}
if (m_pConfigUI)
{
delete m_pConfigUI;
m_pConfigUI = nullptr;
}
if (m_pTcpDebugUI)
{
delete m_pTcpDebugUI;
m_pTcpDebugUI = nullptr;
}
}
QString valueMainUI::SecondTimeString(quint64 value)
{
QString strTime;
int seconds = value % 60;
int minutes = value / 60;
QString strDay = tr("");
QString strHour = tr("");
QString strMinute = tr("");
QString strSecond = tr("");
strTime = QString("%1%2%3%4").arg(minutes).arg(strMinute).arg(seconds).arg(strSecond);
if (minutes >= 60) {
minutes = (value / 60) % 60;
int hours = (value / 60) / 60;
strTime = QString("%1%2%3%4%5%6").arg(hours).arg(strHour).arg(minutes).arg(strMinute).arg(seconds).arg(strSecond);
if (hours >= 24) {
hours = ((value / 60) / 60) % 24;
int day = ((value / 60) / 60) / 24;
strTime = QString("%1%2%3%4%5%6%7%8").arg(day).arg(strDay).arg(hours).arg(strHour).arg(minutes).arg(strMinute).arg(seconds).arg(strSecond);
}
}
return strTime;
}
Q_SLOT void valueMainUI::onRecvDataStr_A(const QJsonObject& json)
{
if (!json.empty()) {
QString strcmd = json.value("cmd").toString();
if (strcmd == "algRlt")
{
QString strName = json.value("modeName").toString();
double score = json.value("score").toDouble();
double angle = json.value("angle").toDouble();
double cnt_x = json.value("center_x").toDouble();
double cnt_y = json.value("center_y").toDouble();
int resultType = json.value("resultType").toInt(-1);
QString strPath = json.value("imagePath").toString();
QString strBase64 = json.value("imageData_base64").toString();
QByteArray imgbase64 = strBase64.toUtf8();
QImage newImg;
QByteArray imgArr = QByteArray::fromBase64(imgbase64);
newImg.loadFromData(imgArr);
m_ImgViewer_A->setImg(newImg);
QString sMsg = QString("%1 : %2->%3 %4 (%5,%6)")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(strName).arg(score).arg(angle).arg(cnt_x).arg(cnt_y);
if (ui.textEdit->toPlainText().size() > 10000)
ui.textEdit->clear();
ui.textEdit->append(sMsg);
switch (resultType)
{
case 0://识别OK定位OK
{
ui.label_A->setText(tr("型号 %1,定位角度 %2 °").arg(strName).arg(angle));
ui.label_A->setStyleSheet("font: bold 14px;background-color: rgb(0, 170, 0);");
}
break;
case 1://识别OK定位异常
{
ui.label_A->setText(tr("型号 %1,定位异常").arg(strName));
ui.label_A->setStyleSheet("font: bold 14px;background-color: rgb(255, 0, 0);");
}
break;
case 2://识别OK定位未标定
{
ui.label_A->setText(tr("型号 %1,定位模板未标定").arg(strName));
ui.label_A->setStyleSheet("font: bold 14px;background-color: rgb(255, 170, 0);");
}
break;
case 3://识别OK,但没有相关task
{
ui.label_A->setText(tr("型号 %1,没有相关定位模板").arg(strName));
ui.label_A->setStyleSheet("font: bold 14px;background-color: rgb(255, 0, 0);");
}
break;
case 4:
{
ui.label_A->setText(tr("型号识别失败"));
ui.label_A->setStyleSheet("font: bold 14px;background-color: rgb(255, 0, 0);");
}
break;
default:
break;
}
ValueResult rlt;
rlt.stationID = 1;
rlt.angle = angle;
rlt.center = QPointF(cnt_x, cnt_y);
rlt.strModel = strName;
lpGlobalData::instance()->m_StationResult_1 = rlt;
if (m_pPlcDevice)
{
m_pPlcDevice->onAckTrigerCam(1);
}
}
else if (strcmd == "heartbit")
{
m_CoutA = 0;
m_bServerA = true;
m_bRunStateA = json.value("runState").toBool(false);
}
else if (strcmd == "triggerAck")
{
}
}
}
Q_SLOT void valueMainUI::onRecvDataStr_B(const QJsonObject& json)
{
if (!json.empty()) {
QString strcmd = json.value("cmd").toString();
if (strcmd == "algRlt")
{
QString strName = json.value("modeName").toString();
double score = json.value("score").toDouble();
double angle = json.value("angle").toDouble();
double cnt_x = json.value("center_x").toDouble();
double cnt_y = json.value("center_y").toDouble();
int resultType = json.value("resultType").toInt(-1);
QString strPath = json.value("imagePath").toString();
QString strBase64 = json.value("imageData_base64").toString();
QByteArray imgbase64 = strBase64.toUtf8();
QImage newImg;
QByteArray imgArr = QByteArray::fromBase64(imgbase64);
newImg.loadFromData(imgArr);
m_ImgViewer_B->setImg(newImg);
QString sMsg = QString("%1 : %2->%3 %4 (%5,%6)")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(strName).arg(score).arg(angle).arg(cnt_x).arg(cnt_y);
if (ui.textEdit_2->toPlainText().size() > 10000)
ui.textEdit_2->clear();
ui.textEdit_2->append(sMsg);
switch (resultType)
{
case 0://识别OK定位OK
{
ui.label_B->setText(tr("型号 %1,定位角度 %2°").arg(strName).arg(angle));
ui.label_B->setStyleSheet("font: bold 14px;background-color: rgb(0, 170, 0);");
}
break;
case 1://识别OK定位异常
{
ui.label_B->setText(tr("型号 %1,定位异常").arg(strName));
ui.label_B->setStyleSheet("font: bold 14px;background-color: rgb(255, 0, 0);");
}
break;
case 2://识别OK定位未标定
{
ui.label_B->setText(tr("型号 %1,定位模板未标定").arg(strName));
ui.label_B->setStyleSheet("font: bold 14px;background-color: rgb(255, 170, 0);");
}
break;
case 3://识别OK,但没有相关task
{
ui.label_B->setText(tr("型号 %1,没有相关定位模板").arg(strName));
ui.label_B->setStyleSheet("font: bold 14px;background-color: rgb(255, 0, 0);");
}
break;
case 4:
{
ui.label_B->setText(tr("型号识别失败"));
ui.label_B->setStyleSheet("font: bold 14px;background-color: rgb(255, 0, 0);");
}
break;
default:
break;
}
ValueResult rlt;
rlt.stationID = 2;
rlt.angle = angle;
rlt.center = QPointF(cnt_x, cnt_y);
rlt.strModel = strName;
lpGlobalData::instance()->m_StationResult_2 = rlt;
if (m_pPlcDevice)
{
m_pPlcDevice->onAckTrigerCam(2);
}
}
else if (strcmd == "heartbit")
{
m_CoutB = 0;
m_bServerB = true;
m_bRunStateB = json.value("runState").toBool(false);
}
else if (strcmd == "triggerAck")
{
}
}
}
void valueMainUI::onConnectA()
{//tcpClient 连接
TP_TCP_CLIENT_PARAM init_param;
init_param.host_ip_ = "127.0.0.1";
init_param.port_ = lpConfig::instance()->portA;
init_param.user_ = "A";
init_param.enable_ssl_ = false;
init_param.auto_reconnect_ = true;
if (tcp_client_ptr_A) {
delete tcp_client_ptr_A;
tcp_client_ptr_A = nullptr;
}
if (!tcp_client_ptr_A) {
tcp_client_ptr_A = new lpTcpClient(init_param, this);
}
connect(tcp_client_ptr_A, SIGNAL(signal_connected()), this, SLOT(on_connected_A()));//连接状态
connect(tcp_client_ptr_A, SIGNAL(signal_disconnected()), this, SLOT(on_disconnected_A()));
connect(tcp_client_ptr_A, &lpTcpClient::signal_message_receive, this, &valueMainUI::on_message_receive_A);//数据接收
connect(tcp_client_ptr_A, &lpTcpClient::signal_data_send_completed, this, &valueMainUI::on_data_send_completed_A);
connect(tcp_client_ptr_A, &lpTcpClient::signal_data_recv_completed, this, &valueMainUI::on_data_recv_completed_A);
tcp_client_ptr_A->onStartDevice();
}
void valueMainUI::onConnectB()
{
TP_TCP_CLIENT_PARAM init_param;
init_param.host_ip_ = "127.0.0.1";
init_param.port_ = lpConfig::instance()->portB;
init_param.user_ = "B";
init_param.enable_ssl_ = false;
init_param.auto_reconnect_ = true;
if (tcp_client_ptr_B) {
delete tcp_client_ptr_B;
tcp_client_ptr_B = nullptr;
}
if (!tcp_client_ptr_B) {
tcp_client_ptr_B = new lpTcpClient(init_param, this);
}
connect(tcp_client_ptr_B, SIGNAL(signal_connected()), this, SLOT(on_connected_B()));
connect(tcp_client_ptr_B, SIGNAL(signal_disconnected()), this, SLOT(on_disconnected_B()));
connect(tcp_client_ptr_B, &lpTcpClient::signal_message_receive, this, &valueMainUI::on_message_receive_B);
connect(tcp_client_ptr_B, &lpTcpClient::signal_data_send_completed, this, &valueMainUI::on_data_send_completed_B);
connect(tcp_client_ptr_B, &lpTcpClient::signal_data_recv_completed, this, &valueMainUI::on_data_recv_completed_B);
tcp_client_ptr_B->onStartDevice();
}
Q_SLOT void valueMainUI::onButtonClicked()
{
QString strObj = sender()->objectName();
if (strObj == "actionsettingA")//A侧客户端展示
{
if (m_bServerA == false)
{
QMessageBox info(this);
info.setWindowIcon(QIcon(WINDOWICON));
info.setWindowTitle(QObject::tr("警告"));
info.setText(QObject::tr("A侧检测程序未连接,请检查配置或通讯"));
info.setStandardButtons(QMessageBox::Ok);
info.setButtonText(QMessageBox::Ok, QObject::tr("关闭"));
info.exec();
return;
}
if (tcp_client_ptr_A)
{
TP_PROTOCOL_MESSAGE msg;
QJsonObject sObj;
sObj.insert("cmd", "showWidget");
msg.body = sObj;//发送命令 A侧检测程序显示客户端
tcp_client_ptr_A->ISendMessage(msg);
}
}
else if (strObj == "actionsettingB")
{
if (m_bServerB == false)
{
QMessageBox info(this);
info.setWindowIcon(QIcon(WINDOWICON));
info.setWindowTitle(QObject::tr("警告"));
info.setText(QObject::tr("B侧检测程序未连接,请检查配置或通讯"));
info.setStandardButtons(QMessageBox::Ok);
info.setButtonText(QMessageBox::Ok, QObject::tr("关闭"));
info.exec();
return;
}
if (tcp_client_ptr_B)
{
TP_PROTOCOL_MESSAGE msg;
QJsonObject sObj;
sObj.insert("cmd", "showWidget");
msg.body = sObj;
tcp_client_ptr_B->ISendMessage(msg);
}
}
else if (strObj == "actiondebug")
{
m_pDebugUI->setParent(this);
m_pDebugUI->setWindowTitle(tr("调试页面"));
m_pDebugUI->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
m_pDebugUI->setWindowIcon(QIcon(WINDOWICON));
//屏蔽 窗口不阻塞
//m_pDebugUI->setWindowModality(Qt::ApplicationModal);
//m_pDebugUI->setAttribute(Qt::WA_ShowModal, true);
m_pDebugUI->show();
}
else if (strObj == "actionConfig")
{
m_pConfigUI->setParent(this);
m_pConfigUI->setWindowTitle(tr("系统参数配置页面"));
m_pConfigUI->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
m_pConfigUI->setWindowIcon(QIcon(WINDOWICON));
m_pConfigUI->setWindowModality(Qt::ApplicationModal);
m_pConfigUI->setAttribute(Qt::WA_ShowModal, true);
m_pConfigUI->show();
}
else if (strObj == "action_communicate")
{
m_pTcpDebugUI->setParent(this);
m_pTcpDebugUI->setWindowTitle(tr("通讯调试页面"));
m_pTcpDebugUI->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
m_pTcpDebugUI->setWindowIcon(QIcon(WINDOWICON));
m_pTcpDebugUI->show();
}
}
QJsonObject valueMainUI::byte2Json(QByteArray data)
{
QJsonDocument doc = QJsonDocument::fromJson(data);
return doc.object();
}
QByteArray valueMainUI::Json2byte(QJsonObject obj)
{
QJsonDocument doc = QJsonDocument(obj);
return doc.toJson();
}
void valueMainUI::timerEvent(QTimerEvent *event)
{
if (m_timerA == event->timerId())//调试 定时触发相机拍照 相机触发模式为软件触发
{
TP_PROTOCOL_MESSAGE msg;
QJsonObject sObj;
sObj.insert("cmd", "trigerCam");
msg.body = sObj;
if(tcp_client_ptr_A)
tcp_client_ptr_A->ISendMessage(msg);
}
else if (m_timerB == event->timerId())
{
TP_PROTOCOL_MESSAGE msg;
QJsonObject sObj;
sObj.insert("cmd", "trigerCam");
msg.body = sObj;
if (tcp_client_ptr_B)
tcp_client_ptr_B->ISendMessage(msg);
}
else if (m_SysTimerID == event->timerId())
{//系统状态信息定时刷新展示
m_tickCount++;
m_CoutB++;
m_CoutA++;
if (m_CoutA > 15)
{
onConnectA();
m_CoutA = 0;
m_bServerA = false;
if (ui.textEdit->toPlainText().size() > 10000)
ui.textEdit->clear();
ui.textEdit->append(tr("连接超时,尝试重新连接检测软件"));
}
if (m_CoutB > 15)
{
m_bServerB = false;
m_CoutB = 0;
onConnectB();
if (ui.textEdit_2->toPlainText().size() > 10000)
ui.textEdit_2->clear();
ui.textEdit_2->append(tr("连接超时,尝试重新连接检测软件"));
}
{
if (m_bServerA == true)
{
if (m_bRunStateA == true)
{
m_pLabelA->setText(tr("A侧设备已连接(检测中)"));
m_pLabelA->setStyleSheet("background-color: rgb(63, 170, 24);");
}
else {
m_pLabelA->setText(tr("A侧设备已连接(暂停中)"));
m_pLabelA->setStyleSheet("background-color: rgb(200, 200, 10);");
}
}
else {
m_pLabelA->setText(tr("A侧设备未连接"));
m_pLabelA->setStyleSheet("background-color: rgb(200, 10, 10);");
}
if (lpConfig::instance()->doubleStation == true)
{
if (m_bServerB == true)
{
if (m_bRunStateB == true)
{
m_pLabelB->setText(tr("B侧设备已连接(检测中)"));
m_pLabelB->setStyleSheet("background-color: rgb(63, 170, 24);");
}
else {
m_pLabelB->setText(tr("B侧设备已连接(暂停中)"));
m_pLabelB->setStyleSheet("background-color: rgb(200, 200, 10);");
}
}
else {
m_pLabelB->setText(tr("B侧设备未连接"));
m_pLabelB->setStyleSheet("background-color: rgb(200, 10, 10);");
}
}
else {//不启用双工位
m_pLabelB->setText("");
m_pLabelB->setStyleSheet("");
}
if (m_pPlcDevice)
{
QString clientInfo;
bool bConnect = m_pPlcDevice->getClinetInfo(clientInfo);
if (bConnect == true)
{
QString plc = QString(tr("PLC客户端 %1").arg(clientInfo));
m_pLabelPLC->setText(plc);
m_pLabelPLC->setStyleSheet("background-color: rgb(63, 170, 24);");
}
else {
m_pLabelPLC->setText(tr("PLC客户端未连接"));
m_pLabelPLC->setStyleSheet("background-color: rgb(200, 200, 10);");
}
}
QString m_currentTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
QString strlong = SecondTimeString(m_tickCount);
QString strTimeTitle = tr("运行时长:");
QString strShow = QString("%1 %2 %3").arg(m_currentTime).arg(strTimeTitle).arg(strlong);
m_pLabelSystem->setText(strShow);
}
}
else if (m_timerStartID == event->timerId())
{
killTimer(m_timerStartID);
m_timerStartID = 0;
onStartProcess();//启动应用
onConnectA();//建立tcp通讯连接
if (lpConfig::instance()->doubleStation == true)
{
onConnectB();
}
}
}
void valueMainUI::closeEvent(QCloseEvent *event)
{
QMessageBox info(this);
info.setWindowIcon(QIcon(WINDOWICON));
info.setWindowTitle(QObject::tr("警告"));
info.setText(QObject::tr("本检测系统正在运行,您真的要关闭?"));
info.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
info.setButtonText(QMessageBox::Ok, QObject::tr("关闭"));
info.setButtonText(QMessageBox::Cancel, QObject::tr("后台运行"));
if (info.exec() != QMessageBox::Ok)
{
m_bExit = false;
if (m_bExit == false)
{
setMainWindowVisibility(false);
return event->ignore();
}
}
return event->accept();
}
void valueMainUI::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange)
{
ui.retranslateUi(this);
}
}
Q_SLOT void valueMainUI::onDebugClicked(int nID)
{//调试测试页面按钮响应事件 自定义命令
if (nID == 1)//A侧相机单次触发
{
TP_PROTOCOL_MESSAGE msg;
QJsonObject sObj;
sObj.insert("cmd", "trigerCam");
msg.body = sObj;
if (tcp_client_ptr_A)
tcp_client_ptr_A->ISendMessage(msg);
}
else if (nID == 2)//B侧相机单次触发
{
TP_PROTOCOL_MESSAGE msg;
QJsonObject sObj;
sObj.insert("cmd", "trigerCam");
msg.body = sObj;
if (tcp_client_ptr_B)
tcp_client_ptr_B->ISendMessage(msg);
}
else if (nID == 3)//A侧相机定时触发
{
if (m_timerA != 0)
{
killTimer(m_timerA);
m_timerA = 0;
}
else {
m_timerA = startTimer(5000);//时间固定5s
}
}
else if (nID == 4)//B侧相机定时触发
{
if (m_timerB != 0)
{
killTimer(m_timerB);
m_timerB = 0;
}
else {
m_timerB = startTimer(5000);
}
}
}
Q_SLOT void valueMainUI::onConnected_A(bool bFalg)
{
m_bServerA = bFalg;
static bool bLastState = false;
if (bLastState != m_bServerA)
{
bLastState = m_bServerA;
if (bLastState == false)
{
ui.textEdit->append(tr("检测系统已离线!!!"));
}
else {
ui.textEdit->append(tr("检测系统已在线..."));
}
}
}
Q_SLOT void valueMainUI::onConnected_B(bool bFalg)
{
m_bServerB = bFalg;
static bool bLastState = false;
if (bLastState != m_bServerB)
{
bLastState = m_bServerB;
if (bLastState == false)
{
ui.textEdit_2->append(tr("检测系统已离线!!!"));
}
else {
ui.textEdit_2->append(tr("检测系统已在线..."));
}
}
}
//测试代码 备用 查找系统中相关task是否在运行
void valueMainUI::check()
{
QString strExeName = QString("%1.exe").arg("Enchanter");
QProcess process(this);
QString strCMD = QString("wmic process where caption=\"%1\" get caption,commandline /value").arg(strExeName);
process.start(strCMD);
process.waitForStarted();
process.waitForFinished();
QString strTmp = QString(process.readAllStandardOutput());
bool bRun = false;
if (strTmp.contains(strExeName))
bRun = true;
}
void valueMainUI::onRunApp(QString strAppPath)
{
QProcess *process = new QProcess(this);
QFileInfo fileInfo(strAppPath);
QString strPath = fileInfo.absolutePath();
QString strExeName = fileInfo.fileName();
m_appNames.append(strExeName);
//process->setWorkingDirectory(strPath);
process->startDetached(strAppPath, QStringList(), strPath);
bool ret = process->waitForStarted();
if (ret) {
qDebug() << "start ok";
}
else {
qDebug() << "start failed";
}
}
//启动检测程序 路径固定
Q_SLOT void valueMainUI::onStartProcess()
{
QString strAppPath = lpConfig::instance()->appPathA;
if (strAppPath.isEmpty())
return;
onRunApp(lpConfig::instance()->appPathA);
if (lpConfig::instance()->doubleStation == false)//双工位启动
return;
strAppPath = lpConfig::instance()->appPathB;
if (strAppPath.isEmpty())
return;
onRunApp(lpConfig::instance()->appPathB);
}
Q_SLOT void valueMainUI::onStopProcess()
{
foreach(QString var, m_appNames) {
QString strExeName = QString("%1").arg(var);
SystemTool::killZombieProcess(strExeName.toStdString().c_str());
}
}
void valueMainUI::setupTrayIcon()
{
m_trayIcon = new QSystemTrayIcon(this);
m_trayIconMenu = new QMenu(this);
m_restoreAction = new QAction(tr("后台运行"), this);
m_quitAction = new QAction(tr("退出"), this);
m_trayIconMenu->addAction(m_restoreAction);
m_trayIconMenu->addSeparator();
m_trayIconMenu->addAction(m_quitAction);
QIcon icon(":/Resources/icon.png");
m_trayIcon->setIcon(icon);
m_trayIcon->setContextMenu(m_trayIconMenu);
m_trayIcon->show();
connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onActivated(QSystemTrayIcon::ActivationReason)));
connect(m_restoreAction, &QAction::triggered, this, [this]() {
setMainWindowVisibility(isHidden());
});
connect(m_quitAction, &QAction::triggered, this, &valueMainUI::onQuitApplication);
}
Q_SLOT void valueMainUI::onActivated(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::DoubleClick)
{
setMainWindowVisibility(true);
}
}
void valueMainUI::setMainWindowVisibility(bool state)
{
if (state) {
onShowMainWindow();
m_restoreAction->setText(tr("后台运行"));
}
else {
m_restoreAction->setText(tr("显示界面"));
hide();
if (m_pTcpDebugUI)
{
if (!m_pTcpDebugUI->isHidden())
{
m_pTcpDebugUI->hide();
}
}
if (m_pConfigUI)
{
if (!m_pConfigUI->isHidden())
{
m_pConfigUI->hide();
}
}
if (m_pDebugUI)
{
if (!m_pDebugUI->isHidden())
{
m_pDebugUI->hide();
}
}
}
}
Q_SLOT void valueMainUI::onQuitApplication()
{
m_bExit = true;
valueMainUI::close();
}
Q_SLOT void valueMainUI::onImageScale(qreal value)
{
QString strPath = QApplication::applicationDirPath() + "/showImg.ini";
QSettings setting(strPath, QSettings::IniFormat);
QObject *obj = sender();
if (m_ImgViewer_A == obj)
{
setting.setValue("ShowImg/ScaleA", value);
}
else if (m_ImgViewer_B == obj)
{
setting.setValue("ShowImg/ScaleB", value);
}
}
Q_SLOT void valueMainUI::onShowUI(bool bDouble)
{
if (bDouble == true)
{
ui.groupBox_2->setVisible(true);
ui.actionsettingB->setVisible(true);
}
else {
ui.groupBox_2->setVisible(false);
ui.actionsettingB->setVisible(false);
}
}
Q_SLOT void valueMainUI::onUpdateConfig()
{
onShowUI(lpConfig::instance()->doubleStation);
}
Q_SLOT void valueMainUI::on_message_receive_A(QSharedPointer<TP_PROTOCOL_MESSAGE> msg_ptr)
{
onRecvDataStr_A(msg_ptr->body);
}
Q_SLOT void valueMainUI::on_data_send_completed_A(QSharedPointer<TP_PROTOCOL_MESSAGE>)
{
}
Q_SLOT void valueMainUI::on_data_recv_completed_A(QSharedPointer<TP_PROTOCOL_MESSAGE> msg_ptr, QSharedPointer<QByteArray> pData)
{
}
Q_SLOT void valueMainUI::on_connected_A()
{
onConnected_A(true);
}
Q_SLOT void valueMainUI::on_disconnected_A()
{
onConnected_A(false);
}
//B
Q_SLOT void valueMainUI::on_message_receive_B(QSharedPointer<TP_PROTOCOL_MESSAGE> msg_ptr)
{
onRecvDataStr_B(msg_ptr->body);
}
Q_SLOT void valueMainUI::on_data_send_completed_B(QSharedPointer<TP_PROTOCOL_MESSAGE>)
{
}
Q_SLOT void valueMainUI::on_data_recv_completed_B(QSharedPointer<TP_PROTOCOL_MESSAGE> msg_ptr, QSharedPointer<QByteArray> pData)
{
}
Q_SLOT void valueMainUI::on_connected_B()
{
onConnected_B(true);
}
Q_SLOT void valueMainUI::on_disconnected_B()
{
onConnected_B(false);
}
void valueMainUI::onShowMainWindow()
{
if (this->isMinimized() == true)
{
this->showNormal();
}
if (this->isHidden())
{
Qt::WindowStates winStatus = Qt::WindowNoState;
if (this->windowState() & Qt::WindowMaximized)
{
winStatus = Qt::WindowMaximized;
}
this->setWindowState(Qt::WindowMinimized);
this->setWindowState(Qt::WindowActive | winStatus);
this->activateWindow();
this->raise();
this->show();
}
else {
QRect curGemRect = this->geometry();
//激活mpWindow窗体为桌面的顶层窗体
Qt::WindowStates winStatus = Qt::WindowNoState;
if (this->windowState() & Qt::WindowMaximized)
{
winStatus = Qt::WindowMaximized;
}
this->setWindowState(Qt::WindowMinimized);
this->setWindowState(Qt::WindowActive | winStatus);
this->setGeometry(curGemRect);
this->activateWindow();
this->raise();
}
}
Q_SLOT void valueMainUI::onPLCTrigerCam(int camID)
{
//onTriggerCam();
if (camID == 1)//A侧相机触发拍照
{
TP_PROTOCOL_MESSAGE msg;
QJsonObject sObj;
sObj.insert("cmd", "trigerCam");
msg.body = sObj;
if (tcp_client_ptr_A)
tcp_client_ptr_A->ISendMessage(msg);
}
else if (camID == 2)//B侧相机拍照
{
TP_PROTOCOL_MESSAGE msg;
QJsonObject sObj;
sObj.insert("cmd", "trigerCam");
msg.body = sObj;
if (tcp_client_ptr_B)
tcp_client_ptr_B->ISendMessage(msg);
}
}
Q_SLOT void valueMainUI::onShowRecvTcpData(QByteArray data)
{
if (lpGlobalData::instance()->bShowRecv == true)
{
m_pTcpDebugUI->appendRecvData(data);
}
}
Q_SLOT void valueMainUI::onShowSendTcpData(QByteArray data)
{
if (lpGlobalData::instance()->bShowSend == true)
{
m_pTcpDebugUI->appendSendData(data);
}
}
void valueMainUI::SetLanguage(QString strLangage)
{
QString strDirPath = QString(QCoreApplication::applicationDirPath() + "/language/");
QString translatorFileName = strLangage;
if (!translatorFileName.isEmpty())
{
if (m_VecTranPtr.size() > 0)
{
while (m_VecTranPtr.size())
{
QTranslator *pVa = m_VecTranPtr.takeFirst();
qApp->removeTranslator(pVa);
delete pVa;
pVa = NULL;
}
}
QLocale::setDefault(QLocale(translatorFileName));
QString transDir = strDirPath + translatorFileName;
SearchQmFile(transDir);
}
}
void valueMainUI::SearchQmFile(const QString & strDir)
{
QDir dir(strDir);
if (!dir.exists())
{
return;
}
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
dir.setSorting(QDir::DirsFirst); // 文件夹优先
// 转换成一个List
QFileInfoList list = dir.entryInfoList();
if (list.size() < 1)
{
return;
}
int i = 0;
do
{
QFileInfo fileInfo = list.at(i);
QString tt = fileInfo.fileName();
// 如果是文件夹
bool bisDir = fileInfo.isDir();
if (bisDir)
{
SearchQmFile(fileInfo.filePath());
}
else
{
bool bQm = fileInfo.fileName().endsWith(".qm");
SetTranslator(fileInfo.filePath());
}
i++;
} while (i < list.size());
}
void valueMainUI::SetTranslator(const QString strPath)
{
if (strPath.isEmpty())
{
return;
}
QTranslator *pTrans = new QTranslator;
if (pTrans->load(strPath)) // 如果加载成功
{
qApp->installTranslator(pTrans);
m_VecTranPtr.append(pTrans);
}
else
{
delete pTrans;
pTrans = NULL;
}
}
Q_SLOT void valueMainUI::onLanguageChange(QString strLanguage)
{
QSettings languageSetting("hubdetect.ini", QSettings::IniFormat);
languageSetting.setValue("language", strLanguage);
SetLanguage(strLanguage);
}
Q_SLOT void valueMainUI::onSendAlgResult(int camID)
{
if (camID == 1)
{
if (m_pPlcDevice)
{
m_pPlcDevice->onSendValueRlt(lpGlobalData::instance()->m_StationResult_1);
}
}
else if (camID == 2)
{
if (m_pPlcDevice)
{
m_pPlcDevice->onSendValueRlt(lpGlobalData::instance()->m_StationResult_2);
}
}
}