File indexing completed on 2024-06-16 11:34:23

0001 //
0002 // C++ Implementation: electronicconnector
0003 //
0004 // Description:
0005 //
0006 //
0007 // Author: David Saxton, Alan Grimes, Zoltan Padrah <zoltan.padrah@gmail.com>, (C) 2008
0008 //
0009 // Copyright: See COPYING file that comes with this distribution
0010 //
0011 //
0012 #include "electronicconnector.h"
0013 
0014 #include "ecnode.h"
0015 #include "junctionnode.h"
0016 #include "wire.h"
0017 
0018 #include <algorithm>
0019 
0020 ElectronicConnector::ElectronicConnector(ECNode *startNode, ECNode *endNode, ICNDocument *_ICNDocument, QString *id)
0021     : Connector(startNode, endNode, _ICNDocument, id)
0022 {
0023     m_startEcNode = startNode;
0024     m_endEcNode = endNode;
0025 
0026     if (startNode && endNode) {
0027         connect(startNode, &ECNode::numPinsChanged, this, &ElectronicConnector::syncWiresWithNodes);
0028         connect(endNode, &ECNode::numPinsChanged, this, &ElectronicConnector::syncWiresWithNodes);
0029         syncWiresWithNodes();
0030     }
0031 }
0032 
0033 ElectronicConnector::~ElectronicConnector()
0034 {
0035 }
0036 
0037 void ElectronicConnector::syncWiresWithNodes()
0038 {
0039     ECNode *startEcNode = m_startEcNode;
0040     ECNode *endEcNode = m_endEcNode;
0041 
0042     if (!startEcNode || !endEcNode)
0043         return;
0044 
0045     // FIXME more dynamic_cast to avoid using type() member
0046     const bool isStartNodeJunction = dynamic_cast<JunctionNode *>(startEcNode) != nullptr;
0047     const bool isEndNodeJunction = dynamic_cast<JunctionNode *>(endEcNode) != nullptr;
0048 
0049     unsigned newNumWires = 0;
0050 
0051     if (isStartNodeJunction || isEndNodeJunction)
0052         newNumWires = std::max(startEcNode->numPins(), endEcNode->numPins());
0053     else
0054         newNumWires = std::min(startEcNode->numPins(), endEcNode->numPins());
0055 
0056     unsigned oldNumWires = m_wires.size();
0057 
0058     if (newNumWires == oldNumWires)
0059         return;
0060 
0061     // Critical section? ################3
0062     //  m_bIsSyncingWires = true;
0063     if (isStartNodeJunction)
0064         startEcNode->setNumPins(newNumWires);
0065 
0066     if (isEndNodeJunction)
0067         endEcNode->setNumPins(newNumWires);
0068 
0069     //  m_bIsSyncingWires = false;
0070     // ####################################
0071 
0072     if (newNumWires > oldNumWires) {
0073         m_wires.resize(newNumWires);
0074 
0075         for (unsigned i = oldNumWires; i < newNumWires; i++) {
0076             if (startEcNode->pin(i) && endEcNode->pin(i))
0077                 m_wires[i] = new Wire(startEcNode->pin(i), endEcNode->pin(i));
0078         }
0079     } else {
0080         for (unsigned i = newNumWires; i < oldNumWires; i++)
0081             delete m_wires[i];
0082 
0083         m_wires.resize(newNumWires);
0084     }
0085 
0086     updateConnectorLines();
0087     emit numWiresChanged(newNumWires);
0088 }
0089 
0090 #include "moc_electronicconnector.cpp"