1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_PROJECT_CIRCUIT_H
21 #define LIBREPCB_PROJECT_CIRCUIT_H
22 
23 /*******************************************************************************
24  *  Includes
25  ******************************************************************************/
26 #include <librepcb/common/circuitidentifier.h>
27 #include <librepcb/common/elementname.h>
28 #include <librepcb/common/exceptions.h>
29 #include <librepcb/common/fileio/filepath.h>
30 #include <librepcb/common/fileio/serializableobject.h>
31 #include <librepcb/common/uuid.h>
32 #include <librepcb/library/cmp/componentprefix.h>
33 
34 #include <QtCore>
35 
36 /*******************************************************************************
37  *  Namespace / Forward Declarations
38  ******************************************************************************/
39 namespace librepcb {
40 
41 class TransactionalDirectory;
42 
43 namespace library {
44 class Component;
45 }
46 
47 namespace project {
48 
49 class Project;
50 class NetClass;
51 class NetSignal;
52 class ComponentInstance;
53 
54 /*******************************************************************************
55  *  Class Circuit
56  ******************************************************************************/
57 
58 /**
59  * @brief   The Circuit class represents all electrical connections in a project
60  * (drawn in the schematics)
61  *
62  * Each ::librepcb::project::Project object contains exactly one
63  * ::librepcb::project::Circuit object which contains the whole electrical
64  * components and connections. They are created with the schematic editor and
65  * used by the board editor. The whole circuit is saved in the file
66  * "circuit.lp" in the project's "circuit" directory.
67  *
68  * Each #Circuit object contains:
69  *  - All net classes (::librepcb::project::NetClass objects)
70  *  - All net signals (::librepcb::project::NetSignal objects)
71  *  - All component instances (::librepcb::project::ComponentInstance objects)
72  */
73 class Circuit final : public QObject, public SerializableObject {
74   Q_OBJECT
75 
76 public:
77   // Constructors / Destructor
78   Circuit() = delete;
79   Circuit(const Circuit& other) = delete;
80   Circuit(Project& project, const Version& fileFormat, bool create);
81   ~Circuit() noexcept;
82 
83   // Getters
getProject()84   Project& getProject() const noexcept { return mProject; }
85 
86   // NetClass Methods
getNetClasses()87   const QMap<Uuid, NetClass*>& getNetClasses() const noexcept {
88     return mNetClasses;
89   }
90   NetClass* getNetClassByUuid(const Uuid& uuid) const noexcept;
91   NetClass* getNetClassByName(const ElementName& name) const noexcept;
92   void addNetClass(NetClass& netclass);
93   void removeNetClass(NetClass& netclass);
94   void setNetClassName(NetClass& netclass, const ElementName& newName);
95 
96   // NetSignal Methods
97   QString generateAutoNetSignalName() const noexcept;
getNetSignals()98   const QMap<Uuid, NetSignal*>& getNetSignals() const noexcept {
99     return mNetSignals;
100   }
101   NetSignal* getNetSignalByUuid(const Uuid& uuid) const noexcept;
102   NetSignal* getNetSignalByName(const QString& name) const noexcept;
103   NetSignal* getNetSignalWithMostElements() const noexcept;
104   void addNetSignal(NetSignal& netsignal);
105   void removeNetSignal(NetSignal& netsignal);
106   void setNetSignalName(NetSignal& netsignal, const CircuitIdentifier& newName,
107                         bool isAutoName);
108   void setHighlightedNetSignal(NetSignal* signal) noexcept;
109 
110   // ComponentInstance Methods
111   QString generateAutoComponentInstanceName(
112       const library::ComponentPrefix& cmpPrefix) const noexcept;
getComponentInstances()113   const QMap<Uuid, ComponentInstance*>& getComponentInstances() const noexcept {
114     return mComponentInstances;
115   }
116   ComponentInstance* getComponentInstanceByUuid(const Uuid& uuid) const
117       noexcept;
118   ComponentInstance* getComponentInstanceByName(const QString& name) const
119       noexcept;
120   void addComponentInstance(ComponentInstance& cmp);
121   void removeComponentInstance(ComponentInstance& cmp);
122   void setComponentInstanceName(ComponentInstance& cmp,
123                                 const CircuitIdentifier& newName);
124 
125   // General Methods
126   void save();
127 
128   // Operator Overloadings
129   Circuit& operator=(const Circuit& rhs) = delete;
130   bool operator==(const Circuit& rhs) noexcept { return (this == &rhs); }
131   bool operator!=(const Circuit& rhs) noexcept { return (this != &rhs); }
132 
133 signals:
134 
135   void netClassAdded(NetClass& netclass);
136   void netClassRemoved(NetClass& netclass);
137   void netSignalAdded(NetSignal& netsignal);
138   void netSignalRemoved(NetSignal& netsignal);
139   void componentAdded(ComponentInstance& cmp);
140   void componentRemoved(ComponentInstance& cmp);
141 
142 private:
143   /// @copydoc librepcb::SerializableObject::serialize()
144   void serialize(SExpression& root) const override;
145 
146   // General
147   Project& mProject;  ///< A reference to the Project object (from the ctor)
148   QScopedPointer<TransactionalDirectory> mDirectory;
149 
150   QMap<Uuid, NetClass*> mNetClasses;
151   QMap<Uuid, NetSignal*> mNetSignals;
152   QMap<Uuid, ComponentInstance*> mComponentInstances;
153 };
154 
155 /*******************************************************************************
156  *  End of File
157  ******************************************************************************/
158 
159 }  // namespace project
160 }  // namespace librepcb
161 
162 #endif  // LIBREPCB_PROJECT_CIRCUIT_H
163