167 lines
4.8 KiB
C++
167 lines
4.8 KiB
C++
|
|
#include "dialoghistoryresult.h"
|
||
|
|
#include "ui_dialoghistoryresult.h"
|
||
|
|
|
||
|
|
#include <QDateTime>
|
||
|
|
#include <QDir>
|
||
|
|
#include <QFile>
|
||
|
|
#include <QFileInfo>
|
||
|
|
#include <QItemSelectionModel>
|
||
|
|
#include <QModelIndex>
|
||
|
|
#include <QStringListModel>
|
||
|
|
#include <QTextStream>
|
||
|
|
#include <algorithm>
|
||
|
|
|
||
|
|
DialogHistoryResult::DialogHistoryResult(const QString& historyDir, QWidget* parent)
|
||
|
|
: QDialog(parent)
|
||
|
|
, ui(new Ui::DialogHistoryResult)
|
||
|
|
, m_historyDir(historyDir)
|
||
|
|
{
|
||
|
|
ui->setupUi(this);
|
||
|
|
setWindowTitle(QStringLiteral("历史汇总结果"));
|
||
|
|
m_listModel = new QStringListModel(this);
|
||
|
|
ui->list_history->setModel(m_listModel);
|
||
|
|
connect(ui->list_history->selectionModel(),
|
||
|
|
&QItemSelectionModel::currentChanged,
|
||
|
|
this,
|
||
|
|
&DialogHistoryResult::onHistoryCurrentChanged);
|
||
|
|
reloadHistory();
|
||
|
|
}
|
||
|
|
|
||
|
|
DialogHistoryResult::~DialogHistoryResult()
|
||
|
|
{
|
||
|
|
delete ui;
|
||
|
|
}
|
||
|
|
|
||
|
|
void DialogHistoryResult::SetHistoryDir(const QString& historyDir)
|
||
|
|
{
|
||
|
|
if (m_historyDir == historyDir)
|
||
|
|
return;
|
||
|
|
|
||
|
|
m_historyDir = historyDir;
|
||
|
|
reloadHistory();
|
||
|
|
}
|
||
|
|
|
||
|
|
void DialogHistoryResult::on_btn_refresh_clicked()
|
||
|
|
{
|
||
|
|
reloadHistory();
|
||
|
|
}
|
||
|
|
|
||
|
|
void DialogHistoryResult::on_btn_close_clicked()
|
||
|
|
{
|
||
|
|
close();
|
||
|
|
}
|
||
|
|
|
||
|
|
void DialogHistoryResult::reloadHistory()
|
||
|
|
{
|
||
|
|
if (!ui->list_history || !ui->edit_detail || !m_listModel)
|
||
|
|
return;
|
||
|
|
|
||
|
|
m_listModel->setStringList(QStringList());
|
||
|
|
ui->edit_detail->clear();
|
||
|
|
m_filePaths.clear();
|
||
|
|
|
||
|
|
QDir dir(m_historyDir);
|
||
|
|
if (!dir.exists())
|
||
|
|
{
|
||
|
|
ui->edit_detail->setPlainText(QStringLiteral("暂无历史数据"));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
QFileInfoList files = dir.entryInfoList(QStringList() << QStringLiteral("*.txt"),
|
||
|
|
QDir::Files | QDir::Readable,
|
||
|
|
QDir::NoSort);
|
||
|
|
std::sort(files.begin(), files.end(), [](const QFileInfo& lhs, const QFileInfo& rhs) {
|
||
|
|
return lhs.lastModified() > rhs.lastModified();
|
||
|
|
});
|
||
|
|
|
||
|
|
QStringList titles;
|
||
|
|
for (const QFileInfo& fileInfo : files)
|
||
|
|
{
|
||
|
|
const QString filePath = fileInfo.absoluteFilePath();
|
||
|
|
const QString content = readHistoryFile(filePath);
|
||
|
|
titles << makeItemTitle(fileInfo, content);
|
||
|
|
m_filePaths << filePath;
|
||
|
|
}
|
||
|
|
m_listModel->setStringList(titles);
|
||
|
|
|
||
|
|
if (m_filePaths.isEmpty())
|
||
|
|
{
|
||
|
|
ui->edit_detail->setPlainText(QStringLiteral("暂无历史数据"));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
ui->list_history->setCurrentIndex(m_listModel->index(0, 0));
|
||
|
|
}
|
||
|
|
|
||
|
|
void DialogHistoryResult::showRecord(int row)
|
||
|
|
{
|
||
|
|
if (!ui->edit_detail)
|
||
|
|
return;
|
||
|
|
|
||
|
|
if (row < 0 || row >= m_filePaths.size())
|
||
|
|
{
|
||
|
|
ui->edit_detail->clear();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
ui->edit_detail->setPlainText(readHistoryFile(m_filePaths.at(row)));
|
||
|
|
}
|
||
|
|
|
||
|
|
void DialogHistoryResult::onHistoryCurrentChanged(const QModelIndex& current,
|
||
|
|
const QModelIndex& previous)
|
||
|
|
{
|
||
|
|
Q_UNUSED(previous);
|
||
|
|
showRecord(current.row());
|
||
|
|
}
|
||
|
|
|
||
|
|
QString DialogHistoryResult::readHistoryFile(const QString& filePath) const
|
||
|
|
{
|
||
|
|
QFile file(filePath);
|
||
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
||
|
|
return QStringLiteral("读取历史数据失败:%1").arg(file.errorString());
|
||
|
|
|
||
|
|
QTextStream in(&file);
|
||
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||
|
|
in.setCodec("UTF-8");
|
||
|
|
#endif
|
||
|
|
return in.readAll();
|
||
|
|
}
|
||
|
|
|
||
|
|
QString DialogHistoryResult::makeItemTitle(const QFileInfo& fileInfo, const QString& content) const
|
||
|
|
{
|
||
|
|
QString timeText = fieldValue(content, QStringLiteral("保存时间"));
|
||
|
|
if (timeText.isEmpty())
|
||
|
|
timeText = fileInfo.lastModified().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss"));
|
||
|
|
|
||
|
|
const QString frameId = fieldValue(content, QStringLiteral("frameId"));
|
||
|
|
const QString saveIndex = fieldValue(content, QStringLiteral("saveIndex"));
|
||
|
|
const QString heightCount = fieldValue(content, QStringLiteral("height_mm_count"));
|
||
|
|
const QString spacingCount = fieldValue(content, QStringLiteral("rod_spacing_mm_count"));
|
||
|
|
|
||
|
|
QStringList parts;
|
||
|
|
parts << timeText;
|
||
|
|
if (!saveIndex.isEmpty())
|
||
|
|
parts << QStringLiteral("#%1").arg(saveIndex);
|
||
|
|
if (!frameId.isEmpty())
|
||
|
|
parts << QStringLiteral("帧%1").arg(frameId);
|
||
|
|
if (!heightCount.isEmpty())
|
||
|
|
parts << QStringLiteral("高度%1").arg(heightCount);
|
||
|
|
if (!spacingCount.isEmpty())
|
||
|
|
parts << QStringLiteral("杆距%1").arg(spacingCount);
|
||
|
|
|
||
|
|
return parts.join(QStringLiteral(" "));
|
||
|
|
}
|
||
|
|
|
||
|
|
QString DialogHistoryResult::fieldValue(const QString& content, const QString& fieldName) const
|
||
|
|
{
|
||
|
|
const QString prefix = fieldName + QStringLiteral(":");
|
||
|
|
const QStringList lines = content.split(QLatin1Char('\n'));
|
||
|
|
for (const QString& line : lines)
|
||
|
|
{
|
||
|
|
const QString trimmed = line.trimmed();
|
||
|
|
if (trimmed.startsWith(prefix))
|
||
|
|
return trimmed.mid(prefix.size()).trimmed();
|
||
|
|
}
|
||
|
|
return QString();
|
||
|
|
}
|