Cleanup python script running
This commit is contained in:
parent
87db38b08e
commit
1eac3f6a83
|
@ -48,7 +48,6 @@ add_executable(${PROJECT_NAME}
|
||||||
triggerwidget.h
|
triggerwidget.h
|
||||||
pythonrunner.cpp
|
pythonrunner.cpp
|
||||||
pythonrunner.h
|
pythonrunner.h
|
||||||
pythonembed.cpp
|
|
||||||
)
|
)
|
||||||
set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE ON)
|
set_target_properties(${PROJECT_NAME} PROPERTIES WIN32_EXECUTABLE ON)
|
||||||
target_compile_options(${PROJECT_NAME} PUBLIC "-Wall")
|
target_compile_options(${PROJECT_NAME} PUBLIC "-Wall")
|
||||||
|
|
|
@ -14,9 +14,9 @@ MainWindow::MainWindow(QWidget *parent):
|
||||||
ui(new Ui::MainWindow),
|
ui(new Ui::MainWindow),
|
||||||
codeEditor(this),
|
codeEditor(this),
|
||||||
pythonOutput(this),
|
pythonOutput(this),
|
||||||
|
pythonRunner(&pythonOutput),
|
||||||
currentFilePath(""),
|
currentFilePath(""),
|
||||||
isFileModified(false),
|
isFileModified(false)
|
||||||
pythonRunner(nullptr)
|
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
enumerateDevices();
|
enumerateDevices();
|
||||||
|
@ -52,9 +52,9 @@ MainWindow::MainWindow(QWidget *parent):
|
||||||
});
|
});
|
||||||
|
|
||||||
// Connect Run and Stop buttons
|
// Connect Run and Stop buttons
|
||||||
pythonRunner = new PythonRunner(&pythonOutput, this);
|
connect(ui->pushButtonRun, &QPushButton::clicked, this, &MainWindow::runScript);
|
||||||
connect(ui->pushButtonRun, &QPushButton::clicked, this, &MainWindow::onPushButtonRunClicked);
|
connect(ui->pushButtonStop, &QPushButton::clicked, this, &MainWindow::stopScript);
|
||||||
connect(ui->pushButtonStop, &QPushButton::clicked, this, &MainWindow::onPushButtonStopClicked);
|
connect(&pythonRunner, &PythonRunner::scriptFinished, this, &MainWindow::stopScript);
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
|
@ -232,17 +232,21 @@ void MainWindow::enumerateDevices()
|
||||||
generateExample();
|
generateExample();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::onPushButtonRunClicked() {
|
void MainWindow::runScript() {
|
||||||
QString scriptContent = codeEditor.toPlainText();
|
QString scriptContent = codeEditor.toPlainText();
|
||||||
pythonRunner->runScript(scriptContent);
|
pythonRunner.runScript(scriptContent);
|
||||||
ui->pushButtonRun->setEnabled(false);
|
ui->pushButtonRun->setEnabled(false);
|
||||||
ui->pushButtonStop->setEnabled(true);
|
ui->pushButtonStop->setEnabled(true);
|
||||||
|
codeEditor.setEnabled(false);
|
||||||
|
ui->scrollArea->setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::onPushButtonStopClicked() {
|
void MainWindow::stopScript() {
|
||||||
pythonRunner->stopScript();
|
pythonRunner.stopScript();
|
||||||
ui->pushButtonRun->setEnabled(true);
|
ui->pushButtonRun->setEnabled(true);
|
||||||
ui->pushButtonStop->setEnabled(false);
|
ui->pushButtonStop->setEnabled(false);
|
||||||
|
codeEditor.setEnabled(true);
|
||||||
|
ui->scrollArea->setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::generateExample()
|
void MainWindow::generateExample()
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
|
|
||||||
|
|
||||||
#ifndef MAINWINDOW_H
|
#ifndef MAINWINDOW_H
|
||||||
#define MAINWINDOW_H
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
|
@ -8,6 +6,7 @@
|
||||||
#include <QCodeEditor>
|
#include <QCodeEditor>
|
||||||
#include <QPythonCompleter>
|
#include <QPythonCompleter>
|
||||||
#include <QPythonHighlighter>
|
#include <QPythonHighlighter>
|
||||||
|
#include <QProgressBar>
|
||||||
|
|
||||||
#include "channelwidget.h"
|
#include "channelwidget.h"
|
||||||
#include "triggerwidget.h"
|
#include "triggerwidget.h"
|
||||||
|
@ -28,7 +27,7 @@ class MainWindow : public QMainWindow
|
||||||
QPythonHighlighter highligter;
|
QPythonHighlighter highligter;
|
||||||
QPythonCompleter completer;
|
QPythonCompleter completer;
|
||||||
QTextEdit pythonOutput;
|
QTextEdit pythonOutput;
|
||||||
PythonRunner* pythonRunner;
|
PythonRunner pythonRunner;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void channelStateChanged(uint16_t device, uint16_t channel);
|
void channelStateChanged(uint16_t device, uint16_t channel);
|
||||||
|
@ -41,8 +40,8 @@ private slots:
|
||||||
void onActionOpenTriggered();
|
void onActionOpenTriggered();
|
||||||
void onActionSaveTriggered();
|
void onActionSaveTriggered();
|
||||||
void onActionSaveAsTriggered();
|
void onActionSaveAsTriggered();
|
||||||
void onPushButtonRunClicked();
|
void runScript();
|
||||||
void onPushButtonStopClicked();
|
void stopScript();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void enumerateDevices();
|
void enumerateDevices();
|
||||||
|
|
|
@ -1,61 +0,0 @@
|
||||||
|
|
||||||
|
|
||||||
#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"
|
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
|
|
||||||
|
|
||||||
#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
|
|
||||||
|
|
|
@ -1,26 +1,54 @@
|
||||||
|
|
||||||
#include "pythonrunner.h"
|
#include "pythonrunner.h"
|
||||||
#include "pythonembed.h"
|
#include <QDebug>
|
||||||
|
|
||||||
PythonRunner::PythonRunner(QTextEdit* outputWidget, QObject* parent)
|
PythonRunner::PythonRunner(QTextEdit* outputWidget, QObject* parent)
|
||||||
: QObject(parent), m_outputWidget(outputWidget), m_pythonEmbed(nullptr) {
|
: QObject(parent), m_outputWidget(outputWidget), m_process(nullptr) {
|
||||||
m_pythonEmbed = new PythonEmbed(m_outputWidget, this);
|
m_process = new QProcess(this);
|
||||||
|
connect(m_process, &QProcess::readyReadStandardOutput, this, &PythonRunner::onOutputAvailable);
|
||||||
|
connect(m_process, &QProcess::readyReadStandardError, this, &PythonRunner::onErrorAvailable);
|
||||||
|
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &PythonRunner::onProcessFinished);
|
||||||
}
|
}
|
||||||
|
|
||||||
PythonRunner::~PythonRunner() {
|
PythonRunner::~PythonRunner() {
|
||||||
delete m_pythonEmbed;
|
if (m_process) {
|
||||||
|
m_process->terminate();
|
||||||
|
m_process->waitForFinished(1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PythonRunner::runScript(const QString& scriptContent) {
|
void PythonRunner::runScript(const QString& scriptContent) {
|
||||||
if (m_pythonEmbed) {
|
if (m_process->state() == QProcess::Running) {
|
||||||
m_pythonEmbed->runScript(scriptContent);
|
m_process->terminate();
|
||||||
|
m_process->waitForFinished(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_outputWidget->clear();
|
||||||
|
m_process->start("python3", QStringList() << "-u" << "-c" << scriptContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PythonRunner::stopScript() {
|
void PythonRunner::stopScript() {
|
||||||
if (m_pythonEmbed) {
|
if (m_process && m_process->state() == QProcess::Running) {
|
||||||
m_pythonEmbed->stopScript();
|
m_process->terminate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "pythonrunner.moc"
|
void PythonRunner::onOutputAvailable() {
|
||||||
|
QByteArray output = m_process->readAllStandardOutput();
|
||||||
|
m_outputWidget->append(output);
|
||||||
|
m_outputWidget->moveCursor(QTextCursor::End);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PythonRunner::onErrorAvailable() {
|
||||||
|
QByteArray error = m_process->readAllStandardError();
|
||||||
|
m_outputWidget->append(error);
|
||||||
|
m_outputWidget->moveCursor(QTextCursor::End);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PythonRunner::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 was stopped\n");
|
||||||
|
}
|
||||||
|
emit scriptFinished(exitCode);
|
||||||
|
}
|
||||||
|
|
|
@ -1,26 +1,28 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#ifndef PYTHONRUNNER_H
|
|
||||||
#define PYTHONRUNNER_H
|
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QTextEdit>
|
#include <QTextEdit>
|
||||||
|
#include <QProcess>
|
||||||
class PythonEmbed;
|
#include <QObject>
|
||||||
|
|
||||||
class PythonRunner : public QObject {
|
class PythonRunner : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit PythonRunner(QTextEdit* outputWidget, QObject* parent = nullptr);
|
PythonRunner(QTextEdit* outputWidget, QObject* parent = nullptr);
|
||||||
~PythonRunner();
|
~PythonRunner();
|
||||||
|
|
||||||
public slots:
|
|
||||||
void runScript(const QString& scriptContent);
|
void runScript(const QString& scriptContent);
|
||||||
void stopScript();
|
void stopScript();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void onOutputAvailable();
|
||||||
|
void onErrorAvailable();
|
||||||
|
void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void scriptFinished(int code);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QTextEdit* m_outputWidget;
|
QTextEdit* m_outputWidget;
|
||||||
PythonEmbed* m_pythonEmbed;
|
QProcess* m_process;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PYTHONRUNNER_H
|
|
||||||
|
|
Loading…
Reference in a new issue