From bafb4702c7176a08e4a9b2088297071a7bed6656 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Wed, 27 Jun 2018 11:45:19 +0200 Subject: reinit python tab --- gui/pythontab.cc | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'gui/pythontab.cc') diff --git a/gui/pythontab.cc b/gui/pythontab.cc index 397920d9..816039ba 100644 --- a/gui/pythontab.cc +++ b/gui/pythontab.cc @@ -25,10 +25,8 @@ NEXTPNR_NAMESPACE_BEGIN -PythonTab::PythonTab(QWidget *parent) : QWidget(parent) +PythonTab::PythonTab(QWidget *parent) : QWidget(parent),initialized(false) { - PyImport_ImportModule("emb"); - // Add text area for Python output and input line plainTextEdit = new QPlainTextEdit(); plainTextEdit->setReadOnly(true); @@ -57,7 +55,25 @@ PythonTab::PythonTab(QWidget *parent) : QWidget(parent) setLayout(mainLayout); connect(lineEdit, SIGNAL(textLineInserted(QString)), this, SLOT(editLineReturnPressed(QString))); +} + +PythonTab::~PythonTab() +{ + if (initialized) + deinit_python(); +} +void PythonTab::newContext(Context *ctx) +{ + if (initialized) + deinit_python(); + + plainTextEdit->clear(); + + init_python("nextpnr", !initialized); + python_export_global("ctx", ctx); + + PyImport_ImportModule("emb"); write = [this](std::string s) { plainTextEdit->moveCursor(QTextCursor::End); plainTextEdit->insertPlainText(s.c_str()); @@ -65,6 +81,8 @@ PythonTab::PythonTab(QWidget *parent) : QWidget(parent) }; emb::set_stdout(write); + initialized = true; + char buff[1024]; sprintf(buff, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform()); print(buff); @@ -121,9 +139,12 @@ int PythonTab::executePython(std::string &command) void PythonTab::editLineReturnPressed(QString text) { - std::string input = text.toStdString(); - print(std::string(">>> " + input + "\n")); - executePython(input); + if (initialized) + { + std::string input = text.toStdString(); + print(std::string(">>> " + input + "\n")); + executePython(input); + } } void PythonTab::showContextMenu(const QPoint &pt) { contextMenu->exec(mapToGlobal(pt)); } -- cgit v1.2.3