GrabBag/Test/BeltTearingTcpProtocolTest/detectresultpage.cpp

149 lines
5.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "detectresultpage.h"
#include "ui_detectresultpage.h"
#include <QDateTime>
#include <QHeaderView>
#include <QJsonArray>
#include <QJsonValue>
#include <QPixmap>
#include <QResizeEvent>
#include <QStringList>
#include <QTableWidgetItem>
namespace
{
QString statusText(int status)
{
switch (status) {
case 1: return QStringLiteral("新发现");
case 2: return QStringLiteral("增长中");
case 3: return QStringLiteral("已结束");
case 4: return QStringLiteral("无效");
default: return QStringLiteral("未知");
}
}
QString numberText(const QJsonObject& object, const QString& key)
{
return QString::number(object.value(key).toInt());
}
}
DetectResultPage::DetectResultPage(QWidget* parent)
: QWidget(parent),
ui(new Ui::DetectResultPage)
{
ui->setupUi(this);
// 居中拉伸各列,Designer 不支持直接设置该模式
ui->tearTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// 与接收统计页的实时结果保持一致:明细 3 : 图片 5
ui->resultSplitter->setStretchFactor(0, 3);
ui->resultSplitter->setStretchFactor(1, 5);
}
DetectResultPage::~DetectResultPage()
{
delete ui;
}
void DetectResultPage::clearResults()
{
m_receivedCount = 0;
m_image = QImage();
ui->timeValueLabel->setText(QStringLiteral("-"));
ui->countValueLabel->setText(QStringLiteral("-"));
ui->alarmValueLabel->setText(QStringLiteral("-"));
ui->maxLengthValueLabel->setText(QStringLiteral("-"));
ui->maxWidthValueLabel->setText(QStringLiteral("-"));
ui->maxIdValueLabel->setText(QStringLiteral("-"));
ui->maxValueLabel->setText(QStringLiteral("-"));
ui->versionValueLabel->setText(QStringLiteral("-"));
ui->totalValueLabel->setText(QStringLiteral("-"));
ui->tearTable->setRowCount(0);
ui->imageLabel->setPixmap(QPixmap());
ui->imageLabel->setText(QStringLiteral("暂无图片"));
}
void DetectResultPage::handleDetectResult(const QJsonObject& result)
{
++m_receivedCount;
const QJsonArray tears = result.value(QStringLiteral("tears")).toArray();
const int count = result.value(QStringLiteral("count")).toInt(tears.size());
const qint64 timestamp = result.value(QStringLiteral("timestamp")).toVariant().toLongLong();
const QString timeText = timestamp > 0
? QDateTime::fromMSecsSinceEpoch(timestamp).toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz"))
: QStringLiteral("-");
ui->timeValueLabel->setText(timeText);
// count 应与 tears.length 一致,不一致时同时展示实际条数便于排查
ui->countValueLabel->setText(count == tears.size()
? QString::number(count)
: QStringLiteral("%1 (tears=%2)").arg(count).arg(tears.size()));
ui->alarmValueLabel->setText(result.value(QStringLiteral("alarm")).toBool()
? QStringLiteral("报警") : QStringLiteral("正常"));
ui->maxLengthValueLabel->setText(numberText(result, QStringLiteral("maxLength")));
ui->maxWidthValueLabel->setText(numberText(result, QStringLiteral("maxWidth")));
ui->maxIdValueLabel->setText(numberText(result, QStringLiteral("maxId")));
ui->maxValueLabel->setText(numberText(result, QStringLiteral("max")));
const QString version = result.value(QStringLiteral("protocolVersion")).toString();
ui->versionValueLabel->setText(version.isEmpty() ? QStringLiteral("-") : version);
ui->totalValueLabel->setText(QString::number(m_receivedCount));
ui->tearTable->setRowCount(tears.size());
for (int row = 0; row < tears.size(); ++row) {
const QJsonObject tear = tears.at(row).toObject();
const QStringList values = {
QString::number(tear.value(QStringLiteral("id")).toInt()),
statusText(tear.value(QStringLiteral("status")).toInt()),
QString::number(tear.value(QStringLiteral("length")).toDouble()),
QString::number(tear.value(QStringLiteral("width")).toDouble()),
QString::number(tear.value(QStringLiteral("depth")).toDouble())
};
for (int column = 0; column < values.size(); ++column) {
auto* item = new QTableWidgetItem(values.at(column));
item->setTextAlignment(Qt::AlignCenter);
ui->tearTable->setItem(row, column, item);
}
}
QByteArray encodedImage = result.value(QStringLiteral("visimg")).toString().toLatin1();
const int dataSeparator = encodedImage.indexOf(',');
if (encodedImage.startsWith("data:") && dataSeparator >= 0)
encodedImage.remove(0, dataSeparator + 1);
const QByteArray imageData = QByteArray::fromBase64(encodedImage);
QImage image;
if (!imageData.isEmpty() && image.loadFromData(imageData)) {
m_image = image;
updateImageDisplay();
} else if (encodedImage.isEmpty()) {
m_image = QImage();
ui->imageLabel->setPixmap(QPixmap());
ui->imageLabel->setText(QStringLiteral("暂无图片"));
} else {
emit parseError(QStringLiteral("DETECT_RESULT 结果图片解码失败"));
}
}
void DetectResultPage::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
updateImageDisplay();
}
void DetectResultPage::updateImageDisplay()
{
if (m_image.isNull())
return;
const QSize targetSize = ui->imageLabel->contentsRect().size();
if (targetSize.isEmpty())
return;
ui->imageLabel->setText(QString());
ui->imageLabel->setPixmap(QPixmap::fromImage(m_image).scaled(
targetSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}