EnergySpectrumAnalyer/src/MeasureAnalysisHistoryForm/MeasureAnalysisHistoryForm.cpp

164 lines
7.2 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 "MeasureAnalysisHistoryForm.h"
#include "MeasureAnalysisProjectModel.h"
#include "ui_MeasureAnalysisHistoryForm.h"
#include <QDir>
#include <QFileInfo>
#include <QMessageBox>
MeasureAnalysisHistoryForm::MeasureAnalysisHistoryForm(QWidget* parent)
: QWidget(parent)
, ui(new Ui::MeasureAnalysisHistoryForm)
{
ui->setupUi(this);
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);
connect(ui->btn_query, &QPushButton::clicked, this, &MeasureAnalysisHistoryForm::onQueryHistory);
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);
loadHistoryInfo();
}
MeasureAnalysisHistoryForm::~MeasureAnalysisHistoryForm()
{
delete ui;
}
void MeasureAnalysisHistoryForm::loadHistoryInfo()
{
auto row_count = ui->tablew_history->rowCount();
for (int row = row_count - 1; row >= 0; --row) {
ui->tablew_history->removeRow(row);
}
// 遍历可执行文件目录下Projects目录下的所有文件夹找出所有项目文件夹下的.msproject文件
QDir projects_dir(QDir(qApp->applicationDirPath()).filePath("Projects"));
if (!projects_dir.exists()) {
return;
}
QList<QString> project_dirs = projects_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);
for (const QString& project_dir : project_dirs) {
QDir project_dir_path(projects_dir.filePath(project_dir));
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);
int row = ui->tablew_history->rowCount();
ui->tablew_history->insertRow(row);
ui->tablew_history->setItem(row, 0, new QTableWidgetItem(project_model.GetProjectName()));
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));
ui->tablew_history->setItem(row, 7, new QTableWidgetItem(project_model.GetDescriptionInfo()));
}
}
}
void MeasureAnalysisHistoryForm::onQueryHistory()
{
const QString& query_text = ui->linedit_query->text();
}
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()
{
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()) {
const QString& project_filename = ui->tablew_history->item(row, 0)->data(Qt::UserRole).toString();
QFileInfo project_file_info(project_filename);
const QString& project_dir_path = project_file_info.absoluteDir().absolutePath();
QDir(project_dir_path).removeRecursively();
ui->tablew_history->removeRow(row);
}
}
}
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);
item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
} else {
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
}
}
emit openProject(project_filename);
}
}
}