File indexing completed on 2024-04-28 13:39:10

0001 //
0002 // C++ Implementation: junctionflownode
0003 //
0004 // Description:
0005 //
0006 //
0007 // Copyright: See COPYING file that comes with this distribution
0008 //
0009 //
0010 #include "junctionflownode.h"
0011 #include "connector.h"
0012 #include "flowconnector.h"
0013 
0014 #include <QPainter>
0015 
0016 JunctionFlowNode::JunctionFlowNode(ICNDocument *_icnView, int dir, const QPoint &pos, QString *id)
0017     : FPNode(_icnView, Node::fp_junction, dir, pos, id)
0018 {
0019 }
0020 
0021 JunctionFlowNode::~JunctionFlowNode()
0022 {
0023 }
0024 
0025 void JunctionFlowNode::initPoints()
0026 {
0027     setPoints(QPolygon(QRect(-4, -4, 9, 9)));
0028 }
0029 
0030 bool JunctionFlowNode::acceptInput() const
0031 {
0032     return true;
0033 }
0034 
0035 bool JunctionFlowNode::acceptOutput() const
0036 {
0037     return true;
0038 }
0039 
0040 void JunctionFlowNode::checkForRemoval(Connector *connector)
0041 {
0042     FPNode::checkForRemoval(connector);
0043 
0044     if (!m_outputConnector)
0045         removeNode();
0046 }
0047 
0048 inline QPolygon arrowPoints(int dir)
0049 {
0050     QPolygon pa(3);
0051     switch (dir) {
0052     case 0:
0053         pa[0] = QPoint(3, 0);
0054         pa[1] = QPoint(0, 2);
0055         pa[2] = QPoint(0, -2);
0056         break;
0057     case 180:
0058         pa[0] = QPoint(-3, 0);
0059         pa[1] = QPoint(0, 2);
0060         pa[2] = QPoint(0, -2);
0061         break;
0062     case 90:
0063         pa[0] = QPoint(2, 0);
0064         pa[1] = QPoint(-2, 0);
0065         pa[2] = QPoint(0, 3);
0066         break;
0067     case 270:
0068         pa[0] = QPoint(2, 0);
0069         pa[1] = QPoint(-2, 0);
0070         pa[2] = QPoint(0, -3);
0071         break;
0072     };
0073     return pa;
0074 }
0075 
0076 void JunctionFlowNode::drawShape(QPainter &p)
0077 {
0078     const int _x = int(x());
0079     const int _y = int(y());
0080 
0081     if (!m_inFlowConnList.isEmpty()) {
0082         const FlowConnectorList::iterator end = m_inFlowConnList.end();
0083         for (FlowConnectorList::iterator it = m_inFlowConnList.begin(); it != end; ++it) {
0084             Connector *connector = *it;
0085             if (!connector)
0086                 continue;
0087 
0088             // Work out the direction of the connector
0089             const QPointList points = connector->connectorPoints(false);
0090 
0091             const int count = points.size();
0092             if (count < 2)
0093                 continue;
0094 
0095             QPoint end_0 = points[count - 1];
0096             QPoint end_1 = points[count - 2];
0097 
0098             QPolygon pa;
0099             if (end_0.x() < end_1.x()) {
0100                 pa = arrowPoints(180);
0101                 pa.translate(4, 0);
0102             } else if (end_0.x() > end_1.x()) {
0103                 pa = arrowPoints(0);
0104                 pa.translate(-4, 0);
0105             } else if (end_0.y() < end_1.y()) {
0106                 pa = arrowPoints(270);
0107                 pa.translate(0, 4);
0108             } else if (end_0.y() > end_1.y()) {
0109                 pa = arrowPoints(90);
0110                 pa.translate(0, -4);
0111             } else
0112                 continue;
0113 
0114             pa.translate(_x, _y);
0115             p.setPen(connector->isSelected() ? m_selectedColor : Qt::black);
0116             p.drawPolygon(pa);
0117         }
0118         return;
0119     }
0120 
0121     if (m_dir == 0)
0122         p.drawLine(_x, _y, _x - 8, _y);
0123     else if (m_dir == 90)
0124         p.drawLine(_x, _y, _x, _y - 8);
0125     else if (m_dir == 180)
0126         p.drawLine(_x, _y, _x + 8, _y);
0127     else if (m_dir == 270)
0128         p.drawLine(_x, _y, _x, _y + 8);
0129 }