EnergySpectrumAnalyer/src/MeasureAnalysisHistoryForm/MeasureAnalysisHistoryForm.cpp

245 lines
11 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>
#include <QFileSystemWatcher>
#include "GlobalDefine.h"
MeasureAnalysisHistoryForm::MeasureAnalysisHistoryForm(QWidget* parent)
: QWidget(parent)
, ui(new Ui::MeasureAnalysisHistoryForm)
{
ui->setupUi(this);
ui->linedit_query->setPlaceholderText(QStringLiteral(u"请输入查询测量分析项目名称的关键字"));
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->linedit_query, &QLineEdit::textChanged , 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);
connect(ProjectList::Instance(), &MeasureAnalysisProjectModelList::removedProjectModel, this, &MeasureAnalysisHistoryForm::onUpdateCloseHistoryProject);
loadHistoryInfo();
}
MeasureAnalysisHistoryForm::~MeasureAnalysisHistoryForm()
{
delete ui;
}
void MeasureAnalysisHistoryForm::loadHistoryInfo(const QString &history_name)
{
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);
const QString& project_name = project_model.GetProjectName();
int row = ui->tablew_history->rowCount();
ui->tablew_history->insertRow(row);
ui->tablew_history->setItem(row, 0, new QTableWidgetItem(project_name));
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, 4, new QTableWidgetItem());
ui->tablew_history->setItem(row, 5, new QTableWidgetItem());
ui->tablew_history->setItem(row, 6, new QTableWidgetItem());
ui->tablew_history->setItem(row, 7, new QTableWidgetItem(project_model.GetDescriptionInfo()));
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));
}
}
}
}
}
}
void MeasureAnalysisHistoryForm::onQueryHistory()
{
const QString& query_text = ui->linedit_query->text();
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);
}
}
}
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()
{
QMap<int, QPair<QString, QString> > selected_project_list;
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_name = ui->tablew_history->item(row, 0)->text();
const QString& project_filename = ui->tablew_history->item(row, 0)->data(Qt::UserRole).toString();
selected_project_list[row] = qMakePair(project_name, project_filename);
}
}
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()) {
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);
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);
LOG_INFO(QStringLiteral(u"测量分析项目\"%1\"已经删除.").arg(project_name));
}
}
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::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));
}
}
} else {
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);
}
}
}
}
emit openProject(project_filename);
}
}
}
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)
{
loadHistoryInfo(project_name);
}