aboutsummaryrefslogtreecommitdiffstats
path: root/gui/pythontab.cc
diff options
context:
space:
mode:
authorMiodrag Milanovic <mmicko@gmail.com>2018-06-28 17:56:44 +0200
committerMiodrag Milanovic <mmicko@gmail.com>2018-06-28 17:57:26 +0200
commit1676b285ae726eb858d4d7ed089496133ce3de4b (patch)
treeb7c2a9a59c2a9a0377d266567e545f5a1c23ae58 /gui/pythontab.cc
parent2037fff7426e252a8692b971f88689c1dd8c03e8 (diff)
downloadnextpnr-1676b285ae726eb858d4d7ed089496133ce3de4b.tar.gz
nextpnr-1676b285ae726eb858d4d7ed089496133ce3de4b.tar.bz2
nextpnr-1676b285ae726eb858d4d7ed089496133ce3de4b.zip
adapted python-console for easier use
Diffstat (limited to 'gui/pythontab.cc')
-rw-r--r--gui/pythontab.cc111
1 files changed, 25 insertions, 86 deletions
diff --git a/gui/pythontab.cc b/gui/pythontab.cc
index 53e88541..897f87b3 100644
--- a/gui/pythontab.cc
+++ b/gui/pythontab.cc
@@ -21,126 +21,65 @@
#include "pythontab.h"
#include <QGridLayout>
#include "pybindings.h"
+#include "pyinterpreter.h"
NEXTPNR_NAMESPACE_BEGIN
-PythonTab::PythonTab(QWidget *parent) : QWidget(parent),initialized(false)
+PythonTab::PythonTab(QWidget *parent) : QWidget(parent), initialized(false)
{
// Add text area for Python output and input line
- plainTextEdit = new QPlainTextEdit();
- plainTextEdit->setReadOnly(true);
- plainTextEdit->setMinimumHeight(100);
- QFont f("unexistent");
- f.setStyleHint(QFont::Monospace);
- plainTextEdit->setFont(f);
+ console = new PythonConsole();
+ console->setMinimumHeight(100);
+ console->setEnabled(false);
- plainTextEdit->setContextMenuPolicy(Qt::CustomContextMenu);
+ console->setContextMenuPolicy(Qt::CustomContextMenu);
QAction *clearAction = new QAction("Clear &buffer", this);
clearAction->setStatusTip("Clears display buffer");
connect(clearAction, SIGNAL(triggered()), this, SLOT(clearBuffer()));
- contextMenu = plainTextEdit->createStandardContextMenu();
+ contextMenu = console->createStandardContextMenu();
contextMenu->addSeparator();
contextMenu->addAction(clearAction);
- connect(plainTextEdit, SIGNAL(customContextMenuRequested(const QPoint)), this, SLOT(showContextMenu(const QPoint)));
-
- lineEdit = new LineEditor();
- lineEdit->setMinimumHeight(30);
- lineEdit->setMaximumHeight(30);
- lineEdit->setFont(f);
+ connect(console, SIGNAL(customContextMenuRequested(const QPoint)), this, SLOT(showContextMenu(const QPoint)));
QGridLayout *mainLayout = new QGridLayout();
- mainLayout->addWidget(plainTextEdit, 0, 0);
- mainLayout->addWidget(lineEdit, 1, 0);
+ mainLayout->addWidget(console, 0, 0);
setLayout(mainLayout);
-
- connect(lineEdit, SIGNAL(textLineInserted(QString)), this, SLOT(editLineReturnPressed(QString)));
}
PythonTab::~PythonTab()
{
- if (initialized)
+ if (initialized) {
+ pyinterpreter_finalize();
deinit_python();
+ }
}
void PythonTab::newContext(Context *ctx)
{
- if (initialized)
+ if (initialized) {
+ pyinterpreter_finalize();
deinit_python();
-
- plainTextEdit->clear();
+ }
+ console->setEnabled(true);
+ console->clear();
+ pyinterpreter_preinit();
init_python("nextpnr", !initialized);
+ pyinterpreter_initialize();
+ pyinterpreter_aquire();
python_export_global("ctx", ctx);
+ pyinterpreter_release();
initialized = true;
- char buff[1024];
- sprintf(buff, "Python %s on %s\n", Py_GetVersion(), Py_GetPlatform());
- print(buff);
-}
-
-void PythonTab::print(std::string line)
-{
- plainTextEdit->moveCursor(QTextCursor::End);
- plainTextEdit->insertPlainText(line.c_str());
- plainTextEdit->moveCursor(QTextCursor::End);
-}
-
-void handle_system_exit() { exit(-1); }
-
-int PythonTab::executePython(std::string &command)
-{
- PyObject *m, *d, *v;
- m = PyImport_AddModule("__main__");
- if (m == NULL)
- return -1;
- d = PyModule_GetDict(m);
- v = PyRun_StringFlags(command.c_str(), (command.empty() ? Py_file_input : Py_single_input), d, d, NULL);
- if (v == NULL) {
- PyObject *exception, *v, *tb;
-
- if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
- handle_system_exit();
- }
- PyErr_Fetch(&exception, &v, &tb);
- if (exception == NULL)
- return 0;
- PyErr_NormalizeException(&exception, &v, &tb);
- if (tb == NULL) {
- tb = Py_None;
- Py_INCREF(tb);
- }
- PyException_SetTraceback(v, tb);
- if (exception == NULL)
- return 0;
- PyErr_Clear();
-
- PyObject *objectsRepresentation = PyObject_Str(v);
- std::string errorStr = PyUnicode_AsUTF8(objectsRepresentation) + std::string("\n");
- print(errorStr);
- Py_DECREF(objectsRepresentation);
- Py_XDECREF(exception);
- Py_XDECREF(v);
- Py_XDECREF(tb);
- return -1;
- }
- Py_DECREF(v);
- return 0;
-}
-
-void PythonTab::editLineReturnPressed(QString text)
-{
- if (initialized)
- {
- std::string input = text.toStdString();
- print(std::string(">>> " + input + "\n"));
- executePython(input);
- }
+ QString version = QString("Python %1 on %2\n").arg(Py_GetVersion(), Py_GetPlatform());
+ console->displayString(version);
+ console->displayPrompt();
}
void PythonTab::showContextMenu(const QPoint &pt) { contextMenu->exec(mapToGlobal(pt)); }
-void PythonTab::clearBuffer() { plainTextEdit->clear(); }
+void PythonTab::clearBuffer() { console->clear(); }
NEXTPNR_NAMESPACE_END