Implementation using python invocation using qprocess
This commit is contained in:
parent
cd67e8ddd7
commit
66937e2cfc
|
@ -28,6 +28,7 @@ find_package(PkgConfig REQUIRED)
|
|||
pkg_check_modules(EISMULIPLEXER REQUIRED eismuliplexer)
|
||||
find_package(Qt6 REQUIRED COMPONENTS Widgets)
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core)
|
||||
# find_package(Python3 REQUIRED) - Removed since we're using QProcess instead of embedding Python
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
|
@ -45,11 +46,16 @@ add_executable(${PROJECT_NAME}
|
|||
multiplexer.cpp
|
||||
triggerwidget.cpp
|
||||
triggerwidget.h
|
||||
pythonrunner.cpp
|
||||
pythonrunner.h
|
||||
pythonembed.cpp
|
||||
)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE ON)
|
||||
target_compile_options(${PROJECT_NAME} PUBLIC "-Wall")
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC QCodeEditor)
|
||||
# target_include_directories(${PROJECT_NAME} PRIVATE ${Python3_INCLUDE_DIRS}) - Removed since we're using QProcess instead of embedding Python
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Widgets Qt6::Core ${EISMULIPLEXER_LIBRARIES} QCodeEditor)
|
||||
# ${Python3_LIBRARIES} - Removed since we're using QProcess instead of embedding Python
|
||||
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/resources/eismultiplexerqt.png PROPERTIES QT_RESOURCE_ALIAS eismultiplexerqt.png)
|
||||
qt_add_resources(${PROJECT_NAME} "resources" PREFIX "/" FILES ${CMAKE_CURRENT_SOURCE_DIR}/resources/eismultiplexerqt.png )
|
||||
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
|
||||
|
|
|
@ -15,7 +15,8 @@ MainWindow::MainWindow(QWidget *parent):
|
|||
codeEditor(this),
|
||||
pythonOutput(this),
|
||||
currentFilePath(""),
|
||||
isFileModified(false)
|
||||
isFileModified(false),
|
||||
pythonRunner(nullptr)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
enumerateDevices();
|
||||
|
@ -49,6 +50,11 @@ MainWindow::MainWindow(QWidget *parent):
|
|||
isFileModified = true;
|
||||
updateStatus();
|
||||
});
|
||||
|
||||
// Connect Run and Stop buttons
|
||||
pythonRunner = new PythonRunner(&pythonOutput, this);
|
||||
connect(ui->pushButtonRun, &QPushButton::clicked, this, &MainWindow::onPushButtonRunClicked);
|
||||
connect(ui->pushButtonStop, &QPushButton::clicked, this, &MainWindow::onPushButtonStopClicked);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
|
@ -226,6 +232,19 @@ void MainWindow::enumerateDevices()
|
|||
generateExample();
|
||||
}
|
||||
|
||||
void MainWindow::onPushButtonRunClicked() {
|
||||
QString scriptContent = codeEditor.toPlainText();
|
||||
pythonRunner->runScript(scriptContent);
|
||||
ui->pushButtonRun->setEnabled(false);
|
||||
ui->pushButtonStop->setEnabled(true);
|
||||
}
|
||||
|
||||
void MainWindow::onPushButtonStopClicked() {
|
||||
pythonRunner->stopScript();
|
||||
ui->pushButtonRun->setEnabled(true);
|
||||
ui->pushButtonStop->setEnabled(false);
|
||||
}
|
||||
|
||||
void MainWindow::generateExample()
|
||||
{
|
||||
QString example =
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
#include "channelwidget.h"
|
||||
#include "triggerwidget.h"
|
||||
#include "pythonrunner.h"
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
|
@ -27,6 +28,7 @@ class MainWindow : public QMainWindow
|
|||
QPythonHighlighter highligter;
|
||||
QPythonCompleter completer;
|
||||
QTextEdit pythonOutput;
|
||||
PythonRunner* pythonRunner;
|
||||
|
||||
signals:
|
||||
void channelStateChanged(uint16_t device, uint16_t channel);
|
||||
|
@ -39,6 +41,8 @@ private slots:
|
|||
void onActionOpenTriggered();
|
||||
void onActionSaveTriggered();
|
||||
void onActionSaveAsTriggered();
|
||||
void onPushButtonRunClicked();
|
||||
void onPushButtonStopClicked();
|
||||
|
||||
private:
|
||||
void enumerateDevices();
|
||||
|
|
61
pythonembed.cpp
Normal file
61
pythonembed.cpp
Normal file
|
@ -0,0 +1,61 @@
|
|||
|
||||
|
||||
#include "pythonembed.h"
|
||||
#include <QDebug>
|
||||
|
||||
PythonEmbed::PythonEmbed(QTextEdit* outputWidget, QObject* parent)
|
||||
: QObject(parent), m_outputWidget(outputWidget), m_process(nullptr) {
|
||||
m_process = new QProcess(this);
|
||||
connect(m_process, &QProcess::readyReadStandardOutput, this, &PythonEmbed::onOutputAvailable);
|
||||
connect(m_process, &QProcess::readyReadStandardError, this, &PythonEmbed::onErrorAvailable);
|
||||
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &PythonEmbed::onProcessFinished);
|
||||
}
|
||||
|
||||
PythonEmbed::~PythonEmbed() {
|
||||
if (m_process) {
|
||||
m_process->terminate();
|
||||
m_process->waitForFinished(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void PythonEmbed::runScript(const QString& scriptContent) {
|
||||
if (m_process->state() == QProcess::Running) {
|
||||
m_process->terminate();
|
||||
m_process->waitForFinished(1000);
|
||||
}
|
||||
|
||||
m_outputWidget->clear();
|
||||
m_outputWidget->append("Python 3.11.2 (main, Oct 5 2023, 17:20:59) [GCC 11.4.0] on linux\n");
|
||||
m_outputWidget->append("Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n\n");
|
||||
|
||||
m_process->start("python3", QStringList() << "-c" << scriptContent);
|
||||
}
|
||||
|
||||
void PythonEmbed::stopScript() {
|
||||
if (m_process && m_process->state() == QProcess::Running) {
|
||||
m_process->terminate();
|
||||
}
|
||||
}
|
||||
|
||||
void PythonEmbed::onOutputAvailable() {
|
||||
QByteArray output = m_process->readAllStandardOutput();
|
||||
m_outputWidget->append(output);
|
||||
m_outputWidget->moveCursor(QTextCursor::End);
|
||||
}
|
||||
|
||||
void PythonEmbed::onErrorAvailable() {
|
||||
QByteArray error = m_process->readAllStandardError();
|
||||
m_outputWidget->append(error);
|
||||
m_outputWidget->moveCursor(QTextCursor::End);
|
||||
}
|
||||
|
||||
void PythonEmbed::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {
|
||||
if (exitStatus == QProcess::NormalExit && exitCode != 0) {
|
||||
m_outputWidget->append(QString("Process exited with code %1\n").arg(exitCode));
|
||||
} else if (exitStatus == QProcess::CrashExit) {
|
||||
m_outputWidget->append("Python process crashed\n");
|
||||
}
|
||||
}
|
||||
|
||||
#include "pythonembed.moc"
|
||||
|
31
pythonembed.h
Normal file
31
pythonembed.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
|
||||
|
||||
#ifndef PYTHONEMBED_H
|
||||
#define PYTHONEMBED_H
|
||||
|
||||
#include <QTextEdit>
|
||||
#include <QProcess>
|
||||
#include <QObject>
|
||||
|
||||
class PythonEmbed : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
PythonEmbed(QTextEdit* outputWidget, QObject* parent = nullptr);
|
||||
~PythonEmbed();
|
||||
|
||||
void runScript(const QString& scriptContent);
|
||||
void stopScript();
|
||||
|
||||
private slots:
|
||||
void onOutputAvailable();
|
||||
void onErrorAvailable();
|
||||
void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
|
||||
|
||||
private:
|
||||
QTextEdit* m_outputWidget;
|
||||
QProcess* m_process;
|
||||
};
|
||||
|
||||
#endif // PYTHONEMBED_H
|
||||
|
26
pythonrunner.cpp
Normal file
26
pythonrunner.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
|
||||
#include "pythonrunner.h"
|
||||
#include "pythonembed.h"
|
||||
|
||||
PythonRunner::PythonRunner(QTextEdit* outputWidget, QObject* parent)
|
||||
: QObject(parent), m_outputWidget(outputWidget), m_pythonEmbed(nullptr) {
|
||||
m_pythonEmbed = new PythonEmbed(m_outputWidget, this);
|
||||
}
|
||||
|
||||
PythonRunner::~PythonRunner() {
|
||||
delete m_pythonEmbed;
|
||||
}
|
||||
|
||||
void PythonRunner::runScript(const QString& scriptContent) {
|
||||
if (m_pythonEmbed) {
|
||||
m_pythonEmbed->runScript(scriptContent);
|
||||
}
|
||||
}
|
||||
|
||||
void PythonRunner::stopScript() {
|
||||
if (m_pythonEmbed) {
|
||||
m_pythonEmbed->stopScript();
|
||||
}
|
||||
}
|
||||
|
||||
#include "pythonrunner.moc"
|
26
pythonrunner.h
Normal file
26
pythonrunner.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
|
||||
#ifndef PYTHONRUNNER_H
|
||||
#define PYTHONRUNNER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QTextEdit>
|
||||
|
||||
class PythonEmbed;
|
||||
|
||||
class PythonRunner : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit PythonRunner(QTextEdit* outputWidget, QObject* parent = nullptr);
|
||||
~PythonRunner();
|
||||
|
||||
public slots:
|
||||
void runScript(const QString& scriptContent);
|
||||
void stopScript();
|
||||
|
||||
private:
|
||||
QTextEdit* m_outputWidget;
|
||||
PythonEmbed* m_pythonEmbed;
|
||||
};
|
||||
|
||||
#endif // PYTHONRUNNER_H
|
Loading…
Reference in a new issue