Skip to content

Commit

Permalink
Multithreaded Execution
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed Nov 30, 2014
1 parent c87e6d4 commit ab42d7b
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 59 deletions.
37 changes: 37 additions & 0 deletions About.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<html>
<body>
<b> Express Python </b>
<br />
Written by Bhathiya Perera<br />
<br />
<b>Credits</b>
<ul>
<li>Qt 5.3.2</li>
<li>Python 3.4.2</li>
<li>Frankie Simon's Python Syntax Highlight Code (Modified)</li>
<li>Mateusz Loskot's Embedding Code (Modified)</li>
<li>Train Icon from awicons.com</li>
<li>All Other Icons from Open Icon Library</li>
</ul>
<br />
<b>License</b>
<pre>
Express Python
Copyright (C) 2014 Bhathiya Perera

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
</pre>
</body>
</html>
8 changes: 4 additions & 4 deletions CodeEditor/pythonsyntaxhighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
<< "\\("
<< "\\)"
<< "\\["
<< "]";
<< "\\]";

setStyles();

Expand Down Expand Up @@ -141,16 +141,16 @@ void PythonSyntaxHighlighter::initializeRules()
// 'class' followed by an identifier
rules.append(HighlightingRule("\\bclass\\b\\s*(\\w+)", 1, basicStyles.value("defclass")));

// From '#' until a newline
rules.append(HighlightingRule("#[^\\n]*", 0, basicStyles.value("comment")));

// Numeric literals
rules.append(HighlightingRule("\\b[+-]?[0-9]+[lL]?\\b", 0, basicStyles.value("numbers")));
rules.append(HighlightingRule("\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b", 0, basicStyles.value("numbers")));
rules.append(HighlightingRule("\\b[+-]?[0-9]+(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b", 0, basicStyles.value("numbers")));

// tab and space mixed
rules.append(HighlightingRule("[^\\n]*(?:\\t | \\t)[^\\n]*", 0, basicStyles.value("bugs")));

// From '#' until a newline
rules.append(HighlightingRule("#[^\\n]*", 0, basicStyles.value("comment")));
}

void PythonSyntaxHighlighter::highlightBlock(const QString& text)
Expand Down
2 changes: 2 additions & 0 deletions PyRun.pro
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ win32: LIBS += -L$$PWD/../Python34/libs/ -lpython34

INCLUDEPATH += $$PWD/../Python34/include
DEPENDPATH += $$PWD/../Python34/include

OTHER_FILES +=
1 change: 1 addition & 0 deletions PyRunResources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<file>Icons/Remove.png</file>
<file>Icons/PyRunImg.png</file>
<file>startme.py</file>
<file>About.htm</file>
</qresource>
</RCC>
15 changes: 11 additions & 4 deletions PythonAccess/emb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@
#include <functional>
#include <iostream>
#include <string>

#include "PythonAccess/emb.h"

namespace emb {
MainView* mainView;
PythonWorker* worker;
void setMainView(MainView* _mainView)
{
mainView = _mainView;
}
void setWorker(PythonWorker* _worker)
{
worker = _worker;
}
MainView* getMainView()
{
return mainView;

}
struct StdOut {
PyObject_HEAD
Expand Down Expand Up @@ -153,7 +160,7 @@ PyObject* ApiSetInput(PyObject* self, PyObject* args)
if (!PyArg_ParseTuple(args, "s", &data))
return NULL;

mainView->SetInput(QString(data));
emit worker->SetInput(QString(data));

return Py_BuildValue("i", 0);
}
Expand All @@ -178,7 +185,7 @@ PyObject* ApiSetOutput(PyObject* self, PyObject* args)
if (!PyArg_ParseTuple(args, "s", &data))
return NULL;

mainView->SetOutput(QString(data));
emit worker->SetOutput(QString(data));

return Py_BuildValue("i", 0);
}
Expand All @@ -196,7 +203,7 @@ PyObject* ApiSetCode(PyObject* self, PyObject* args)
if (!PyArg_ParseTuple(args, "s", &data))
return NULL;

mainView->SetCode(QString(data));
emit worker->SetCode(QString(data));

return Py_BuildValue("i", 0);
}
Expand All @@ -206,7 +213,7 @@ PyObject* ApiWriteOutput(PyObject* self, PyObject* args)
if (!PyArg_ParseTuple(args, "s", &data))
return NULL;

mainView->WriteOutput(QString(data));
emit worker->WriteOutput(QString(data));

return Py_BuildValue("i", 0);
}
Expand Down
2 changes: 2 additions & 0 deletions PythonAccess/emb.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cmath>
#include "Python.h"
#include "UI/mainview.h"
#include "PythonAccess/pythonworker.h"
namespace emb {


Expand All @@ -19,6 +20,7 @@ PyObject* ApiSetInput(PyObject* self, PyObject* args);
PyObject* ApiGetInput(PyObject* self, PyObject* args);
void ResetStdOut();
void setMainView(MainView* _mainView);
void setWorker(PythonWorker* _worker);
MainView* getMainView();
void SetStdout(StdOutWriteType write);
PyMODINIT_FUNC PyInitEmbConnect(void);
Expand Down
25 changes: 23 additions & 2 deletions PythonAccess/pythonworker.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
#include "PythonAccess/emb.h"
#include "pythonworker.h"

PythonWorker::PythonWorker(QObject *parent) :
QObject(parent)
PythonWorker::PythonWorker(QObject* parent)
: QObject(parent)
{
}

void PythonWorker::RunPython(const QString &startme, const QString& code)
{
emit StartPythonRun();
PyImport_AppendInittab("emb", emb::PyInitEmbConnect);
PyImport_AppendInittab("express_api", emb::PyInitApiConnection);
Py_Initialize();
PyImport_ImportModule("emb");

emb::StdOutWriteType write = [this](std::string s) {
emit this->WriteOutput(QString::fromStdString(s));
};

emb::SetStdout(write);
PyRun_SimpleString(startme.toStdString().c_str());
PyRun_SimpleString(code.toStdString().c_str());
emb::ResetStdOut();
Py_Finalize();
emit EndPythonRun();
}
10 changes: 9 additions & 1 deletion PythonAccess/pythonworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ class PythonWorker : public QObject
public:
explicit PythonWorker(QObject *parent = 0);

private:

signals:
void WriteOutput(QString result);
void SetInput(QString txt);
void SetOutput(QString txt);
void SetCode(QString txt);
void StartPythonRun();
void EndPythonRun();

public slots:

void RunPython(const QString &startme,const QString &code);
};

#endif // PYTHONWORKER_H
67 changes: 40 additions & 27 deletions UI/mainview.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <functional>
#include "PythonAccess/emb.h"
#include "PythonAccess/pythonworker.h"
#include "UI/mainview.h"
#include "ui_mainview.h"
#include <QSettings>
Expand All @@ -16,15 +17,40 @@ MainView::MainView(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainView)
{
emb::setMainView(this);
ui->setupUi(this);
SetupHighlighter();
LoadStartupScript();
LoadResources();
LoadSettings();
SetupPython();
}
void MainView::SetupPython()
{
emb::setMainView(this);
PythonWorker* worker = new PythonWorker();
emb::setWorker(worker);
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &MainView::operate, worker, &PythonWorker::RunPython);
connect(worker, &PythonWorker::WriteOutput, this, &MainView::WriteOutput);
connect(worker, &PythonWorker::SetCode, this, &MainView::SetCode);
connect(worker, &PythonWorker::SetInput, this, &MainView::SetInput);
connect(worker, &PythonWorker::SetOutput, this, &MainView::SetOutput);
connect(worker, &PythonWorker::StartPythonRun, this, &MainView::StartPythonRun);
connect(worker, &PythonWorker::EndPythonRun, this, &MainView::EndPythonRun);
workerThread.start();
}
void MainView::StartPythonRun()
{
ui->btnRun->setEnabled(false);
ui->btnRunSnippet->setEnabled(false);
}
void MainView::EndPythonRun()
{
ui->btnRun->setEnabled(true);
ui->btnRunSnippet->setEnabled(true);
}
void MainView::LoadSettings()
{

QSettings settings;
this->restoreState(settings.value(KEY_DOCK_LOCATIONS).toByteArray(), SAVE_STATE_VERSION);
this->restoreGeometry(settings.value(KEY_GEOMETRY).toByteArray());
Expand All @@ -39,7 +65,7 @@ void MainView::LoadSettings()
if (pos != -1) {
ui->fntCombo->setCurrentIndex(pos);
}
if (sizeIndex >= 0){
if (sizeIndex >= 0) {
ui->cmbFontSize->setCurrentIndex(sizeIndex);
}

Expand All @@ -57,14 +83,19 @@ void MainView::SetupHighlighter()
ui->txtCode->setFocus();
}

void MainView::LoadStartupScript()
void MainView::LoadResources()
{
bool success;
mStartMe = LoadFile(":/data/startme.py", success);

if (!success) {
QMessageBox::warning(this, tr(APP_NAME), tr("Loading startup script failed"));
}
mAbout = LoadFile(":/data/About.htm", success);
if (!success) {
mAbout = tr(APP_NAME " Written by Bhathiya Perera");
}

}
MainView::~MainView()
{
Expand All @@ -76,6 +107,8 @@ MainView::~MainView()
settings.setValue(KEY_OUTPUTBOX, this->GetOutput());
settings.setValue(KEY_FONT, ui->fntCombo->currentText());
settings.setValue(KEY_FONTSIZE, ui->cmbFontSize->currentIndex());
workerThread.quit();
workerThread.wait();
delete ui;
}

Expand Down Expand Up @@ -173,20 +206,7 @@ void MainView::WriteOutput(QString output)

void MainView::RunPythonCode(const QString& code)
{
PyImport_AppendInittab("emb", emb::PyInitEmbConnect);
PyImport_AppendInittab("expressApi", emb::PyInitApiConnection);
Py_Initialize();
PyImport_ImportModule("emb");

emb::StdOutWriteType write = [](std::string s) {
emb::getMainView()->WriteOutput(QString::fromStdString(s));
};

emb::SetStdout(write);
PyRun_SimpleString(mStartMe.toStdString().c_str());
PyRun_SimpleString(code.toStdString().c_str());
emb::ResetStdOut();
Py_Finalize();
emit operate(mStartMe, code);
}

void MainView::on_btnRun_clicked()
Expand Down Expand Up @@ -326,14 +346,7 @@ void MainView::on_btnAddSnippet_clicked()

void MainView::on_btnAbout_clicked()
{
QMessageBox::about(this, tr(APP_NAME), tr("<b>" APP_NAME "</b><br />"
"Written by Bhathiya Perera<br />"
"<br />"
"This Project Depends on<br />"
"Qt5.3, Python<br />"
"Frankie Simon's Python Syntax Highlight Code<br />"
"Mateusz Loskot's Embedding Code<br />"
"<br />"));
QMessageBox::about(this, tr(APP_NAME), mAbout);
}

void MainView::LoadSnippetsToCombo()
Expand Down
19 changes: 13 additions & 6 deletions UI/mainview.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <QFileDialog>
#include <QMainWindow>
#include <QInputDialog>

#include <QThread>
// internal
#include "CodeEditor/pythonsyntaxhighlighter.h"
#include "CodeEditor/codeeditor.h"
Expand All @@ -29,12 +29,8 @@ class MainView : public QMainWindow {
explicit MainView(QWidget* parent = 0);
~MainView();
QString GetInput();
void SetInput(QString txt);
QString GetOutput();
void SetOutput(QString txt);
QString GetCode();
void SetCode(QString txt);
void WriteOutput(QString output);
void SetSnippets(Snippets* snip);
private slots:
void on_btnRun_clicked();
Expand All @@ -55,21 +51,32 @@ private slots:
void on_btnRemoveSnippet_clicked();
void on_btnAddSnippet_clicked();
void on_btnAbout_clicked();
void SetInput(QString txt);
void SetOutput(QString txt);
void SetCode(QString txt);
void WriteOutput(QString output);
void StartPythonRun();
void EndPythonRun();

private:
QThread workerThread;
Ui::MainView* ui;
PythonSyntaxHighlighter* mHighlighter;
QString mStartMe;
QString mAbout;
Snippets* mSnippets;
void ChangeFontSize(QFont font, int size);
void SetupHighlighter();
void SaveFile(CodeEditor* codeEditor);
void BrowseAndLoadFile(CodeEditor* codeEditor);
QString LoadFile(const QString& fileName, bool& success);
void LoadStartupScript();
void LoadResources();
void LoadSnippetsToCombo();
void RunPythonCode(const QString& code);
void LoadSettings();
void SetupPython();
signals:
void operate(const QString&, const QString&);
};

#endif // MAINVIEW_H
Loading

0 comments on commit ab42d7b

Please sign in to comment.