LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
componentinstance.h
Go to the documentation of this file.
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_COMPONENTINSTANCE_H
21 #define LIBREPCB_PROJECT_COMPONENTINSTANCE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../erc/if_ercmsgprovider.h"
27 
33 #include <librepcb/common/uuid.h>
34 
35 #include <QtCore>
36 
37 /*******************************************************************************
38  * Namespace / Forward Declarations
39  ******************************************************************************/
40 namespace librepcb {
41 
42 class DomElement;
43 
44 namespace library {
45 class Component;
46 class ComponentSymbolVariant;
47 } // namespace library
48 
49 namespace project {
50 
51 class Circuit;
52 class ComponentSignalInstance;
53 class BI_Device;
54 class SI_Symbol;
55 class ErcMsg;
56 
57 /*******************************************************************************
58  * Class ComponentInstance
59  ******************************************************************************/
60 
64 class ComponentInstance : public QObject,
65  public AttributeProvider,
66  public IF_ErcMsgProvider,
67  public SerializableObject {
68  Q_OBJECT
70 
71 public:
72  // Constructors / Destructor
73  ComponentInstance() = delete;
74  ComponentInstance(const ComponentInstance& other) = delete;
75  explicit ComponentInstance(Circuit& circuit, const SExpression& node);
76  explicit ComponentInstance(
77  Circuit& circuit, const library::Component& cmp, const Uuid& symbVar,
78  const CircuitIdentifier& name,
79  const tl::optional<Uuid>& defaultDevice = tl::nullopt);
80  ~ComponentInstance() noexcept;
81 
82  // Getters: Attributes
83  const Uuid& getUuid() const noexcept { return mUuid; }
84  const CircuitIdentifier& getName() const noexcept { return mName; }
85  QString getValue(bool replaceAttributes = false) const noexcept;
86  const tl::optional<Uuid>& getDefaultDeviceUuid() const noexcept {
87  return mDefaultDeviceUuid;
88  }
89  const library::Component& getLibComponent() const noexcept {
90  return *mLibComponent;
91  }
93  return *mCompSymbVar;
94  }
96  noexcept {
97  return mSignals.value(signalUuid);
98  }
99  const AttributeList& getAttributes() const noexcept { return *mAttributes; }
100 
101  // Getters: General
102  Circuit& getCircuit() const noexcept { return mCircuit; }
103  int getPlacedSymbolsCount() const noexcept {
104  return mRegisteredSymbols.count();
105  }
106  int getUnplacedSymbolsCount() const noexcept;
107  int getUnplacedRequiredSymbolsCount() const noexcept;
108  int getUnplacedOptionalSymbolsCount() const noexcept;
109  int getRegisteredElementsCount() const noexcept;
110  bool isUsed() const noexcept;
111 
112  // Setters
113 
126  void setName(const CircuitIdentifier& name) noexcept;
127 
135  void setValue(const QString& value) noexcept;
136 
137  void setAttributes(const AttributeList& attributes) noexcept;
138 
139  // General Methods
140  void addToCircuit();
141  void removeFromCircuit();
142  void registerSymbol(SI_Symbol& symbol);
143  void unregisterSymbol(SI_Symbol& symbol);
144  void registerDevice(BI_Device& device);
145  void unregisterDevice(BI_Device& device);
146 
148  void serialize(SExpression& root) const override;
149 
150  // Inherited from AttributeProvider
152  QString getUserDefinedAttributeValue(const QString& key) const
153  noexcept override;
155  QString getBuiltInAttributeValue(const QString& key) const noexcept override;
157  QVector<const AttributeProvider*> getAttributeProviderParents() const
158  noexcept override;
159 
160  // Operator Overloadings
161  ComponentInstance& operator=(const ComponentInstance& rhs) = delete;
162 
163 signals:
164 
166  void attributesChanged() override;
167 
168 private:
169  void init();
170  bool checkAttributesValidity() const noexcept;
171  void updateErcMessages() noexcept;
172  const QStringList& getLocaleOrder() const noexcept;
173 
174  // General
177 
178  // Attributes
179 
182 
186 
189  QString mValue;
190 
192  tl::optional<Uuid> mDefaultDeviceUuid;
193 
195  const library::Component* mLibComponent;
196 
199 
201  QScopedPointer<AttributeList> mAttributes;
202 
205 
206  // Registered Elements
207 
218 
225 
226  // ERC Messages
227 
230 
233 };
234 
235 /*******************************************************************************
236  * End of File
237  ******************************************************************************/
238 
239 } // namespace project
240 } // namespace librepcb
241 
242 #endif // LIBREPCB_PROJECT_COMPONENTINSTANCE_H
The Circuit class represents all electrical connections in a project (drawed in the schematics) ...
Definition: circuit.h:72
QScopedPointer< ErcMsg > mErcMsgUnplacedRequiredSymbols
The ERC message for unplaced required symbols of this component.
Definition: componentinstance.h:229
int getUnplacedSymbolsCount() const noexcept
Definition: componentinstance.cpp:173
QVector< const AttributeProvider * > getAttributeProviderParents() const noexceptoverride
Get all parent attribute providers (fallback if attribute not found)
Definition: componentinstance.cpp:387
bool checkAttributesValidity() const noexcept
Definition: componentinstance.cpp:398
The SI_Symbol class.
Definition: si_symbol.h:57
#define DECLARE_ERC_MSG_CLASS_NAME(msgOwnerClassName)
Definition: if_ercmsgprovider.h:44
The ErcMsg class represents a message in the ERC (Electrical Rule Check) list.
Definition: ercmsg.h:46
void unregisterDevice(BI_Device &device)
Definition: componentinstance.cpp:338
QString getUserDefinedAttributeValue(const QString &key) const noexceptoverride
Get the value of a user defined attribute (if available)
Definition: componentinstance.cpp:364
int getUnplacedRequiredSymbolsCount() const noexcept
Definition: componentinstance.cpp:177
int getPlacedSymbolsCount() const noexcept
Definition: componentinstance.h:103
Circuit & mCircuit
Definition: componentinstance.h:175
QString getBuiltInAttributeValue(const QString &key) const noexceptoverride
Get the value of a built-in attribute (if available)
Definition: componentinstance.cpp:373
QList< BI_Device * > mRegisteredDevices
All registered devices (of all boards)
Definition: componentinstance.h:224
int getUnplacedOptionalSymbolsCount() const noexcept
Definition: componentinstance.cpp:188
CircuitIdentifier mName
The unique name of this component instance in the circuit (e.g. "R42")
Definition: componentinstance.h:185
void unregisterSymbol(SI_Symbol &symbol)
Definition: componentinstance.cpp:317
const library::Component * mLibComponent
Pointer to the component in the project's library.
Definition: componentinstance.h:195
const QStringList & getLocaleOrder() const noexcept
Definition: componentinstance.cpp:421
The IF_ErcMsgProvider class.
Definition: if_ercmsgprovider.h:59
QScopedPointer< AttributeList > mAttributes
All attributes of this component.
Definition: componentinstance.h:201
const library::ComponentSymbolVariant * mCompSymbVar
Pointer to the used symbol variant of mLibComponent.
Definition: componentinstance.h:198
void registerSymbol(SI_Symbol &symbol)
Definition: componentinstance.cpp:284
QString getValue(bool replaceAttributes=false) const noexcept
Definition: componentinstance.cpp:165
bool mIsAddedToCircuit
Definition: componentinstance.h:176
const tl::optional< Uuid > & getDefaultDeviceUuid() const noexcept
Definition: componentinstance.h:86
const library::Component & getLibComponent() const noexcept
Definition: componentinstance.h:89
The SerializableObject class is the base class for all classes which need to be serializable/deserial...
Definition: serializableobject.h:43
Uuid mUuid
The unique UUID of this component instance in the circuit.
Definition: componentinstance.h:181
~ComponentInstance() noexcept
Definition: componentinstance.cpp:153
QString mValue
The value of this component instance in the circuit (e.g. the resistance of a resistor) ...
Definition: componentinstance.h:189
ComponentSignalInstance * getSignalInstance(const Uuid &signalUuid) const noexcept
Definition: componentinstance.h:95
void setAttributes(const AttributeList &attributes) noexcept
Definition: componentinstance.cpp:238
type_safe::constrained_type< QString, CircuitIdentifierConstraint, CircuitIdentifierVerifier > CircuitIdentifier
Definition: circuitidentifier.h:89
void registerDevice(BI_Device &device)
Definition: componentinstance.cpp:327
void addToCircuit()
Definition: componentinstance.cpp:250
const library::ComponentSymbolVariant & getSymbolVariant() const noexcept
Definition: componentinstance.h:92
Circuit & getCircuit() const noexcept
Definition: componentinstance.h:102
QHash< Uuid, SI_Symbol * > mRegisteredSymbols
All registered symbols.
Definition: componentinstance.h:217
void attributesChanged() override
This signal is emited when the value of attributes has changed.
void removeFromCircuit()
Definition: componentinstance.cpp:264
QMap< Uuid, ComponentSignalInstance * > mSignals
All signal instances (Key: component signal UUID)
Definition: componentinstance.h:204
The BI_Device class.
Definition: bi_device.h:62
void setValue(const QString &value) noexcept
Set the value of this component instance in the circuit.
Definition: componentinstance.cpp:231
The ComponentSymbolVariant class represents a symbol variant of a component.
Definition: componentsymbolvariant.h:58
const CircuitIdentifier & getName() const noexcept
Definition: componentinstance.h:84
QScopedPointer< ErcMsg > mErcMsgUnplacedOptionalSymbols
The ERC message for unplaced optional symbols of this component.
Definition: componentinstance.h:232
void init()
Definition: componentinstance.cpp:136
The ComponentSignalInstance class.
Definition: componentsignalinstance.h:61
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
tl::optional< Uuid > mDefaultDeviceUuid
THe default device when adding the component to a board.
Definition: componentinstance.h:192
void updateErcMessages() noexcept
Definition: componentinstance.cpp:404
void setName(const CircuitIdentifier &name) noexcept
Set the name of this component instance in the circuit.
Definition: componentinstance.cpp:223
The Component class represents a "generic" device in the library.
Definition: component.h:73
The ComponentInstance class.
Definition: componentinstance.h:64
The SExpression class.
Definition: sexpression.h:57
const Uuid & getUuid() const noexcept
Definition: componentinstance.h:83
bool isUsed() const noexcept
Definition: componentinstance.cpp:207
int getRegisteredElementsCount() const noexcept
Definition: componentinstance.cpp:200
The AttributeProvider class defines an interface for classes which provides some attributes which can...
Definition: attributeprovider.h:59
const AttributeList & getAttributes() const noexcept
Definition: componentinstance.h:99
void serialize(SExpression &root) const override
Serialize the object into an existing S-Expression node.
Definition: componentinstance.cpp:347