#include "mainwindow.h" #include "./ui_mainwindow.h" #include "titlebar.h" #include "mywidget.h" #include "cfgwindow.h" #include "processtask.h" #include "hpromptwidget.h" #include "HMessageBox.h" #include #include #include #include #include #include QMutex* r_Mutex = new QMutex(); MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setMouseTracking(true);//鼠标追踪 //定时器处理消息 m_timer = new QTimer(this); connect(m_timer, &QTimer::timeout, this, [this]() { r_Mutex->lock(); if (listmsg.size() > 0) { if (m_left && GetInitState()) { QString time = listmsg.firstKey(); QString msg = time + listmsg.value(time); listmsg.remove(time); if (msg.indexOf("TCP") != -1) { emit m_left->GetLogMsg(msg, 1); } else { emit m_left->GetLogMsg(msg); } } emit sig_finished(false); } else { emit sig_finished(false); } r_Mutex->unlock(); qApp->processEvents(); }); connect(this, &MainWindow::sig_finished, [this](bool isok) { if (isok) { m_timer->stop(); } else { m_timer->setInterval(10); m_timer->start(); } }); } MainWindow::~MainWindow() { emit sig_finished(true); m_timer->stop(); if (m_timer) { delete m_timer; m_timer = NULL; } if (m_task) { delete m_task; m_task = nullptr; } if (m_title) { delete m_title; m_title = nullptr; } if (m_area) { delete m_area; m_area = nullptr; } if (m_right) { delete m_right; m_right = nullptr; } if (m_left) { delete m_left; m_left = nullptr; } my_Para_Process.close(); delete ui; qDebug() << __FUNCTION__ << "line:" << __LINE__ << " ~MainWindow success!"; } void MainWindow::initWidget() { QDir dir = QApplication::applicationDirPath(); currPath = dir.path(); setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint); setStyleSheet("QWidget{background-color: rgb(85, 85, 85);}" "QToolTip{border-style: none;color:black; font-size:12px;}"); m_jsonObj = loadCameraCfg(); if (m_jsonObj.isEmpty()) { qCritical() << "[Error] " << " m_jsonObj is null"; //return; } int taskFlag; QHBoxLayout* h_layout = new QHBoxLayout; QVBoxLayout* m_vBox = new QVBoxLayout; QWidget* m_widget = new QWidget; m_task = new ProcessTask; #pragma omp parallel for for (int i = 0; i < 2; i++) { switch (i) { case 0: m_title = new TitleBar(this); m_title->setMouseTracking(true); m_title->setPath(currPath); m_title->setFixedHeight(38); m_title->setStyleSheet("Titl eBar{background-color: rgb(85, 85, 85);border:0px;}"); m_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); // 加载界面 m_area = new MyWidget(this); m_area->setParent(this); m_area->setMouseTracking(true); m_area->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_area->layoutWindow_setting(bridge.back.json.showWidgets.basicView); // 日志消息界面 m_left = new LeftWindow(this); // 配置参数界面 m_right = new RightWidget(this); m_right->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_right->hide(); h_layout->addWidget(m_left); h_layout->addWidget(m_area); h_layout->addWidget(m_right); h_layout->setStretchFactor(m_left, 1); h_layout->setStretchFactor(m_area, 5); h_layout->setStretchFactor(m_right, 1); h_layout->setContentsMargins(1, 1, 1, 1); vboxPtr.reset(m_vBox); m_vBox->setSpacing(0); m_vBox->addWidget(m_title); m_vBox->addLayout(h_layout); m_vBox->setContentsMargins(1, 1, 1, 1); m_widget->setMouseTracking(true); delete m_widget->layout(); m_widget->setLayout(m_vBox); this->setCentralWidget(m_widget); this->showMaximized(); break; case 1: m_task->setJsonObject(m_jsonObj); m_task->setJsonPath(currPath); connect(m_task, &ProcessTask::SendStationLabel, [this](int label) { if (label > 0) { emit m_left->GetShowGif(label - 1); } else { emit m_left->PausedGif(); } }); taskFlag = m_task->initTask(); break; // 加载模块 创建线程 } } switch (taskFlag) { case ProcessTask::OK: qDebug() << "[INFO] " << QString::fromUtf8("相机和检测模块加载成功!"); break; case ProcessTask::ERROR_initAllModule: { HPromptWidget* topwindow = new HPromptWidget(QString::fromUtf8("initAllModule失败!"), this); topwindow->show(); break; } case ProcessTask::ERROR_createModuleThread: { HPromptWidget* topwindow = new HPromptWidget(QString::fromUtf8("createModuleThread失败!"), this); topwindow->show(); break; } case ProcessTask::LOAD_FINS_FAILURE: { HPromptWidget* topwindow = new HPromptWidget(QString::fromUtf8("FINS模块加载失败!"), this); topwindow->show(); break; } case ProcessTask::LOAD_CAMERA_ALGO_FAILURE: { HPromptWidget* topwindow = new HPromptWidget(QString::fromUtf8("相机或算法模块加载失败!"), this); topwindow->show(); break; } case ProcessTask::INIT_FINS_FAILURE: { HPromptWidget* topwindow = new HPromptWidget(QString::fromUtf8("FINS初始化数据有误,或服务器连接失败!"), this); topwindow->show(); break; } case ProcessTask::INIT_CAMERA_ALGO_FAILURE: { HPromptWidget* topwindow = new HPromptWidget(QString::fromUtf8("相机模块初始化失败!"), this); topwindow->show(); break; } case ProcessTask::THREAD_CAMERA_FAILURE: { HPromptWidget* topwindow = new HPromptWidget(QString::fromUtf8("相机线程加载失败!"), this); topwindow->show(); break; } default: break; } m_task->startTask(); if (!Isinit) Isinit = true; signalAndSlot(); } void MainWindow::signalAndSlot() { //加载 外部exe connect(m_title, &TitleBar::mouseRecover, this, [=]() { setCursor(Qt::ArrowCursor); }); connect(m_title, &TitleBar::OpenProjectSignal, this, [=](QString path) { m_right->Load_Param_Config(path); setCursor(Qt::ArrowCursor); }); connect(m_title, &TitleBar::SaveProjectSignal, this, [=](QString path) { m_right->Save_Param_Config(path); setCursor(Qt::ArrowCursor); }); connect(m_title, &TitleBar::showStatisticalTableSignal, this, [=]() { if (my_Para_Process.state() == QProcess::ProcessState::NotRunning) { my_Para_Process.start("./parm.exe"); if (my_Para_Process.waitForStarted(15)) { qDebug() << "[INFO] " << tr("图像参数配置界面启动成功!"); //等待外部程序结束,如果在给定的时间内关闭外部程序,返回为真,超时返回false } else { qDebug() << "[Error] " << tr("图像参数配置界面启动失败!"); }; } setCursor(Qt::ArrowCursor); }); connect(m_area, &MyWidget::mouseRecover, this, [=]() { setCursor(Qt::ArrowCursor); }); connect(m_right, &RightWidget::sendBtnSignal, this, &MainWindow::On_sendCommond); connect(this, SIGNAL(sendImage(QString, QImage*, int)), m_area, SLOT(On_reciveImage(QString, QImage*, int))); // 传递图片 connect(&bridge.front, &frontEnd::showImgSingal, m_area, &MyWidget::On_reciveImage); // 传递图片 //connect(bridge, SIGNAL(showImgSingal(QString, QImage*, int)), m_area, SLOT(On_reciveImage(QString, QImage*, int))); connect(m_task, SIGNAL(sendImage(QString, QImage*, int)), m_area, SLOT(On_reciveImage(QString, QImage*, int))); // 传递图片 connect(m_task, SIGNAL(sendData(QString, QByteArray)), m_area, SLOT(On_reciveData(QString, QByteArray))); // 传递数据 } void MainWindow::SendImage(cv::Mat& mtx, QString outstr, QString outstrRes) { QStringList ap = outstr.split("_"); if (ap.size() < 3)return; QString name = "2D"; cv::Mat rgb; QImage* itemImage = nullptr; if (mtx.type() == CV_8UC1) { mtx.copyTo(rgb); itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_Grayscale8); QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_Grayscale8); *itemImage = qim.copy(); } else if (mtx.type() == CV_16U) // 双通道转单通道显示 { cv::normalize(mtx, mtx, 0, 255, cv::NORM_MINMAX); cv::convertScaleAbs(mtx, rgb); itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_Grayscale8); QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_Grayscale8); *itemImage = qim.copy(); } else if (mtx.type() == CV_8UC3) { cv::cvtColor(mtx, rgb, cv::COLOR_BGR2RGB); itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_RGB888); QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB888); *itemImage = qim.copy(); } else if (mtx.type() == CV_8UC4) { cv::cvtColor(mtx, rgb, cv::COLOR_BGRA2RGBA); itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_ARGB32); QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_ARGB32); *itemImage = qim.copy(); } if (m_area) emit sendImage(name, itemImage, 0); } void MainWindow::SendImage2(cv::Mat& mtx, QString outstr, QString outstrRes) { QStringList ap = outstr.split("_"); if (ap.size() < 3)return; QString name = "3D"; cv::Mat rgb; QImage* itemImage = nullptr; if (mtx.type() == CV_8UC1) { mtx.copyTo(rgb); itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_Grayscale8); QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_Grayscale8); *itemImage = qim.copy(); } else if (mtx.type() == CV_16U) // 双通道转单通道显示 { cv::normalize(mtx, mtx, 0, 255, cv::NORM_MINMAX); cv::convertScaleAbs(mtx, rgb); itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_Grayscale8); QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_Grayscale8); *itemImage = qim.copy(); } else if (mtx.type() == CV_8UC3) { cv::cvtColor(mtx, rgb, cv::COLOR_BGR2RGB); itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_RGB888); QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB888); *itemImage = qim.copy(); } else if (mtx.type() == CV_8UC4) { cv::cvtColor(mtx, rgb, cv::COLOR_BGRA2RGBA); itemImage = new QImage(QSize(rgb.cols, rgb.rows), QImage::Format_ARGB32); QImage qim((const unsigned char*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_ARGB32); *itemImage = qim.copy(); } if (m_area) emit sendImage(name, itemImage, 0); } void MainWindow::mousePressEvent(QMouseEvent* event) { if (Qt::LeftButton == event->button() && m_bCanDragResize) { m_start_drag_point = event->globalPos(); } return QWidget::mousePressEvent(event); } void MainWindow::mouseMoveEvent(QMouseEvent* event) { if (Qt::LeftButton & event->buttons()) { if (m_bCanDragResize) { if (m_emHorizontal == DRAG_LOCK_RIGHT && m_emVertical == DRAG_LOCK_TOP) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl, rb); if (rb.x() - gloPoint.x() <= minimumWidth()) rMove.setX(tl.x()); else rMove.setX(gloPoint.x()); if (rb.y() - gloPoint.y() <= minimumHeight()) { rMove.setY(tl.y()); rMove.setHeight(gloPoint.y() - tl.y()); } else rMove.setY(gloPoint.y()); setGeometry(rMove); } else if (m_emHorizontal == DRAG_LOCK_RIGHT && m_emVertical == DRAG_LOCK_BOTTOM) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl, rb); if (rb.x() - gloPoint.x() <= minimumWidth()) rMove.setX(tl.x()); else rMove.setX(gloPoint.x()); if (rb.y() - gloPoint.y() <= minimumHeight()) { rMove.setY(tl.y()); rMove.setHeight(gloPoint.y() - tl.y()); } else rMove.setY(gloPoint.y()); setGeometry(rMove); } else if (m_emHorizontal == DRAG_LOCK_LEFT && m_emVertical == DRAG_LOCK_BOTTOM) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl, rb); if (rb.x() - gloPoint.x() <= minimumWidth()) { rMove.setX(tl.x()); rMove.setWidth(gloPoint.x() - tl.x()); } else rMove.setX(gloPoint.x()); if (rb.y() - gloPoint.y() <= minimumHeight()) { rMove.setY(tl.y()); rMove.setHeight(gloPoint.y() - tl.y()); } else rMove.setY(gloPoint.y()); setGeometry(rMove); } else if (m_emHorizontal == DRAG_LOCK_RIGHT) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl, rb); if (rb.x() - gloPoint.x() <= minimumWidth()) rMove.setX(tl.x()); else rMove.setX(gloPoint.x()); this->setGeometry(rMove); } else if (m_emVertical == DRAG_LOCK_BOTTOM) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl, rb); if (rb.y() - gloPoint.y() <= minimumHeight()) rMove.setY(tl.y()); else rMove.setY(gloPoint.y()); this->setGeometry(rMove); } else if (m_emVertical == DRAG_LOCK_TOP && m_emHorizontal != DRAG_LOCK_LEFT) { QPoint gloPoint = event->globalPos(); QRect rect = this->rect(); QPoint tl = mapToGlobal(rect.topLeft()); QPoint rb = mapToGlobal(rect.bottomRight()); QRect rMove(tl, rb); qreal y_val = rb.y() - gloPoint.y(); if (y_val <= minimumHeight()) { rMove.setY(tl.y()); rMove.setHeight(gloPoint.y() - tl.y()); } else rMove.setY(gloPoint.y()); qreal screen_h = qApp->desktop()->screenGeometry(this).height(); if (rMove.height() + rMove.y() > screen_h - 60) { rMove.setHeight(rMove.height() - 30); } this->setGeometry(rMove); } else { const QPoint point_offset = event->globalPos() - m_start_drag_point; m_start_drag_point = event->globalPos(); int nOffsetX1 = DRAG_LOCK_RIGHT == m_emHorizontal ? point_offset.x() : 0; int nOffsetY1 = DRAG_LOCK_BOTTOM == m_emVertical ? point_offset.y() : 0; int nOffsetX2 = DRAG_LOCK_LEFT == m_emHorizontal ? point_offset.x() : 0; int nOffsetY2 = DRAG_LOCK_TOP == m_emVertical ? point_offset.y() : 0; setGeometry(geometry().adjusted(nOffsetX1, nOffsetY1, nOffsetX2, nOffsetY2)); } } } else if (Qt::NoButton == event->button()) { const QPoint& globalPos = event->globalPos(); const QPoint& pos = event->pos(); bool bHorLeft = qAbs(globalPos.x() - this->geometry().x()) < g_nBorder; bool bHorRight = (pos.x() > this->rect().right() - g_nBorder); bool bVertTop = pos.y() < g_nBorder - 2; bool bVertBottom = pos.y() > this->rect().bottom() - g_nBorder; if (bHorLeft || bHorRight || bVertTop || bVertBottom) { m_bCanDragResize = true; if (bHorLeft && bVertTop) //左上角拖拽 { setCursor(Qt::SizeFDiagCursor); m_emHorizontal = DRAG_LOCK_RIGHT; m_emVertical = DRAG_LOCK_BOTTOM; } else if (bHorLeft && bVertBottom) //左下角拖拽 { setCursor(Qt::SizeBDiagCursor); m_emHorizontal = DRAG_LOCK_RIGHT; m_emVertical = DRAG_LOCK_TOP; } else if (bHorRight && bVertTop) //右上角拖拽 { setCursor(Qt::SizeBDiagCursor); m_emHorizontal = DRAG_LOCK_LEFT; m_emVertical = DRAG_LOCK_BOTTOM; } else if (bHorRight && bVertBottom) //右下角拖拽 { setCursor(Qt::SizeFDiagCursor); m_emHorizontal = DRAG_LOCK_LEFT; m_emVertical = DRAG_LOCK_TOP; } else if (bHorLeft) //左边框拖拽 { setCursor(Qt::SizeHorCursor); m_emHorizontal = DRAG_LOCK_RIGHT; m_emVertical = DRAG_KEEP; } else if (bHorRight) //右边框拖拽 { setCursor(Qt::SizeHorCursor); m_emHorizontal = DRAG_LOCK_LEFT; m_emVertical = DRAG_KEEP; } else if (bVertTop) //上边框拖拽 { setCursor(Qt::SizeVerCursor); m_emHorizontal = DRAG_KEEP; m_emVertical = DRAG_LOCK_BOTTOM; } else if (bVertBottom) //下边框拖拽 { setCursor(Qt::SizeVerCursor); m_emHorizontal = DRAG_KEEP; m_emVertical = DRAG_LOCK_TOP; } } else if (m_bCanDragResize) { m_bCanDragResize = false; setCursor(Qt::ArrowCursor); } } } void MainWindow::mouseReleaseEvent(QMouseEvent* event) { return QWidget::mouseReleaseEvent(event); } void MainWindow::resizeEvent(QResizeEvent* event) { Q_UNUSED(event); // ui->View->fitInView(retPixmap,Qt::KeepAspectRatio); } void MainWindow::On_sendCommond(int type) { switch (type) { //case 1: //{ // //界面切换 // if (m_area->GetMyWidgetType() == MyWidgetType::RESULT) { // m_area->layoutWindow(m_jsonObj, MyWidgetType::CAMERA); // } // else // { // m_area->layoutWindow(m_jsonObj, MyWidgetType::RESULT); // } // // break; //} case 1: { //界面切换 if (m_area->GetMyWidgetType() == MyWidgetType::RESULT) { m_area->layoutWindow_setting("Camera"); } else { m_area->layoutWindow_setting("Algorithm"); } break; } case 2: { //m_task->startTask(); // ////m_left->GetShowGif(0); //if(!Isinit) // Isinit = true; ////test(); break; } case 3: { // m_left->GetShowGif(1); //m_left->PausedGif(); break; } case 4: { int result = HMessageBox::showMyMessageBox(NULL, QString::fromUtf8("请问是否清空数据?"), 1, true); if (result == 0) { m_task->clearData(); } break; } } } bool MainWindow::GetInitState() { return Isinit; } void MainWindow::getMsg(std::string time, std::string msg) { QString q_msg = QString::fromStdString(msg); QString q_time = QString::fromStdString(time); r_Mutex->lock(); listmsg.insert(q_time, q_msg); r_Mutex->unlock(); } void MainWindow::disconnectMV(QString msg) { HPromptWidget* topwindow = new HPromptWidget(QString::fromUtf8("相机断开连接!") + msg, this); topwindow->show(); } QJsonObject MainWindow::loadCameraCfg() { QJsonObject json_object; try { QJsonParseError jsonError; QString json_cfg = currPath + "/config/camera_cfg.json"; if (json_cfg.isEmpty()) { qCritical() << "[Error] " << " JsonPath is null!"; return json_object; } QFile JsonFile; JsonFile.setFileName(json_cfg); JsonFile.open(QIODevice::ReadOnly); QByteArray m_Byte = JsonFile.readAll(); if (m_Byte.isEmpty()) { qDebug() << "[Error] " << json_cfg + " Content is empty"; JsonFile.close(); return json_object; } QJsonDocument jsonDocument(QJsonDocument::fromJson(m_Byte, &jsonError)); if (!jsonDocument.isNull() && jsonError.error == QJsonParseError::NoError) { if (jsonDocument.isObject()) { json_object = jsonDocument.object(); return json_object; } } else { qCritical() << "[Error] " << json_cfg + " is error!"; } JsonFile.close(); } catch (QString ev) { qCritical() << "[Error] " << " ev:" << ev; } return json_object; }