aboutsummaryrefslogtreecommitdiff
path: root/qtmips_gui/coreview/connection.cpp
blob: 85f7a9653bf73d2d28a6ef39e90834efb3340443 (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
#include "connection.h"
#include <cmath>

using namespace coreview;

Connector::Connector(qreal angle) {
    ang = angle;
}

void Connector::setPos(qreal x, qreal y) {
    qx = x;
    qy = y;
    emit updated(QPointF(qx, qy));
}

qreal Connector::x() const {
    return qx;
}

qreal Connector::y() const {
    return qy;
}

QLineF Connector::vector() const {
    return QLineF(point(), QPointF(x() + cos(ang), y() + sin(ang)));
}

QPointF Connector::point() const {
    return QPointF(qx, qy);
}

qreal Connector::angle() const {
    return ang;
}

Connection::Connection(const Connector *a, const Connector *b) : QGraphicsObject(nullptr) {
    pen_width = 1;

    ang_start = a->angle();
    ang_end = b->angle();

    connect(a, SIGNAL(updated(QPointF)), this, SLOT(moved_start(QPointF)));
    connect(b, SIGNAL(updated(QPointF)), this, SLOT(moved_end(QPointF)));
    moved_start(a->point());
    moved_end(b->point());
}

void Connection::setHasText(bool has) {
    if (has && value == nullptr) {
        value = new QGraphicsSimpleTextItem(this);
        value->setText(text);
    } else if (!has && value != nullptr) {
        delete value;
    }
}

void Connection::setText(QString val) {
    text = val;
    if (value != nullptr)
        value->setText(val);
}

void Connection::setAxes(QVector<QLineF> axes) {
    break_axes = axes;
}

void Connection::moved_start(QPointF p) {
    p_start = p;
    recalc_line();
}

void Connection::moved_end(QPointF p) {
    p_end = p;
    recalc_line();
}

QRectF Connection::boundingRect() const {
    QRectF rect;
    for (int i = 0; i < (points.size() - 1); i++) {
        qreal x = points[i].x() > points[i+1].x() ? points[i].x() : points[i+1].x();
        qreal y = points[i].y() > points[i+1].y() ? points[i].y() : points[i+1].y();
        rect |= QRectF(x - pen_width/2.0, y - pen_width/2.0, fabs(points[i].x() - points[i+1].x()) + pen_width, fabs(points[i].y() - points[i+1].y()) + pen_width);
    }
    return rect;
}

void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option __attribute__((unused)), QWidget *widget __attribute__((unused))) {
    QPen pen;
    pen.setWidth(pen_width);
    // TODO color?
    painter->setPen(pen);

    painter->drawPolyline(QPolygonF(points));
}

void Connection::recalc_line() {
    points.clear();

    points.append(p_start);

    QLineF cur_l(p_start, QPointF(p_start.x() + cos(ang_start), p_start.y() + sin(ang_start)));
    for (int i = 0; i < break_axes.size(); i++) {
        recalc_line_add_point(cur_l, break_axes[i]);
        cur_l = break_axes[i];
    }
    recalc_line_add_point(cur_l, QLineF(QPoint(p_end.x() + cos(ang_end), p_end.y() + sin(ang_end)), p_end));

    points.append(p_end);
}

void Connection::recalc_line_add_point(const QLineF &l1, const QLineF &l2) {
    QPointF intersec;
    if (l1.intersect(l2, &intersec) != QLineF::NoIntersection)
        points.append(intersec);
}