LibrePCB Developers Documentation
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>
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 values << deserialize<typename T::value_type>(child->getChild("@0"));
127 }
128 set(values);
129 }
130
138 T makeCanonical(const QList<typename T::value_type>& value) const noexcept {
139 return value; // Do not sort QList since the order is relevant!
140 }
141
149 QList<typename T::value_type> makeCanonical(
150 const QSet<typename T::value_type>& value) const noexcept {
151 return Toolbox::sortedQSet(value); // Make file format canonical.
152 }
153
157 void serializeImpl(SExpression& root) const override {
158 foreach (const auto& item, makeCanonical(mCurrentValue)) {
159 root.ensureLineBreak();
160 root.appendChild(mItemKey, item);
161 }
162 root.ensureLineBreak();
163 }
164
165private:
166 QString mItemKey;
169};
170
171/*******************************************************************************
172 * End of File
173 ******************************************************************************/
174
175} // namespace librepcb
176
177#endif
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:152
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:168
QList< typename T::value_type > makeCanonical(const QSet< typename T::value_type > &value) const noexcept
Helper for serialization of QSet.
Definition: workspacesettingsitem_genericvaluelist.h:149
~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:138
QString mItemKey
Inner key used for serialization.
Definition: workspacesettingsitem_genericvaluelist.h:166
T mDefaultValue
Initial, default value.
Definition: workspacesettingsitem_genericvaluelist.h:167
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:157
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