File: NodeEditorConnection.cpp

package info (click to toggle)
bornagain 1.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,800 kB
  • sloc: cpp: 469,684; python: 38,920; xml: 805; awk: 630; sh: 286; ansic: 37; makefile: 25
file content (137 lines) | stat: -rw-r--r-- 3,408 bytes parent folder | download
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
// ************************************************************************** //
//
//  BornAgain: simulate and fit scattering at grazing incidence
//
//! @file      GUI/coregui/Views/SampleDesigner/NodeEditorConnection.cpp
//! @brief     Implements class NodeEditorConnection
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************** //

#include "GUI/coregui/Views/SampleDesigner/NodeEditorConnection.h"
#include "Base/Utils/Assert.h"
#include "GUI/coregui/Views/SampleDesigner/ConnectableView.h"
#include <QBrush>
#include <QGraphicsScene>
#include <QPainter>
#include <QPen>

NodeEditorConnection::NodeEditorConnection(QGraphicsItem* parent, QGraphicsScene* scene)
    : QGraphicsPathItem(parent), m_port1(0), m_port2(0)
{
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setPen(QPen(Qt::darkGray, 2));
    setBrush(Qt::NoBrush);
    setZValue(-1);
    if (scene)
        scene->addItem(this);
}

NodeEditorConnection::~NodeEditorConnection()
{
    if (m_port1)
        m_port1->remove(this);

    if (m_port2)
        m_port2->remove(this);
}

void NodeEditorConnection::setPos1(const QPointF& p)
{
    pos1 = p;
}

void NodeEditorConnection::setPos2(const QPointF& p)
{
    pos2 = p;
}

void NodeEditorConnection::setPort1(NodeEditorPort* p)
{
    m_port1 = p;
    m_port1->append(this);
    setPos1(p->scenePos());
}

void NodeEditorConnection::setPort2(NodeEditorPort* p)
{
    m_port2 = p;
    m_port2->append(this);
    setPos2(p->scenePos());
}

void NodeEditorConnection::updatePosFromPorts()
{
    pos1 = m_port1->scenePos();
    pos2 = m_port2->scenePos();
}

void NodeEditorConnection::updatePath()
{
    QPainterPath p;
    p.moveTo(pos1);
    qreal dx = pos2.x() - pos1.x();
    dx = qMax(dx, 200.);
    QPointF ctr1(pos1.x() + dx * 0.25, pos1.y());
    QPointF ctr2(pos2.x() - dx * 0.25, pos2.y());
    p.cubicTo(ctr1, ctr2, pos2);
    setPath(p);
}

NodeEditorPort* NodeEditorConnection::port1() const
{
    return m_port1;
}

NodeEditorPort* NodeEditorConnection::port2() const
{
    return m_port2;
}

NodeEditorPort* NodeEditorConnection::inputPort()
{
    ASSERT(m_port1 && m_port2);
    return (m_port1->isInput() ? m_port1 : m_port2);
}

NodeEditorPort* NodeEditorConnection::outputPort()
{
    ASSERT(m_port1 && m_port2);
    return (m_port1->isOutput() ? m_port1 : m_port2);
}

void NodeEditorConnection::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
                                 QWidget* widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)

    painter->setPen(QPen(Qt::darkGray, 2));
    painter->setBrush(Qt::NoBrush);

    if (isSelected()) {
        painter->setPen(Qt::DashLine);
    }

    painter->drawPath(path());
}

ConnectableView* NodeEditorConnection::getParentView()
{
    ASSERT(inputPort() != outputPort());
    ConnectableView* result = dynamic_cast<ConnectableView*>(inputPort()->parentItem());
    ASSERT(result);
    return result;
}

ConnectableView* NodeEditorConnection::getChildView()
{
    ASSERT(inputPort() != outputPort());
    ConnectableView* result = dynamic_cast<ConnectableView*>(outputPort()->parentItem());
    ASSERT(result);
    return result;
}