Skip to content

Commit 886baf8

Browse files
committed
Add stop execution button. Sleeps on all API calls.
Signed-off-by: Bhathiya Perera <[email protected]>
1 parent 16eb721 commit 886baf8

File tree

11 files changed

+85
-15
lines changed

11 files changed

+85
-15
lines changed

About.htm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<br />
77
<b>Credits</b>
88
<ul>
9-
<li>Qt 5.3.2</li>
10-
<li>Python 3.4.2</li>
9+
<li>Qt 5.9.0</li>
10+
<li>Python 3.6.1</li>
1111
<li>Frankie Simon's Python Syntax Highlight Code (Modified)</li>
1212
<li>Mateusz Loskot's Embedding Code (Modified)</li>
1313
<li>Train Icon from awicons.com</li>

Features/snippets.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Features/snippets.h"
22
#include <iostream>
3+
#include <QDataStream>
34

45
Snippets::Snippets(QObject *parent) : QObject(parent), m_snippets(nullptr) {
56
bool success;

Icons/Stop.png

6.09 KB
Loading

PyRun.pro

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ HEADERS += UI/mainview.h \
3535

3636
FORMS += UI/mainview.ui
3737

38-
QMAKE_CXXFLAGS += -std=c++11 -Wpedantic
39-
4038
RESOURCES += \
4139
PyRunResources.qrc
4240

4341
RC_FILE = WindowsResources/win_rsrc.rc
4442

4543

46-
win32: LIBS += -LC:\Python34\libs\ -lpython34
47-
win32: INCLUDEPATH += C:\Python34\include
48-
win32: DEPENDPATH += C:\Python34\include
44+
win32: LIBS += -LC:\Users\Bhathiya\AppData\Local\Programs\Python\Python36-32\libs\ -lpython36
45+
win32: INCLUDEPATH += C:\Users\Bhathiya\AppData\Local\Programs\Python\Python36-32\include
46+
win32: DEPENDPATH += C:\Users\Bhathiya\AppData\Local\Programs\Python\Python36-32\include
47+
48+
greaterThan(QT_MAJOR_VERSION, 4){
49+
CONFIG += c++11
50+
} else {
51+
QMAKE_CXXFLAGS += -std=c++11 -Wpedantic
52+
}

PyRunResources.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<file>Icons/TuteLoad.png</file>
1919
<file>Icons/TuteFail.png</file>
2020
<file>Icons/TutePass.png</file>
21+
<file>Icons/Stop.png</file>
2122
</qresource>
2223
<qresource prefix="/"/>
2324
</RCC>

PythonAccess/emb.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ PyObject *ApiGetInput(PyObject *self, PyObject *args) {
134134
if (!PyArg_ParseTuple(args, ":numargs"))
135135
return NULL;
136136

137+
QThread::msleep(10);
137138
return Py_BuildValue("s", mainView->GetInput().toStdString().c_str());
138139
}
139140
PyObject *ApiSetInput(PyObject *self, PyObject *args) {
@@ -142,20 +143,23 @@ PyObject *ApiSetInput(PyObject *self, PyObject *args) {
142143
return NULL;
143144

144145
emit worker->SetInput(QString(data));
146+
QThread::msleep(10);
145147

146148
return Py_BuildValue("i", 0);
147149
}
148150
PyObject *ApiGetAppPath(PyObject *self, PyObject *args) {
149151
if (!PyArg_ParseTuple(args, ":numargs"))
150152
return NULL;
151153

154+
QThread::msleep(10);
152155
return Py_BuildValue(
153156
"s", QCoreApplication::applicationDirPath().toStdString().c_str());
154157
}
155158
PyObject *ApiGetOutput(PyObject *self, PyObject *args) {
156159
if (!PyArg_ParseTuple(args, ":numargs"))
157160
return NULL;
158161

162+
QThread::msleep(10);
159163
return Py_BuildValue("s", mainView->GetOutput().toStdString().c_str());
160164
}
161165

@@ -165,13 +169,15 @@ PyObject *ApiSetOutput(PyObject *self, PyObject *args) {
165169
return NULL;
166170

167171
emit worker->SetOutput(QString(data));
172+
QThread::msleep(10);
168173

169174
return Py_BuildValue("i", 0);
170175
}
171176
PyObject *ApiGetCode(PyObject *self, PyObject *args) {
172177
if (!PyArg_ParseTuple(args, ":numargs"))
173178
return NULL;
174179

180+
QThread::msleep(10);
175181
return Py_BuildValue("s", mainView->GetCode().toStdString().c_str());
176182
}
177183

@@ -181,6 +187,7 @@ PyObject *ApiSetCode(PyObject *self, PyObject *args) {
181187
return NULL;
182188

183189
emit worker->SetCode(QString(data));
190+
QThread::msleep(10);
184191

185192
return Py_BuildValue("i", 0);
186193
}
@@ -191,6 +198,7 @@ PyObject *ApiSetSearchRegex(PyObject *self, PyObject *args) {
191198
return NULL;
192199

193200
emit worker->SetSearchRegex(QString(data));
201+
QThread::msleep(10);
194202

195203
return Py_BuildValue("i", 0);
196204
}
@@ -201,6 +209,7 @@ PyObject *ApiWriteOutput(PyObject *self, PyObject *args) {
201209
return NULL;
202210

203211
emit worker->WriteOutput(QString(data));
212+
QThread::msleep(10);
204213

205214
return Py_BuildValue("i", 0);
206215
}

PythonAccess/pythonworker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ void PythonWorker::RunPython(const QString &startme, const QString &code) {
1212

1313
emb::StdOutWriteType write = [this](std::string s) {
1414
emit this->WriteOutput(QString::fromStdString(s));
15+
QThread::msleep(10);
1516
};
1617

1718
emb::SetStdout(write);

PythonAccess/pythonworker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define PYTHONWORKER_H
33

44
#include <QObject>
5+
#include <QThread>
56

67
class PythonWorker : public QObject {
78
Q_OBJECT

UI/mainview.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ void MainView::SetupPython() {
2323
emb::setMainView(this);
2424
PythonWorker *worker = new PythonWorker();
2525
emb::setWorker(worker);
26-
worker->moveToThread(&m_workerThread);
27-
connect(&m_workerThread, &QThread::finished, worker, &QObject::deleteLater);
26+
m_workerThread = new QThread();
27+
worker->moveToThread(m_workerThread);
28+
connect(m_workerThread, &QThread::finished, worker, &QObject::deleteLater);
2829
connect(this, &MainView::operate, worker, &PythonWorker::RunPython);
2930
connect(worker, &PythonWorker::WriteOutput, this, &MainView::WriteOutput);
3031
connect(worker, &PythonWorker::SetCode, this, &MainView::SetCode);
@@ -35,14 +36,15 @@ void MainView::SetupPython() {
3536
connect(worker, &PythonWorker::EndPythonRun, this, &MainView::EndPythonRun);
3637
connect(worker, &PythonWorker::SetSearchRegex, this,
3738
&MainView::SetSearchRegex);
38-
m_workerThread.start();
39+
m_workerThread->start();
3940
}
4041
// Buttons to enable when you execute a python script
4142
void MainView::StartPythonRun() {
4243
ui->btnRun->setEnabled(false);
4344
ui->btnRunSnippet->setEnabled(false);
4445
ui->btnRunSnippetFromCombo->setEnabled(false);
4546
ui->dwTutorial->setEnabled(false);
47+
ui->btnStopPython->setEnabled(true);
4648
}
4749
// End python script
4850
void MainView::EndPythonRun() {
@@ -56,6 +58,7 @@ void MainView::EndPythonRun() {
5658
ui->btnRunSnippet->setEnabled(true);
5759
ui->btnRunSnippetFromCombo->setEnabled(true);
5860
ui->dwTutorial->setEnabled(true);
61+
ui->btnStopPython->setEnabled(false);
5962
}
6063
// Util function: Confirm message box
6164
bool MainView::Confirm(const QString &what) {
@@ -169,8 +172,9 @@ MainView::~MainView() {
169172
settings.setValue(KEY_NOTESBOX, ui->txtNotes->toPlainText());
170173
settings.setValue(KEY_FONT, ui->fntCombo->currentText());
171174
settings.setValue(KEY_FONTSIZE, ui->cmbFontSize->currentIndex());
172-
m_workerThread.quit();
173-
m_workerThread.wait();
175+
m_workerThread->quit();
176+
m_workerThread->wait();
177+
delete m_workerThread;
174178
delete ui;
175179
delete m_tute;
176180
}
@@ -542,3 +546,12 @@ void MainView::on_btnTuteMark_clicked()
542546

543547
emit operate(m_startMe, ui->txtCode->toPlainText());
544548
}
549+
550+
void MainView::on_btnStopPython_clicked()
551+
{
552+
m_workerThread->terminate();
553+
this->EndPythonRun();
554+
m_workerThread->wait();
555+
delete m_workerThread;
556+
SetupPython();
557+
}

UI/mainview.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ private slots:
8686
void on_btnTuteLoad_clicked();
8787
void on_btnTuteMark_clicked();
8888

89+
void on_btnStopPython_clicked();
90+
8991
private:
9092
const QString FILETYPES_PYTHON = tr("Python Code (*.py);;All files (*.*)");
9193
const QString FILETYPES_OTHER = tr("Text files (*.txt);;All files (*.*)");
9294
const QString FILETYPES_TUTE = tr("Tutorial files (*.tute);;All files (*.*)");
9395

94-
QThread m_workerThread;
96+
QThread* m_workerThread;
9597
Ui::MainView *ui;
9698
PythonSyntaxHighlighter *m_highlighterCodeArea;
9799
PythonSyntaxHighlighter *m_highlighterSnippetArea;

0 commit comments

Comments
 (0)