#include "valueMainUI.h" #include #include #include "lpConfig.h" #include #include "SystemTool.h" #include #include #include "QSettings" #pragma execution_character_set("utf-8") #define WINDOWICON ":/Resources/icon.png" valueMainUI::valueMainUI(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); setupTrayIcon(); setWindowIcon(QIcon(WINDOWICON)); setWindowTitle(tr("识别定位一体检测系统")); m_pLabelA = new QLabel(this); m_pLabelB = 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_pLabelSystem->setFont(font); m_pLabelA->setText("A侧连接状态"); m_pLabelB->setText("B侧连接状态"); m_pLabelSystem->setText("时间"); m_pLabelA->setAlignment(Qt::AlignCenter); m_pLabelB->setAlignment(Qt::AlignCenter); m_pLabelSystem->setAlignment(Qt::AlignCenter); ui.statusBar->addWidget(m_pLabelA, 50); ui.statusBar->addWidget(m_pLabelB, 50); ui.statusBar->addPermanentWidget(m_pLabelSystem, 50); m_pLabelA->setText(""); m_pLabelB->setText(""); { QGridLayout *pLayout_A = new QGridLayout(ui.widget_A); 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); 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); } 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())); m_pConfigUI = new lpConfigUI(); connect(m_pConfigUI, SIGNAL(sgUpdateConfig()), this, SLOT(onUpdateConfig())); m_pDebugUI = new lpDebugUI(); connect(m_pDebugUI, SIGNAL(sgButtonClicked(int)), this, SLOT(onDebugClicked(int))); m_SysTimerID = startTimer(1000); lpConfig::instance()->loadConfig(); m_timerStartID = startTimer(2000); onShowUI(lpConfig::instance()->doubleStation); } valueMainUI::~valueMainUI() { onStopProcess(); { 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; } } 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); 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; } } else if (strcmd == "heartbit") { m_CoutA = 0; m_bServerA = true; } } } 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); 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; } } else if (strcmd == "heartbit") { m_CoutB = 0; m_bServerB = true; } } } void valueMainUI::onConnectA() { 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") { 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; 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(); //check(); } } 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; ui.textEdit->append(tr("连接超时,尝试重新连接检测软件")); } if (m_CoutB > 15) { m_bServerB = false; m_CoutB = 0; onConnectB(); ui.textEdit_2->append(tr("连接超时,尝试重新连接检测软件")); } { if (m_bServerA == true) { m_pLabelA->setText("A侧设备已连接"); m_pLabelA->setStyleSheet("background-color: rgb(63, 170, 24);"); } else { m_pLabelA->setText("A侧设备未连接"); m_pLabelA->setStyleSheet(""); } if (lpConfig::instance()->doubleStation == true) { if (m_bServerB == true) { m_pLabelB->setText("B侧设备已连接"); m_pLabelB->setStyleSheet("background-color: rgb(63, 170, 24);"); } else { m_pLabelB->setText("B侧设备未连接"); m_pLabelB->setStyleSheet(""); } } else { m_pLabelB->setText(""); } 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(); 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(); } Q_SLOT void valueMainUI::onDebugClicked(int nID) { if (nID == 1) { 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) { 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) { if (m_timerA != 0) { killTimer(m_timerA); m_timerA = 0; } else { m_timerA = startTimer(5000); } } else if (nID == 4) { 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("设备断开连接"); } else { ui.textEdit->append("设备已连接"); } } } 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("设备断开连接"); } else { ui.textEdit_2->append("设备已连接"); } } } 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(); } } 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 msg_ptr) { onRecvDataStr_A(msg_ptr->body); } Q_SLOT void valueMainUI::on_data_send_completed_A(QSharedPointer) { } Q_SLOT void valueMainUI::on_data_recv_completed_A(QSharedPointer msg_ptr, QSharedPointer 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 msg_ptr) { onRecvDataStr_B(msg_ptr->body); } Q_SLOT void valueMainUI::on_data_send_completed_B(QSharedPointer) { } Q_SLOT void valueMainUI::on_data_recv_completed_B(QSharedPointer msg_ptr, QSharedPointer 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(); } }