aboutsummaryrefslogtreecommitdiffstats
path: root/gui/fpgaviewwidget.cc
blob: 58df05860870c0b5ae6bc416b4e1676793622455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "fpgaviewwidget.h"
#include <QApplication>
#include <QCoreApplication>
#include <QMouseEvent>
#include <QWidget>
#include <math.h>
#include "mainwindow.h"

FPGAViewWidget::FPGAViewWidget(QWidget *parent)
        : QOpenGLWidget(parent), m_xMove(0), m_yMove(0), m_zDistance(1.0)
{
    design = qobject_cast<MainWindow *>(getMainWindow())->getDesign();
}

QMainWindow *FPGAViewWidget::getMainWindow()
{
    QWidgetList widgets = qApp->topLevelWidgets();
    for (QWidgetList::iterator i = widgets.begin(); i != widgets.end(); ++i)
        if ((*i)->objectName() == "MainWindow")
            return (QMainWindow *)(*i);
    return NULL;
}

FPGAViewWidget::~FPGAViewWidget() {}

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) {
        m_xMove = t_x;
        update();
    }
}

void FPGAViewWidget::setYTranslation(float t_y)
{
    if (t_y != m_yMove) {
        m_yMove = t_y;
        update();
    }
}

void FPGAViewWidget::setZoom(float t_z)
{
    if (t_z != m_zDistance) {
        m_zDistance -= t_z;
        if (m_zDistance < 0.1f)
            m_zDistance = 0.1f;
        if (m_zDistance > 10.0f)
            m_zDistance = 10.0f;

        update();
    }
}

void FPGAViewWidget::initializeGL()
{
    initializeOpenGLFunctions();
    glClearColor(1.0, 1.0, 1.0, 0.0);
}

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();
    }
}

void FPGAViewWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(m_xMove, m_yMove, -10.0);
    glScalef(m_zDistance, m_zDistance, 0.0f);

    // Grid
    glColor3f(0.9, 0.9, 0.9);
    glBegin(GL_LINES);
    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.7, 0.7, 0.7);
    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);
        glVertex3f(100.0f, (float)i, 0.0f);
    }
    glEnd();

    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);
}

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();
    glOrtho(-1.0 * aspect, +1.0 * aspect, -1.0, +1.0, 1.0, 15.0);
    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);

    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);
    }
    m_lastPos = event->pos();
}

void FPGAViewWidget::wheelEvent(QWheelEvent *event)
{
    QPoint degree = event->angleDelta() / 8;

    if (!degree.isNull()) {
        QPoint step = degree / 15;
        setZoom(step.y() * -0.1f);
    }
}