Split to classes
This commit is contained in:
parent
9c0640240f
commit
4e82ed46d2
@ -9,10 +9,14 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/generated)
|
||||
|
||||
qt5_generate_moc(gui/mainwindow.h ${CMAKE_CURRENT_BINARY_DIR}/generated/moc_mainwindow.cc)
|
||||
qt5_generate_moc(gui/fpgaviewwidget.h ${CMAKE_CURRENT_BINARY_DIR}/generated/moc_fpgaviewwidget.cc)
|
||||
qt5_generate_moc(gui/pythontab.h ${CMAKE_CURRENT_BINARY_DIR}/generated/moc_pythontab.cc)
|
||||
qt5_generate_moc(gui/infotab.h ${CMAKE_CURRENT_BINARY_DIR}/generated/moc_infotab.cc)
|
||||
|
||||
set(GENERATED_MOC_FILES
|
||||
${CMAKE_CURRENT_BINARY_DIR}/generated/moc_mainwindow.cc
|
||||
${CMAKE_CURRENT_BINARY_DIR}/generated/moc_fpgaviewwidget.cc
|
||||
${CMAKE_CURRENT_BINARY_DIR}/generated/moc_pythontab.cc
|
||||
${CMAKE_CURRENT_BINARY_DIR}/generated/moc_infotab.cc
|
||||
)
|
||||
|
||||
set(UI_SOURCES
|
||||
@ -21,7 +25,7 @@ set(UI_SOURCES
|
||||
qt5_wrap_ui_custom(GENERATED_UI_HEADERS ${UI_SOURCES})
|
||||
qt5_add_resources_custom(GUI_RESOURCE_FILES gui/nextpnr.qrc)
|
||||
|
||||
set(GUI_SOURCE_FILES gui/mainwindow.cc gui/fpgaviewwidget.cc gui/emb.cc ${GENERATED_MOC_FILES} ${GENERATED_UI_HEADERS} ${GUI_RESOURCE_FILES})
|
||||
set(GUI_SOURCE_FILES gui/mainwindow.cc gui/fpgaviewwidget.cc gui/pythontab.cc gui/infotab.cc gui/emb.cc ${GENERATED_MOC_FILES} ${GENERATED_UI_HEADERS} ${GUI_RESOURCE_FILES})
|
||||
set(GUI_LIBRARY_FILES Qt5::Widgets Qt5::OpenGL ${OPENGL_LIBRARIES} QtPropertyBrowser)
|
||||
|
||||
|
||||
|
20
gui/infotab.cc
Normal file
20
gui/infotab.cc
Normal file
@ -0,0 +1,20 @@
|
||||
#include "infotab.h"
|
||||
#include <QGridLayout>
|
||||
|
||||
InfoTab::InfoTab(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
plainTextEdit = new QPlainTextEdit();
|
||||
plainTextEdit->setReadOnly(true);
|
||||
|
||||
QGridLayout *mainLayout = new QGridLayout();
|
||||
mainLayout->addWidget(plainTextEdit);
|
||||
setLayout(mainLayout);
|
||||
}
|
||||
|
||||
void InfoTab::info(std::string str)
|
||||
{
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->insertPlainText(str.c_str());
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
}
|
||||
|
22
gui/infotab.h
Normal file
22
gui/infotab.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef INFOTAB_H
|
||||
#define INFOTAB_H
|
||||
|
||||
#include "nextpnr.h"
|
||||
#include <QPlainTextEdit>
|
||||
|
||||
// FIXME
|
||||
USING_NEXTPNR_NAMESPACE
|
||||
|
||||
class InfoTab : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit InfoTab(QWidget *parent = 0);
|
||||
void info(std::string str);
|
||||
|
||||
private:
|
||||
QPlainTextEdit *plainTextEdit;
|
||||
};
|
||||
|
||||
#endif // INFOTAB_H
|
@ -79,110 +79,6 @@ class PipTreeItem : public ElementTreeItem
|
||||
IdString data;
|
||||
};
|
||||
|
||||
PythonTab::PythonTab(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
PyImport_ImportModule("emb");
|
||||
|
||||
// Add text area for Python output and input line
|
||||
plainTextEdit = new QPlainTextEdit();
|
||||
plainTextEdit->setReadOnly(true);
|
||||
plainTextEdit->setMinimumHeight(100);
|
||||
lineEdit = new QLineEdit();
|
||||
lineEdit->setMinimumHeight(30);
|
||||
lineEdit->setMaximumHeight(30);
|
||||
|
||||
QGridLayout *mainLayout = new QGridLayout();
|
||||
mainLayout->addWidget(plainTextEdit, 0, 0);
|
||||
mainLayout->addWidget(lineEdit, 1, 0);
|
||||
setLayout(mainLayout);
|
||||
|
||||
connect(lineEdit, SIGNAL(returnPressed()), this,
|
||||
SLOT(editLineReturnPressed()));
|
||||
|
||||
write = [this](std::string s) {
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->insertPlainText(s.c_str());
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
};
|
||||
emb::set_stdout(write);
|
||||
}
|
||||
|
||||
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");
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->insertPlainText(errorStr.c_str());
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
Py_DECREF(objectsRepresentation);
|
||||
Py_XDECREF(exception);
|
||||
Py_XDECREF(v);
|
||||
Py_XDECREF(tb);
|
||||
return -1;
|
||||
}
|
||||
Py_DECREF(v);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void PythonTab::editLineReturnPressed()
|
||||
{
|
||||
std::string input = lineEdit->text().toStdString();
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->insertPlainText(std::string(">>> " + input + "\n").c_str());
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->update();
|
||||
lineEdit->clear();
|
||||
int error = executePython(input);
|
||||
}
|
||||
|
||||
InfoTab::InfoTab(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
// Add text area for Python output and input line
|
||||
plainTextEdit = new QPlainTextEdit();
|
||||
plainTextEdit->setReadOnly(true);
|
||||
|
||||
QGridLayout *mainLayout = new QGridLayout();
|
||||
mainLayout->addWidget(plainTextEdit);
|
||||
setLayout(mainLayout);
|
||||
}
|
||||
|
||||
void InfoTab::info(std::string str)
|
||||
{
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->insertPlainText(str.c_str());
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
}
|
||||
|
||||
MainWindow::MainWindow(Design *_design, QWidget *parent)
|
||||
: QMainWindow(parent), ui(new Ui::MainWindow), design(_design)
|
||||
{
|
||||
|
@ -6,8 +6,9 @@
|
||||
#include "qtpropertymanager.h"
|
||||
#include "qttreepropertybrowser.h"
|
||||
#include "qtvariantproperty.h"
|
||||
#include "pythontab.h"
|
||||
#include "infotab.h"
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QMainWindow>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QTabWidget>
|
||||
@ -19,36 +20,6 @@ namespace Ui {
|
||||
class MainWindow;
|
||||
}
|
||||
|
||||
class PythonTab : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit PythonTab(QWidget *parent = 0);
|
||||
|
||||
private:
|
||||
int executePython(std::string command);
|
||||
private Q_SLOTS:
|
||||
void editLineReturnPressed();
|
||||
|
||||
private:
|
||||
QPlainTextEdit *plainTextEdit;
|
||||
QLineEdit *lineEdit;
|
||||
emb::stdout_write_type write;
|
||||
};
|
||||
|
||||
class InfoTab : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit InfoTab(QWidget *parent = 0);
|
||||
void info(std::string str);
|
||||
|
||||
private:
|
||||
QPlainTextEdit *plainTextEdit;
|
||||
};
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
90
gui/pythontab.cc
Normal file
90
gui/pythontab.cc
Normal file
@ -0,0 +1,90 @@
|
||||
#include "pythontab.h"
|
||||
#include "emb.h"
|
||||
#include "pybindings.h"
|
||||
#include <QGridLayout>
|
||||
|
||||
PythonTab::PythonTab(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
PyImport_ImportModule("emb");
|
||||
|
||||
// Add text area for Python output and input line
|
||||
plainTextEdit = new QPlainTextEdit();
|
||||
plainTextEdit->setReadOnly(true);
|
||||
plainTextEdit->setMinimumHeight(100);
|
||||
lineEdit = new QLineEdit();
|
||||
lineEdit->setMinimumHeight(30);
|
||||
lineEdit->setMaximumHeight(30);
|
||||
|
||||
QGridLayout *mainLayout = new QGridLayout();
|
||||
mainLayout->addWidget(plainTextEdit, 0, 0);
|
||||
mainLayout->addWidget(lineEdit, 1, 0);
|
||||
setLayout(mainLayout);
|
||||
|
||||
connect(lineEdit, SIGNAL(returnPressed()), this,
|
||||
SLOT(editLineReturnPressed()));
|
||||
|
||||
write = [this](std::string s) {
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->insertPlainText(s.c_str());
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
};
|
||||
emb::set_stdout(write);
|
||||
}
|
||||
|
||||
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");
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->insertPlainText(errorStr.c_str());
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
Py_DECREF(objectsRepresentation);
|
||||
Py_XDECREF(exception);
|
||||
Py_XDECREF(v);
|
||||
Py_XDECREF(tb);
|
||||
return -1;
|
||||
}
|
||||
Py_DECREF(v);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void PythonTab::editLineReturnPressed()
|
||||
{
|
||||
std::string input = lineEdit->text().toStdString();
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->insertPlainText(std::string(">>> " + input + "\n").c_str());
|
||||
plainTextEdit->moveCursor(QTextCursor::End);
|
||||
plainTextEdit->update();
|
||||
lineEdit->clear();
|
||||
int error = executePython(input);
|
||||
}
|
30
gui/pythontab.h
Normal file
30
gui/pythontab.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef PYTHONTAB_H
|
||||
#define PYTHONTAB_H
|
||||
|
||||
#include "nextpnr.h"
|
||||
#include "emb.h"
|
||||
#include <QLineEdit>
|
||||
#include <QPlainTextEdit>
|
||||
|
||||
// FIXME
|
||||
USING_NEXTPNR_NAMESPACE
|
||||
|
||||
class PythonTab : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit PythonTab(QWidget *parent = 0);
|
||||
|
||||
private:
|
||||
int executePython(std::string command);
|
||||
private Q_SLOTS:
|
||||
void editLineReturnPressed();
|
||||
|
||||
private:
|
||||
QPlainTextEdit *plainTextEdit;
|
||||
QLineEdit *lineEdit;
|
||||
emb::stdout_write_type write;
|
||||
};
|
||||
|
||||
#endif // PYTHONTAB_H
|
Loading…
Reference in New Issue
Block a user