#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); { 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"); 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); } { m_pClient_A = new webClient(); connect(m_pClient_A, SIGNAL(sgConnect(bool)), this, SLOT(onConnected_A(bool))); connect(m_pClient_A, SIGNAL(sgRecvStrData(QString)), this, SLOT(onRecvDataStr_A(QString))); //connect(m_pClient_A, SIGNAL(sgRecvBinData(QByteArray)), this, SLOT(onRecvDataBin(QByteArray))); m_pClient_A->moveToThread(&m_task_A); m_task_A.start(QThread::HighPriority); m_pClient_B = new webClient(); connect(m_pClient_B, SIGNAL(sgConnect(bool)), this, SLOT(onConnected_B(bool))); connect(m_pClient_B, SIGNAL(sgRecvStrData(QString)), this, SLOT(onRecvDataStr_B(QString))); //connect(m_pClient_B, SIGNAL(sgRecvBinData(QByteArray)), this, SLOT(onRecvDataBin(QByteArray))); m_pClient_B->moveToThread(&m_task_B); m_task_B.start(QThread::HighPriority); } 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(); m_pDebugUI = new lpDebugUI(); connect(m_pDebugUI, SIGNAL(sgButtonClicked(int)), this, SLOT(onDebugClicked(int))); m_SysTimerID = startTimer(1000); lpConfig::instance()->loadConfig(); onConnectA(); if (lpConfig::instance()->doubleStation == true) { onConnectB(); } m_timerStartID = startTimer(2000); } valueMainUI::~valueMainUI() { onStopProcess(); { m_task_A.quit(); m_task_A.wait(); if (m_pClient_A) { m_pClient_A->onClose(); delete m_pClient_A; m_pClient_A = nullptr; } m_task_B.quit(); m_task_B.wait(); if (m_pClient_B) { m_pClient_B->onClose(); delete m_pClient_B; m_pClient_B = nullptr; } } { if (m_ImgViewer_A) { delete m_ImgViewer_A; m_ImgViewer_A = nullptr; } if (m_ImgViewer_B) { delete m_ImgViewer_B; m_ImgViewer_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(QString str) { QJsonDocument doc = QJsonDocument::fromJson(str.toLatin1()); QJsonObject json = doc.object(); 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(); 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); } else if (strcmd == "heartbit") { m_CoutA = 0; } } } Q_SLOT void valueMainUI::onRecvDataStr_B(QString str) { QJsonDocument doc = QJsonDocument::fromJson(str.toLatin1()); QJsonObject json = doc.object(); 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(); 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); } else if (strcmd == "heartbit") { m_CoutB = 0; } } } void valueMainUI::onConnectA() { if (m_pClient_A) { m_pClient_A->onClose(); QString strUrl = lpConfig::instance()->strUrlA; m_pClient_A->openUrl(strUrl); } } void valueMainUI::onConnectB() { if (m_pClient_B) { m_pClient_B->onClose(); QString strUrl = lpConfig::instance()->strUrlB; m_pClient_B->openUrl(strUrl); } } void valueMainUI::sendData() { QJsonObject obj; obj.insert("StartSendImage", true); obj.insert("time", 200); if (m_pClient_A) { m_pClient_A->onSendJson(obj); } } Q_SLOT void valueMainUI::onButtonClicked() { QString strObj = sender()->objectName(); if (strObj == "actionsettingA") { if (m_pClient_A) { QJsonObject sObj; sObj.insert("cmd", "showWidget"); m_pClient_A->onSendJson(sObj); } } else if (strObj == "actionsettingB") { if (m_pClient_B) { QJsonObject sObj; sObj.insert("cmd", "showWidget"); m_pClient_B->onSendJson(sObj); } } else if (strObj == "actiondebug") { m_pDebugUI->setParent(this); m_pDebugUI->setWindowTitle(tr("调试页面")); m_pDebugUI->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint); m_pDebugUI->setWindowIcon(QIcon(":/image/leaper")); 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(":/image/leaper")); 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_timer == event->timerId()) { QJsonObject sObj; sObj.insert("cmd", "trigerCam"); m_pClient_A->onSendJson(sObj); m_pClient_B->onSendJson(sObj); } else if (m_SysTimerID == event->timerId()) { m_tickCount++; m_CoutB++; m_CoutA++; if (m_CoutA > 5) { onConnectA(); m_CoutA = 0; m_bServerA = false; ui.textEdit->append(tr("连接超时,尝试重新连接检测软件")); } if (m_CoutB > 5) { 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 (m_bServerB == true) { m_pLabelB->setText("B侧设备已连接"); m_pLabelB->setStyleSheet("background-color: rgb(63, 170, 24);"); } else { m_pLabelB->setText("B侧设备未连接"); m_pLabelB->setStyleSheet(""); } QString strTitle = tr("系统时间:"); 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%4").arg(strTitle).arg(m_currentTime).arg(strTimeTitle).arg(strlong); m_pLabelSystem->setText(strShow); } } else if (m_timerStartID == event->timerId()) { killTimer(m_timerStartID); m_timerStartID = 0; onStartProcess(); } } 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) { if (m_timer != 0) { killTimer(m_timer); m_timer = 0; } else { m_timer = 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); 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() || windowState() == Qt::WindowMinimized || (qApp->applicationState() == Qt::ApplicationInactive)); }); 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) { show(); qApp->processEvents(); qApp->setActiveWindow(this); qApp->processEvents(); 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); } }