11#include < functional>
22#include " PythonAccess/emb.h"
3+ #include " PythonAccess/pythonworker.h"
34#include " UI/mainview.h"
45#include " ui_mainview.h"
56#include < QSettings>
@@ -16,15 +17,40 @@ MainView::MainView(QWidget* parent)
1617 : QMainWindow(parent)
1718 , ui(new Ui::MainView)
1819{
19- emb::setMainView (this );
2020 ui->setupUi (this );
2121 SetupHighlighter ();
22- LoadStartupScript ();
22+ LoadResources ();
2323 LoadSettings ();
24+ SetupPython ();
25+ }
26+ void MainView::SetupPython ()
27+ {
28+ emb::setMainView (this );
29+ PythonWorker* worker = new PythonWorker ();
30+ emb::setWorker (worker);
31+ worker->moveToThread (&workerThread);
32+ connect (&workerThread, &QThread::finished, worker, &QObject::deleteLater);
33+ connect (this , &MainView::operate, worker, &PythonWorker::RunPython);
34+ connect (worker, &PythonWorker::WriteOutput, this , &MainView::WriteOutput);
35+ connect (worker, &PythonWorker::SetCode, this , &MainView::SetCode);
36+ connect (worker, &PythonWorker::SetInput, this , &MainView::SetInput);
37+ connect (worker, &PythonWorker::SetOutput, this , &MainView::SetOutput);
38+ connect (worker, &PythonWorker::StartPythonRun, this , &MainView::StartPythonRun);
39+ connect (worker, &PythonWorker::EndPythonRun, this , &MainView::EndPythonRun);
40+ workerThread.start ();
41+ }
42+ void MainView::StartPythonRun ()
43+ {
44+ ui->btnRun ->setEnabled (false );
45+ ui->btnRunSnippet ->setEnabled (false );
46+ }
47+ void MainView::EndPythonRun ()
48+ {
49+ ui->btnRun ->setEnabled (true );
50+ ui->btnRunSnippet ->setEnabled (true );
2451}
2552void MainView::LoadSettings ()
2653{
27-
2854 QSettings settings;
2955 this ->restoreState (settings.value (KEY_DOCK_LOCATIONS).toByteArray (), SAVE_STATE_VERSION);
3056 this ->restoreGeometry (settings.value (KEY_GEOMETRY).toByteArray ());
@@ -39,7 +65,7 @@ void MainView::LoadSettings()
3965 if (pos != -1 ) {
4066 ui->fntCombo ->setCurrentIndex (pos);
4167 }
42- if (sizeIndex >= 0 ){
68+ if (sizeIndex >= 0 ) {
4369 ui->cmbFontSize ->setCurrentIndex (sizeIndex);
4470 }
4571
@@ -57,14 +83,19 @@ void MainView::SetupHighlighter()
5783 ui->txtCode ->setFocus ();
5884}
5985
60- void MainView::LoadStartupScript ()
86+ void MainView::LoadResources ()
6187{
6288 bool success;
6389 mStartMe = LoadFile (" :/data/startme.py" , success);
6490
6591 if (!success) {
6692 QMessageBox::warning (this , tr (APP_NAME), tr (" Loading startup script failed" ));
6793 }
94+ mAbout = LoadFile (" :/data/About.htm" , success);
95+ if (!success) {
96+ mAbout = tr (APP_NAME " Written by Bhathiya Perera" );
97+ }
98+
6899}
69100MainView::~MainView ()
70101{
@@ -76,6 +107,8 @@ MainView::~MainView()
76107 settings.setValue (KEY_OUTPUTBOX, this ->GetOutput ());
77108 settings.setValue (KEY_FONT, ui->fntCombo ->currentText ());
78109 settings.setValue (KEY_FONTSIZE, ui->cmbFontSize ->currentIndex ());
110+ workerThread.quit ();
111+ workerThread.wait ();
79112 delete ui;
80113}
81114
@@ -173,20 +206,7 @@ void MainView::WriteOutput(QString output)
173206
174207void MainView::RunPythonCode (const QString& code)
175208{
176- PyImport_AppendInittab (" emb" , emb::PyInitEmbConnect);
177- PyImport_AppendInittab (" expressApi" , emb::PyInitApiConnection);
178- Py_Initialize ();
179- PyImport_ImportModule (" emb" );
180-
181- emb::StdOutWriteType write = [](std::string s) {
182- emb::getMainView ()->WriteOutput (QString::fromStdString (s));
183- };
184-
185- emb::SetStdout (write);
186- PyRun_SimpleString (mStartMe .toStdString ().c_str ());
187- PyRun_SimpleString (code.toStdString ().c_str ());
188- emb::ResetStdOut ();
189- Py_Finalize ();
209+ emit operate (mStartMe , code);
190210}
191211
192212void MainView::on_btnRun_clicked ()
@@ -326,14 +346,7 @@ void MainView::on_btnAddSnippet_clicked()
326346
327347void MainView::on_btnAbout_clicked ()
328348{
329- QMessageBox::about (this , tr (APP_NAME), tr (" <b>" APP_NAME " </b><br />"
330- " Written by Bhathiya Perera<br />"
331- " <br />"
332- " This Project Depends on<br />"
333- " Qt5.3, Python<br />"
334- " Frankie Simon's Python Syntax Highlight Code<br />"
335- " Mateusz Loskot's Embedding Code<br />"
336- " <br />" ));
349+ QMessageBox::about (this , tr (APP_NAME), mAbout );
337350}
338351
339352void MainView::LoadSnippetsToCombo ()
0 commit comments