204 lines
5.6 KiB
C++
204 lines
5.6 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
#include <QImage>
|
|
#include <QStringList>
|
|
#include <QDebug>
|
|
#include <QVBoxLayout>
|
|
#include <QMessageBox>
|
|
#include <QStatusBar>
|
|
#include "VrLog.h"
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::MainWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
// 设置窗口图标
|
|
this->setWindowIcon(QIcon(":/resource/logo.png"));
|
|
|
|
// 隐藏标题栏
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
|
|
|
// 启动后自动最大化显示
|
|
this->showMaximized();
|
|
|
|
// 初始化时隐藏label_work
|
|
ui->label_work->setVisible(false);
|
|
|
|
// 创建组合控件
|
|
// m_gridWithTable = new ImageGridWithTableWidget();
|
|
m_gridView = new ImageGridWidget();
|
|
m_deviceStatusWidget = new DeviceStatusWidget();
|
|
|
|
// 将设备状态widget添加到frame_dev中
|
|
QVBoxLayout* frameResultLayout = new QVBoxLayout(ui->detect_result);
|
|
frameResultLayout->setContentsMargins(0, 0, 0, 0);
|
|
frameResultLayout->addWidget(m_gridView);
|
|
|
|
QVBoxLayout* frameDevLayout = new QVBoxLayout(ui->device_status);
|
|
frameDevLayout->setContentsMargins(0, 0, 0, 0);
|
|
frameDevLayout->addWidget(m_deviceStatusWidget);
|
|
|
|
// 初始化Presenter
|
|
m_presenter = new BeltTearingPresenter();
|
|
|
|
// 将设备状态控件与Presenter关联
|
|
m_presenter->setDeviceStatusWidget(m_deviceStatusWidget);
|
|
m_presenter->setStatusUpdate(this);
|
|
|
|
m_presenter->Init();
|
|
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
if(m_gridWithTable){
|
|
delete m_gridWithTable;
|
|
m_gridWithTable = nullptr;
|
|
}
|
|
|
|
if (m_deviceStatusWidget) {
|
|
delete m_deviceStatusWidget;
|
|
m_deviceStatusWidget = nullptr;
|
|
}
|
|
|
|
if (m_presenter) {
|
|
delete m_presenter;
|
|
m_presenter = nullptr;
|
|
}
|
|
|
|
delete ui;
|
|
}
|
|
|
|
void MainWindow::resizeToFitContent() {
|
|
if (!m_gridWithTable || m_gridWithTable->columnCount() == 0) {
|
|
return;
|
|
}
|
|
|
|
// 计算网格总宽度(列数 * 格子宽度 + 边距)
|
|
int gridWidth = m_gridWithTable->columnCount() * m_gridWithTable->tileBaseSize().width() + 32;
|
|
|
|
// 加上表格宽度
|
|
int totalWidth = gridWidth + 260;
|
|
|
|
// 计算总高度(行数 * 格子高度 + 边距)
|
|
int totalHeight = m_gridWithTable->rowCount() * m_gridWithTable->tileBaseSize().height() + 32;
|
|
|
|
// 设置窗口大小
|
|
resize(totalWidth, totalHeight);
|
|
}
|
|
|
|
void MainWindow::on_btn_hide_clicked()
|
|
{
|
|
// 最小化窗口
|
|
this->showMinimized();
|
|
}
|
|
|
|
void MainWindow::on_btn_close_clicked()
|
|
{
|
|
// 关闭应用程序
|
|
this->close();
|
|
}
|
|
|
|
void MainWindow::resizeEvent(QResizeEvent* event)
|
|
{
|
|
// QMainWindow::resizeEvent(event);
|
|
}
|
|
|
|
void MainWindow::on_btn_test_clicked()
|
|
{
|
|
// 定义图片资源路径
|
|
QStringList imageResources = {
|
|
"C:/project/QT/GrabBag/GrabBagApp/resource/config_camera.png",
|
|
"C:/project/QT/GrabBag/GrabBagApp/resource/config_algo.png",
|
|
"C:/project/QT/GrabBag/GrabBagApp/resource/config_data_test.png",
|
|
"C:/project/QT/GrabBag/GrabBagApp/resource/result_icon.png"
|
|
};
|
|
|
|
// 加载并设置图片
|
|
for (int i = 0; i < imageResources.size(); ++i) {
|
|
QImage image(imageResources[i]);
|
|
if (!image.isNull()) {
|
|
m_gridView->setImages(i, image);
|
|
} else {
|
|
LOG_WARNING("Failed to load image: %s", imageResources[i].toStdString().c_str());
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// IStatusUpdate接口方法实现
|
|
void MainWindow::OnStatusUpdate(const QString &statusMessage)
|
|
{
|
|
statusBar()->showMessage(statusMessage);
|
|
LOG_DEBUG("Status update: %s", statusMessage.toStdString().c_str());
|
|
}
|
|
|
|
// 回调需要显示几个图像
|
|
void MainWindow::OnNeedShowImageCount(int count)
|
|
{
|
|
m_gridView->initImages(count);
|
|
}
|
|
|
|
void MainWindow::OnTearingResult(const BeltTearingResult &result)
|
|
{
|
|
// 如果图像有效,显示在网格控件中
|
|
if (result.bImageValid && !result.image.isNull()) {
|
|
m_gridView->setImages(0, result.image);
|
|
}
|
|
}
|
|
|
|
void MainWindow::OnServerConnected(const QString &serverName)
|
|
{
|
|
QString message = QString("已连接到服务器: %1").arg(serverName);
|
|
statusBar()->showMessage(message);
|
|
LOG_DEBUG("%s", message.toStdString().c_str());
|
|
}
|
|
|
|
void MainWindow::OnServerDisconnected(const QString &serverName)
|
|
{
|
|
QString message = QString("与服务器断开连接: %1").arg(serverName);
|
|
statusBar()->showMessage(message);
|
|
LOG_DEBUG("%s", message.toStdString().c_str());
|
|
}
|
|
|
|
void MainWindow::OnWorkStatusChanged(BeltTearingWorkStatus status)
|
|
{
|
|
QString statusStr;
|
|
switch (status) {
|
|
case BeltTearingWorkStatus::InitIng:
|
|
statusStr = "初始化中";
|
|
break;
|
|
case BeltTearingWorkStatus::Ready:
|
|
statusStr = "准备就绪";
|
|
break;
|
|
case BeltTearingWorkStatus::Working:
|
|
statusStr = "正在检测";
|
|
break;
|
|
case BeltTearingWorkStatus::Completed:
|
|
statusStr = "检测完成";
|
|
break;
|
|
case BeltTearingWorkStatus::Error:
|
|
statusStr = "设备异常";
|
|
break;
|
|
case BeltTearingWorkStatus::Disconnected:
|
|
statusStr = "连接断开";
|
|
break;
|
|
default:
|
|
statusStr = "未知状态";
|
|
break;
|
|
}
|
|
|
|
QString message = QString("工作状态: %1").arg(statusStr);
|
|
statusBar()->showMessage(message);
|
|
LOG_DEBUG("%s", message.toStdString().c_str());
|
|
}
|
|
|
|
void MainWindow::OnErrorOccurred(const QString &errorMessage)
|
|
{
|
|
statusBar()->showMessage("错误: " + errorMessage);
|
|
LOG_ERROR("Error occurred: %s", errorMessage.toStdString().c_str());
|
|
}
|
|
|