BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
NodeEditorConnection.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/SampleDesigner/NodeEditorConnection.cpp
6 //! @brief Implements class NodeEditorConnection
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Utils/Assert.h"
18 #include <QBrush>
19 #include <QGraphicsScene>
20 #include <QPainter>
21 #include <QPen>
22 
23 NodeEditorConnection::NodeEditorConnection(QGraphicsItem* parent, QGraphicsScene* scene)
24  : QGraphicsPathItem(parent), m_port1(0), m_port2(0)
25 {
26  setFlag(QGraphicsItem::ItemIsSelectable, true);
27  setPen(QPen(Qt::darkGray, 2));
28  setBrush(Qt::NoBrush);
29  setZValue(-1);
30  if (scene)
31  scene->addItem(this);
32 }
33 
35 {
36  if (m_port1)
37  m_port1->remove(this);
38 
39  if (m_port2)
40  m_port2->remove(this);
41 }
42 
43 void NodeEditorConnection::setPos1(const QPointF& p)
44 {
45  pos1 = p;
46 }
47 
48 void NodeEditorConnection::setPos2(const QPointF& p)
49 {
50  pos2 = p;
51 }
52 
54 {
55  m_port1 = p;
56  m_port1->append(this);
57  setPos1(p->scenePos());
58 }
59 
61 {
62  m_port2 = p;
63  m_port2->append(this);
64  setPos2(p->scenePos());
65 }
66 
68 {
69  pos1 = m_port1->scenePos();
70  pos2 = m_port2->scenePos();
71 }
72 
74 {
75  QPainterPath p;
76  p.moveTo(pos1);
77  qreal dx = pos2.x() - pos1.x();
78  dx = qMax(dx, 200.);
79  QPointF ctr1(pos1.x() + dx * 0.25, pos1.y());
80  QPointF ctr2(pos2.x() - dx * 0.25, pos2.y());
81  p.cubicTo(ctr1, ctr2, pos2);
82  setPath(p);
83 }
84 
86 {
87  return m_port1;
88 }
89 
91 {
92  return m_port2;
93 }
94 
96 {
98  return (m_port1->isInput() ? m_port1 : m_port2);
99 }
100 
102 {
103  ASSERT(m_port1 && m_port2);
104  return (m_port1->isOutput() ? m_port1 : m_port2);
105 }
106 
107 void NodeEditorConnection::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
108  QWidget* widget)
109 {
110  Q_UNUSED(option)
111  Q_UNUSED(widget)
112 
113  painter->setPen(QPen(Qt::darkGray, 2));
114  painter->setBrush(Qt::NoBrush);
115 
116  if (isSelected()) {
117  painter->setPen(Qt::DashLine);
118  }
119 
120  painter->drawPath(path());
121 }
122 
124 {
125  ASSERT(inputPort() != outputPort());
126  ConnectableView* result = dynamic_cast<ConnectableView*>(inputPort()->parentItem());
127  ASSERT(result);
128  return result;
129 }
130 
132 {
133  ASSERT(inputPort() != outputPort());
134  ConnectableView* result = dynamic_cast<ConnectableView*>(outputPort()->parentItem());
135  ASSERT(result);
136  return result;
137 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
Defines class ConnectableView.
Defines class NodeEditorConnection.
view of ISampleNode's with rectangular shape and node functionality
void setPort2(NodeEditorPort *p)
ConnectableView * getChildView()
returns child view, i.e. the view which owns output port of given connection
NodeEditorPort * m_port2
NodeEditorPort * inputPort()
NodeEditorPort * m_port1
NodeEditorPort * port2() const
NodeEditorPort * outputPort()
void setPos1(const QPointF &p)
void setPort1(NodeEditorPort *p)
ConnectableView * getParentView()
returns parent view, i.e. the view which owns input port of given connection
NodeEditorConnection(QGraphicsItem *parent=0, QGraphicsScene *scene=0)
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
void setPos2(const QPointF &p)
NodeEditorPort * port1() const
void append(NodeEditorConnection *connection)
void remove(NodeEditorConnection *connection)