2025-03-10 09:35:07 +00:00
|
|
|
#include <qdir.h>
|
|
|
|
|
#include <qdebug.h>
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
|
|
|
|
|
#include "leftwidget.h"
|
2025-03-19 07:19:45 +00:00
|
|
|
#include "filemanager.h"
|
|
|
|
|
#include "global.h"
|
2025-03-10 09:35:07 +00:00
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
LeftWidget::LeftWidget(QWidget *parent) : QDockWidget(parent) {
|
|
|
|
|
setWindowTitle("文件浏览");
|
2025-03-10 09:35:07 +00:00
|
|
|
|
|
|
|
|
_treeWidget = new QTreeWidget();
|
|
|
|
|
_treeWidget->setHeaderHidden(true);
|
|
|
|
|
|
|
|
|
|
_sampleDataItem = new QTreeWidgetItem(_treeWidget);
|
2025-10-24 09:06:37 +00:00
|
|
|
_sampleDataItem->setText(0, "样品数据");
|
2025-10-20 08:16:12 +00:00
|
|
|
#if 0
|
2025-04-22 08:59:26 +00:00
|
|
|
_baseLineItem = new QTreeWidgetItem(_treeWidget);
|
|
|
|
|
_baseLineItem->setText(0,"基线");
|
2025-10-20 08:16:12 +00:00
|
|
|
#endif
|
2025-04-22 08:59:26 +00:00
|
|
|
_analysisStateItem = new QTreeWidgetItem(_treeWidget);
|
2025-10-24 09:06:37 +00:00
|
|
|
_analysisStateItem->setText(0, "分析状态");
|
2025-03-10 09:35:07 +00:00
|
|
|
|
|
|
|
|
_treeWidget->setSortingEnabled(false);
|
|
|
|
|
|
2025-04-22 08:59:26 +00:00
|
|
|
_treeWidget->insertTopLevelItem(0, _sampleDataItem);
|
2025-10-24 09:06:37 +00:00
|
|
|
// _treeWidget->insertTopLevelItem(1, _baseLineItem);
|
2025-04-22 08:59:26 +00:00
|
|
|
_treeWidget->insertTopLevelItem(2, _analysisStateItem);
|
2025-03-10 09:35:07 +00:00
|
|
|
|
2025-03-19 07:19:45 +00:00
|
|
|
setWidget(_treeWidget);
|
2025-03-10 09:35:07 +00:00
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
// init file name.
|
|
|
|
|
initFileName(_sampleDataItem, Global::SampleDataFloder);
|
2025-10-20 08:16:12 +00:00
|
|
|
#if 0
|
2025-04-15 08:03:00 +00:00
|
|
|
initFileName(_baseLineItem,Global::BaseLineFolder);
|
2025-10-20 08:16:12 +00:00
|
|
|
#endif
|
2025-10-24 09:06:37 +00:00
|
|
|
initFileName(_analysisStateItem, Global::AnalysisStateFolder);
|
2025-03-10 09:35:07 +00:00
|
|
|
|
|
|
|
|
expandAll(_sampleDataItem);
|
2025-10-24 09:06:37 +00:00
|
|
|
// expandAll(_baseLineItem);
|
2025-03-10 09:35:07 +00:00
|
|
|
expandAll(_analysisStateItem);
|
|
|
|
|
|
2025-09-26 08:50:48 +00:00
|
|
|
Global::updateFileList();
|
|
|
|
|
|
2025-10-20 08:16:12 +00:00
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
// connections
|
|
|
|
|
connect(_treeWidget, &QTreeWidget::itemDoubleClicked,
|
|
|
|
|
this, &LeftWidget::slotTreeWidgetItemClicked);
|
|
|
|
|
|
|
|
|
|
// 右键菜单
|
|
|
|
|
_contextMenu = new QMenu(_treeWidget);
|
|
|
|
|
_deleteAction = new QAction("删除", this);
|
|
|
|
|
_contextMenu->addAction(_deleteAction);
|
|
|
|
|
|
|
|
|
|
_treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
connect(_treeWidget, &QTreeWidget::customContextMenuRequested,
|
|
|
|
|
this, &LeftWidget::slotShowContextMenu);
|
2025-03-10 09:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
void LeftWidget::reloadFileName() {
|
2025-04-22 08:59:26 +00:00
|
|
|
clearAllChildItems(_sampleDataItem);
|
2025-10-24 09:06:37 +00:00
|
|
|
// clearAllChildItems(_baseLineItem);
|
2025-04-22 08:59:26 +00:00
|
|
|
clearAllChildItems(_analysisStateItem);
|
|
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
initFileName(_sampleDataItem, Global::SampleDataFloder);
|
2025-10-20 08:16:12 +00:00
|
|
|
#if 0
|
2025-04-22 08:59:26 +00:00
|
|
|
initFileName(_baseLineItem,Global::BaseLineFolder);
|
2025-10-20 08:16:12 +00:00
|
|
|
#endif
|
2025-10-24 09:06:37 +00:00
|
|
|
initFileName(_analysisStateItem, Global::AnalysisStateFolder);
|
2025-09-26 08:50:48 +00:00
|
|
|
// 更新文件列表
|
|
|
|
|
Global::updateFileList();
|
2025-04-22 08:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
QString LeftWidget::filePathCheck(const QString fileName, const QString folderPath) {
|
2025-06-20 09:28:45 +00:00
|
|
|
QString resultFileName = fileName;
|
|
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
QDir dir(folderPath);
|
2025-06-20 09:28:45 +00:00
|
|
|
QStringList files = dir.entryList(QDir::Files);
|
2025-10-24 09:06:37 +00:00
|
|
|
for (const QString &existedFileName : files) {
|
2025-06-20 09:28:45 +00:00
|
|
|
QFileInfo fileInfo(existedFileName);
|
2025-10-24 09:06:37 +00:00
|
|
|
if (fileName == fileInfo.baseName()) {
|
2025-06-20 09:28:45 +00:00
|
|
|
QDateTime currentDateTime = QDateTime::currentDateTime();
|
2025-10-24 09:06:37 +00:00
|
|
|
QString formattedTime = currentDateTime.toString("yyyy_MM_dd_HH_mm_ss");
|
2025-06-20 09:28:45 +00:00
|
|
|
|
|
|
|
|
resultFileName = fileName + QString("_") + formattedTime;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultFileName;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
void LeftWidget::initData() {
|
2025-04-22 08:59:26 +00:00
|
|
|
// const QString folderPath = QDir::currentPath()+"/../experiment_data";
|
|
|
|
|
const QString folderPath = Global::SampleDataFloder;
|
2025-03-10 09:35:07 +00:00
|
|
|
#if 1
|
|
|
|
|
QDir dir(folderPath);
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
qWarning() << "文件夹不存在: " << folderPath;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 遍历文件
|
|
|
|
|
QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
|
2025-10-24 09:06:37 +00:00
|
|
|
for (const QFileInfo &fileInfo : fileList) {
|
2025-03-10 09:35:07 +00:00
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
|
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
|
QString content = file.readAll();
|
|
|
|
|
qDebug() << "读取文件 " << fileInfo.absoluteFilePath() << " 内容: " << content;
|
|
|
|
|
file.close();
|
|
|
|
|
// 写入文件操作示例:这里简单在文件末尾追加一行内容
|
|
|
|
|
if (file.open(QIODevice::Append | QIODevice::Text)) {
|
|
|
|
|
file.write("\n这是通过递归写入添加的内容");
|
|
|
|
|
qDebug() << "已向文件 " << fileInfo.absoluteFilePath() << " 写入内容";
|
|
|
|
|
file.close();
|
|
|
|
|
} else {
|
|
|
|
|
qWarning() << "无法打开文件进行写入: " << fileInfo.absoluteFilePath();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
qWarning() << "无法打开文件进行读取: " << fileInfo.absoluteFilePath();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2025-10-24 09:06:37 +00:00
|
|
|
void LeftWidget::slotTreeWidgetItemClicked(QTreeWidgetItem *item, int column) {
|
|
|
|
|
qDebug() << "item clicked:" << item->text(0) << column;
|
2025-03-10 09:35:07 +00:00
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
if (Global::Mode::Analysis != Global::_mode) {
|
2025-03-19 07:19:45 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString fileName;
|
|
|
|
|
// 获取父节点
|
|
|
|
|
QTreeWidgetItem *parentItem = item->parent();
|
|
|
|
|
if (parentItem) {
|
|
|
|
|
qDebug() << "parent item text:" << parentItem->text(0);
|
2025-10-24 09:06:37 +00:00
|
|
|
if (parentItem == _sampleDataItem) {
|
|
|
|
|
fileName = Global::SampleDataFloder + "/" + item->text(0);
|
2025-10-20 08:16:12 +00:00
|
|
|
#if 0
|
2025-03-19 07:19:45 +00:00
|
|
|
}else if(parentItem == _baseLineItem){
|
2025-04-15 08:03:00 +00:00
|
|
|
fileName =Global::BaseLineFolder + "/" +item->text(0);
|
2025-10-20 08:16:12 +00:00
|
|
|
#endif
|
2025-10-24 09:06:37 +00:00
|
|
|
} else if (parentItem == _analysisStateItem) {
|
|
|
|
|
fileName = Global::AnalysisStateFolder + "/" + item->text(0);
|
2025-03-19 07:19:45 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
qDebug() << "item has no parent (it is a top-level item)";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit sigSendAnalysisFileName(fileName);
|
|
|
|
|
}
|
2025-10-24 09:06:37 +00:00
|
|
|
void LeftWidget::initFileName(QTreeWidgetItem *parentItem, const QString &folderPath) {
|
|
|
|
|
QDir dir(folderPath);
|
2025-03-10 09:35:07 +00:00
|
|
|
QStringList files = dir.entryList(QDir::Files);
|
2025-10-24 09:06:37 +00:00
|
|
|
for (const QString &fileName : files) {
|
2025-03-10 09:35:07 +00:00
|
|
|
QTreeWidgetItem *subItem = new QTreeWidgetItem();
|
2025-10-24 09:06:37 +00:00
|
|
|
subItem->setText(0, fileName);
|
|
|
|
|
subItem->setData(0, Qt::UserRole, QVariant::fromValue(folderPath + "/" + fileName));
|
2025-03-10 09:35:07 +00:00
|
|
|
parentItem->addChild(subItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
void LeftWidget::expandAll(QTreeWidgetItem *item) {
|
2025-03-10 09:35:07 +00:00
|
|
|
item->setExpanded(true);
|
|
|
|
|
for (int i = 0; i < item->childCount(); ++i) {
|
|
|
|
|
expandAll(item->child(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
void LeftWidget::clearAllChildItems(QTreeWidgetItem *parentItem) {
|
2025-04-22 08:59:26 +00:00
|
|
|
int childCount = parentItem->childCount();
|
|
|
|
|
for (int i = 0; i < childCount; ++i) {
|
2025-10-24 09:06:37 +00:00
|
|
|
QTreeWidgetItem *childItem = parentItem->takeChild(0);
|
2025-04-22 08:59:26 +00:00
|
|
|
delete childItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-26 08:50:48 +00:00
|
|
|
|
2025-03-10 09:35:07 +00:00
|
|
|
#if 0
|
|
|
|
|
void LeftWidget::recursiveFolderOperation(const QString& folderPath) {
|
|
|
|
|
QDir dir(folderPath);
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
qWarning() << "文件夹不存在: " << folderPath;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 遍历文件
|
|
|
|
|
QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
|
for (const QFileInfo& fileInfo : fileList) {
|
|
|
|
|
QFile file(fileInfo.absoluteFilePath());
|
|
|
|
|
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
|
QString content = file.readAll();
|
|
|
|
|
qDebug() << "读取文件 " << fileInfo.absoluteFilePath() << " 内容: " << content;
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
|
|
// 写入文件操作示例:这里简单在文件末尾追加一行内容
|
|
|
|
|
if (file.open(QIODevice::Append | QIODevice::Text)) {
|
|
|
|
|
file.write("\n这是通过递归写入添加的内容");
|
|
|
|
|
qDebug() << "已向文件 " << fileInfo.absoluteFilePath() << " 写入内容";
|
|
|
|
|
file.close();
|
|
|
|
|
} else {
|
|
|
|
|
qWarning() << "无法打开文件进行写入: " << fileInfo.absoluteFilePath();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
qWarning() << "无法打开文件进行读取: " << fileInfo.absoluteFilePath();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 递归遍历子文件夹
|
|
|
|
|
QFileInfoList subDirList = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
|
for (const QFileInfo& subDirInfo : subDirList) {
|
|
|
|
|
recursiveFolderOperation(subDirInfo.absoluteFilePath());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2025-09-26 08:50:48 +00:00
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
void LeftWidget::slotShowContextMenu(const QPoint &pos) {
|
|
|
|
|
QPoint globalPos = _treeWidget->mapToGlobal(pos);
|
|
|
|
|
_contextMenu->exec(globalPos);
|
|
|
|
|
|
|
|
|
|
// 获取当前选中的项
|
|
|
|
|
QTreeWidgetItem *currentItem = _treeWidget->itemAt(pos);
|
|
|
|
|
if (currentItem == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString filePath = currentItem->data(0, Qt::UserRole).toString();
|
|
|
|
|
if (filePath.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// qDebug() << "file:" << filePath;
|
|
|
|
|
if (QFile::remove(filePath)) {
|
|
|
|
|
qDebug() << "文件删除成功:" << filePath;
|
|
|
|
|
} else {
|
|
|
|
|
qWarning() << "文件删除失败:" << filePath;
|
|
|
|
|
}
|
2025-09-26 08:50:48 +00:00
|
|
|
|
2025-10-24 09:06:37 +00:00
|
|
|
reloadFileName();
|
|
|
|
|
}
|