aboutsummaryrefslogtreecommitdiff
path: root/qtmips_gui/coreview/multiplexer.cpp
blob: 57fa443af50f6695047c3c5e5e01666bd256124f (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
#include "multiplexer.h"

using namespace coreview;

//////////////////////
#define WIDTH 20
#define HEIGHT 20
#define PENW 1
//////////////////////

Multiplexer::Multiplexer(unsigned size) {
    this->size = size;
    seton = 0;
    ctlfrom = false;
    con_ctl = new Connector();
    con_out = new Connector();
    con_in = new Connector*[size];
    for (unsigned i = 0; i < size; i++)
        con_in[i] = new Connector();
    setPos(x(), y()); // Set connectors possitions
}

Multiplexer::~Multiplexer() {
    delete con_ctl;
    delete con_out;
    for (unsigned i = 0; i < size; i++)
        delete con_in[i];
    delete con_in;
}

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

void Multiplexer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
    // Draw pointing line first so it isn't over the border lines
    painter->setPen(QColor(200, 200, 200));
    painter->drawLine(0, (HEIGHT / 2) + (seton * HEIGHT), WIDTH, (HEIGHT * size) / 2);

    painter->setPen(QColor());
    painter->drawLine(0, 0, 0, (HEIGHT * size)); // (|)
    painter->drawLine(0, 0, WIDTH, WIDTH); // (\)
    painter->drawLine(0, (HEIGHT * size), WIDTH, (HEIGHT * size) - WIDTH); // (/)
    painter->drawLine(WIDTH, WIDTH, WIDTH, (HEIGHT * size) - WIDTH); // (|)
}

void Multiplexer::setPos(qreal x, qreal y) {
    QGraphicsItem::setPos(x, y);
    if (ctlfrom)
        con_ctl->setPos(x + (WIDTH / 2), y + (WIDTH / 2));
    else
        con_ctl->setPos(x + (WIDTH / 2), y + (HEIGHT * size) - (WIDTH / 2));
    con_out->setPos(x + WIDTH, y + ((HEIGHT *size) / 2));
    for (unsigned i = 0; i < size; i++)
        con_in[i]->setPos(x, y + (HEIGHT / 2) + (i * HEIGHT));
}

const Connector *Multiplexer::connector_ctl() const {
    return con_ctl;
}

const Connector *Multiplexer::connector_out() const {
    return con_out;
}

const Connector *Multiplexer::connector_in(unsigned i) const {
    SANITY_ASSERT(i < size, "Multiplexer: requested out of range input connector");
    return con_in[i];
}

void Multiplexer::set(unsigned i) {
    seton = i;
    update(boundingRect());
}

void Multiplexer::setCtl(bool up) {
    ctlfrom = up;
    setPos(x(), y()); // Update connectors
}