20240708 11:06
parent
e271b483f8
commit
79ea61570b
|
|
@ -20,96 +20,122 @@
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QTextCodec>
|
#include <QTextCodec>
|
||||||
|
#include <QFileDialog>
|
||||||
|
#include <QInputDialog>
|
||||||
|
#include <QMessageBox>
|
||||||
|
|
||||||
namespace WBFZ {
|
namespace WBFZ {
|
||||||
DialogImportModelDataset::DialogImportModelDataset(GUI::MainWindow* _mainwindow, QWidget* parent)
|
DialogImportModelDataset::DialogImportModelDataset(GUI::MainWindow* _mainwindow, QWidget* parent)
|
||||||
: QDialog(_mainwindow),
|
: QDialog(_mainwindow),
|
||||||
_mainwindow(_mainwindow),
|
_mainwindow(_mainwindow),
|
||||||
ui(new Ui::DialogImportModelDataset)
|
ui(new Ui::DialogImportModelDataset)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
this->initLibrary();
|
|
||||||
this->initVTKView();// 初始化VTK视图
|
|
||||||
// 初始化 comboBox 的选项
|
|
||||||
QMap<QString, QString> categories = {
|
|
||||||
{"forest", "森林"},
|
|
||||||
{"crop", "农作物"},
|
|
||||||
{"grass", "草地"},
|
|
||||||
{"uav", "人工"},
|
|
||||||
{"water", "水体"},
|
|
||||||
{"dwater", "动态水体"},
|
|
||||||
{"road", "道路"},
|
|
||||||
{"geo", "几何"},
|
|
||||||
{"radi", "辐射"},
|
|
||||||
{"soil", "土壤"},
|
|
||||||
{"land", "陆表"},
|
|
||||||
{"vegetation", "植被"},
|
|
||||||
{"water_scene", "水体场景"}
|
|
||||||
};
|
|
||||||
for (auto category : categories.values()) {
|
|
||||||
ui->FiltercomboBox->addItem(category);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 连接 comboBox 的 currentIndexChanged 信号与更新 listWidget 的槽函数
|
this->initLibrary(); //初始化comboBox和listMidget内容
|
||||||
connect(ui->FiltercomboBox, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
|
|
||||||
this, &DialogImportModelDataset::updateModellistWidget);
|
|
||||||
|
|
||||||
// 初始显示第一个选项对应的 listWidget 内容
|
this->initVTKView(); // 初始化VTK视图
|
||||||
updateModellistWidget(ui->FiltercomboBox->currentText());
|
|
||||||
|
|
||||||
// 初始化其它设置
|
|
||||||
_librarySettingPath = "D:/WBFZCPP/source/FastCAE/extlib/model/library.ini"; // 替换为实际的配置文件路径
|
|
||||||
initLibrary(); // 初始化库
|
|
||||||
|
|
||||||
// 可以在这里进行其它的初始化操作
|
}
|
||||||
// connect(ui->ModellistWidget, QOverload<QListWidgetItem *, QListWidgetItem *>::of(&QListWidget::currentItemChanged),
|
|
||||||
// this, &DialogImportModelDataset::handleItemClicked);
|
|
||||||
connect(ui->ModellistWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
|
|
||||||
this,SLOT(handleItemClicked(QListWidgetItem*, QListWidgetItem*)));
|
|
||||||
}
|
|
||||||
|
|
||||||
DialogImportModelDataset::~DialogImportModelDataset()
|
DialogImportModelDataset::~DialogImportModelDataset()
|
||||||
{
|
{
|
||||||
delete ui;
|
delete ui;
|
||||||
delete _qvtkWidget; // 释放内存
|
delete _qvtkWidget; // 释放内存
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化模型库
|
// 初始化模型库
|
||||||
void DialogImportModelDataset::initLibrary() {
|
void DialogImportModelDataset::initLibrary() {
|
||||||
QSettings settings(_librarySettingPath, QSettings::IniFormat);
|
|
||||||
|
|
||||||
QStringList categories = {"forest", "crop", "grass", "uav", "water", "dwater", "road", "geo", "radi", "soil", "land", "vegetation", "water_scene"};
|
//清除FiltercomboBox内容
|
||||||
QMap<QString, QStringList> models = {
|
ui->FiltercomboBox->clear();
|
||||||
{"forest", {"forest.stl"}},
|
|
||||||
{"crop", {"crop.stl"}},
|
|
||||||
{"grass", {"grass.stl"}},
|
|
||||||
{"uav", {"uav.ply", "uav.stl"}},
|
|
||||||
{"water", {"water.stl"}},
|
|
||||||
{"dwater", {"dwater.stl"}},
|
|
||||||
{"road", {"road.ply", "road.stl"}},
|
|
||||||
{"geo", {"geo.stl"}},
|
|
||||||
{"radi", {"radi.stl"}},
|
|
||||||
{"soil", {"soil.ply", "soil.stl"}},
|
|
||||||
{"land", {"land.stl"}},
|
|
||||||
{"vegetation", {"vegetation.stl"}},
|
|
||||||
{"water_scene", {"water_scene.stl"}}
|
|
||||||
};
|
|
||||||
|
|
||||||
settings.beginGroup("Models");
|
// 初始化其它设置
|
||||||
for (const QString &category : categories) {
|
_librarySettingPath = "D:\\WBFZCPP\\source\\LAMPCAE\\cmake-build-release\\Release\\model\\library.ini"; // 替换为实际的配置文件路径
|
||||||
settings.setValue(category, models[category]);
|
|
||||||
}
|
|
||||||
settings.endGroup();
|
|
||||||
|
|
||||||
settings.beginGroup("Detail");
|
// 读取INI文件中的Names分组
|
||||||
for (const QString &category : categories) {
|
QSettings settings(_librarySettingPath, QSettings::IniFormat);
|
||||||
for (const QString &model : models[category]) {
|
settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
|
||||||
QString path = QString("../../extlib/model/%1").arg(model);
|
|
||||||
// QString path = QString("D:/WBFZCPP/source/FastCAE/extlib/model/%1").arg(model);
|
settings.beginGroup("Names");
|
||||||
settings.setValue(model, path);
|
QStringList keys = settings.childKeys();
|
||||||
}
|
for (const QString &key : keys) {
|
||||||
}
|
_categories[settings.value(key).toString()] = key; // 将中文名称作为键,英文名称作为值存储
|
||||||
settings.endGroup();
|
}
|
||||||
|
settings.endGroup();
|
||||||
|
|
||||||
|
for (auto category : _categories.keys()) {
|
||||||
|
ui->FiltercomboBox->addItem(category);
|
||||||
|
}
|
||||||
|
// 连接 comboBox 的 setCurrentText 信号与更新 listWidget 的槽函数
|
||||||
|
connect(ui->FiltercomboBox, QOverload<const QString &>::of(&QComboBox::setCurrentText),
|
||||||
|
this, &DialogImportModelDataset::updateModellistWidget);
|
||||||
|
|
||||||
|
// 设置comboBox默认选项为"森林"
|
||||||
|
ui->FiltercomboBox->setCurrentText("森林"); // 使用中文名称
|
||||||
|
|
||||||
|
// 初始显示默认选项对应的 listWidget 内容
|
||||||
|
updateModellistWidget(ui->FiltercomboBox->currentText());
|
||||||
|
|
||||||
|
// 连接 comboBox 的 currentIndexChanged 信号与更新 listWidget 的槽函数
|
||||||
|
connect(ui->FiltercomboBox, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
|
||||||
|
this, &DialogImportModelDataset::updateModellistWidget);
|
||||||
|
|
||||||
|
// 初始显示选项对应的 listWidget 内容
|
||||||
|
updateModellistWidget(ui->FiltercomboBox->currentText());
|
||||||
|
|
||||||
|
// 连接 listWidget 的 currentItemChanged 信号与更新 VTK 的槽函数
|
||||||
|
// connect(ui->ModellistWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
|
||||||
|
// this, SLOT(handleItemClicked(QListWidgetItem*, QListWidgetItem*)));
|
||||||
|
connect(ui->ModellistWidget, QOverload<QListWidgetItem *, QListWidgetItem *>::of(&QListWidget::currentItemChanged),
|
||||||
|
this, &DialogImportModelDataset::handleItemClicked);
|
||||||
|
|
||||||
|
|
||||||
|
// 连接 pushButton 的 clicked 信号与更新 listWidget 的槽函数
|
||||||
|
// connect(ui->Addmodel_pushButton, SIGNAL(clicked()), this, SLOT(on_push_add_models()));
|
||||||
|
connect(ui->Addmodel_pushButton, &QPushButton::clicked, this, &DialogImportModelDataset::on_push_add_models);
|
||||||
|
|
||||||
|
// // 检查文件是否存在
|
||||||
|
// QFile iniFile(_librarySettingPath);
|
||||||
|
// if (iniFile.exists()) {
|
||||||
|
// // Here you can add more initialization code if needed
|
||||||
|
// Setting::BusAPI* busapi = Setting::BusAPI::instance(); // 指针调用 -> ,类::静态方法
|
||||||
|
// return; // 如果文件存在,则跳过写入操作
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// QSettings settings(_librarySettingPath, QSettings::IniFormat);
|
||||||
|
// QStringList categories = {"forest", "crop", "grass", "uav", "water", "dwater", "road", "geo", "radi", "soil", "land", "vegetation", "water_scene"};
|
||||||
|
// QMap<QString, QStringList> models = {
|
||||||
|
// {"forest", {"forest.stl"}},
|
||||||
|
// {"crop", {"crop.stl"}},
|
||||||
|
// {"grass", {"grass.stl"}},
|
||||||
|
// {"uav", {"uav.ply", "uav.stl"}},
|
||||||
|
// {"water", {"water.stl"}},
|
||||||
|
// {"dwater", {"dwater.stl"}},
|
||||||
|
// {"road", {"road.ply", "road.stl"}},
|
||||||
|
// {"geo", {"geo.stl"}},
|
||||||
|
// {"radi", {"radi.stl"}},
|
||||||
|
// {"soil", {"soil.ply", "soil.stl"}},
|
||||||
|
// {"land", {"land.stl"}},
|
||||||
|
// {"vegetation", {"vegetation.stl"}},
|
||||||
|
// {"water_scene", {"water_scene.stl"}}
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// settings.beginGroup("Models");
|
||||||
|
// for (const QString &category : categories) {
|
||||||
|
// settings.setValue(category, models[category]);
|
||||||
|
// }
|
||||||
|
// settings.endGroup();
|
||||||
|
//
|
||||||
|
// settings.beginGroup("Detail");
|
||||||
|
// for (const QString &category : categories) {
|
||||||
|
// for (const QString &model : models[category]) {
|
||||||
|
// QString path = QString("../../cmake-build-release/Release/model/%1").arg(model);
|
||||||
|
// settings.setValue(model, path);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// settings.endGroup();
|
||||||
|
|
||||||
// Here you can add more initialization code if needed
|
// Here you can add more initialization code if needed
|
||||||
Setting::BusAPI* busapi = Setting::BusAPI::instance(); // 指针调用 -> ,类::静态方法
|
Setting::BusAPI* busapi = Setting::BusAPI::instance(); // 指针调用 -> ,类::静态方法
|
||||||
|
|
@ -132,33 +158,54 @@ namespace WBFZ {
|
||||||
|
|
||||||
// 更新 listWidget 的槽函数
|
// 更新 listWidget 的槽函数
|
||||||
void DialogImportModelDataset::updateModellistWidget(const QString &filterStr) {
|
void DialogImportModelDataset::updateModellistWidget(const QString &filterStr) {
|
||||||
|
qDebug() << "void DialogImportModelDataset::handleItemClicked";
|
||||||
filterModel(filterStr);
|
filterModel(filterStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据中文分类名称获取英文分类名称
|
// 根据中文分类名称获取英文分类名称
|
||||||
QString DialogImportModelDataset::getEnglishCategory(const QString &chineseCategory) {
|
QString DialogImportModelDataset::getEnglishCategory(const QString &chineseCategory) {
|
||||||
QMap<QString, QString> categories = {
|
return _categories.value(chineseCategory, "");
|
||||||
{"森林", "forest"},
|
}
|
||||||
{"农作物", "crop"},
|
|
||||||
{"草地", "grass"},
|
|
||||||
{"人工", "uav"},
|
|
||||||
{"水体", "water"},
|
|
||||||
{"动态水体", "dwater"},
|
|
||||||
{"道路", "road"},
|
|
||||||
{"几何", "geo"},
|
|
||||||
{"辐射", "radi"},
|
|
||||||
{"土壤", "soil"},
|
|
||||||
{"陆表", "land"},
|
|
||||||
{"植被", "vegetation"},
|
|
||||||
{"水体场景", "water_scene"}
|
|
||||||
};
|
|
||||||
return categories.value(chineseCategory, "");
|
|
||||||
}
|
|
||||||
void DialogImportModelDataset::handleItemClicked(QListWidgetItem *current, QListWidgetItem *previous)
|
void DialogImportModelDataset::handleItemClicked(QListWidgetItem *current, QListWidgetItem *previous)
|
||||||
{
|
{
|
||||||
qDebug()<<"void DialogImportModelDataset::handleItemClicked(QListWidgetItem *current, QListWidgetItem *previous)";
|
|
||||||
this->importPreViewModel("D:\\WBFZCPP\\LAMPCAETest\\modle\\crop.stl");
|
qDebug() << "void DialogImportModelDataset::handleItemClicked(QListWidgetItem *current, QListWidgetItem *previous)";
|
||||||
this->importMeshModelToMainWindows("D:\\WBFZCPP\\LAMPCAETest\\modle\\crop.stl");
|
|
||||||
|
if (!current) return; // 确保当前项非空
|
||||||
|
|
||||||
|
// 读取 item 文本或其他相关信息
|
||||||
|
QString itemName = current->text();
|
||||||
|
qDebug() << "Current item selected: " << itemName;
|
||||||
|
|
||||||
|
// 定义 INI 文件的路径
|
||||||
|
QString iniFilePath = "D:\\WBFZCPP\\source\\LAMPCAE\\cmake-build-release\\Release\\model\\library.ini";
|
||||||
|
|
||||||
|
// 检查文件是否存在
|
||||||
|
if (!QFileInfo::exists(iniFilePath)) {
|
||||||
|
qDebug() << "INI file not found: " << iniFilePath;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用 QSettings 读取 INI 文件
|
||||||
|
QSettings settings(iniFilePath, QSettings::IniFormat);
|
||||||
|
settings.beginGroup("Detail");
|
||||||
|
|
||||||
|
// 获取与当前 item 名称匹配的路径
|
||||||
|
QString modelPath = settings.value(itemName).toString();
|
||||||
|
|
||||||
|
if (modelPath.isEmpty()) {
|
||||||
|
qDebug() << "No model path found for item: " << itemName;
|
||||||
|
} else {
|
||||||
|
qDebug() << "Model path: " << modelPath;
|
||||||
|
|
||||||
|
// 根据获取的路径导入预览模型
|
||||||
|
this->importPreViewModel(modelPath);
|
||||||
|
this->importMeshModelToMainWindows(modelPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
settings.endGroup();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 导入网格
|
// 导入网格
|
||||||
|
|
@ -173,29 +220,26 @@ namespace WBFZ {
|
||||||
// 初始化三维模型
|
// 初始化三维模型
|
||||||
void DialogImportModelDataset::importPreViewModel(QString stdPath) {
|
void DialogImportModelDataset::importPreViewModel(QString stdPath) {
|
||||||
//清楚所有模型
|
//清楚所有模型
|
||||||
// _render->RemoveAllViewProps();
|
_render->RemoveAllViewProps();
|
||||||
// 在你的主窗口或者其他适当的位置初始化 VTK 相关变量
|
// 在你的主窗口或者其他适当的位置初始化 VTK 相关变量
|
||||||
QTextCodec *codec = QTextCodec::codecForName("GB18030");
|
|
||||||
QByteArray ba = codec->fromUnicode(stdPath);
|
|
||||||
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
|
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
|
||||||
reader->SetFileName(ba); // 设置 STL 模型文件路径
|
reader->SetFileName(stdPath.toLocal8Bit().data()); // 设置 STL 模型文件路径
|
||||||
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
||||||
mapper->SetInputConnection(reader->GetOutputPort());
|
mapper->SetInputConnection(reader->GetOutputPort());
|
||||||
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
||||||
actor->SetMapper(mapper);
|
actor->SetMapper(mapper);
|
||||||
_render->AddActor(actor);
|
_render->AddActor(actor);
|
||||||
// 更新渲染窗口
|
// 更新渲染窗口
|
||||||
// _renderWindow->Render();
|
_renderWindow->Render();
|
||||||
_qvtkWidget->update();
|
_qvtkWidget->update();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
void DialogImportModelDataset::initVTKView() {
|
void DialogImportModelDataset::initVTKView() {
|
||||||
// 添加控件
|
// 添加控件
|
||||||
_qvtkWidget = new QVTKOpenGLNativeWidget(this);
|
_qvtkWidget = new QVTKOpenGLNativeWidget(this);
|
||||||
QVBoxLayout *mainLayout = new QVBoxLayout(ui->modelViewContent);
|
QVBoxLayout *mainLayout = new QVBoxLayout(ui->modelViewContent);
|
||||||
mainLayout->addWidget(_qvtkWidget);
|
mainLayout->addWidget(_qvtkWidget);
|
||||||
|
|
||||||
// 初始化渲染器
|
// 初始化渲染器
|
||||||
_renderWindow = _qvtkWidget->renderWindow();
|
_renderWindow = _qvtkWidget->renderWindow();
|
||||||
_render = vtkSmartPointer<vtkOpenGLRenderer>::New();
|
_render = vtkSmartPointer<vtkOpenGLRenderer>::New();
|
||||||
|
|
@ -206,20 +250,243 @@ namespace WBFZ {
|
||||||
QColor bottomcolor = option->getBackgroundBottomColor();
|
QColor bottomcolor = option->getBackgroundBottomColor();
|
||||||
_render->SetBackground2(topcolor.redF(), topcolor.greenF(), topcolor.blueF());
|
_render->SetBackground2(topcolor.redF(), topcolor.greenF(), topcolor.blueF());
|
||||||
_render->SetBackground(bottomcolor.redF(), bottomcolor.greenF(), bottomcolor.blueF());
|
_render->SetBackground(bottomcolor.redF(), bottomcolor.greenF(), bottomcolor.blueF());
|
||||||
|
|
||||||
// 开启硬件加速特性
|
// 开启硬件加速特性
|
||||||
_render->UseDepthPeelingOn();
|
_render->UseDepthPeelingOn();
|
||||||
_render->SetUseFXAA(true);
|
_render->SetUseFXAA(true);
|
||||||
_interactor = _renderWindow->GetInteractor();
|
_interactor = _renderWindow->GetInteractor();
|
||||||
_renderWindow->AddRenderer(_render);
|
_renderWindow->AddRenderer(_render);
|
||||||
}
|
}
|
||||||
// void DialogImportModelDataset::on_TestLoadModelImport()
|
|
||||||
// {
|
void DialogImportModelDataset::on_push_add_models() {
|
||||||
// WBFZ::DialogImportModelDataset* dialog=new WBFZ::DialogImportModelDataset(_mainwindow);
|
QString filePath = QFileDialog::getOpenFileName(this, tr("请选择要上传的模型文件"), "", tr("ALL Files (*)"));
|
||||||
// dialog->importPreViewModel("D:\\WBFZCPP\\source\\FastCAE\\extlib\\model\\crop.stl");
|
if (filePath.isEmpty()) {
|
||||||
// dialog->exec();
|
return;
|
||||||
// dialog->importMeshModelToMainWindows("D:\\WBFZCPP\\source\\FastCAE\\extlib\\model\\crop.stl");
|
}
|
||||||
// }
|
|
||||||
|
QString chineseName = QInputDialog::getText(this, tr("提示"), tr("请输入模型的中文名称:"));
|
||||||
|
if (chineseName.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString iniFilePath = "D:\\WBFZCPP\\source\\LAMPCAE\\cmake-build-release\\Release\\model\\Library.ini";
|
||||||
|
QSettings iniFile(iniFilePath, QSettings::IniFormat);
|
||||||
|
iniFile.setIniCodec(QTextCodec::codecForName("UTF-8"));
|
||||||
|
|
||||||
|
QString fileName = QFileInfo(filePath).fileName();
|
||||||
|
QString baseName = QFileInfo(filePath).baseName();
|
||||||
|
|
||||||
|
// 进入Names分组
|
||||||
|
iniFile.beginGroup("Names");
|
||||||
|
// 获取Names分组下所有的键
|
||||||
|
QStringList keys = iniFile.childKeys();
|
||||||
|
// 用于存储Names分组下所有的值
|
||||||
|
QStringList values;
|
||||||
|
// 用于存储匹配的键
|
||||||
|
QString matchedKey;
|
||||||
|
// 遍历所有的键,获取对应的值
|
||||||
|
for (const QString &key : keys) {
|
||||||
|
QString value = iniFile.value(key).toString();
|
||||||
|
values.append(value);
|
||||||
|
if (value == chineseName) {
|
||||||
|
matchedKey = key; // 存储匹配的键
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 退出Names分组
|
||||||
|
iniFile.endGroup();
|
||||||
|
|
||||||
|
if (iniFile.value("Models/" + baseName).toString() == fileName) {
|
||||||
|
QMessageBox::warning(this, tr("错误"), tr("模型文件已经存在"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if(values.contains(chineseName)) {
|
||||||
|
QDir dir("D:\\WBFZCPP\\source\\LAMPCAE\\cmake-build-release\\Release\\model");
|
||||||
|
if(!dir.exists()) {
|
||||||
|
dir.mkpath(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
QString newFilePath = dir.filePath(fileName);
|
||||||
|
QFile::copy(filePath, newFilePath);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//此方法更新之后会带引号
|
||||||
|
// iniFile.beginGroup("Models");
|
||||||
|
//
|
||||||
|
// // 获取当前的值
|
||||||
|
// QString currentValue = iniFile.value(matchedKey, "").toString();
|
||||||
|
// QString newValue;
|
||||||
|
// if(!currentValue.isEmpty()) {
|
||||||
|
// newValue = currentValue + "," + fileName;
|
||||||
|
// } else {
|
||||||
|
// newValue = fileName;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 更新Models分组中的值
|
||||||
|
// iniFile.setValue(matchedKey, newValue);
|
||||||
|
// iniFile.endGroup();
|
||||||
|
|
||||||
|
// 更新Models分组中的值
|
||||||
|
iniFile.beginGroup("Models");
|
||||||
|
|
||||||
|
// 获取当前的值
|
||||||
|
QString currentValue = iniFile.value(matchedKey, "").toString();
|
||||||
|
QString newValue;
|
||||||
|
if (!currentValue.isEmpty()) {
|
||||||
|
newValue = currentValue + "," + fileName;
|
||||||
|
} else {
|
||||||
|
newValue = fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
iniFile.endGroup();
|
||||||
|
|
||||||
|
// 手动更新ini文件,避免引号
|
||||||
|
QFile file(iniFilePath);
|
||||||
|
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
|
||||||
|
QTextStream in(&file);
|
||||||
|
in.setCodec("UTF-8"); // 确保读取时使用UTF-8编码
|
||||||
|
QString content = in.readAll();
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
// 查找Models分组的位置
|
||||||
|
int modelsStartPos = content.indexOf("[Models]");
|
||||||
|
int modelsEndPos = content.indexOf("[", modelsStartPos + 1);
|
||||||
|
if (modelsEndPos == -1) modelsEndPos = content.length();
|
||||||
|
|
||||||
|
QString modelsSection = content.mid(modelsStartPos, modelsEndPos - modelsStartPos);
|
||||||
|
QString modelsKey = matchedKey + "=";
|
||||||
|
int pos = modelsSection.indexOf(modelsKey);
|
||||||
|
|
||||||
|
if (pos != -1) {
|
||||||
|
// 找到现有键,更新其值
|
||||||
|
int endPos = modelsSection.indexOf('\n', pos);
|
||||||
|
if (endPos == -1) endPos = modelsSection.length();
|
||||||
|
QString currentLine = modelsSection.mid(pos, endPos - pos);
|
||||||
|
if (!currentLine.contains(fileName)) {
|
||||||
|
currentLine += "," + fileName;
|
||||||
|
modelsSection.replace(pos, endPos - pos, currentLine);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 添加新的键值对
|
||||||
|
modelsSection += "\n" + modelsKey + newValue + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重建ini文件内容,确保只替换Models分组
|
||||||
|
content.replace(modelsStartPos, modelsEndPos - modelsStartPos, modelsSection);
|
||||||
|
|
||||||
|
if (file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
|
||||||
|
QTextStream out(&file);
|
||||||
|
out.setCodec("UTF-8"); // 确保写入时使用UTF-8编码
|
||||||
|
out << content;
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 更新Names分组中的键名
|
||||||
|
iniFile.beginGroup("Names");
|
||||||
|
|
||||||
|
// 获取当前的值
|
||||||
|
QString newKey;
|
||||||
|
if (!matchedKey.isEmpty()) {
|
||||||
|
newKey = matchedKey + "," + baseName;
|
||||||
|
} else {
|
||||||
|
newKey = baseName;
|
||||||
|
}
|
||||||
|
qDebug()<<newKey;
|
||||||
|
iniFile.endGroup();
|
||||||
|
|
||||||
|
// 手动更新ini文件,避免引号
|
||||||
|
// QFile file(iniFilePath);
|
||||||
|
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
|
||||||
|
QTextStream in(&file);
|
||||||
|
in.setCodec("UTF-8"); // 确保读取时使用UTF-8编码
|
||||||
|
QString content = in.readAll();
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
// 查找Names分组的位置
|
||||||
|
int namesStartPos = content.indexOf("[Names]");
|
||||||
|
int namesEndPos = content.indexOf("[", namesStartPos + 1);
|
||||||
|
if (namesEndPos == -1) namesEndPos = content.length();
|
||||||
|
|
||||||
|
QString namesSection = content.mid(namesStartPos, namesEndPos - namesStartPos);
|
||||||
|
QString namesKey = matchedKey + "=";
|
||||||
|
int pos = namesSection.indexOf(namesKey);
|
||||||
|
|
||||||
|
if (pos != -1) {
|
||||||
|
// 找到现有键,更新其键名
|
||||||
|
int endPos = namesSection.indexOf('\n', pos);
|
||||||
|
if (endPos == -1) endPos = namesSection.length();
|
||||||
|
QString currentLine = namesSection.mid(pos, endPos - pos);
|
||||||
|
|
||||||
|
// 替换键名
|
||||||
|
QString newLine = newKey + currentLine.mid(matchedKey.length());
|
||||||
|
namesSection.replace(pos, endPos - pos, newLine);
|
||||||
|
} else {
|
||||||
|
// 添加新的键值对
|
||||||
|
namesSection += "\n" + newKey + "=" + chineseName + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重建ini文件内容,确保只替换Names分组
|
||||||
|
content.replace(namesStartPos, namesEndPos - namesStartPos, namesSection);
|
||||||
|
|
||||||
|
if (file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
|
||||||
|
QTextStream out(&file);
|
||||||
|
out.setCodec("UTF-8"); // 确保写入时使用UTF-8编码
|
||||||
|
out << content;
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//更新Deatail分组的内容
|
||||||
|
iniFile.beginGroup("Detail");
|
||||||
|
iniFile.setValue(fileName, newFilePath);
|
||||||
|
iniFile.endGroup();
|
||||||
|
|
||||||
|
QMessageBox::information(this, tr("提示"), tr("模型文件加载成功"));
|
||||||
|
|
||||||
|
// 更新comboBOX选项 与 listWidget内容
|
||||||
|
this->initLibrary();
|
||||||
|
}
|
||||||
|
|
||||||
|
// else{
|
||||||
|
// QDir dir("D:\\WBFZCPP\\source\\LAMPCAE\\cmake-build-release\\Release\\model");
|
||||||
|
// if (!dir.exists()) {
|
||||||
|
// dir.mkpath(".");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// QString newFilePath = dir.filePath(fileName);
|
||||||
|
// QFile::copy(filePath, newFilePath); //复制文件
|
||||||
|
// // if (!QFile::copy(filePath, newFilePath)) {
|
||||||
|
// // QMessageBox::warning(this, tr("错误"), tr("加载模型文件失败"));
|
||||||
|
// // return;
|
||||||
|
// // }
|
||||||
|
//
|
||||||
|
// // 确保写入INI文件时正确处理UTF-8编码
|
||||||
|
// iniFile.beginGroup("Names");
|
||||||
|
// iniFile.setValue(baseName, chineseName);
|
||||||
|
// iniFile.endGroup();
|
||||||
|
//
|
||||||
|
// iniFile.beginGroup("Models");
|
||||||
|
// iniFile.setValue(baseName, fileName);
|
||||||
|
// iniFile.endGroup();
|
||||||
|
//
|
||||||
|
// iniFile.beginGroup("Detail");
|
||||||
|
// iniFile.setValue(fileName, newFilePath);
|
||||||
|
// iniFile.endGroup();
|
||||||
|
//
|
||||||
|
// QMessageBox::information(this, tr("提示"), tr("模型文件加载成功"));
|
||||||
|
//
|
||||||
|
// //更新comboBOX选项 与 listWidget内容
|
||||||
|
// this->initLibrary();
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace WBFZ
|
} // namespace WBFZ
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue