#include "valueMainUI.h" #include #include #include "lpConfig.h" #pragma execution_character_set("utf-8") valueMainUI::valueMainUI(QWidget *parent) : QMainWindow(parent) { setWindowTitle(tr("识别定位一体检测系统")); ui.setupUi(this); 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"); 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); } { 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())); //connect(ui.actionsettingA, 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); onConnectA(); onConnectB(); } valueMainUI::~valueMainUI() { { 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_timer = startTimer(5000); 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(); } } 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); } 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); } } } 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("设备已连接"); } } }