aboutsummaryrefslogtreecommitdiff
path: root/qtmips_gui/coreview/instructionview.cpp
blob: 0c9ec687aff5318055e421a0920e0b3d4d0dc768 (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
#include "instructionview.h"
#include <QPainter>
#include <QFont>

using namespace coreview;

//////////////////////
#define WIDTH 120
#define HEIGHT 14
#define ROUND 5
#define GAP 2
#define PENW 1
//////////////////////

InstructionView::InstructionView() : QGraphicsObject(nullptr), text(this) {
    QFont f;
    f.setPointSize(6);
    text.setFont(f);

    instruction_update(machine::Instruction()); // Initialize to NOP
}

QRectF InstructionView::boundingRect() const {
    return QRectF(-WIDTH/2 - PENW/2, -PENW/2, WIDTH + PENW, HEIGHT + PENW);
}

void InstructionView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option __attribute__((unused)), QWidget *widget __attribute__((unused))) {
    painter->setPen(QPen(QColor(240, 240, 240)));
    painter->setBrush(QBrush(QColor(240, 240, 240)));
    painter->drawRoundRect(-WIDTH/2, 0, WIDTH, HEIGHT, ROUND, ROUND);
}

void InstructionView::instruction_update(const machine::Instruction &i) {
    QRectF prev_box = boundingRect();
    text.setText(i.to_str());
    QRectF box = text.boundingRect();
    text.setPos(-box.width()/2, GAP);
    update(prev_box.united(boundingRect()));
}