修改matlab执行程序

This commit is contained in:
15712809671 2025-01-19 23:11:20 +08:00
parent b900683dbe
commit 5105c48f53
8 changed files with 156 additions and 33 deletions

View File

@ -9,7 +9,10 @@
#include <QVBoxLayout>
#include <QFont>
#include <qdebug.h>
#include <QDir>
#include <QProcess>
#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,12 +76,46 @@ 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);
@ -83,10 +123,13 @@ void CodeEdtUI::openMainFile() {
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();
}

View File

@ -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();

View File

@ -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
}

View File

@ -1,7 +1,9 @@
#include "MatlabObject.h"
#include <QTextCodec>
#include <QFile>
#include <QTextStream>
#include <qDebug>
#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;
}
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);
}

View File

@ -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;
};

View File

@ -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
{

View File

@ -21,5 +21,4 @@ signals:
private:
Ui::ChartPlotMenuClass ui;
};

View File

@ -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();