2018-06-22 22:21:20 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-06-15 00:53:32 +08:00
|
|
|
#include "pythontab.h"
|
2018-06-15 00:53:48 +08:00
|
|
|
#include <QGridLayout>
|
2018-06-15 00:53:32 +08:00
|
|
|
#include "pybindings.h"
|
2018-06-28 23:56:44 +08:00
|
|
|
#include "pyinterpreter.h"
|
2018-06-15 00:53:32 +08:00
|
|
|
|
2018-06-22 19:10:27 +08:00
|
|
|
NEXTPNR_NAMESPACE_BEGIN
|
|
|
|
|
2018-07-14 01:56:11 +08:00
|
|
|
const QString PythonTab::PROMPT = ">>> ";
|
|
|
|
const QString PythonTab::MULTILINE_PROMPT = "... ";
|
|
|
|
|
2018-06-28 23:56:44 +08:00
|
|
|
PythonTab::PythonTab(QWidget *parent) : QWidget(parent), initialized(false)
|
2018-06-15 00:53:32 +08:00
|
|
|
{
|
2018-07-14 01:56:11 +08:00
|
|
|
QFont f("unexistent");
|
|
|
|
f.setStyleHint(QFont::Monospace);
|
|
|
|
|
2018-06-15 00:53:32 +08:00
|
|
|
// Add text area for Python output and input line
|
2018-06-28 23:56:44 +08:00
|
|
|
console = new PythonConsole();
|
|
|
|
console->setMinimumHeight(100);
|
2018-07-14 01:56:11 +08:00
|
|
|
console->setReadOnly(true);
|
|
|
|
console->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
|
|
|
|
console->setFont(f);
|
2018-06-19 21:17:10 +08:00
|
|
|
|
2018-06-28 23:56:44 +08:00
|
|
|
console->setContextMenuPolicy(Qt::CustomContextMenu);
|
2018-06-20 18:34:06 +08:00
|
|
|
QAction *clearAction = new QAction("Clear &buffer", this);
|
|
|
|
clearAction->setStatusTip("Clears display buffer");
|
2018-08-03 16:53:38 +08:00
|
|
|
connect(clearAction, &QAction::triggered, this, &PythonTab::clearBuffer);
|
2018-06-28 23:56:44 +08:00
|
|
|
contextMenu = console->createStandardContextMenu();
|
2018-06-20 18:34:06 +08:00
|
|
|
contextMenu->addSeparator();
|
|
|
|
contextMenu->addAction(clearAction);
|
2018-08-03 16:53:38 +08:00
|
|
|
connect(console, &PythonConsole::customContextMenuRequested, this, &PythonTab::showContextMenu);
|
2018-06-15 00:53:32 +08:00
|
|
|
|
2018-07-14 01:56:11 +08:00
|
|
|
lineEdit = new LineEditor(&parseHelper);
|
|
|
|
lineEdit->setMinimumHeight(30);
|
|
|
|
lineEdit->setMaximumHeight(30);
|
|
|
|
lineEdit->setFont(f);
|
|
|
|
lineEdit->setPlaceholderText(PythonTab::PROMPT);
|
2018-08-03 16:53:38 +08:00
|
|
|
connect(lineEdit, &LineEditor::textLineInserted, this, &PythonTab::editLineReturnPressed);
|
2018-07-14 01:56:11 +08:00
|
|
|
|
2018-06-15 00:53:32 +08:00
|
|
|
QGridLayout *mainLayout = new QGridLayout();
|
2018-06-28 23:56:44 +08:00
|
|
|
mainLayout->addWidget(console, 0, 0);
|
2018-07-14 01:56:11 +08:00
|
|
|
mainLayout->addWidget(lineEdit, 1, 0);
|
2018-06-15 00:53:32 +08:00
|
|
|
setLayout(mainLayout);
|
2018-07-14 01:56:11 +08:00
|
|
|
|
|
|
|
parseHelper.subscribe(console);
|
|
|
|
|
|
|
|
prompt = PythonTab::PROMPT;
|
2018-06-27 17:45:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PythonTab::~PythonTab()
|
|
|
|
{
|
2018-06-28 23:56:44 +08:00
|
|
|
if (initialized) {
|
|
|
|
pyinterpreter_finalize();
|
2018-06-27 17:45:19 +08:00
|
|
|
deinit_python();
|
2018-06-28 23:56:44 +08:00
|
|
|
}
|
2018-06-27 17:45:19 +08:00
|
|
|
}
|
2018-06-15 00:53:32 +08:00
|
|
|
|
2018-07-14 01:56:11 +08:00
|
|
|
void PythonTab::editLineReturnPressed(QString text)
|
|
|
|
{
|
|
|
|
console->displayString(prompt + text + "\n");
|
|
|
|
|
|
|
|
parseHelper.process(text.toStdString());
|
|
|
|
|
|
|
|
if (parseHelper.buffered())
|
|
|
|
prompt = PythonTab::MULTILINE_PROMPT;
|
|
|
|
else
|
|
|
|
prompt = PythonTab::PROMPT;
|
|
|
|
|
|
|
|
lineEdit->setPlaceholderText(prompt);
|
|
|
|
}
|
|
|
|
|
2018-06-27 17:45:19 +08:00
|
|
|
void PythonTab::newContext(Context *ctx)
|
|
|
|
{
|
2018-06-28 23:56:44 +08:00
|
|
|
if (initialized) {
|
|
|
|
pyinterpreter_finalize();
|
2018-06-27 17:45:19 +08:00
|
|
|
deinit_python();
|
2018-06-28 23:56:44 +08:00
|
|
|
}
|
|
|
|
console->clear();
|
2018-06-27 17:45:19 +08:00
|
|
|
|
2018-06-28 23:56:44 +08:00
|
|
|
pyinterpreter_preinit();
|
2018-06-27 17:45:19 +08:00
|
|
|
init_python("nextpnr", !initialized);
|
2018-06-28 23:56:44 +08:00
|
|
|
pyinterpreter_initialize();
|
|
|
|
pyinterpreter_aquire();
|
2018-06-27 17:45:19 +08:00
|
|
|
python_export_global("ctx", ctx);
|
2018-06-28 23:56:44 +08:00
|
|
|
pyinterpreter_release();
|
2018-06-27 17:45:19 +08:00
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
|
2018-06-28 23:56:44 +08:00
|
|
|
QString version = QString("Python %1 on %2\n").arg(Py_GetVersion(), Py_GetPlatform());
|
|
|
|
console->displayString(version);
|
2018-06-15 00:53:32 +08:00
|
|
|
}
|
2018-06-20 18:34:06 +08:00
|
|
|
|
2018-06-23 22:06:49 +08:00
|
|
|
void PythonTab::showContextMenu(const QPoint &pt) { contextMenu->exec(mapToGlobal(pt)); }
|
2018-06-20 18:34:06 +08:00
|
|
|
|
2018-06-28 23:56:44 +08:00
|
|
|
void PythonTab::clearBuffer() { console->clear(); }
|
2018-06-22 19:10:27 +08:00
|
|
|
|
2018-07-14 20:06:05 +08:00
|
|
|
void PythonTab::info(std::string str) { console->displayString(str.c_str()); }
|
2018-06-23 20:32:18 +08:00
|
|
|
|
2018-07-14 20:06:05 +08:00
|
|
|
NEXTPNR_NAMESPACE_END
|