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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file GUI/coregui/Views/SampleDesigner/ConnectableView.cpp
//! @brief Implements class ConnectableView
//!
//! @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/ConnectableView.h"
#include "GUI/coregui/Models/SessionItem.h"
#include "GUI/coregui/Views/SampleDesigner/DesignerHelper.h"
#include "GUI/coregui/Views/SampleDesigner/NodeEditorConnection.h"
#include "GUI/coregui/utils/GUIHelpers.h"
#include "GUI/coregui/utils/StyleUtils.h"
#include <QObject>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <iostream>
ConnectableView::ConnectableView(QGraphicsItem* parent, QRectF rect)
: IView(parent), m_name("Unnamed"), m_color(Qt::gray), m_rect(rect), m_roundpar(0),
m_label_vspace(0)
{
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);
m_label_vspace = StyleUtils::SizeOfLetterM().height() * 2.5;
m_roundpar = StyleUtils::SizeOfLetterM().height() / 3.0;
}
void ConnectableView::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
QWidget* widget)
{
Q_UNUSED(widget);
painter->setPen(Qt::gray);
if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus)) {
painter->setPen(Qt::DashLine);
}
painter->setBrush(DesignerHelper::getDecorationGradient(m_color, getRectangle()));
painter->drawRoundedRect(getRectangle(), m_roundpar, m_roundpar);
if (m_label.isEmpty())
return;
painter->setPen(Qt::black);
double width = getRectangle().width() * 0.9;
double yoffset = StyleUtils::SizeOfLetterM().height() / 2; // space above the label
double height = m_label_vspace - yoffset;
QFont serifFont("Monospace", DesignerHelper::getLabelFontSize(), QFont::Normal);
painter->setFont(serifFont);
QRectF textRect(getRectangle().x() + (getRectangle().width() - width) / 2.,
getRectangle().y() + yoffset, width, height);
painter->drawText(textRect, Qt::AlignCenter, m_label);
}
NodeEditorPort* ConnectableView::addPort(const QString& name,
NodeEditorPort::EPortDirection direction,
NodeEditorPort::EPortType port_type)
{
NodeEditorPort* port = new NodeEditorPort(this, name, direction, port_type);
if (direction == NodeEditorPort::INPUT) {
m_input_ports.append(port);
} else if (direction == NodeEditorPort::OUTPUT) {
m_output_ports.append(port);
} else {
throw GUIHelpers::Error("ConnectableView::addPort() -> Unknown port type");
}
setPortCoordinates();
return port;
}
void ConnectableView::setLabel(const QString& name)
{
m_label = name;
setPortCoordinates();
}
void ConnectableView::connectInputPort(ConnectableView* other, int port_number)
{
ASSERT(other);
if (port_number >= m_input_ports.size())
throw GUIHelpers::Error("ConnectableView::connectInputPort() -> Wrong input port number");
if (other->getOutputPorts().size() != 1)
throw GUIHelpers::Error("ConnectableView::connectInputPort() -> Wrong output port number");
if (port_number < 0)
return;
NodeEditorPort* input = m_input_ports.at(port_number);
NodeEditorPort* output = other->getOutputPorts().at(0);
if (!input->isConnected(output)) {
NodeEditorConnection* conn = new NodeEditorConnection(0, scene());
conn->setPort2(input);
conn->setPort1(output);
conn->updatePath();
}
}
int ConnectableView::getInputPortIndex(NodeEditorPort* port)
{
return m_input_ports.indexOf(port);
}
// calculation of y-pos for ports
void ConnectableView::setPortCoordinates()
{
if (!getNumberOfPorts())
return;
// without main label ports can be placed over all rectangle vertical space
double hspace = getRectangle().height();
if (!getLabel().isEmpty())
hspace -= m_label_vspace;
double nintervals =
getNumberOfPorts() + 2; // one spare interval for margin between input/output ports
double dy = hspace / double(nintervals);
double ypos = getRectangle().height() - hspace + dy;
if (getNumberOfPorts() == 1) {
// if total number of ports is 1, place it in the middle
ypos = getRectangle().height() - hspace + hspace / 2;
}
int nOutPorts = getNumberOfOutputPorts();
int nport(0);
for (QGraphicsItem* item : childItems()) {
NodeEditorPort* port = dynamic_cast<NodeEditorPort*>(item);
if (!port)
continue;
if (port->isOutput()) {
port->setPos(getRectangle().width(), ypos);
} else {
if (nport == nOutPorts && nOutPorts != 0)
ypos += dy; // additional margin between output and input ports
port->setPos(0.0, ypos);
}
ypos += dy;
nport++;
}
}
int ConnectableView::getNumberOfPorts()
{
return m_input_ports.size() + m_output_ports.size();
}
int ConnectableView::getNumberOfOutputPorts()
{
return m_output_ports.size();
}
int ConnectableView::getNumberOfInputPorts()
{
return m_input_ports.size();
}
void ConnectableView::update_appearance()
{
setLabel(hyphenate(m_item->displayName()));
IView::update_appearance();
}
QString ConnectableView::hyphenate(const QString& name) const
{
QRegExp capital_letter("[A-Z]");
QRegExp number("[0-9]");
int next_capital = capital_letter.indexIn(name, 1);
int next_number = number.indexIn(name, 1);
if (next_capital > 0 && next_capital < name.size() - 2) {
int first_split_index =
(next_number > 0 && next_number < next_capital) ? next_number : next_capital;
QString result = name.left(first_split_index) + QString("\n")
+ name.right(name.size() - first_split_index);
return result;
}
return name;
}
|