nextpnr/gui/fpgaviewwidget.cc

166 lines
4.5 KiB
C++
Raw Normal View History

2018-06-07 04:53:52 +08:00
#include "fpgaviewwidget.h"
2018-06-06 03:03:06 +08:00
#include <QCoreApplication>
2018-06-07 04:53:52 +08:00
#include <QMouseEvent>
2018-06-06 03:03:06 +08:00
#include <math.h>
2018-06-11 00:33:39 +08:00
#include "mainwindow.h"
2018-06-06 03:03:06 +08:00
FPGAViewWidget::FPGAViewWidget(QWidget *parent)
2018-06-07 04:53:52 +08:00
: QOpenGLWidget(parent), m_xMove(0), m_yMove(0), m_zDistance(1.0)
2018-06-06 03:03:06 +08:00
{
2018-06-11 01:56:17 +08:00
design = qobject_cast<MainWindow*>(parentWidget()->parentWidget()->parentWidget()->parentWidget())->getDesign();
2018-06-06 03:03:06 +08:00
}
2018-06-07 04:53:52 +08:00
FPGAViewWidget::~FPGAViewWidget() {}
2018-06-06 03:03:06 +08:00
2018-06-07 04:53:52 +08:00
QSize FPGAViewWidget::minimumSizeHint() const { return QSize(640, 480); }
2018-06-06 03:03:06 +08:00
2018-06-07 04:53:52 +08:00
QSize FPGAViewWidget::sizeHint() const { return QSize(640, 480); }
2018-06-06 03:03:06 +08:00
void FPGAViewWidget::setXTranslation(float t_x)
{
2018-06-07 04:53:52 +08:00
if (t_x != m_xMove) {
2018-06-06 03:03:06 +08:00
m_xMove = t_x;
update();
}
}
void FPGAViewWidget::setYTranslation(float t_y)
{
2018-06-07 04:53:52 +08:00
if (t_y != m_yMove) {
2018-06-06 03:03:06 +08:00
m_yMove = t_y;
update();
}
}
void FPGAViewWidget::setZoom(float t_z)
{
2018-06-07 04:53:52 +08:00
if (t_z != m_zDistance) {
2018-06-06 03:03:06 +08:00
m_zDistance -= t_z;
2018-06-07 04:53:52 +08:00
if (m_zDistance < 0.1f)
2018-06-06 03:03:06 +08:00
m_zDistance = 0.1f;
2018-06-07 04:53:52 +08:00
if (m_zDistance > 10.0f)
2018-06-06 03:03:06 +08:00
m_zDistance = 10.0f;
update();
}
}
void FPGAViewWidget::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(1.0, 1.0, 1.0, 0.0);
}
2018-06-11 01:56:17 +08:00
void FPGAViewWidget::drawElement(const GraphicElement &el)
{
float scale = 1.0, offset = 0.0;
if (el.type == GraphicElement::G_BOX) {
glBegin(GL_LINES);
glVertex3f((offset + scale * el.x1), (offset + scale * el.y1), 0.0f);
glVertex3f((offset + scale * el.x2), (offset + scale * el.y1), 0.0f);
glVertex3f((offset + scale * el.x2), (offset + scale * el.y1), 0.0f);
glVertex3f((offset + scale * el.x2), (offset + scale * el.y2), 0.0f);
glVertex3f((offset + scale * el.x2), (offset + scale * el.y2), 0.0f);
glVertex3f((offset + scale * el.x1), (offset + scale * el.y2), 0.0f);
glVertex3f((offset + scale * el.x1), (offset + scale * el.y2), 0.0f);
glVertex3f((offset + scale * el.x1), (offset + scale * el.y1), 0.0f);
glEnd();
}
if (el.type == GraphicElement::G_LINE) {
glBegin(GL_LINES);
glVertex3f((offset + scale * el.x1), (offset + scale * el.y1), 0.0f);
glVertex3f((offset + scale * el.x2), (offset + scale * el.y2), 0.0f);
glEnd();
}
}
2018-06-06 03:03:06 +08:00
void FPGAViewWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(m_xMove, m_yMove, -10.0);
2018-06-07 04:53:52 +08:00
glScalef(m_zDistance, m_zDistance, 0.0f);
2018-06-11 01:56:17 +08:00
// Grid
glColor3f(0.9, 0.9, 0.9);
2018-06-06 03:03:06 +08:00
glBegin(GL_LINES);
2018-06-07 04:53:52 +08:00
for (float i = -100; i <= 100; i += 0.1) {
2018-06-06 03:03:06 +08:00
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);
}
2018-06-11 01:56:17 +08:00
glColor3f(0.7, 0.7, 0.7);
2018-06-07 04:53:52 +08:00
for (int i = -100; i <= 100; i += 1) {
2018-06-06 03:03:06 +08:00
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);
}
glEnd();
2018-06-11 01:56:17 +08:00
glColor3f(0.1, 0.1, 0.1);
glLineWidth(0.1);
// Draw Bels
for (auto bel : design->chip.getBels()) {
for (auto &el : design->chip.getBelGraphics(bel))
drawElement(el);
}
// Draw Frame Graphics
for (auto &el : design->chip.getFrameGraphics())
drawElement(el);
2018-06-06 03:03:06 +08:00
}
void FPGAViewWidget::resizeGL(int width, int height)
{
m_windowWidth = width;
m_windowHeight = height;
glViewport(0, 0, m_windowWidth, m_windowHeight);
float aspect = width * 1.0 / height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
2018-06-07 04:53:52 +08:00
glOrtho(-1.0 * aspect, +1.0 * aspect, -1.0, +1.0, 1.0, 15.0);
2018-06-06 03:03:06 +08:00
glMatrixMode(GL_MODELVIEW);
}
void FPGAViewWidget::mousePressEvent(QMouseEvent *event)
{
m_lastPos = event->pos();
}
void FPGAViewWidget::mouseMoveEvent(QMouseEvent *event)
{
int dx = event->x() - m_lastPos.x();
int dy = event->y() - m_lastPos.y();
float dx_scale = dx * (1 / (float)640);
float dy_scale = -dy * (1 / (float)480);
2018-06-07 04:53:52 +08:00
if (event->buttons() & Qt::LeftButton) {
2018-06-06 03:03:06 +08:00
float xpos = m_xMove + dx_scale;
float ypos = m_yMove + dy_scale;
2018-06-07 04:53:52 +08:00
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);
2018-06-06 03:03:06 +08:00
}
m_lastPos = event->pos();
}
void FPGAViewWidget::wheelEvent(QWheelEvent *event)
{
QPoint degree = event->angleDelta() / 8;
2018-06-07 04:53:52 +08:00
if (!degree.isNull()) {
2018-06-06 03:03:06 +08:00
QPoint step = degree / 15;
setZoom(step.y() * -0.1f);
}
}