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 CIRCUITDOCUMENT_H
12 #define CIRCUITDOCUMENT_H
13 
14 #include "circuiticndocument.h"
15 #include "pin.h"
16 
17 class Circuit;
18 class Component;
19 class Connector;
20 class ECNode;
21 class Element;
22 class CircuitICNDocument;
23 class KTechlab;
24 class Pin;
25 class QTimer;
26 class Switch;
27 class Wire;
28 
29 class KActionMenu;
30 
31 typedef QList<Circuit*> CircuitList;
32 typedef QList<Component*> ComponentList;
33 typedef QList<QPointer<Connector> > ConnectorList;
34 typedef QList<ECNode*> ECNodeList;
35 typedef QList<Element*> ElementList;
36 typedef QList<QPointer<Pin> > PinList;
37 typedef QList<Switch*> SwitchList;
38 typedef QList<QPointer<Wire> > WireList;
39 
40 class Circuitoid
41 {
42 public:
contains(Pin * node)43 	bool contains( Pin *node ) { return pinList.contains(node); }
contains(Element * ele)44 	bool contains( Element *ele ) { return elementList.contains(ele); }
45 
addPin(Pin * node)46 	void addPin( Pin *node ) { if (node && !contains(node)) pinList += node; }
addElement(Element * ele)47 	void addElement( Element *ele ) { if (ele && !contains(ele)) elementList += ele; }
48 
49 	PinList pinList;
50 	ElementList elementList;
51 };
52 
53 /**
54 CircuitDocument handles allocation of the components displayed in the ICNDocument
55 to various Circuits, where the simulation can be performed, and displays the
56 information from those simulations back on the ICNDocument
57 @short Circuit view
58 @author David Saxton
59 */
60 class CircuitDocument : public CircuitICNDocument
61 {
62 	Q_OBJECT
63 	public:
64 		CircuitDocument( const QString &caption, const char *name = nullptr );
65 		~CircuitDocument() override;
66 
67 		View *createView( ViewContainer *viewContainer, uint viewAreaId, const char *name = nullptr ) override;
68 
69 		void calculateConnectorCurrents();
70 		/**
71 		 * Count the number of ExternalConnection components in the CNItemList
72 		 */
73 		int countExtCon( const ItemList &cnItemList ) const;
74 
75 		void update() override;
76 
77 	public slots:
78 		/**
79 		 * Creates a subcircuit from the currently selected components
80 		 */
81 		void createSubcircuit();
82 		void displayEquations();
83 		void setOrientation0();
84 		void setOrientation90();
85 		void setOrientation180();
86 		void setOrientation270();
87 		void rotateCounterClockwise();
88 		void rotateClockwise();
89 		void flipHorizontally();
90 		void flipVertically();
91 		/**
92 		 * Enables / disables / selects various actions depending on what is
93 		 * selected or not.
94 		 */
95 		void slotInitItemActions() override;
96 		void requestAssignCircuits();
97 		void componentAdded( Item *item );
98 		void componentRemoved( Item *item );
99 		void connectorAdded( Connector *connector );
100 		void slotUpdateConfiguration() override;
101 
102 	protected:
103 		void itemAdded( Item *item ) override;
104 		void fillContextMenu( const QPoint &pos ) override;
105 		bool isValidItem( Item *item ) override;
106 		bool isValidItem( const QString &itemId ) override;
107 
108 		KActionMenu *m_pOrientationAction;
109 
110 	private slots:
111 		void assignCircuits();
112 
113 	private:
114 		/**
115 		 * If the given circuitoid can be a LogicCircuit, then it will be added to
116 		 * m_logicCircuits, and return true. Else returns false.
117 		 */
118 		bool tryAsLogicCircuit( Circuitoid *circuitoid );
119 		/**
120 		 * Creates a circuit from the circuitoid
121 		 */
122 		Circuit *createCircuit( Circuitoid *circuitoid );
123 
124 		/**
125 		 * @param pin Current node (will be added, then tested for further
126 		 * connections).
127 		 * @param pinList List of nodes in current partition.
128 		 * @param unassignedPins The pool of all nodes in the CircuitDocument
129 		 * waiting for assignment.
130 		 * @param onlyGroundDependent if true, then the partition will not use
131 		 * circuit-dependent pins to include new pins while growing the
132 		 * partition.
133 		 */
134 		void getPartition(Pin *pin, PinList *pinList, PinList *unassignedPins, bool onlyGroundDependent = false);
135 		/**
136 		 * Takes the nodeList (generated by getPartition), splits it at ground nodes,
137 		 * and creates circuits from each split.
138 		 */
139 		void splitIntoCircuits(PinList *pinList);
140 		/**
141 		 * Construct a circuit from the given node, stopping at the groundnodes
142 		 */
143 		void recursivePinAdd(Pin *pin, Circuitoid *circuitoid, PinList *unassignedPins);
144 
145 		void deleteCircuits();
146 
147 		QTimer *m_updateCircuitsTmr;
148 		CircuitList m_circuitList;
149 		ComponentList m_toSimulateList;
150 		ComponentList m_componentList; // List is built up during call to assignCircuits
151 
152 // hmm, we have one of these in circuit too....
153 		PinList m_pinList;
154 		WireList m_wireList;
155 		SwitchList m_switchList;
156 };
157 
158 #endif
159 
160