1 /***************************************************************************
2  *   Copyright (C) 2003-2005 by David Saxton                               *
3  *   david@bluehaze.org                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10 
11 #ifndef NODE_H
12 #define NODE_H
13 
14 //#include <canvas.h> // 2018.10.16 - not needed
15 #include "canvasitems.h"
16 #include <QPointer>
17 
18 class CNItem;
19 class Item;
20 class ICNDocument;
21 class ICNDocument;
22 class Connector;
23 class Node;
24 class NodeData;
25 class NodeGroup;
26 class QTimer;
27 
28 typedef QList<QPointer<Connector> > ConnectorList;
29 typedef QList<QPointer<Node> > NodeList;
30 
31 /**
32 @short A standard node that can be associated with a Connector or a CNItem
33 @author David Saxton
34 */
35 class Node : /* public QObject, */ public KtlQCanvasPolygon
36 {
37 Q_OBJECT
38 public:
39 	// this shall disappear one day
40 	/**
41 	 * Used for run-time identification of the node:
42 	 * Can be electronic node (so has values of current, voltage, etc)
43 	 * or a pic part node
44 	 * this enum will be cleared soon
45 	 */
46 	enum node_type
47 	{
48 		ec_pin,
49 		ec_junction,
50 		fp_in,
51 		fp_out,
52 		fp_junction
53 	};
54 
55 	/**
56 	 * @param dir the direction of the node; 0 degrees for left, 90 degrees for
57 	 * up, etc in an anti-clockwise direction. An "up" node has the
58 	 * wire-connection point at the top and the (component/flowpart)-end at the
59 	 * bottom.
60 	 */
61 	Node( ICNDocument *icnDocument, Node::node_type type, int dir, const QPoint &pos, QString *id = nullptr );
62 	~Node() override;
63 
64 	/**
65 	 * Sets the node's visibility, as well as updating the visibility of the
66 	 * attached connectors as appropriate
67 	 */
68 	void setVisible( bool yes ) override;
69 	/**
70 	 * Returns the global id, that is unique to the node
71 	 * amongst all the nodes on the canvas
72 	 */
id()73 	const QString id() const { return m_id; }
74 	/**
75 	 * Returns the id that is internal to the CNItem to which the
76 	 * node belongs to. Returns a null QString if no parentitem
77 	 */
childId()78 	const QString childId() const { return m_childId; }
79 	/**
80 	 * Use this function to set the child-id, that is unique to the node
81 	 * amongst the other nodes associated with its parent CNItem
82 	 */
setChildId(const QString & id)83 	void setChildId( const QString &id ) { m_childId = id; }
84 	/**
85 	 * Sets the "level" of the node. By default, the level is 0. The level of
86 	 * the node tells the node what CNItems it can be connected to through
87 	 * a connector.
88 	 * @see level
89 	 */
90 	virtual void setLevel( const int level );
91 	/**
92 	 * Returns the level of the nodes
93 	 * @see setLevel
94 	 */
level()95 	int level() const { return m_level; }
96 
97 
98 	/**
99 	 * Sets the orientation of the node.
100 	 */
101 	void setOrientation( int dir );
102 	/**
103 	 * Changes the lenght of the node. By default, this is 8. Some node types
104 	 * (such as junctions) do not make use of this value.
105 	 */
106 	void setLength( int length );
107 	/**
108 	 * Associates a CNItem with the node - ie the node belongs to the CNItem,
109 	 * and hence gets deleted when the CNItem gets deleted.s
110 	 */
111 	virtual void setParentItem( CNItem *parentItem );
112 	/**
113 	 * Returns true if the node is part of a CNItem
114 	 * (i.e. not between multiple connectors)
115 	 */
isChildNode()116 	bool isChildNode() const { return (p_parentItem != nullptr); }
117 	/**
118 	 * Returns a pointer to the CNItem to which the node belongs,
119 	 * or Null if it doesn't.
120 	 */
parentItem()121 	CNItem *parentItem() const { return p_parentItem; }
122 
123 	NodeData nodeData() const;
124 
125 
setNodeGroup(NodeGroup * ng)126 	void setNodeGroup( NodeGroup *ng ) { p_nodeGroup = ng; }
nodeGroup()127 	NodeGroup *nodeGroup() const { return p_nodeGroup; }
128 
129 	/* interface common to ecnode and fpnode; these might be required by ItemDocumentData, ICNDocument  */
130 
131 	virtual bool isConnected( Node *node, NodeList *checkedNodes = nullptr ) = 0;
132 
133 	virtual void removeConnector( Connector *connector ) = 0;
134 
135 	/**
136 	 * Returns the total number of connections to the node. This is the number
137 	 * of connectors and the parent
138 	 * item connector if it exists and is requested.
139 	 * @param includeParentItem Count the parent item as a connector if it exists
140 	 * @param includeHiddenConnectors hidden connectors are those as e.g. part of a subcircuit
141 	 */
142 	virtual int numCon( bool includeParentItem, bool includeHiddenConnectors ) const = 0;
143 
144 	/**
145 	 * @return the list of all the connectors attached to the node
146 	 */
147 	virtual ConnectorList getAllConnectors() const = 0;
148 
149 	/**
150 	 * For a flownode: returns the first input connector, if it exist, or the fist outptut connector, if it exists.
151 	 * For an electric node: returns the first connector
152 	 * If the node isn't connected to anyithing, returns null ( 0 )
153 	 * @return pointer to the desired connector
154 	 */
155 	virtual Connector* getAConnector() const = 0;
156 
157 	/**
158 	 * Removes all the NULL connectors
159 	 */
160 	virtual void removeNullConnectors() = 0;
161 
162 	/**
163 	 * Draw shape. Note that this has to remain public.
164 	 */
165 	void drawShape( QPainter &p ) override = 0;
166 
167     void setICNDocument(ICNDocument *documentPtr);
168 
169 public slots:
170 	void moveBy( double dx, double dy ) override;
removeNode(Item *)171 	void removeNode(Item*) { removeNode(); }
172 	void removeNode();
173 	void setNodeSelected( bool yes );
174 
175 signals:
176 	void moved( Node *node );
177 	/**
178 	 * Emitted when the CNItem is removed. Normally, this signal is caught by associated
179 	 * nodes, who will remove themselves as well.
180 	 */
181 	void removed( Node* node );
182 
183 protected:
184 	virtual void initPoints();
185 	/**
186 	 * Moves and rotates (according to m_dir) the painter, so that our current
187 	 * position is (0,0).
188 	 */
189 	void initPainter( QPainter & p );
190 	/**
191 	 * Undoes the effects of initPainter.
192 	 */
193 	void deinitPainter( QPainter & p );
194 
195 
196 	/** If this node has precisely two connectors emerging from it, then this
197 	 * function will trace the two connectors until the point where they
198 	 * diverge; this point is returned. */
199 	virtual QPoint findConnectorDivergePoint( bool * found ) = 0;
200 
201 	/** The node's type. This member will be removed! */
202 	node_type m_type;
203 
204 	int m_dir;
205 	int m_length;
206 	int m_level;
207 
208 	ICNDocument *p_icnDocument;
209 	CNItem *p_parentItem;
210 
211 
212 	NodeGroup *p_nodeGroup;
213 
214 	static QColor m_selectedColor;
215 
216 private:
217 // these fields are critical to saved circuit documents.
218 	QString m_id;
219 	QString m_childId;
220 
221 	bool b_deleted;
222 };
223 
224 #endif
225