From 4e82ed46d209d05508c7af24cfe135c78ee353db Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 14 Jun 2018 18:53:32 +0200 Subject: Split to classes --- gui/gui.cmake | 6 +++- gui/infotab.cc | 20 +++++++++++ gui/infotab.h | 22 ++++++++++++ gui/mainwindow.cc | 104 ------------------------------------------------------ gui/mainwindow.h | 33 ++--------------- gui/pythontab.cc | 90 ++++++++++++++++++++++++++++++++++++++++++++++ gui/pythontab.h | 30 ++++++++++++++++ 7 files changed, 169 insertions(+), 136 deletions(-) create mode 100644 gui/infotab.cc create mode 100644 gui/infotab.h create mode 100644 gui/pythontab.cc create mode 100644 gui/pythontab.h (limited to 'gui') diff --git a/gui/gui.cmake b/gui/gui.cmake index 23f955f9..5b9b1833 100644 --- a/gui/gui.cmake +++ b/gui/gui.cmake @@ -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) diff --git a/gui/infotab.cc b/gui/infotab.cc new file mode 100644 index 00000000..9a523d4f --- /dev/null +++ b/gui/infotab.cc @@ -0,0 +1,20 @@ +#include "infotab.h" +#include + +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); +} + diff --git a/gui/infotab.h b/gui/infotab.h new file mode 100644 index 00000000..bf673ee6 --- /dev/null +++ b/gui/infotab.h @@ -0,0 +1,22 @@ +#ifndef INFOTAB_H +#define INFOTAB_H + +#include "nextpnr.h" +#include + +// 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 diff --git a/gui/mainwindow.cc b/gui/mainwindow.cc index 2880ccc2..eaebe809 100644 --- a/gui/mainwindow.cc +++ b/gui/mainwindow.cc @@ -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) { diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 5464a6dd..f0818be4 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -6,8 +6,9 @@ #include "qtpropertymanager.h" #include "qttreepropertybrowser.h" #include "qtvariantproperty.h" +#include "pythontab.h" +#include "infotab.h" -#include #include #include #include @@ -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 diff --git a/gui/pythontab.cc b/gui/pythontab.cc new file mode 100644 index 00000000..8f620958 --- /dev/null +++ b/gui/pythontab.cc @@ -0,0 +1,90 @@ +#include "pythontab.h" +#include "emb.h" +#include "pybindings.h" +#include + +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); +} diff --git a/gui/pythontab.h b/gui/pythontab.h new file mode 100644 index 00000000..f6cffcf3 --- /dev/null +++ b/gui/pythontab.h @@ -0,0 +1,30 @@ +#ifndef PYTHONTAB_H +#define PYTHONTAB_H + +#include "nextpnr.h" +#include "emb.h" +#include +#include + +// 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 -- cgit v1.2.3