BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
NodeEditorPort.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/NodeEditorPort.cpp
6 //! @brief Implements class NodeEditorPort
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 
19 #include <QGraphicsScene>
20 #include <QPainter>
21 #include <QPen>
22 
23 NodeEditorPort::NodeEditorPort(QGraphicsItem* parent, const QString& name,
25  NodeEditorPort::EPortType port_type)
26  : QGraphicsPathItem(parent)
27  , m_name(name)
28  , m_direction(direction)
29  , m_port_type(port_type)
30  , m_radius(0)
31  , m_margin(0)
32  , m_label(nullptr)
33 {
34  m_radius = StyleUtils::SizeOfLetterM().width() * 0.4;
35  m_margin = m_radius * 0.5;
36  m_color = getPortTypeColor(port_type);
37 
38  QPainterPath p;
39  p.addEllipse(-m_radius, -m_radius, 2 * m_radius, 2 * m_radius);
40  setPath(p);
41 
42  setPen(QPen(m_color.darker(180)));
43  setBrush(m_color);
44 
45  setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
46 
47  if (!m_name.isEmpty()) {
49  }
50 }
51 
53 {
54  while (m_connections.size() > 0) {
55  auto conn = m_connections.last();
56  conn->setSelected(false);
57  delete conn;
58  }
59 }
60 
62 {
63  return (m_direction == OUTPUT);
64 }
65 
67 {
68  return !isOutput();
69 }
70 
72 {
73  if (m_connections.contains(connection))
74  m_connections.remove(m_connections.indexOf(connection));
75 }
76 
78 {
79  m_connections.append(connection);
80 }
81 
83 {
84  for (auto conn : m_connections)
85  if (conn->port1() == other || conn->port2() == other)
86  return true;
87 
88  return false;
89 }
90 
92 {
93  switch (port_type) {
94  case DEFAULT:
95  return QColor(Qt::gray);
96  case INTERFERENCE:
97  return QColor(Qt::yellow);
98  case PARTICLE_LAYOUT:
99  return QColor(Qt::green);
100  case FORM_FACTOR:
101  return QColor(Qt::blue);
102  case TRANSFORMATION:
103  return QColor(Qt::magenta);
104  default:
105  return QColor(Qt::red);
106  }
107 }
108 
109 QVariant NodeEditorPort::itemChange(GraphicsItemChange change, const QVariant& value)
110 {
111  if (change == ItemScenePositionHasChanged) {
112  for (auto conn : m_connections) {
113  conn->updatePosFromPorts();
114  conn->updatePath();
115  }
116  }
117  return value;
118 }
119 
121 {
122  if (!m_label)
123  m_label = new QGraphicsTextItem(this);
124  m_label->setPlainText(name);
125  QFont serifFont("Monospace", DesignerHelper::getPortFontSize(), QFont::Normal);
126  m_label->setFont(serifFont);
127 
128  if (isOutput()) {
129  m_label->setPos(-m_radius - m_margin - m_label->boundingRect().width(),
130  -m_label->boundingRect().height() / 2);
131  } else {
132  m_label->setPos(m_radius + m_margin, -m_label->boundingRect().height() / 2);
133  }
134 }
Defines class DesignerHelper.
Defines class NodeEditorConnection.
Defines class NodeEditorPort.
DefinesStyleUtils namespace.
static int getPortFontSize()
void append(NodeEditorConnection *connection)
virtual ~NodeEditorPort()
NodeEditorPort(QGraphicsItem *parent=0, const QString &name="unnamed", EPortDirection direction=INPUT, EPortType port_type=DEFAULT)
void remove(NodeEditorConnection *connection)
EPortType
type of ports, same type can be connected together
QVector< NodeEditorConnection * > m_connections
EPortDirection m_direction
static QColor getPortTypeColor(NodeEditorPort::EPortType port_type)
EPortDirection
port direction
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
void setLabel(QString name)
QGraphicsTextItem * m_label
QString const & name(EShape k)
Definition: particles.cpp:21
QSize SizeOfLetterM(const QWidget *widget=nullptr)
Returns size of largest letter of default system font.
Definition: StyleUtils.cpp:110