From 5105c48f5362eb40f0559b12f20b8818fb32baf0 Mon Sep 17 00:00:00 2001 From: 15712809671 <136563253+15712809671@users.noreply.github.com> Date: Sun, 19 Jan 2025 23:11:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9matlab=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/Layout/CodeEdtUI.cpp | 114 +++++++++++++++---- src/ui/Layout/CodeEdtUI.h | 3 + src/ui/MainWindow.cpp | 6 +- src/ui/Matlab/MatlabObject.cpp | 34 +++++- src/ui/Matlab/MatlabObject.h | 4 + src/ui/Menu/ChartPlotMenu.cpp | 5 +- src/ui/Menu/ChartPlotMenu.h | 1 - src/ui/PropertyBrowser/qtpropertymanager.cpp | 22 +++- 8 files changed, 156 insertions(+), 33 deletions(-) diff --git a/src/ui/Layout/CodeEdtUI.cpp b/src/ui/Layout/CodeEdtUI.cpp index 2f499358..18ca4965 100644 --- a/src/ui/Layout/CodeEdtUI.cpp +++ b/src/ui/Layout/CodeEdtUI.cpp @@ -9,7 +9,10 @@ #include #include #include +#include +#include +#include "../../common/RecourceHelper.h" #include "../../workspace/WorkSpaceManager.h" #include "SyntaxHighlighter.h" @@ -32,25 +35,28 @@ CodeEdtUI::CodeEdtUI(QWidget *parent) setCentralWidget(editor); - // 创建菜单 - QMenu* fileMenu = menuBar()->addMenu(tr("&file")); - QAction* openMainAction = new QAction(tr("&Import the template"), this); - /* QAction* openLDAction = new QAction(tr("&Import the LD template"), this); - QAction* openSeekerSimAction = new QAction(tr("&Import the SeekerSim template"), this);*/ - QAction* saveAction = new QAction(tr("&save"), this); - fileMenu->addAction(openMainAction); - //fileMenu->addAction(openLDAction); - //fileMenu->addAction(openSeekerSimAction); - fileMenu->addAction(saveAction); - connect(openMainAction, &QAction::triggered, this, &CodeEdtUI::openMainFile); - /* connect(openLDAction, &QAction::triggered, this, &CodeEdtUI::openLDFile); - connect(openSeekerSimAction, &QAction::triggered, this, &CodeEdtUI::openSeekerSimFile);*/ + InitBat(); - connect(saveAction, &QAction::triggered, this, &CodeEdtUI::saveFile); + // // 创建菜单 + // QMenu* fileMenu = menuBar()->addMenu(tr("&file")); + // QAction* openMainAction = new QAction(tr("&Import the template"), this); + ///* QAction* openLDAction = new QAction(tr("&Import the LD template"), this); + // QAction* openSeekerSimAction = new QAction(tr("&Import the SeekerSim template"), this);*/ + // QAction* saveAction = new QAction(tr("&save"), this); + // fileMenu->addAction(openMainAction); + // //fileMenu->addAction(openLDAction); + // //fileMenu->addAction(openSeekerSimAction); + // fileMenu->addAction(saveAction); - // 状态栏 - statusBar(); + // connect(openMainAction, &QAction::triggered, this, &CodeEdtUI::openMainFile); + ///* connect(openLDAction, &QAction::triggered, this, &CodeEdtUI::openLDFile); + // connect(openSeekerSimAction, &QAction::triggered, this, &CodeEdtUI::openSeekerSimFile);*/ + + // connect(saveAction, &QAction::triggered, this, &CodeEdtUI::saveFile); + + // // 状态栏 + // statusBar(); } void CodeEdtUI::AttachDock(DockWidget* dockWidget) @@ -70,23 +76,60 @@ void CodeEdtUI::AttachDock(DockWidget* dockWidget) dockWidget->SetDockWidgetTitleBar(dockTitleBar); } -void CodeEdtUI::openMainFile() { - QString fileName = ""; //QFileDialog::getOpenFileName(this, "Open File", "", "Matlab Files (*.m)"); - if (fileName.isEmpty()) +void CodeEdtUI::InitBat() +{ { - fileName = QFileDialog::getOpenFileName(this, "Open File", "", "Matlab Files (*.m)"); + QMenu* fileMenu = menuBar()->addMenu(tr("&bat")); + + QDir dir(RecourceHelper::Get().GetBasePath() + "/bat"); + QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks); + for (int i = 0; i < fileInfoList.size(); i++) + { + QFileInfo fileInfo = fileInfoList[i]; + QString strSuff = fileInfo.suffix(); + strSuff = strSuff.toLower(); + if (strSuff == "bat") + { + QAction* fileAction = new QAction(fileInfo.fileName(), this); + fileMenu->addAction(fileAction); + + connect(fileAction, &QAction::triggered, this, &CodeEdtUI::openMainFile); + } + } } + { + QMenu* ctrlMenu = menuBar()->addMenu(tr("&Control")); + + QAction* runAction = new QAction(tr("&Run"), this); + ctrlMenu->addAction(runAction); + + QAction* saveAction = new QAction(tr("&Save"), this); + ctrlMenu->addAction(saveAction); + + connect(runAction, &QAction::triggered, this, &CodeEdtUI::runFile); + connect(saveAction, &QAction::triggered, this, &CodeEdtUI::saveFile); + } +} + +void CodeEdtUI::openMainFile() { + + QAction* fileAction = (QAction*)(sender()); + QString fileName = RecourceHelper::Get().GetBasePath() + "/bat/" + fileAction->text(); //QFileDialog::getOpenFileName(this, "Open File", "", "Matlab Files (*.bat)"); + if (!fileName.isEmpty()) { QFile file(fileName); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { editor->clear(); QTextStream in(&file); + in.setCodec("utf-8"); editor->setPlainText(in.readAll()); file.close(); } } + + m_strCurOpenFile = fileName; } void CodeEdtUI::openLDFile() @@ -133,12 +176,37 @@ void CodeEdtUI::openSeekerSimFile() } } +void CodeEdtUI::runFile() +{ + if (!m_strCurOpenFile.isEmpty()) + { + saveFile(); + + // 创建QProcess对象 + QProcess process; + + // 启动批处理文件 + process.start(m_strCurOpenFile); + + // 等待过程完成 + process.waitForFinished(); + + // 获取输出 + QString output = process.readAllStandardOutput(); + QString errorOutput = process.readAllStandardError(); + + // 打印输出 + qDebug() << "Output:" << output; + qDebug() << "Error Output:" << errorOutput; + } +} + void CodeEdtUI::saveFile() { - QString fileName = QFileDialog::getSaveFileName(this, "Save File", "", "Matlab Files (*.m)"); - if (!fileName.isEmpty()) { - QFile file(fileName); + if (!m_strCurOpenFile.isEmpty()) { + QFile file(m_strCurOpenFile); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&file); + out.setCodec("utf-8"); out << editor->toPlainText(); file.close(); } diff --git a/src/ui/Layout/CodeEdtUI.h b/src/ui/Layout/CodeEdtUI.h index 746150a3..95225718 100644 --- a/src/ui/Layout/CodeEdtUI.h +++ b/src/ui/Layout/CodeEdtUI.h @@ -11,10 +11,13 @@ public: CodeEdtUI(QWidget *parent = Q_NULLPTR); void AttachDock(class DockWidget* dockWidget); + void InitBat(); + protected slots: void openMainFile(); void openLDFile(); void openSeekerSimFile(); + void runFile(); void saveFile(); diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index 3b5e1d34..a1ee2269 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -214,9 +214,9 @@ void MainWindow::InitUI() { OsgViewer::Get().Initialize(); OsgViewer::Get().OnFrame(); -#if 0 - MatlabObject* mtlb = new MatlabObject; - mtlb->RunMatlabFile("D:\\DYT\\TestGUI\\TestGUI\\LDPlatformTest.m"); +#if 1 + // MatlabObject* mtlb = new MatlabObject; + MatlabObject::GetInstance()->RunMatlabFile(""); #endif // 1 } diff --git a/src/ui/Matlab/MatlabObject.cpp b/src/ui/Matlab/MatlabObject.cpp index 334347c1..8db108f7 100644 --- a/src/ui/Matlab/MatlabObject.cpp +++ b/src/ui/Matlab/MatlabObject.cpp @@ -1,7 +1,9 @@ #include "MatlabObject.h" #include - +#include +#include +#include #include "engine.h" MatlabObject::MatlabObject(QObject *parent) @@ -15,6 +17,16 @@ MatlabObject::~MatlabObject() } +MatlabObject* MatlabObject::m_pInstance = nullptr; +MatlabObject* MatlabObject::GetInstance() +{ + if (!m_pInstance) + { + m_pInstance = new MatlabObject; + } + return m_pInstance; +} + void MatlabObject::RunMatlabFile(const QString& strFile) { QString strMatlabRun = QString("run('%1')").arg(strFile); @@ -23,11 +35,27 @@ void MatlabObject::RunMatlabFile(const QString& strFile) std::string strRun = code->fromUnicode(strMatlabRun.toUtf8().data()).data(); Engine* ep; - if (!(ep = engOpen("\0"))) { + if (!(ep = engOpen(nullptr))) { fprintf(stderr, "\nCan't start MATLAB engine\n"); return; // EXIT_FAILURE; } - engClose(ep); + if (!strFile.isEmpty()) + { + engEvalString(ep, strRun.c_str()); + } + + + /*engEvalString(ep, "shu=666666;"); + engEvalString(ep, "fileItgt1 = fopen('tgt1.txt', 'w');"); + engEvalString(ep, "if fileItgt1 == -1"); + engEvalString(ep, " error('无法打开文件');"); + engEvalString(ep, "end"); + + engEvalString(ep, "fprintf(fileItgt1, '%f ', shu);"); + engEvalString(ep, "fprintf(fileItgt1, '\n');"); + engEvalString(ep, "fclose(fileItgt1);");*/ + + engClose(ep); } diff --git a/src/ui/Matlab/MatlabObject.h b/src/ui/Matlab/MatlabObject.h index c0c960d8..7eb62526 100644 --- a/src/ui/Matlab/MatlabObject.h +++ b/src/ui/Matlab/MatlabObject.h @@ -10,9 +10,13 @@ public: MatlabObject(QObject *parent=nullptr); ~MatlabObject(); + static MatlabObject* MatlabObject::GetInstance(); + void RunMatlabFile(const QString& strFile); protected: //std::u16string string2u16string(std::string& str); + + static MatlabObject* MatlabObject::m_pInstance; }; diff --git a/src/ui/Menu/ChartPlotMenu.cpp b/src/ui/Menu/ChartPlotMenu.cpp index b90fcc36..7d4d9407 100644 --- a/src/ui/Menu/ChartPlotMenu.cpp +++ b/src/ui/Menu/ChartPlotMenu.cpp @@ -24,13 +24,14 @@ void ChartPlotMenu::InitMenu() connect(ui.toolButton, &QToolButton::clicked, this, [=] { if (WorkSpaceManager::Get().GetCurrent()) { - MatlabObject mtlb; + QString strFile = WorkSpaceManager::Get().GetCurrent()->GetSimMatlab(); if (!strFile.isEmpty()) { strFile = RecourceHelper::Get().GetBasePath() + "/" + WorkSpaceManager::Get().GetCurrent()->GetSimMatlab(); - mtlb.RunMatlabFile(strFile); + //MatlabObject::GetInstance()->RunMatlabFile("D:\\DYT\\TestGUI\\xierutest.m"); + MatlabObject::GetInstance()->RunMatlabFile(strFile); } else { diff --git a/src/ui/Menu/ChartPlotMenu.h b/src/ui/Menu/ChartPlotMenu.h index e7212410..036971dd 100644 --- a/src/ui/Menu/ChartPlotMenu.h +++ b/src/ui/Menu/ChartPlotMenu.h @@ -21,5 +21,4 @@ signals: private: Ui::ChartPlotMenuClass ui; - }; diff --git a/src/ui/PropertyBrowser/qtpropertymanager.cpp b/src/ui/PropertyBrowser/qtpropertymanager.cpp index 8d61fec7..1c9bcb9b 100644 --- a/src/ui/PropertyBrowser/qtpropertymanager.cpp +++ b/src/ui/PropertyBrowser/qtpropertymanager.cpp @@ -7933,6 +7933,26 @@ void QtWorkspacePropertyManagerPrivate::slotPropertyDestroyed(QtProperty* proper m_simMatlabToPropery[subProp] = 0; m_simMatlabToPropery.remove(property); } + + if (QtProperty* subProp = m_matlabParamToPropery.value(property, nullptr)) { + m_matlabParamToPropery[subProp] = 0; + m_matlabParamToPropery.remove(property); + } + + if (QtProperty* subProp = m_waveToPropery.value(property, nullptr)) { + m_waveToPropery[subProp] = 0; + m_waveToPropery.remove(property); + } + + if (QtProperty* subProp = m_reportToPropery.value(property, nullptr)) { + m_reportToPropery[subProp] = 0; + m_reportToPropery.remove(property); + } + + if (QtProperty* subProp = m_rdToPropery.value(property, nullptr)) { + m_rdToPropery[subProp] = 0; + m_rdToPropery.remove(property); + } } QtWorkspacePropertyManager::QtWorkspacePropertyManager(QObject* parent) @@ -8067,7 +8087,7 @@ void QtWorkspacePropertyManager::initializeProperty(QtProperty* property) { prop->setPropertyName(tr("MatlabParam")); d_ptr->m_filesProperyManager->setValueOnly(prop, val.GetMatlabParam()); d_ptr->m_properyToMatlabParam[property] = prop; - d_ptr->m_simMatlabToPropery[prop] = property; + d_ptr->m_matlabParamToPropery[prop] = property; property->addSubProperty(prop); prop = d_ptr->m_filesProperyManager->addProperty();