20 #ifndef LIBREPCB_CORE_SERIALIZABLEKEYVALUEMAP_H 21 #define LIBREPCB_CORE_SERIALIZABLEKEYVALUEMAP_H 26 #include "../exceptions.h" 27 #include "../types/elementname.h" 28 #include "../utils/signalslot.h" 33 #include <optional.hpp> 74 const typename T::ValueType& defaultValue) noexcept
76 mValues.insert(
"", defaultValue);
91 tr(
"Key \"%1\" defined multiple times.").arg(key));
94 deserialize<typename T::ValueType>(value));
96 if (!
mValues.contains(QString(
""))) {
98 tr(
"No default %1 defined.").arg(T::tagname));
106 auto i =
mValues.find(QString(
""));
108 Q_ASSERT((i !=
mValues.end()) && (i.key() == QString(
"")));
114 tl::optional<typename T::ValueType>
tryGet(
const QString& key)
const 117 if ((i !=
mValues.end()) && (i.key() == key)) {
123 const typename T::ValueType&
value(
const QStringList& keyOrder,
124 QString* usedKey =
nullptr) const
127 foreach (
const QString& key, keyOrder) {
129 if ((i !=
mValues.end()) && (i.key() == key)) {
130 if (usedKey) *usedKey = key;
135 if (usedKey) *usedKey = QString(
"");
145 void insert(
const QString& key,
const typename T::ValueType&
value) noexcept {
150 }
else if (it.value() !=
value) {
162 for (
auto i =
mValues.constBegin(); i !=
mValues.constEnd(); ++i) {
165 if (!i.key().isEmpty()) {
176 foreach (
const QString& key,
mValues.keys()) {
181 QMapIterator<QString, typename T::ValueType> i(rhs.mValues);
182 while (i.hasNext()) {
184 insert(i.key(), i.value());
205 static constexpr
const char* tagname =
"name";
206 static constexpr
const char* keyname =
"locale";
216 static constexpr
const char* tagname =
"description";
217 static constexpr
const char* keyname =
"locale";
228 static constexpr
const char* tagname =
"keywords";
229 static constexpr
const char* keyname =
"locale";
SerializableKeyValueMap(const SExpression &node)
Definition: serializablekeyvaluemap.h:78
Slot< SerializableKeyValueMap< T >, const QString &, Event > OnEditedSlot
Definition: serializablekeyvaluemap.h:67
bool contains(const QString &key) const noexcept
Definition: serializablekeyvaluemap.h:111
const QList< SExpression > & getChildren() const noexcept
Get all children of this node.
Definition: sexpression.h:99
void ensureLineBreak()
Definition: sexpression.cpp:191
Definition: occmodel.cpp:76
QString ValueType
Definition: serializablekeyvaluemap.h:227
Event
Definition: serializablekeyvaluemap.h:61
QStringList keys() const noexcept
Definition: serializablekeyvaluemap.h:104
SExpression & appendList(const QString &name)
Definition: sexpression.cpp:197
SExpression & getChild(const QString &path)
Get a child by path.
Definition: sexpression.cpp:120
bool isList() const noexcept
Definition: sexpression.h:86
QMap< QString, typename T::ValueType > mValues
Definition: serializablekeyvaluemap.h:196
QString ValueType
Definition: serializablekeyvaluemap.h:215
SerializableKeyValueMap< T > & operator=(const SerializableKeyValueMap< T > &rhs) noexcept
Definition: serializablekeyvaluemap.h:174
const T::ValueType & getDefaultValue() const noexcept
Definition: serializablekeyvaluemap.h:105
SerializableKeyValueMap()=delete
Definition: serializablekeyvaluemap.h:226
bool operator!=(const SerializableKeyValueMap< T > &rhs) const noexcept
Definition: serializablekeyvaluemap.h:191
~SerializableKeyValueMap() noexcept
Definition: serializablekeyvaluemap.h:101
The RuntimeError class.
Definition: exceptions.h:216
void notify(Args... args) noexcept
Notify all attached slots.
Definition: signalslot.h:123
ElementName ValueType
Definition: serializablekeyvaluemap.h:204
void insert(const QString &key, const typename T::ValueType &value) noexcept
Definition: serializablekeyvaluemap.h:145
Definition: serializablekeyvaluemap.h:203
tl::optional< typename T::ValueType > tryGet(const QString &key) const noexcept
Definition: serializablekeyvaluemap.h:114
SExpression & appendChild(const SExpression &child)
Definition: sexpression.cpp:201
The Signal class is used to emit signals on non-QObject derived classes.
Definition: signalslot.h:65
bool operator==(const SerializableKeyValueMap< T > &rhs) const noexcept
Definition: serializablekeyvaluemap.h:188
Signal< SerializableKeyValueMap< T >, const QString &, Event > onEdited
Definition: serializablekeyvaluemap.h:66
const T::ValueType & value(const QStringList &keyOrder, QString *usedKey=nullptr) const noexcept
Definition: serializablekeyvaluemap.h:123
Definition: serializablekeyvaluemap.h:214
void setDefaultValue(const typename T::ValueType &value) noexcept
Definition: serializablekeyvaluemap.h:141
SerializableKeyValueMap(const SerializableKeyValueMap< T > &other) noexcept
Definition: serializablekeyvaluemap.h:71
const QString & getValue() const
Definition: sexpression.cpp:71
void serialize(SExpression &root) const
Serialize into librepcb::SExpression node.
Definition: serializablekeyvaluemap.h:161
The Slot class is used to receive signals from non-QObject derived classes.
Definition: signalslot.h:36
The SExpression class.
Definition: sexpression.h:66
SerializableKeyValueMap(const typename T::ValueType &defaultValue) noexcept
Definition: serializablekeyvaluemap.h:73
The SerializableKeyValueMap class provides an easy way to serialize and deserialize ordered key value...
Definition: serializablekeyvaluemap.h:56
type_safe::constrained_type< QString, ElementNameConstraint, ElementNameVerifier > ElementName
Definition: elementname.h:83