aboutsummaryrefslogtreecommitdiff
path: root/qtmips_gui/lcddisplayview.cpp
blob: b18661079bcfb9a9d624eea2c1f046aab4c1a102 (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
#include <QPainter>
#include <QPaintEvent>
#include <QStyle>
#include "lcddisplay.h"
#include "lcddisplayview.h"

LcdDisplayView::LcdDisplayView(QWidget *parent) : Super(parent) {
    setMinimumSize(100, 100);
    fb_pixels = nullptr;
    scale_x = 1.0;
    scale_y = 1.0;
}

LcdDisplayView::~LcdDisplayView() {
    if (fb_pixels != nullptr)
        delete fb_pixels;
}

void LcdDisplayView::setup(machine::LcdDisplay *lcd_display) {
    if (lcd_display == nullptr)
        return;
    connect(lcd_display, SIGNAL(pixel_update(uint,uint,uint,uint,uint)),
            this, SLOT(pixel_update(uint,uint,uint,uint,uint)));
    if (fb_pixels != nullptr)
        delete fb_pixels;
    fb_pixels = nullptr;
    fb_pixels = new QImage(lcd_display->width(),
                           lcd_display->height(), QImage::Format_RGB32);
    fb_pixels->fill(qRgb(0, 0, 0));
    update_scale();
    update();
}

void LcdDisplayView::pixel_update(uint x, uint y, uint r, uint g, uint b) {
    int x1, y1, x2, y2;
    if (fb_pixels != nullptr) {
        fb_pixels->setPixel(x, y, qRgb(r, g, b));
        x1 = x * scale_x - 2;
        if (x1 < 0)
            x1 = 0;
        x2 = x * scale_x + 2;
        if (x2 > width())
            x2 = width();
        y1 = y * scale_y - 2;
        if (y1 < 0)
            y1 = 0;
        y2 = y * scale_y + 2;
        if (y2 > height())
            y2 = height();
        update(x1, y1, x2 - x1, y2 - y1);
    }
}

void LcdDisplayView::update_scale() {
    if (fb_pixels != nullptr) {
        if ((fb_pixels->width() != 0) && (fb_pixels->height() != 0)) {
            scale_x = (float)width() / fb_pixels->width();
            scale_y = (float)height() / fb_pixels->height();
            return;
        }
    }
    scale_x = 1.0;
    scale_y = 1.0;
}

void LcdDisplayView::paintEvent(QPaintEvent *event) {
    if (fb_pixels == nullptr)
        return Super::paintEvent(event);
    if (fb_pixels->width() == 0)
        return Super::paintEvent(event);

    QPainter painter(this);
    painter.drawImage(rect(), *fb_pixels);
#if 0
    painter.setPen(QPen(QColor(255, 255, 0)));
    painter.drawLine(event->rect().topLeft(),event->rect().topRight());
    painter.drawLine(event->rect().topLeft(),event->rect().bottomLeft());
    painter.drawLine(event->rect().topLeft(),event->rect().bottomRight());
#endif
}

void LcdDisplayView::resizeEvent(QResizeEvent *event) {
    Super::resizeEvent(event);
    update_scale();
}

uint LcdDisplayView::fb_width() {
    if (fb_pixels == nullptr)
        return 0;
    return fb_pixels->width();
}

uint LcdDisplayView::fb_height() {
    if (fb_pixels == nullptr)
        return 0;
    return fb_pixels->height();
}