File indexing completed on 2024-04-21 05:43:32

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 CIRCUITDOCUMENT_H
0012 #define CIRCUITDOCUMENT_H
0013 
0014 #include "circuiticndocument.h"
0015 #include "pin.h"
0016 
0017 class Circuit;
0018 class Component;
0019 class Connector;
0020 class ECNode;
0021 class Element;
0022 class CircuitICNDocument;
0023 class KTechlab;
0024 class Pin;
0025 class QTimer;
0026 class Switch;
0027 class Wire;
0028 
0029 class KActionMenu;
0030 
0031 typedef QList<Circuit *> CircuitList;
0032 typedef QList<Component *> ComponentList;
0033 typedef QList<QPointer<Connector>> ConnectorList;
0034 typedef QList<ECNode *> ECNodeList;
0035 typedef QList<Element *> ElementList;
0036 typedef QList<QPointer<Pin>> PinList;
0037 typedef QList<Switch *> SwitchList;
0038 typedef QList<QPointer<Wire>> WireList;
0039 
0040 class Circuitoid
0041 {
0042 public:
0043     bool contains(Pin *node)
0044     {
0045         return pinList.contains(node);
0046     }
0047     bool contains(Element *ele)
0048     {
0049         return elementList.contains(ele);
0050     }
0051 
0052     void addPin(Pin *node)
0053     {
0054         if (node && !contains(node))
0055             pinList += node;
0056     }
0057     void addElement(Element *ele)
0058     {
0059         if (ele && !contains(ele))
0060             elementList += ele;
0061     }
0062 
0063     PinList pinList;
0064     ElementList elementList;
0065 };
0066 
0067 /**
0068 CircuitDocument handles allocation of the components displayed in the ICNDocument
0069 to various Circuits, where the simulation can be performed, and displays the
0070 information from those simulations back on the ICNDocument
0071 @short Circuit view
0072 @author David Saxton
0073 */
0074 class CircuitDocument : public CircuitICNDocument
0075 {
0076     Q_OBJECT
0077 public:
0078     CircuitDocument(const QString &caption);
0079     ~CircuitDocument() override;
0080 
0081     View *createView(ViewContainer *viewContainer, uint viewAreaId) override;
0082 
0083     void calculateConnectorCurrents();
0084     /**
0085      * Count the number of ExternalConnection components in the CNItemList
0086      */
0087     int countExtCon(const ItemList &cnItemList) const;
0088 
0089     void update() override;
0090 
0091 public slots:
0092     /**
0093      * Creates a subcircuit from the currently selected components
0094      */
0095     void createSubcircuit();
0096     void displayEquations();
0097     void setOrientation0();
0098     void setOrientation90();
0099     void setOrientation180();
0100     void setOrientation270();
0101     void rotateCounterClockwise();
0102     void rotateClockwise();
0103     void flipHorizontally();
0104     void flipVertically();
0105     /**
0106      * Enables / disables / selects various actions depending on what is
0107      * selected or not.
0108      */
0109     void slotInitItemActions() override;
0110     void requestAssignCircuits();
0111     void componentAdded(Item *item);
0112     void componentRemoved(Item *item);
0113     void connectorAddedSlot(Connector *connector);
0114     void slotUpdateConfiguration() override;
0115 
0116 protected:
0117     void itemAdded(Item *item) override;
0118     void fillContextMenu(const QPoint &pos) override;
0119     bool isValidItem(Item *item) override;
0120     bool isValidItem(const QString &itemId) override;
0121 
0122     KActionMenu *m_pOrientationAction;
0123 
0124 private slots:
0125     void assignCircuits();
0126 
0127 private:
0128     /**
0129      * If the given circuitoid can be a LogicCircuit, then it will be added to
0130      * m_logicCircuits, and return true. Else returns false.
0131      */
0132     bool tryAsLogicCircuit(Circuitoid *circuitoid);
0133     /**
0134      * Creates a circuit from the circuitoid
0135      */
0136     Circuit *createCircuit(Circuitoid *circuitoid);
0137 
0138     /**
0139      * @param pin Current node (will be added, then tested for further
0140      * connections).
0141      * @param pinList List of nodes in current partition.
0142      * @param unassignedPins The pool of all nodes in the CircuitDocument
0143      * waiting for assignment.
0144      * @param onlyGroundDependent if true, then the partition will not use
0145      * circuit-dependent pins to include new pins while growing the
0146      * partition.
0147      */
0148     void getPartition(Pin *pin, PinList *pinList, PinList *unassignedPins, bool onlyGroundDependent = false);
0149     /**
0150      * Takes the nodeList (generated by getPartition), splits it at ground nodes,
0151      * and creates circuits from each split.
0152      */
0153     void splitIntoCircuits(PinList *pinList);
0154     /**
0155      * Construct a circuit from the given node, stopping at the groundnodes
0156      */
0157     void recursivePinAdd(Pin *pin, Circuitoid *circuitoid, PinList *unassignedPins);
0158 
0159     void deleteCircuits();
0160 
0161     QTimer *m_updateCircuitsTmr;
0162     CircuitList m_circuitList;
0163     ComponentList m_toSimulateList;
0164     ComponentList m_componentList; // List is built up during call to assignCircuits
0165 
0166     // hmm, we have one of these in circuit too....
0167     PinList m_pinList;
0168     WireList m_wireList;
0169     SwitchList m_switchList;
0170 };
0171 
0172 #endif