EnergySpectrumAnalyer/src/MeasureAnalysisHistoryForm/MeasureAnalysisHistoryForm.cpp

245 lines
11 KiB
C++
Raw Normal View History

2026-03-02 11:07:51 +08:00
#include "MeasureAnalysisHistoryForm.h"
2026-03-26 20:07:26 +08:00
#include "MeasureAnalysisProjectModel.h"
2026-03-02 11:07:51 +08:00
#include "ui_MeasureAnalysisHistoryForm.h"
2026-03-26 20:07:26 +08:00
#include <QDir>
2026-03-26 21:27:38 +08:00
#include <QFileInfo>
#include <QMessageBox>
2026-03-27 12:01:18 +08:00
#include <QFileSystemWatcher>
#include "GlobalDefine.h"
2026-03-02 11:07:51 +08:00
2026-03-26 20:07:26 +08:00
MeasureAnalysisHistoryForm::MeasureAnalysisHistoryForm(QWidget* parent)
2026-03-02 11:07:51 +08:00
: QWidget(parent)
, ui(new Ui::MeasureAnalysisHistoryForm)
{
ui->setupUi(this);
2026-03-26 21:27:38 +08:00
2026-03-27 12:01:18 +08:00
ui->linedit_query->setPlaceholderText(QStringLiteral(u"请输入查询测量分析项目名称的关键字"));
2026-03-26 21:27:38 +08:00
ui->tablew_history->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->tablew_history->horizontalHeader()->setSectionResizeMode(
ui->tablew_history->columnCount() - 1, QHeaderView::Stretch);
ui->tablew_history->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
2026-03-27 12:01:18 +08:00
connect(ui->linedit_query, &QLineEdit::textChanged , this, &MeasureAnalysisHistoryForm::onQueryHistory);
2026-03-26 21:27:38 +08:00
connect(ui->btn_remove, &QPushButton::clicked, this, &MeasureAnalysisHistoryForm::onRemoveSelectedProject);
connect(ui->btn_select_all, &QPushButton::clicked, this, &MeasureAnalysisHistoryForm::onAllSelect);
connect(ui->btn_reverse, &QPushButton::clicked, this, &MeasureAnalysisHistoryForm::onReverseSelect);
connect(ui->tablew_history, &QTableWidget::cellDoubleClicked, this, &MeasureAnalysisHistoryForm::onTableCellDoubleClicked);
2026-03-27 12:01:18 +08:00
connect(ProjectList::Instance(), &MeasureAnalysisProjectModelList::removedProjectModel, this, &MeasureAnalysisHistoryForm::onUpdateCloseHistoryProject);
2026-03-26 21:27:38 +08:00
2026-03-26 20:07:26 +08:00
loadHistoryInfo();
2026-03-02 11:07:51 +08:00
}
MeasureAnalysisHistoryForm::~MeasureAnalysisHistoryForm()
{
delete ui;
}
2026-03-31 09:18:46 +08:00
void MeasureAnalysisHistoryForm::loadHistoryInfo(const QString &history_name)
{
2026-03-26 20:07:26 +08:00
auto row_count = ui->tablew_history->rowCount();
for (int row = row_count - 1; row >= 0; --row) {
2026-03-26 21:27:38 +08:00
ui->tablew_history->removeRow(row);
2026-03-26 20:07:26 +08:00
}
// 遍历可执行文件目录下Projects目录下的所有文件夹找出所有项目文件夹下的.msproject文件
QDir projects_dir(QDir(qApp->applicationDirPath()).filePath("Projects"));
if (!projects_dir.exists()) {
return;
}
2026-03-26 21:27:38 +08:00
QList<QString> project_dirs = projects_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);
2026-03-26 20:07:26 +08:00
for (const QString& project_dir : project_dirs) {
2026-03-26 21:27:38 +08:00
QDir project_dir_path(projects_dir.filePath(project_dir));
2026-03-26 20:07:26 +08:00
if (project_dir_path.exists()) {
QList<QString> msproject_files = project_dir_path.entryList(QStringList({ "*.msproject" }));
if (msproject_files.isEmpty()) {
continue;
}
const QString& project_filename = project_dir_path.filePath(msproject_files.first());
MeasureAnalysisProjectModel project_model;
project_model.LoadProjectModel(project_filename);
2026-03-31 09:18:46 +08:00
const QString& project_name = project_model.GetProjectName();
2026-03-26 21:27:38 +08:00
int row = ui->tablew_history->rowCount();
ui->tablew_history->insertRow(row);
2026-03-31 09:18:46 +08:00
ui->tablew_history->setItem(row, 0, new QTableWidgetItem(project_name));
2026-03-26 21:27:38 +08:00
ui->tablew_history->item(row, 0)->setCheckState(Qt::Unchecked);
ui->tablew_history->item(row, 0)->setData(Qt::UserRole, project_filename);
const QString& is_measure_complete = project_model.GetIsMeasureComplete() ? QStringLiteral(u"") : QStringLiteral(u"");
ui->tablew_history->setItem(row, 1, new QTableWidgetItem(is_measure_complete));
QStringList spectrum_type_names { QStringLiteral(u"未知"), QStringLiteral(u"样品谱"), QStringLiteral(u"本底谱") };
const QString& spectrum_type_name = spectrum_type_names.value(project_model.GetSpectrumType(), QStringLiteral(u"未知"));
ui->tablew_history->setItem(row, 2, new QTableWidgetItem(spectrum_type_name));
const QString& is_std_source = project_model.GetIsStdSource() ? QStringLiteral(u"") : QStringLiteral(u"");
ui->tablew_history->setItem(row, 3, new QTableWidgetItem(is_std_source));
2026-03-27 12:01:18 +08:00
ui->tablew_history->setItem(row, 4, new QTableWidgetItem());
ui->tablew_history->setItem(row, 5, new QTableWidgetItem());
ui->tablew_history->setItem(row, 6, new QTableWidgetItem());
2026-03-26 21:27:38 +08:00
ui->tablew_history->setItem(row, 7, new QTableWidgetItem(project_model.GetDescriptionInfo()));
2026-03-31 09:18:46 +08:00
if (history_name == project_name) {
for (int col = 0; col < ui->tablew_history->columnCount(); ++col) {
QTableWidgetItem* other_item = ui->tablew_history->item(row, col);
if (other_item) {
other_item->setFlags(other_item->flags() & ~(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable));
}
}
}
2026-03-26 20:07:26 +08:00
}
}
}
2026-03-26 21:27:38 +08:00
void MeasureAnalysisHistoryForm::onQueryHistory()
{
const QString& query_text = ui->linedit_query->text();
2026-03-27 12:01:18 +08:00
for (int row = 0; row < ui->tablew_history->rowCount(); ++row) {
QTableWidgetItem* item = ui->tablew_history->item(row, 0);
if (item) {
bool b_match = item->text().contains(query_text);
ui->tablew_history->setRowHidden(row, !b_match);
}
}
}
2026-03-26 21:27:38 +08:00
void MeasureAnalysisHistoryForm::onAllSelect()
{
ui->tablew_history->setCurrentItem(nullptr);
auto row_count = ui->tablew_history->rowCount();
for (int i = 0; i < row_count; ++i) {
if (ui->tablew_history->isRowHidden(i)) {
continue;
}
auto item = ui->tablew_history->item(i, 0);
if (item) {
if (item->flags() & Qt::ItemIsUserCheckable) {
item->setCheckState(Qt::Checked);
}
}
}
}
void MeasureAnalysisHistoryForm::onReverseSelect()
{
ui->tablew_history->setCurrentItem(nullptr);
auto row_count = ui->tablew_history->rowCount();
for (int i = 0; i < row_count - 1; i++) {
if (ui->tablew_history->isRowHidden(i)) {
continue;
}
auto item = ui->tablew_history->item(i, 0);
if (item) {
if (item->flags() & Qt::ItemIsUserCheckable) {
Qt::CheckState check_state = (item->checkState() == Qt::Checked) ? Qt::Unchecked : Qt::Checked;
item->setCheckState(check_state);
}
}
}
}
void MeasureAnalysisHistoryForm::onRemoveSelectedProject()
{
2026-03-31 09:18:46 +08:00
QMap<int, QPair<QString, QString> > selected_project_list;
2026-03-26 21:27:38 +08:00
auto row_count = ui->tablew_history->rowCount();
for (int row = row_count - 1; row >= 0; --row) {
if (Qt::Checked == ui->tablew_history->item(row, 0)->checkState()) {
2026-03-31 09:18:46 +08:00
const QString& project_name = ui->tablew_history->item(row, 0)->text();
2026-03-26 21:27:38 +08:00
const QString& project_filename = ui->tablew_history->item(row, 0)->data(Qt::UserRole).toString();
2026-03-31 09:18:46 +08:00
selected_project_list[row] = qMakePair(project_name, project_filename);
2026-03-26 21:27:38 +08:00
}
}
2026-03-27 12:01:18 +08:00
if (selected_project_list.isEmpty()) {
QMessageBox::information(this, QStringLiteral(u"确认"), QStringLiteral(u"请选择想要删除的测量分析项目!"));
return;
}
if (QMessageBox::No == QMessageBox::question(this, QStringLiteral(u"确认"), QStringLiteral(u"是否删除选择的测量分析项目?"))) {
return;
}
for (int row : selected_project_list.keys()) {
2026-03-31 09:18:46 +08:00
const QString& project_name = selected_project_list.value(row).first;
ProjectList::Instance()->RmProjectModel(project_name);
const QString& project_filename = selected_project_list.value(row).second;
QFileInfo project_file_info(project_filename);
2026-03-27 12:01:18 +08:00
const QString& project_dir_path = project_file_info.absoluteDir().absolutePath();
QDir(project_dir_path).removeRecursively();
QDir(project_dir_path).rmdir(project_dir_path);
ui->tablew_history->removeRow(row);
2026-03-31 09:18:46 +08:00
LOG_INFO(QStringLiteral(u"测量分析项目\"%1\"已经删除.").arg(project_name));
2026-03-27 12:01:18 +08:00
}
2026-03-26 21:27:38 +08:00
}
void MeasureAnalysisHistoryForm::onTableCellDoubleClicked(int row, int column)
{
Q_UNUSED(column);
QTableWidgetItem* item = ui->tablew_history->item(row, 0);
if (!item) {
return;
}
const QString& project_name = item->text();
MeasureAnalysisProjectModel* model = ProjectList::Instance()->GetProjectModel(project_name);
if (model) {
QMessageBox::information(this, QStringLiteral(u"提示"), QStringLiteral(u"测量分析项目\"%1\"已经打开.").arg(project_name));
return;
}
if (QMessageBox::No == QMessageBox::question(this, QStringLiteral(u"确认"), QStringLiteral(u"是否打开测量分析项目\"%1\"").arg(project_name))) {
return;
}
const QString& project_filename = item->data(Qt::UserRole).toString();
if (!project_filename.isEmpty()) {
QFileInfo project_file_info(project_filename);
if (project_file_info.exists()) {
if (project_file_info.size() == 0) {
QMessageBox::warning(this, QStringLiteral(u"警告"), QStringLiteral(u"测量分析项目文件异常,打开失败,此记录将被清除!"));
return;
}
MeasureAnalysisProjectModel* model = new MeasureAnalysisProjectModel;
if (model->LoadProjectModel(project_filename)) {
bool loaded = ProjectList::Instance()->AddProjectModel(model);
if (loaded) {
item->setCheckState(Qt::Unchecked);
2026-03-27 12:01:18 +08:00
item->setFlags(item->flags() & ~(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable));
int row = item->row();
for (int col = 1; col < ui->tablew_history->columnCount(); ++col) {
QTableWidgetItem* other_item = ui->tablew_history->item(row, col);
if (other_item) {
other_item->setFlags(other_item->flags() & ~(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable));
}
}
2026-03-26 21:27:38 +08:00
} else {
2026-03-27 12:01:18 +08:00
item->setFlags(item->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
int row = item->row();
for (int col = 1; col < ui->tablew_history->columnCount(); ++col) {
QTableWidgetItem* other_item = ui->tablew_history->item(row, col);
if (other_item) {
other_item->setFlags(other_item->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
}
}
2026-03-26 21:27:38 +08:00
}
}
emit openProject(project_filename);
}
}
}
2026-03-27 12:01:18 +08:00
void MeasureAnalysisHistoryForm::onUpdateCloseHistoryProject(const QString &project_name)
{
for (int row = 0; row < ui->tablew_history->rowCount(); ++row) {
QTableWidgetItem* item = ui->tablew_history->item(row, 0);
if (item) {
if (project_name == item->text()) {
item->setFlags(item->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
int row = item->row();
for (int col = 1; col < ui->tablew_history->columnCount(); ++col) {
QTableWidgetItem* other_item = ui->tablew_history->item(row, col);
if (other_item) {
other_item->setFlags(other_item->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
}
}
}
}
}
}
void MeasureAnalysisHistoryForm::onUpdateNewHistoryProject(const QString &project_name)
{
2026-03-31 09:18:46 +08:00
loadHistoryInfo(project_name);
2026-03-27 12:01:18 +08:00
}