simulationMaster/mainwindow.cpp
2021-12-23 17:46:55 +08:00

263 lines
9.3 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 "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
#include <QtCore>
#include <QtXml>
#include <math.h>
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osg/Point>
#include <osg/MatrixTransform>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <Windows.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
QHBoxLayout * hlayout = new QHBoxLayout(ui->centralwidget);
hlayout->addWidget(osgWidget);
hlayout->setSizeConstraint(QLayout::SetNoConstraint);
osgWidget->setMouseTracking(true);
connect(osgWidget, &osgQOpenGLWidget::initialized, this, &MainWindow::initOsg);
connect(ui->actionOpenModel, &QAction::triggered, [this](){
QString modelFile = QFileDialog::getOpenFileName(this, tr("3D Model File Selection"), "", "*.s3c; *.osg; *.osgb");
if(modelFile != ""){
addModel(modelFile);
}
});
connect(ui->actionUpdateModel, &QAction::triggered, this, &MainWindow::updateModel);
connect(ui->actionZoomToExtent, &QAction::triggered, [this]{
osgWidget->getOsgViewer()->setCameraManipulator(new osgGA::TrackballManipulator());
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if(urls.isEmpty()){
return;
}
QList<QUrl>::iterator it;
for(it = urls.begin(); it != urls.end(); it++){
QFileInfo fileInfo(it->toLocalFile());
if(fileInfo.isFile()){
addModel(fileInfo.filePath());
}
}
}
void MainWindow::initOsg()
{
osgWidget->getOsgViewer()->setSceneData(root);
osgWidget->getOsgViewer()->getCamera()->setSmallFeatureCullingPixelSize(-1.0f);
osgWidget->getOsgViewer()->getCamera()->setClearColor(osg::Vec4(0.5,0.5,0.5,0.5));
osgWidget->getOsgViewer()->getCamera()->setNearFarRatio(0.0001);
float aspectRatio = static_cast<float>( osgWidget->width()) / static_cast<float>( osgWidget->height() );
osgWidget->getOsgViewer()->getCamera()->setProjectionMatrixAsPerspective( 30.f, aspectRatio, 1.f, 1000.f );
osg::ref_ptr<osg::Light> light = new osg::Light();
light->setLightNum(0);
light->setDirection(osg::Vec3(0.0f, 0.0f, -1.0f));
light->setPosition(osg::Vec4(0.0f, 0.0f, 1000, 0.0f));
light->setAmbient(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
light->setDiffuse(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
// //<2F><><EFBFBD>ú<EFBFBD>˥<EFBFBD><CBA5>ָ<EFBFBD><D6B8>
// light->setConstantAttenuation(1.0f);
// //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˥<EFBFBD><CBA5>ָ<EFBFBD><D6B8>
// light->setLinearAttenuation(0.0f);
// //<2F><><EFBFBD>ö<EFBFBD><C3B6>η<EFBFBD>˥<EFBFBD><CBA5>ָ<EFBFBD><D6B8>
// light->setQuadraticAttenuation(0.0f);
osgWidget->getOsgViewer()->setLight(light);
mp = new MousePicker(this);
osgWidget->getOsgViewer()->addEventHandler(mp);
addModel("C:/Users/16284/Desktop/qt/Tile_+000_+005/Tile_+000_+005/Tile_+000_+005.osgb");
}
void MainWindow::addModel(QString modelFile)
{
if(modelFile == filename){
return;
}
filename = modelFile;
QString ext = filename.split('.').back().toLower();
osg::Group* layer = new osg::Group;
if(ext == "osg" || ext == "osgb" || ext == "obj" || ext == "ive"){
clearModel();
osg::Node* node = osgDB::readNodeFile(Utf8ToGbk(filename.toStdString()));
node->setName(filename.toStdString());
layer->addChild(node);
}
else if(ext == "s3c"){
QDir folder(filename);
folder.cdUp();
QDir dataPath(folder.absolutePath() + "/Data");
clearModel();
QStringList tileFolderList = dataPath.entryList();
for(QString tileFolder: tileFolderList){
QString tileFile = dataPath.absolutePath() + '/' + tileFolder + '/' + tileFolder + ".osgb";
if(QDir().exists(tileFile)){
osg::Node* node = osgDB::readNodeFile(Utf8ToGbk(tileFile.toStdString()));
node->setName(tileFolder.toStdString() + ".osgb");
layer->addChild(node);
}
}
}
else{
QMessageBox::warning(this, "simulationMaster", "Unsupported File Format! ");
return;
}
root->addChild(layer);
mp->setRoot(root);
osgGA::TrackballManipulator* tm = new osgGA::TrackballManipulator();
tm->setAllowThrow(false);
tm->setAnimationTime(1000);
osgWidget->getOsgViewer()->setCameraManipulator(tm);
}
void MainWindow::updateModel()
{
// ѡ<><D1A1>ģ<EFBFBD>͸<EFBFBD><CDB8>»<EFBFBD><C2BB>ڵ<EFBFBD><DAB5><EFBFBD>ά<EFBFBD><CEAC><EFBFBD>ſ<EFBFBD>
// QString libFileName = QFileDialog::getOpenFileName(this, "Plz choose the 3dl file", "", "*.3dl");
QString libFileName = QString::fromLocal8Bit("C:/Users/16284/Desktop/qt/LibMASTER/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><CEAC><EFBFBD>ſ<EFBFBD>.3dl");
if(libFileName == ""){
return;
}
QDir dir = QDir(libFileName);
dir.cdUp();
// <20><><EFBFBD><EFBFBD>ά<EFBFBD><CEAC><EFBFBD>ſ<EFBFBD><C5BF><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
QMap<Item, QList<Item>> map;
QDomDocument doc;
QFile file(libFileName);
file.open(QIODevice::ReadOnly);
doc.setContent(&file);
file.close();
QDomNode FLevNode = doc.documentElement().firstChild().toElement().firstChild(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
QDomNode SLevNode = FLevNode.toElement().firstChild(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵڶ<CAB5><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(!FLevNode.isNull()){
if (FLevNode.toElement().tagName() == QStringLiteral("catagory")){
Item FirstItem; //<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
QList<Item> SecondList; //ijһ<C4B3><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5>б<EFBFBD>
FirstItem.name = FLevNode.toElement().attribute("name").trimmed();
FirstItem.code = FLevNode.toElement().attribute("code").trimmed();
SLevNode = FLevNode.toElement().firstChild();
while(!SLevNode.isNull()){
if (SLevNode.toElement().tagName() == QStringLiteral("feature")){
Item SecondItem; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SecondItem.name = SLevNode.toElement().attribute("name").trimmed();
SecondItem.code = SLevNode.toElement().attribute("code").trimmed();
SecondItem.address = SLevNode.toElement().attribute("address").trimmed();
SecondList << SecondItem;
}
SLevNode = SLevNode.nextSibling();
}
map.insert(FirstItem, SecondList);
}
FLevNode = FLevNode.nextSibling(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֵܽڵ<DCBD>
}
// ѡ<><D1A1><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>Ⱦ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
QDialog dialog(this);
QFormLayout form(&dialog);
form.addRow(new QLabel(QStringLiteral("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦʹ<EFBFBD>õķ<EFBFBD><EFBFBD><EFBFBD>")));
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
QComboBox *cataChosen = new QComboBox(&dialog);
form.addRow(QStringLiteral("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: "), cataChosen);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
QComboBox *modelChosen = new QComboBox(&dialog);
connect(cataChosen, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [=](int index){
modelChosen->clear();
QList<Item> itemList = map.values().at(index);
for(int i = 0; i < itemList.count(); i++){
modelChosen->addItem(itemList.at(i).name);
}
});
for(int i = 0; i < map.count(); i++){
cataChosen->addItem(map.keys().at(i).name);
}
form.addRow(QStringLiteral("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: "), modelChosen);
// Add Cancel and OK button
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
Qt::Horizontal, &dialog);
form.addRow(&buttonBox);
QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
dialog.adjustSize();
// dialog.setFixedWidth(250);
// dialog.setFixedHeight(120);
// Process when OK button is clicked
int result = dialog.exec();
while(result == QDialog::Accepted){
if(cataChosen->currentIndex()==-1 || modelChosen->currentIndex()==-1){
QMessageBox::warning(this, "simulationMaster", "Plz choose the catagory and model");
result = dialog.exec();
continue;
}
Item item = map.values().at(cataChosen->currentIndex()).at(modelChosen->currentIndex());
// <20><><EFBFBD><EFBFBD>item.address<73><73>ȡģ<C8A1><C4A3>
osg::Node* model = osgDB::readNodeFile(Utf8ToGbk((dir.absolutePath() + '/' + item.address).toStdString()));
mp->activate(PickingMode::PointPicker, model);
// connect(mp, &MousePicker::editionDone, [=](PickingMode mode, osg::ref_ptr<osg::Vec3Array> vertexArray)
// {
// if(vertexArray == nullptr){
// return;
// }
// if(mode == PickingMode::PointPicker){
// nv.setPoints(vertexArray);
// root->getChild(0)->accept(nv);
// }
// });
break;
}
}
void MainWindow::clearModel()
{
root->removeChildren(0, root->getNumChildren());
}
std::string Utf8ToGbk(std::string str){
const char* src_str = str.data();
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
wchar_t* wszGBK = new wchar_t[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char* szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
if (wszGBK) delete[] wszGBK;
if (szGBK) delete[] szGBK;
return strTemp;
}