LibrePCB Developers Documentation
attribute.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_CORE_ATTRIBUTE_H
21#define LIBREPCB_CORE_ATTRIBUTE_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../serialization/serializableobjectlist.h"
27#include "attributekey.h"
28
29#include <QtCore>
30
31/*******************************************************************************
32 * Namespace / Forward Declarations
33 ******************************************************************************/
34namespace librepcb {
35
36class AttributeType;
37class AttributeUnit;
38
39/*******************************************************************************
40 * Class Attribute
41 ******************************************************************************/
42
46class Attribute final {
47 Q_DECLARE_TR_FUNCTIONS(Attribute)
48
49public:
50 // Signals
51 enum class Event {
52 KeyChanged,
53 TypeValueUnitChanged,
54 };
57
58 // Constructors / Destructor
59 Attribute() = delete;
60 Attribute(const Attribute& other) noexcept;
61 explicit Attribute(const SExpression& node);
62 Attribute(const AttributeKey& key, const AttributeType& type,
63 const QString& value, const AttributeUnit* unit);
64 ~Attribute() noexcept;
65
66 // Getters
67 const AttributeKey& getKey() const noexcept { return mKey; }
68 const QString& getName() const noexcept {
69 return *mKey;
70 } // required for SerializableObjectList
71 const AttributeType& getType() const noexcept { return *mType; }
72 const AttributeUnit* getUnit() const noexcept { return mUnit; }
73 const QString& getValue() const noexcept { return mValue; }
74 QString getValueTr(bool showUnit) const noexcept;
75
76 // Setters
77 bool setKey(const AttributeKey& key) noexcept;
78 bool setTypeValueUnit(const AttributeType& type, const QString& value,
79 const AttributeUnit* unit);
80
81 // General Methods
82
88 void serialize(SExpression& root) const;
89
90 // Operator Overloadings
91 bool operator==(const Attribute& rhs) const noexcept;
92 bool operator!=(const Attribute& rhs) const noexcept {
93 return !(*this == rhs);
94 }
95 Attribute& operator=(const Attribute& rhs) noexcept;
96
97private: // Methods
98 bool checkAttributesValidity() const noexcept;
99
100private: // Data
103 QString mValue;
105};
106
107/*******************************************************************************
108 * Class AttributeList
109 ******************************************************************************/
110
112 static constexpr const char* tagname = "attribute";
113};
117
118} // namespace librepcb
119
120/*******************************************************************************
121 * Non-Member Functions
122 ******************************************************************************/
123
134 const librepcb::AttributeList& lhs,
135 const librepcb::AttributeList& rhs) noexcept {
136 librepcb::AttributeList result = lhs;
137 for (const auto& newAttr : rhs) {
138 if (!result.contains(newAttr.getName())) {
139 result.append(std::make_shared<librepcb::Attribute>(newAttr));
140 }
141 }
142 return result;
143}
144
145/*******************************************************************************
146 * End of File
147 ******************************************************************************/
148
149#endif
librepcb::AttributeList operator|(const librepcb::AttributeList &lhs, const librepcb::AttributeList &rhs) noexcept
Extend an librepcb::AttributeList with additional attributes.
Definition: attribute.h:133
The Attribute class.
Definition: attribute.h:46
Signal< Attribute, Event > onEdited
Definition: attribute.h:55
bool setTypeValueUnit(const AttributeType &type, const QString &value, const AttributeUnit *unit)
Definition: attribute.cpp:87
const AttributeKey & getKey() const noexcept
Definition: attribute.h:67
QString getValueTr(bool showUnit) const noexcept
Definition: attribute.cpp:69
const QString & getName() const noexcept
Definition: attribute.h:68
AttributeKey mKey
Definition: attribute.h:101
Event
Definition: attribute.h:51
bool checkAttributesValidity() const noexcept
Definition: attribute.cpp:146
bool setKey(const AttributeKey &key) noexcept
Definition: attribute.cpp:77
Attribute & operator=(const Attribute &rhs) noexcept
Definition: attribute.cpp:136
QString mValue
Definition: attribute.h:103
const QString & getValue() const noexcept
Definition: attribute.h:73
const AttributeUnit * mUnit
Definition: attribute.h:104
~Attribute() noexcept
Definition: attribute.cpp:62
void serialize(SExpression &root) const
Serialize into librepcb::SExpression node.
Definition: attribute.cpp:112
bool operator!=(const Attribute &rhs) const noexcept
Definition: attribute.h:92
Slot< Attribute, Event > OnEditedSlot
Definition: attribute.h:56
bool operator==(const Attribute &rhs) const noexcept
Definition: attribute.cpp:128
const AttributeType * mType
Definition: attribute.h:102
const AttributeType & getType() const noexcept
Definition: attribute.h:71
const AttributeUnit * getUnit() const noexcept
Definition: attribute.h:72
The AttributeType class.
Definition: attributetype.h:42
The AttributeUnit class.
Definition: attributeunit.h:40
The SExpression class.
Definition: sexpression.h:69
int append(const std::shared_ptr< T > &obj) noexcept
Definition: serializableobjectlist.h:342
bool contains(int index) const noexcept
Definition: serializableobjectlist.h:244
The Signal class is used to emit signals on non-QObject derived classes.
Definition: signalslot.h:65
The Slot class is used to receive signals from non-QObject derived classes.
Definition: signalslot.h:170
Definition: occmodel.cpp:77
type_safe::constrained_type< QString, AttributeKeyConstraint, AttributeKeyVerifier > AttributeKey
Definition: attributekey.h:84
Definition: attribute.h:111