LibrePCB Developers Documentation
Loading...
Searching...
No Matches
workspacesettingsitem_genericvaluelist.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_WORKSPACESETTINGSITEM_GENERICVALUELIST_H
21#define LIBREPCB_CORE_WORKSPACESETTINGSITEM_GENERICVALUELIST_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
27
29
30#include <QtCore>
31
32/*******************************************************************************
33 * Namespace / Forward Declarations
34 ******************************************************************************/
35namespace librepcb {
36
37/*******************************************************************************
38 * Class WorkspaceSettingsItem_GenericValueList
39 ******************************************************************************/
40
45template <typename T, bool SerializeRaw = false>
47 : public WorkspaceSettingsItem {
48public:
49 // Constructors / Destructor
52 const WorkspaceSettingsItem_GenericValueList& other) = delete;
54 const QString& listKey, const QString& itemKey, const T& defaultValue,
55 QObject* parent = nullptr) noexcept
56 : WorkspaceSettingsItem(listKey, parent),
57 mItemKey(itemKey),
58 mDefaultValue(defaultValue),
59 mCurrentValue(defaultValue) {}
61
67 const T& get() const noexcept { return mCurrentValue; }
68
76 bool contains(const typename T::value_type& item) const noexcept {
77 return mCurrentValue.contains(item);
78 }
79
85 void set(const T& value) noexcept {
86 if (value != mCurrentValue) {
87 mCurrentValue = value;
89 }
90 }
91
97 void add(const typename T::value_type& item) noexcept {
98 auto newValue = mCurrentValue;
99 newValue << item;
100 set(newValue);
101 }
102
108 const T& getDefault() const noexcept { return mDefaultValue; }
109
110 // Operator Overloadings
112 const WorkspaceSettingsItem_GenericValueList& rhs) = delete;
113
114private: // Methods
118 virtual void restoreDefaultImpl() noexcept override { set(mDefaultValue); }
119
123 void loadImpl(const SExpression& root) override {
124 T values; // temporary object to make this method atomic
125 foreach (const SExpression* child, root.getChildren(mItemKey)) {
126 if (SerializeRaw) {
127 values << deserialize<typename T::value_type>(*child);
128 } else {
129 values << deserialize<typename T::value_type>(child->getChild("@0"));
130 }
131 }
132 set(values);
133 }
134
142 T makeCanonical(const QList<typename T::value_type>& value) const noexcept {
143 return value; // Do not sort QList since the order is relevant!
144 }
145
153 QList<typename T::value_type> makeCanonical(
154 const QSet<typename T::value_type>& value) const noexcept {
155 return Toolbox::sortedQSet(value); // Make file format canonical.
156 }
157
161 void serializeImpl(SExpression& root) const override {
162 foreach (const auto& item, makeCanonical(mCurrentValue)) {
163 root.ensureLineBreak();
164 std::unique_ptr<SExpression> node = ::librepcb::serialize(item);
165 if (SerializeRaw) {
166 if (node->getName() != mItemKey) {
167 throw LogicError(__FILE__, __LINE__);
168 }
169 root.appendChild(std::move(node));
170 } else {
171 root.appendChild(mItemKey, std::move(node));
172 }
173 }
174 root.ensureLineBreak();
175 }
176
177private:
178 QString mItemKey;
181};
182
183/*******************************************************************************
184 * End of File
185 ******************************************************************************/
186
187} // namespace librepcb
188
189#endif
The LogicError class.
Definition exceptions.h:181
The SExpression class.
Definition sexpression.h:69
QList< SExpression * > getChildren(Type type) noexcept
Definition sexpression.cpp:94
void ensureLineBreak()
Definition sexpression.cpp:206
SExpression & getChild(int index)
Definition sexpression.cpp:86
void appendChild(std::unique_ptr< SExpression > child)
Definition sexpression.cpp:217
static QList< T > sortedQSet(const QSet< T > &set) noexcept
Definition toolbox.h:117
Generic implementation of librepcb::WorkspaceSettingsItem for simple, value-in-list-type settings.
Definition workspacesettingsitem_genericvaluelist.h:47
void loadImpl(const SExpression &root) override
Load value from S-Expression node.
Definition workspacesettingsitem_genericvaluelist.h:123
T mCurrentValue
Current value.
Definition workspacesettingsitem_genericvaluelist.h:180
QList< typename T::value_type > makeCanonical(const QSet< typename T::value_type > &value) const noexcept
Helper for serialization of QSet.
Definition workspacesettingsitem_genericvaluelist.h:153
~WorkspaceSettingsItem_GenericValueList() noexcept
Definition workspacesettingsitem_genericvaluelist.h:60
void add(const typename T::value_type &item) noexcept
Add a single item to the value list.
Definition workspacesettingsitem_genericvaluelist.h:97
WorkspaceSettingsItem_GenericValueList(const QString &listKey, const QString &itemKey, const T &defaultValue, QObject *parent=nullptr) noexcept
Definition workspacesettingsitem_genericvaluelist.h:53
void set(const T &value) noexcept
Set the value.
Definition workspacesettingsitem_genericvaluelist.h:85
T makeCanonical(const QList< typename T::value_type > &value) const noexcept
Helper for serialization of QList.
Definition workspacesettingsitem_genericvaluelist.h:142
QString mItemKey
Inner key used for serialization.
Definition workspacesettingsitem_genericvaluelist.h:178
T mDefaultValue
Initial, default value.
Definition workspacesettingsitem_genericvaluelist.h:179
WorkspaceSettingsItem_GenericValueList(const WorkspaceSettingsItem_GenericValueList &other)=delete
WorkspaceSettingsItem_GenericValueList & operator=(const WorkspaceSettingsItem_GenericValueList &rhs)=delete
bool contains(const typename T::value_type &item) const noexcept
Check if the current value contains a praticular item.
Definition workspacesettingsitem_genericvaluelist.h:76
void serializeImpl(SExpression &root) const override
Serialize the value into S-Expression node.
Definition workspacesettingsitem_genericvaluelist.h:161
const T & getDefault() const noexcept
Get the default value.
Definition workspacesettingsitem_genericvaluelist.h:108
const T & get() const noexcept
Get the current value.
Definition workspacesettingsitem_genericvaluelist.h:67
virtual void restoreDefaultImpl() noexcept override
Restore default value.
Definition workspacesettingsitem_genericvaluelist.h:118
Base class for all workspace settings items.
Definition workspacesettingsitem.h:49
void valueModified() noexcept
Definition workspacesettingsitem.cpp:48
Definition occmodel.cpp:77
std::unique_ptr< SExpression > serialize(const AttributeKey &obj)
Definition attributekey.h:94