From 1676b285ae726eb858d4d7ed089496133ce3de4b Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 28 Jun 2018 17:56:44 +0200 Subject: adapted python-console for easier use --- gui/CMakeLists.txt | 6 ++- gui/pythontab.cc | 111 ++++++++++++----------------------------------------- gui/pythontab.h | 9 ++--- 3 files changed, 33 insertions(+), 93 deletions(-) (limited to 'gui') diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 78c273c5..2021b7ea 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -8,6 +8,10 @@ set(PYTHON_CONSOLE_SRC ../3rdparty/python-console/ParseHelper.BracketParseState.cpp ../3rdparty/python-console/ParseHelper.ContinuationParseState.cpp ../3rdparty/python-console/ParseMessage.cpp + + ../3rdparty/python-console/modified/pyredirector.cc + ../3rdparty/python-console/modified/pyinterpreter.cc + ../3rdparty/python-console/modified/pyconsole.cc ) aux_source_directory(. GUI_SOURCE_FILES) @@ -21,6 +25,6 @@ set(GUI_LIBRARY_FILES_${ufamily} Qt5::Widgets Qt5::OpenGL ${OPENGL_LIBRARIES} Qt add_library(gui_${family} STATIC ${GUI_SOURCE_FILES} ${PYTHON_CONSOLE_SRC} ${GUI_RESOURCE_FILES}) -target_include_directories(gui_${family} PRIVATE ../${family} ${family} ../3rdparty/QtPropertyBrowser/src) +target_include_directories(gui_${family} PRIVATE ../${family} ${family} ../3rdparty/QtPropertyBrowser/src ../3rdparty/python-console ../3rdparty/python-console/modified) target_compile_definitions(gui_${family} PRIVATE NEXTPNR_NAMESPACE=nextpnr_${family} ARCH_${ufamily} ARCHNAME=${family} QT_NO_KEYWORDS) target_link_libraries(gui_${family} Qt5::Widgets) 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 #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 diff --git a/gui/pythontab.h b/gui/pythontab.h index c742b42f..4b22e6a9 100644 --- a/gui/pythontab.h +++ b/gui/pythontab.h @@ -27,6 +27,7 @@ #include #include "line_editor.h" #include "nextpnr.h" +#include "pyconsole.h" NEXTPNR_NAMESPACE_BEGIN @@ -38,18 +39,14 @@ class PythonTab : public QWidget explicit PythonTab(QWidget *parent = 0); ~PythonTab(); - private: - void print(std::string line); - int executePython(std::string &command); private Q_SLOTS: - void editLineReturnPressed(QString text); void showContextMenu(const QPoint &pt); void clearBuffer(); public Q_SLOTS: void newContext(Context *ctx); + private: - QPlainTextEdit *plainTextEdit; - LineEditor *lineEdit; + PythonConsole *console; QMenu *contextMenu; bool initialized; }; -- cgit v1.2.3