File indexing completed on 2024-04-21 05:44:03

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2005 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef NODE_H
0012 #define NODE_H
0013 
0014 //#include <canvas.h> // 2018.10.16 - not needed
0015 #include "canvasitems.h"
0016 #include <QPointer>
0017 
0018 class CNItem;
0019 class Item;
0020 class ICNDocument;
0021 class ICNDocument;
0022 class Connector;
0023 class Node;
0024 class NodeData;
0025 class NodeGroup;
0026 class QTimer;
0027 
0028 typedef QList<QPointer<Connector>> ConnectorList;
0029 typedef QList<QPointer<Node>> NodeList;
0030 
0031 /**
0032 @short A standard node that can be associated with a Connector or a CNItem
0033 @author David Saxton
0034 */
0035 class Node : /* public QObject, */ public KtlQCanvasPolygon
0036 {
0037     Q_OBJECT
0038 public:
0039     // this shall disappear one day
0040     /**
0041      * Used for run-time identification of the node:
0042      * Can be electronic node (so has values of current, voltage, etc)
0043      * or a pic part node
0044      * this enum will be cleared soon
0045      */
0046     enum node_type { ec_pin, ec_junction, fp_in, fp_out, fp_junction };
0047 
0048     /**
0049      * @param dir the direction of the node; 0 degrees for left, 90 degrees for
0050      * up, etc in an anti-clockwise direction. An "up" node has the
0051      * wire-connection point at the top and the (component/flowpart)-end at the
0052      * bottom.
0053      */
0054     Node(ICNDocument *icnDocument, Node::node_type type, int dir, const QPoint &pos, QString *id = nullptr);
0055     ~Node() override;
0056 
0057     /**
0058      * Sets the node's visibility, as well as updating the visibility of the
0059      * attached connectors as appropriate
0060      */
0061     void setVisible(bool yes) override;
0062     /**
0063      * Returns the global id, that is unique to the node
0064      * amongst all the nodes on the canvas
0065      */
0066     const QString id() const
0067     {
0068         return m_id;
0069     }
0070     /**
0071      * Returns the id that is internal to the CNItem to which the
0072      * node belongs to. Returns a null QString if no parentitem
0073      */
0074     const QString childId() const
0075     {
0076         return m_childId;
0077     }
0078     /**
0079      * Use this function to set the child-id, that is unique to the node
0080      * amongst the other nodes associated with its parent CNItem
0081      */
0082     void setChildId(const QString &id)
0083     {
0084         m_childId = id;
0085     }
0086     /**
0087      * Sets the "level" of the node. By default, the level is 0. The level of
0088      * the node tells the node what CNItems it can be connected to through
0089      * a connector.
0090      * @see level
0091      */
0092     virtual void setLevel(const int level);
0093     /**
0094      * Returns the level of the nodes
0095      * @see setLevel
0096      */
0097     int level() const
0098     {
0099         return m_level;
0100     }
0101 
0102     /**
0103      * Sets the orientation of the node.
0104      */
0105     void setOrientation(int dir);
0106     /**
0107      * Changes the lenght of the node. By default, this is 8. Some node types
0108      * (such as junctions) do not make use of this value.
0109      */
0110     void setLength(int length);
0111     /**
0112      * Associates a CNItem with the node - ie the node belongs to the CNItem,
0113      * and hence gets deleted when the CNItem gets deleted.s
0114      */
0115     virtual void setParentItem(CNItem *parentItem);
0116     /**
0117      * Returns true if the node is part of a CNItem
0118      * (i.e. not between multiple connectors)
0119      */
0120     bool isChildNode() const
0121     {
0122         return (p_parentItem != nullptr);
0123     }
0124     /**
0125      * Returns a pointer to the CNItem to which the node belongs,
0126      * or Null if it doesn't.
0127      */
0128     CNItem *parentItem() const
0129     {
0130         return p_parentItem;
0131     }
0132 
0133     NodeData nodeData() const;
0134 
0135     void setNodeGroup(NodeGroup *ng)
0136     {
0137         p_nodeGroup = ng;
0138     }
0139     NodeGroup *nodeGroup() const
0140     {
0141         return p_nodeGroup;
0142     }
0143 
0144     /* interface common to ecnode and fpnode; these might be required by ItemDocumentData, ICNDocument  */
0145 
0146     virtual bool isConnected(Node *node, NodeList *checkedNodes = nullptr) = 0;
0147 
0148     virtual void removeConnector(Connector *connector) = 0;
0149 
0150     /**
0151      * Returns the total number of connections to the node. This is the number
0152      * of connectors and the parent
0153      * item connector if it exists and is requested.
0154      * @param includeParentItem Count the parent item as a connector if it exists
0155      * @param includeHiddenConnectors hidden connectors are those as e.g. part of a subcircuit
0156      */
0157     virtual int numCon(bool includeParentItem, bool includeHiddenConnectors) const = 0;
0158 
0159     /**
0160      * @return the list of all the connectors attached to the node
0161      */
0162     virtual ConnectorList getAllConnectors() const = 0;
0163 
0164     /**
0165      * For a flownode: returns the first input connector, if it exist, or the fist outptut connector, if it exists.
0166      * For an electric node: returns the first connector
0167      * If the node isn't connected to anyithing, returns null ( 0 )
0168      * @return pointer to the desired connector
0169      */
0170     virtual Connector *getAConnector() const = 0;
0171 
0172     /**
0173      * Removes all the NULL connectors
0174      */
0175     virtual void removeNullConnectors() = 0;
0176 
0177     /**
0178      * Draw shape. Note that this has to remain public.
0179      */
0180     void drawShape(QPainter &p) override = 0;
0181 
0182     void setICNDocument(ICNDocument *documentPtr);
0183 
0184 public slots:
0185     void moveBy(double dx, double dy) override;
0186     void removeNode(Item *)
0187     {
0188         removeNode();
0189     }
0190     void removeNode();
0191     void setNodeSelected(bool yes);
0192 
0193 signals:
0194     void moved(Node *node);
0195     /**
0196      * Emitted when the CNItem is removed. Normally, this signal is caught by associated
0197      * nodes, who will remove themselves as well.
0198      */
0199     void removed(Node *node);
0200 
0201 protected:
0202     virtual void initPoints();
0203     /**
0204      * Moves and rotates (according to m_dir) the painter, so that our current
0205      * position is (0,0).
0206      */
0207     void initPainter(QPainter &p);
0208     /**
0209      * Undoes the effects of initPainter.
0210      */
0211     void deinitPainter(QPainter &p);
0212 
0213     /** If this node has precisely two connectors emerging from it, then this
0214      * function will trace the two connectors until the point where they
0215      * diverge; this point is returned. */
0216     virtual QPoint findConnectorDivergePoint(bool *found) = 0;
0217 
0218     /** The node's type. This member will be removed! */
0219     node_type m_type;
0220 
0221     int m_dir;
0222     int m_length;
0223     int m_level;
0224 
0225     ICNDocument *p_icnDocument;
0226     CNItem *p_parentItem;
0227 
0228     NodeGroup *p_nodeGroup;
0229 
0230     static QColor m_selectedColor;
0231 
0232 private:
0233     // these fields are critical to saved circuit documents.
0234     QString m_id;
0235     QString m_childId;
0236 
0237     bool b_deleted;
0238 };
0239 
0240 #endif