20240709模型加载

pull/1/head
wuxiaxin 2024-07-09 09:35:35 +08:00
parent 79ea61570b
commit 3f66cdb38f
3 changed files with 113 additions and 210 deletions

View File

@ -36,6 +36,23 @@ namespace WBFZ {
this->initVTKView(); // 初始化VTK视图
// 连接 comboBox 的 setCurrentText 信号与更新 listWidget 的槽函数
connect(ui->FiltercomboBox, QOverload<const QString &>::of(&QComboBox::setCurrentText),
this, &DialogImportModelDataset::updateModellistWidget);
// 连接 comboBox 的 currentIndexChanged 信号与更新 listWidget 的槽函数
connect(ui->FiltercomboBox, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this, &DialogImportModelDataset::updateModellistWidget);
// 连接 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_slot()));
// connect(ui->Addmodel_pushButton, &QPushButton::clicked, this, &DialogImportModelDataset::on_push_add_models);
}
@ -68,9 +85,6 @@ namespace WBFZ {
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("森林"); // 使用中文名称
@ -78,10 +92,6 @@ namespace WBFZ {
// 初始显示默认选项对应的 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());
@ -96,47 +106,6 @@ namespace WBFZ {
// 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
Setting::BusAPI* busapi = Setting::BusAPI::instance(); // 指针调用 -> ,类::静态方法
}
@ -258,17 +227,27 @@ namespace WBFZ {
_renderWindow->AddRenderer(_render);
}
void DialogImportModelDataset::on_push_add_models() {
void DialogImportModelDataset::on_push_add_models_slot() {
//模型文件选择框
QString filePath = QFileDialog::getOpenFileName(this, tr("请选择要上传的模型文件"), "", tr("ALL Files (*)"));
if (filePath.isEmpty()) {
if (filePath.isEmpty()){
return;
}
QString chineseName = QInputDialog::getText(this, tr("提示"), tr("请输入模型的中文名称:"));
if (chineseName.isEmpty()) {
//中文类名输入框
QString chineseClass = QInputDialog::getText(this, tr("提示"), tr("请输入模型的中文类名:"));
if(chineseClass.isEmpty()){
return;
}
//英文类名输入框
QString englishClass = QInputDialog::getText(this, tr("提示"), tr("请输入模型的英文类名:"));
if(englishClass.isEmpty()){
return;
}
//读入ini文件并获取需要上传的模型文件名称
QString iniFilePath = "D:\\WBFZCPP\\source\\LAMPCAE\\cmake-build-release\\Release\\model\\Library.ini";
QSettings iniFile(iniFilePath, QSettings::IniFormat);
iniFile.setIniCodec(QTextCodec::codecForName("UTF-8"));
@ -276,95 +255,82 @@ namespace WBFZ {
QString fileName = QFileInfo(filePath).fileName();
QString baseName = QFileInfo(filePath).baseName();
// 进入Names分组
//获取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; // 存储匹配的键
QStringList namesKeys = iniFile.childKeys();
QStringList namesValues;
QString matchedKey = ""; //中文类反查英文类
for (const QString &nameKey : namesKeys){
QString value = iniFile.value(nameKey).toString();
namesValues.append(value);
if (value == chineseClass) {
matchedKey = nameKey;
}
}
// 退出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");
//根据englishClass和chineseClass判断模型所属的类别是否存在不需要新建Names分组
if((namesKeys.contains(englishClass)) || (namesValues.contains(chineseClass))) {
// 复制文件
QDir dir(QString("%1\\model").arg(Setting::BusAPI::instance()->getApplicationExePath()));
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分组中的值
// 若matchedKey没有匹配上即中文类有但是英文没有
if(matchedKey.isEmpty()) {
matchedKey = englishClass;
}
// 根据Models分组的值判断模型名称是否已经存在
iniFile.beginGroup("Models");
QString modelFileName = iniFile.value(matchedKey, "").toString();
iniFile.endGroup();
// 获取当前的值
if (modelFileName.contains(fileName)) {
qDebug() << "Models/" + matchedKey;
QMessageBox::warning(this, tr(u8"错误"), tr(u8"模型名称已经存在"));
return;
}
// 构建新的Models分组值
iniFile.beginGroup("Models");
QString currentValue = iniFile.value(matchedKey, "").toString();
QString newValue;
if (!currentValue.isEmpty()) {
if(!currentValue.isEmpty()) {
newValue = currentValue + "," + fileName;
} else {
newValue = fileName;
}
iniFile.endGroup();
// 手动更新ini文件避免引号
QFile file(iniFilePath);
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
if(file.open(QIODevice::ReadWrite | QIODevice::Text)) {
QTextStream in(&file);
in.setCodec("UTF-8"); // 确保读取时使用UTF-8编码
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();
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);
int pos = modelsSection.indexOf(modelsKey);
if (pos != -1) {
if(pos != -1) {
// 找到现有键,更新其值
int endPos = modelsSection.indexOf('\n', pos);
if (endPos == -1) endPos = modelsSection.length();
if(endPos == -1)
endPos = modelsSection.length();
QString currentLine = modelsSection.mid(pos, endPos - pos);
if (!currentLine.contains(fileName)) {
if(!currentLine.contains(fileName)) {
currentLine += "," + fileName;
modelsSection.replace(pos, endPos - pos, currentLine);
}
@ -376,72 +342,13 @@ namespace WBFZ {
// 重建ini文件内容确保只替换Models分组
content.replace(modelsStartPos, modelsEndPos - modelsStartPos, modelsSection);
if (file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
QTextStream out(&file);
out.setCodec("UTF-8"); // 确保写入时使用UTF-8编码
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);
@ -453,38 +360,35 @@ namespace WBFZ {
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();
//
// }
//判断模型所属的类别不存在需要新建Names分组
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); //复制文件
// 确保写入INI文件时正确处理UTF-8编码
iniFile.beginGroup("Names");
iniFile.setValue(englishClass, chineseClass);
iniFile.endGroup();
iniFile.beginGroup("Models");
iniFile.setValue(englishClass, fileName);
iniFile.endGroup();
iniFile.beginGroup("Detail");
iniFile.setValue(fileName, newFilePath);
iniFile.endGroup();
QMessageBox::information(this, tr("提示"), tr("模型文件加载成功"));
//更新comboBOX选项 与 listWidget内容
this->initLibrary();
qDebug()<<"on_click_over";
}
}

View File

@ -46,12 +46,14 @@ namespace WBFZ {
// virtual void accept();
void updateModellistWidget(const QString &filterStr); // 声明 updateModellistWidget 函数
void handleItemClicked(QListWidgetItem *current, QListWidgetItem *previous);
void on_push_add_models_slot();
// void on_TestLoadModelImport();
private:
GUI::MainWindow* _mainwindow;
Ui::DialogImportModelDataset* ui;
QString _librarySettingPath; // ini 配置环境地址
QMap<QString, QString> _categories; // 用于存储Names分组的数据
// 三维模型操作
public: // 陈增辉

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>1003</width>
<height>805</height>
<height>846</height>
</rect>
</property>
<property name="windowTitle">
@ -105,16 +105,13 @@
<height>16777215</height>
</size>
</property>
<item>
<property name="text">
<string>模型1</string>
</property>
</item>
<item>
<property name="text">
<string>模型2</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QPushButton" name="Addmodel_pushButton">
<property name="text">
<string>上传模型</string>
</property>
</widget>
</item>
</layout>
@ -161,7 +158,7 @@
</layout>
</widget>
<resources/>
<connections>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>