clang format for gui related files
This commit is contained in:
parent
41b949832c
commit
e7ff22ce90
136
gui/emb.cc
136
gui/emb.cc
@ -6,27 +6,24 @@
|
||||
//
|
||||
// Blog article: http://mateusz.loskot.net/?p=2819
|
||||
|
||||
#include "emb.h"
|
||||
#include <Python.h>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <Python.h>
|
||||
#include "emb.h"
|
||||
|
||||
namespace emb
|
||||
{
|
||||
namespace emb {
|
||||
struct Stdout
|
||||
{
|
||||
PyObject_HEAD
|
||||
stdout_write_type write;
|
||||
PyObject_HEAD stdout_write_type write;
|
||||
};
|
||||
|
||||
PyObject* Stdout_write(PyObject* self, PyObject* args)
|
||||
PyObject *Stdout_write(PyObject *self, PyObject *args)
|
||||
{
|
||||
std::size_t written(0);
|
||||
Stdout* selfimpl = reinterpret_cast<Stdout*>(self);
|
||||
if (selfimpl->write)
|
||||
{
|
||||
char* data;
|
||||
Stdout *selfimpl = reinterpret_cast<Stdout *>(self);
|
||||
if (selfimpl->write) {
|
||||
char *data;
|
||||
if (!PyArg_ParseTuple(args, "s", &data))
|
||||
return 0;
|
||||
|
||||
@ -37,70 +34,65 @@ PyObject* Stdout_write(PyObject* self, PyObject* args)
|
||||
return PyLong_FromSize_t(written);
|
||||
}
|
||||
|
||||
PyObject* Stdout_flush(PyObject* self, PyObject* args)
|
||||
PyObject *Stdout_flush(PyObject *self, PyObject *args)
|
||||
{
|
||||
// no-op
|
||||
return Py_BuildValue("");
|
||||
}
|
||||
|
||||
PyMethodDef Stdout_methods[] =
|
||||
{
|
||||
{"write", Stdout_write, METH_VARARGS, "sys.stdout.write"},
|
||||
{"flush", Stdout_flush, METH_VARARGS, "sys.stdout.write"},
|
||||
{0, 0, 0, 0} // sentinel
|
||||
PyMethodDef Stdout_methods[] = {
|
||||
{"write", Stdout_write, METH_VARARGS, "sys.stdout.write"},
|
||||
{"flush", Stdout_flush, METH_VARARGS, "sys.stdout.write"},
|
||||
{0, 0, 0, 0} // sentinel
|
||||
};
|
||||
|
||||
PyTypeObject StdoutType =
|
||||
{
|
||||
PyVarObject_HEAD_INIT(0, 0)
|
||||
"emb.StdoutType", /* tp_name */
|
||||
sizeof(Stdout), /* tp_basicsize */
|
||||
0, /* tp_itemsize */
|
||||
0, /* tp_dealloc */
|
||||
0, /* tp_print */
|
||||
0, /* tp_getattr */
|
||||
0, /* tp_setattr */
|
||||
0, /* tp_reserved */
|
||||
0, /* tp_repr */
|
||||
0, /* tp_as_number */
|
||||
0, /* tp_as_sequence */
|
||||
0, /* tp_as_mapping */
|
||||
0, /* tp_hash */
|
||||
0, /* tp_call */
|
||||
0, /* tp_str */
|
||||
0, /* tp_getattro */
|
||||
0, /* tp_setattro */
|
||||
0, /* tp_as_buffer */
|
||||
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
||||
"emb.Stdout objects", /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
Stdout_methods, /* tp_methods */
|
||||
0, /* tp_members */
|
||||
0, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
0, /* tp_descr_set */
|
||||
0, /* tp_dictoffset */
|
||||
0, /* tp_init */
|
||||
0, /* tp_alloc */
|
||||
0, /* tp_new */
|
||||
PyTypeObject StdoutType = {
|
||||
PyVarObject_HEAD_INIT(0, 0) "emb.StdoutType", /* tp_name */
|
||||
sizeof(Stdout), /* tp_basicsize */
|
||||
0, /* tp_itemsize */
|
||||
0, /* tp_dealloc */
|
||||
0, /* tp_print */
|
||||
0, /* tp_getattr */
|
||||
0, /* tp_setattr */
|
||||
0, /* tp_reserved */
|
||||
0, /* tp_repr */
|
||||
0, /* tp_as_number */
|
||||
0, /* tp_as_sequence */
|
||||
0, /* tp_as_mapping */
|
||||
0, /* tp_hash */
|
||||
0, /* tp_call */
|
||||
0, /* tp_str */
|
||||
0, /* tp_getattro */
|
||||
0, /* tp_setattro */
|
||||
0, /* tp_as_buffer */
|
||||
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
||||
"emb.Stdout objects", /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
Stdout_methods, /* tp_methods */
|
||||
0, /* tp_members */
|
||||
0, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
0, /* tp_descr_set */
|
||||
0, /* tp_dictoffset */
|
||||
0, /* tp_init */
|
||||
0, /* tp_alloc */
|
||||
0, /* tp_new */
|
||||
};
|
||||
|
||||
PyModuleDef embmodule =
|
||||
{
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"emb", 0, -1, 0,
|
||||
PyModuleDef embmodule = {
|
||||
PyModuleDef_HEAD_INIT, "emb", 0, -1, 0,
|
||||
};
|
||||
|
||||
// Internal state
|
||||
PyObject* g_stdout;
|
||||
PyObject* g_stdout_saved;
|
||||
PyObject *g_stdout;
|
||||
PyObject *g_stdout_saved;
|
||||
|
||||
PyMODINIT_FUNC PyInit_emb(void)
|
||||
{
|
||||
@ -111,24 +103,23 @@ PyMODINIT_FUNC PyInit_emb(void)
|
||||
if (PyType_Ready(&StdoutType) < 0)
|
||||
return 0;
|
||||
|
||||
PyObject* m = PyModule_Create(&embmodule);
|
||||
if (m)
|
||||
{
|
||||
PyObject *m = PyModule_Create(&embmodule);
|
||||
if (m) {
|
||||
Py_INCREF(&StdoutType);
|
||||
PyModule_AddObject(m, "Stdout", reinterpret_cast<PyObject*>(&StdoutType));
|
||||
PyModule_AddObject(m, "Stdout",
|
||||
reinterpret_cast<PyObject *>(&StdoutType));
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
void set_stdout(stdout_write_type write)
|
||||
{
|
||||
if (!g_stdout)
|
||||
{
|
||||
if (!g_stdout) {
|
||||
g_stdout_saved = PySys_GetObject("stdout"); // borrowed
|
||||
g_stdout = StdoutType.tp_new(&StdoutType, 0, 0);
|
||||
}
|
||||
|
||||
Stdout* impl = reinterpret_cast<Stdout*>(g_stdout);
|
||||
Stdout *impl = reinterpret_cast<Stdout *>(g_stdout);
|
||||
impl->write = write;
|
||||
PySys_SetObject("stdout", g_stdout);
|
||||
}
|
||||
@ -142,9 +133,6 @@ void reset_stdout()
|
||||
g_stdout = 0;
|
||||
}
|
||||
|
||||
void append_inittab()
|
||||
{
|
||||
PyImport_AppendInittab("emb", emb::PyInit_emb);
|
||||
}
|
||||
void append_inittab() { PyImport_AppendInittab("emb", emb::PyInit_emb); }
|
||||
|
||||
} // namespace emb
|
||||
|
13
gui/emb.h
13
gui/emb.h
@ -10,12 +10,11 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace emb
|
||||
{
|
||||
typedef std::function<void(std::string)> stdout_write_type;
|
||||
namespace emb {
|
||||
typedef std::function<void(std::string)> stdout_write_type;
|
||||
|
||||
void set_stdout(stdout_write_type write);
|
||||
void reset_stdout();
|
||||
|
||||
void append_inittab();
|
||||
void set_stdout(stdout_write_type write);
|
||||
void reset_stdout();
|
||||
|
||||
void append_inittab();
|
||||
} // namespace emb
|
||||
|
@ -1,57 +1,42 @@
|
||||
#include <QMouseEvent>
|
||||
#include <QCoreApplication>
|
||||
#include <math.h>
|
||||
#include "fpgaviewwidget.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QMouseEvent>
|
||||
#include <math.h>
|
||||
|
||||
FPGAViewWidget::FPGAViewWidget(QWidget *parent)
|
||||
: QOpenGLWidget(parent),
|
||||
m_xMove(0),m_yMove(0),m_zDistance(1.0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
FPGAViewWidget::~FPGAViewWidget()
|
||||
: QOpenGLWidget(parent), m_xMove(0), m_yMove(0), m_zDistance(1.0)
|
||||
{
|
||||
}
|
||||
|
||||
QSize FPGAViewWidget::minimumSizeHint() const
|
||||
{
|
||||
return QSize(640, 480);
|
||||
}
|
||||
FPGAViewWidget::~FPGAViewWidget() {}
|
||||
|
||||
QSize FPGAViewWidget::sizeHint() const
|
||||
{
|
||||
return QSize(640, 480);
|
||||
}
|
||||
QSize FPGAViewWidget::minimumSizeHint() const { return QSize(640, 480); }
|
||||
|
||||
QSize FPGAViewWidget::sizeHint() const { return QSize(640, 480); }
|
||||
|
||||
void FPGAViewWidget::setXTranslation(float t_x)
|
||||
{
|
||||
if(t_x != m_xMove)
|
||||
{
|
||||
if (t_x != m_xMove) {
|
||||
m_xMove = t_x;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FPGAViewWidget::setYTranslation(float t_y)
|
||||
{
|
||||
if(t_y != m_yMove)
|
||||
{
|
||||
if (t_y != m_yMove) {
|
||||
m_yMove = t_y;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FPGAViewWidget::setZoom(float t_z)
|
||||
{
|
||||
if(t_z != m_zDistance)
|
||||
{
|
||||
if (t_z != m_zDistance) {
|
||||
m_zDistance -= t_z;
|
||||
if(m_zDistance < 0.1f)
|
||||
if (m_zDistance < 0.1f)
|
||||
m_zDistance = 0.1f;
|
||||
if(m_zDistance > 10.0f)
|
||||
if (m_zDistance > 10.0f)
|
||||
m_zDistance = 10.0f;
|
||||
|
||||
update();
|
||||
@ -70,21 +55,19 @@ void FPGAViewWidget::paintGL()
|
||||
glLoadIdentity();
|
||||
|
||||
glTranslatef(m_xMove, m_yMove, -10.0);
|
||||
glScalef(m_zDistance,m_zDistance, 0.0f);
|
||||
|
||||
glScalef(m_zDistance, m_zDistance, 0.0f);
|
||||
|
||||
// Example grid
|
||||
glColor3f(0.8, 0.8, 0.8);
|
||||
glBegin(GL_LINES);
|
||||
for(float i = -100; i <= 100; i += 0.1)
|
||||
{
|
||||
for (float i = -100; i <= 100; i += 0.1) {
|
||||
glVertex3f((float)i, -100.0f, 0.0f);
|
||||
glVertex3f((float)i, 100.0f, 0.0f);
|
||||
glVertex3f(-100.0f, (float)i, 0.0f);
|
||||
glVertex3f(100.0f, (float)i, 0.0f);
|
||||
}
|
||||
glColor3f(0.5, 0.5, 0.5);
|
||||
for(int i = -100; i <= 100; i += 1)
|
||||
{
|
||||
for (int i = -100; i <= 100; i += 1) {
|
||||
glVertex3f((float)i, -100.0f, 0.0f);
|
||||
glVertex3f((float)i, 100.0f, 0.0f);
|
||||
glVertex3f(-100.0f, (float)i, 0.0f);
|
||||
@ -101,7 +84,6 @@ void FPGAViewWidget::paintGL()
|
||||
glColor3f(0.0, 0.0, 1.0);
|
||||
glVertex3f(0, 0.5, 0);
|
||||
glEnd();
|
||||
|
||||
}
|
||||
|
||||
void FPGAViewWidget::resizeGL(int width, int height)
|
||||
@ -114,7 +96,7 @@ void FPGAViewWidget::resizeGL(int width, int height)
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(-1.0* aspect, +1.0* aspect, -1.0, +1.0, 1.0, 15.0);
|
||||
glOrtho(-1.0 * aspect, +1.0 * aspect, -1.0, +1.0, 1.0, 15.0);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
}
|
||||
|
||||
@ -130,13 +112,13 @@ void FPGAViewWidget::mouseMoveEvent(QMouseEvent *event)
|
||||
float dx_scale = dx * (1 / (float)640);
|
||||
float dy_scale = -dy * (1 / (float)480);
|
||||
|
||||
if (event->buttons() & Qt::LeftButton)
|
||||
{
|
||||
if (event->buttons() & Qt::LeftButton) {
|
||||
float xpos = m_xMove + dx_scale;
|
||||
float ypos = m_yMove + dy_scale;
|
||||
if (m_xMove/m_zDistance <= 100.0 && m_xMove/m_zDistance>= -100.0) setXTranslation(xpos);
|
||||
if (m_yMove/m_zDistance <= 100.0 && m_yMove/m_zDistance>= -100.0) setYTranslation(ypos);
|
||||
|
||||
if (m_xMove / m_zDistance <= 100.0 && m_xMove / m_zDistance >= -100.0)
|
||||
setXTranslation(xpos);
|
||||
if (m_yMove / m_zDistance <= 100.0 && m_yMove / m_zDistance >= -100.0)
|
||||
setYTranslation(ypos);
|
||||
}
|
||||
m_lastPos = event->pos();
|
||||
}
|
||||
@ -145,8 +127,7 @@ void FPGAViewWidget::wheelEvent(QWheelEvent *event)
|
||||
{
|
||||
QPoint degree = event->angleDelta() / 8;
|
||||
|
||||
if(!degree.isNull())
|
||||
{
|
||||
if (!degree.isNull()) {
|
||||
QPoint step = degree / 15;
|
||||
setZoom(step.y() * -0.1f);
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
#ifndef MAPGLWIDGET_H
|
||||
#define MAPGLWIDGET_H
|
||||
|
||||
#include <QOpenGLWidget>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QOpenGLWidget>
|
||||
#include <QPainter>
|
||||
|
||||
class FPGAViewWidget : public QOpenGLWidget, protected QOpenGLFunctions
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
public:
|
||||
FPGAViewWidget(QWidget *parent = 0);
|
||||
~FPGAViewWidget();
|
||||
|
||||
@ -24,7 +24,7 @@ public:
|
||||
void yRotationChanged(int angle);
|
||||
void zRotationChanged(int angle);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
void initializeGL() Q_DECL_OVERRIDE;
|
||||
void paintGL() Q_DECL_OVERRIDE;
|
||||
void resizeGL(int width, int height) Q_DECL_OVERRIDE;
|
||||
@ -32,7 +32,7 @@ protected:
|
||||
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||
void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
private:
|
||||
private:
|
||||
int m_windowWidth;
|
||||
int m_windowHeight;
|
||||
float m_xMove;
|
||||
|
@ -1,36 +1,29 @@
|
||||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "pybindings.h"
|
||||
#include "emb.h"
|
||||
#include "pybindings.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::MainWindow)
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent), ui(new Ui::MainWindow)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
PyImport_ImportModule("emb");
|
||||
|
||||
write = [this] (std::string s) {
|
||||
//ui->plainTextEdit->moveCursor(QTextCursor::End);
|
||||
//ui->plainTextEdit->insertPlainText(s.c_str());
|
||||
//ui->plainTextEdit->moveCursor(QTextCursor::End);
|
||||
|
||||
write = [this](std::string s) {
|
||||
// ui->plainTextEdit->moveCursor(QTextCursor::End);
|
||||
// ui->plainTextEdit->insertPlainText(s.c_str());
|
||||
// ui->plainTextEdit->moveCursor(QTextCursor::End);
|
||||
ui->plainTextEdit->appendPlainText(s.c_str());
|
||||
};
|
||||
emb::set_stdout(write);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
MainWindow::~MainWindow() { delete ui; }
|
||||
|
||||
void handle_system_exit()
|
||||
{
|
||||
exit(-1);
|
||||
}
|
||||
void handle_system_exit() { exit(-1); }
|
||||
|
||||
int MainWindow::executePython(std::string command)
|
||||
{
|
||||
@ -39,9 +32,10 @@ int MainWindow::executePython(std::string command)
|
||||
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)
|
||||
{
|
||||
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)) {
|
||||
@ -60,8 +54,8 @@ int MainWindow::executePython(std::string command)
|
||||
return 0;
|
||||
PyErr_Clear();
|
||||
|
||||
PyObject* objectsRepresentation = PyObject_Str(v);
|
||||
const char* errorStr = PyUnicode_AsUTF8(objectsRepresentation);
|
||||
PyObject *objectsRepresentation = PyObject_Str(v);
|
||||
const char *errorStr = PyUnicode_AsUTF8(objectsRepresentation);
|
||||
ui->plainTextEdit->appendPlainText(errorStr);
|
||||
Py_DECREF(objectsRepresentation);
|
||||
Py_XDECREF(exception);
|
||||
@ -75,7 +69,7 @@ int MainWindow::executePython(std::string command)
|
||||
|
||||
void MainWindow::on_lineEdit_returnPressed()
|
||||
{
|
||||
std::string input = ui->lineEdit->text().toStdString();
|
||||
std::string input = ui->lineEdit->text().toStdString();
|
||||
ui->plainTextEdit->appendPlainText(std::string(">>> " + input).c_str());
|
||||
ui->plainTextEdit->update();
|
||||
ui->lineEdit->clear();
|
||||
|
@ -12,17 +12,17 @@ class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = 0);
|
||||
~MainWindow();
|
||||
|
||||
private:
|
||||
private:
|
||||
int executePython(std::string command);
|
||||
|
||||
private Q_SLOTS:
|
||||
private Q_SLOTS:
|
||||
void on_lineEdit_returnPressed();
|
||||
|
||||
private:
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
emb::stdout_write_type write;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user