LibrePCB Developers Documentation
outputjob.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_OUTPUTJOB_H
21#define LIBREPCB_CORE_OUTPUTJOB_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../serialization/serializableobjectlist.h"
27#include "../types/elementname.h"
28#include "../types/uuid.h"
29#include "../utils/toolbox.h"
30
31#include <QtCore>
32#include <QtGui>
33
34#include <memory>
35
36/*******************************************************************************
37 * Namespace / Forward Declarations
38 ******************************************************************************/
39namespace librepcb {
40
41class Project;
42
43/*******************************************************************************
44 * Class OutputJob
45 ******************************************************************************/
46
50class OutputJob {
51 Q_DECLARE_TR_FUNCTIONS(OutputJob)
52
53public:
54 // Signals
55 enum class Event {
56 UuidChanged,
57 NameChanged,
58 PropertyChanged,
59 };
62
63 // Object set
64 template <typename T>
65 struct ObjectSet {
66 ObjectSet(const ObjectSet& other) = default;
67 ObjectSet(const SExpression& node, const QString& childName)
68 : mAll(false), mDefault(false), mSet() {
69 const SExpression* firstNode = node.tryGetChild(childName % "/@0");
70 if (firstNode && (firstNode->getValue() == "all")) {
71 mAll = true;
72 } else if (firstNode && (firstNode->getValue() == "default")) {
73 mDefault = true;
74 } else {
75 foreach (const SExpression* child, node.getChildren(childName)) {
76 mSet.insert(deserialize<T>(child->getChild("@0")));
77 }
78 }
79 }
80 ObjectSet& operator=(const ObjectSet& rhs) = default;
81 bool operator==(const ObjectSet<T>& rhs) const noexcept {
82 return (mAll == rhs.mAll) && (mDefault == rhs.mDefault) &&
83 (mSet == rhs.mSet);
84 }
85 bool operator!=(const ObjectSet<T>& rhs) const noexcept {
86 return !(*this == rhs);
87 }
88
89 bool isAll() const noexcept { return mAll; }
90 bool isDefault() const noexcept { return mDefault; }
91 bool isCustom() const noexcept { return (!mAll) && (!mDefault); }
92 const QSet<T>& getSet() const noexcept { return mSet; }
93 void serialize(SExpression& root, const QString& key) const {
94 if (mAll) {
95 root.ensureLineBreak();
96 root.appendChild(key, SExpression::createToken("all"));
97 } else if (mDefault) {
98 root.ensureLineBreak();
99 root.appendChild(key, SExpression::createToken("default"));
100 } else {
101 foreach (const T& value, Toolbox::sortedQSet(mSet)) {
102 root.ensureLineBreak();
103 root.appendChild(key, value);
104 }
105 }
106 root.ensureLineBreak();
107 }
108
109 static ObjectSet all() noexcept { return ObjectSet(true, false, {}); }
110 static ObjectSet onlyDefault() noexcept {
111 return ObjectSet(false, true, {});
112 }
113 static ObjectSet set(const QSet<T>& set) noexcept {
114 return ObjectSet(false, false, set);
115 }
116
117 private:
118 ObjectSet(bool all, bool onlyDefault, const QSet<T>& set)
120
121 bool mAll;
123 QSet<T> mSet;
124 };
125
126 // Constructors / Destructor
127 OutputJob() = delete;
128 virtual ~OutputJob() noexcept;
129
130 // Getters
131 const QString& getType() const noexcept { return mType; }
132 virtual QString getTypeTr() const noexcept = 0;
133 virtual QIcon getTypeIcon() const noexcept = 0;
134 const Uuid& getUuid() const noexcept { return mUuid; }
135 const ElementName& getName() const noexcept { return mName; }
136 virtual QSet<Uuid> getDependencies() const noexcept { return {}; }
137
138 // Setters
139 void setUuid(const Uuid& uuid) noexcept;
140 void setName(const ElementName& name) noexcept;
141
142 // General Methods
143 virtual void removeDependency(const Uuid& jobUuid) { Q_UNUSED(jobUuid); }
144 virtual std::shared_ptr<OutputJob> cloneShared() const noexcept = 0;
145
151 virtual void serialize(SExpression& root) const;
152
153 // Operator Overloadings
154 bool operator==(const OutputJob& rhs) const noexcept;
155 bool operator!=(const OutputJob& rhs) const noexcept {
156 return !(*this == rhs);
157 }
158 OutputJob& operator=(const OutputJob& rhs) = delete;
159
160protected: // Methods
161 OutputJob(const OutputJob& other) noexcept;
162 explicit OutputJob(const SExpression& node);
163 OutputJob(const QString& type, const Uuid& uuid,
164 const ElementName& name) noexcept;
165 virtual void serializeDerived(SExpression& root) const = 0;
166 virtual bool equals(const OutputJob& rhs) const noexcept = 0;
167
168protected: // Data
169 const QString mType;
172
173 // Arbitrary options for forward compatibility in case we really need to
174 // add new settings in a minor release.
175 QMap<QString, QList<SExpression>> mOptions;
176};
177
178/*******************************************************************************
179 * Class OutputJobList
180 ******************************************************************************/
181
183 static constexpr const char* tagname = "job";
184};
188
189/*******************************************************************************
190 * End of File
191 ******************************************************************************/
192
193} // namespace librepcb
194
195#endif
Base class for all output job types.
Definition: outputjob.h:50
void setName(const ElementName &name) noexcept
Definition: outputjob.cpp:124
virtual bool equals(const OutputJob &rhs) const noexcept=0
OutputJob & operator=(const OutputJob &rhs)=delete
QMap< QString, QList< SExpression > > mOptions
Definition: outputjob.h:175
virtual QSet< Uuid > getDependencies() const noexcept
Definition: outputjob.h:136
virtual QString getTypeTr() const noexcept=0
const ElementName & getName() const noexcept
Definition: outputjob.h:135
Uuid mUuid
Definition: outputjob.h:170
virtual std::shared_ptr< OutputJob > cloneShared() const noexcept=0
Event
Definition: outputjob.h:55
virtual ~OutputJob() noexcept
Definition: outputjob.cpp:110
ElementName mName
Definition: outputjob.h:171
const QString mType
Definition: outputjob.h:169
void setUuid(const Uuid &uuid) noexcept
Definition: outputjob.cpp:117
virtual void serialize(SExpression &root) const
Serialize into librepcb::SExpression node.
Definition: outputjob.cpp:135
const Uuid & getUuid() const noexcept
Definition: outputjob.h:134
virtual void serializeDerived(SExpression &root) const =0
Signal< OutputJob, Event > onEdited
Definition: outputjob.h:60
Slot< OutputJob, Event > OnEditedSlot
Definition: outputjob.h:61
virtual QIcon getTypeIcon() const noexcept=0
virtual void removeDependency(const Uuid &jobUuid)
Definition: outputjob.h:143
const QString & getType() const noexcept
Definition: outputjob.h:131
The SExpression class.
Definition: sexpression.h:69
SExpression * tryGetChild(const QString &path) noexcept
Try get a child by path.
Definition: sexpression.cpp:149
QList< SExpression * > getChildren(Type type) noexcept
Definition: sexpression.cpp:94
const QString & getValue() const
Definition: sexpression.cpp:71
void ensureLineBreak()
Definition: sexpression.cpp:206
static std::unique_ptr< SExpression > createToken(const QString &token)
Definition: sexpression.cpp:401
SExpression & getChild(int index)
Definition: sexpression.cpp:86
void appendChild(std::unique_ptr< SExpression > child)
Definition: sexpression.cpp:217
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
static QList< T > sortedQSet(const QSet< T > &set) noexcept
Definition: toolbox.h:152
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
Definition: occmodel.cpp:77
type_safe::constrained_type< QString, ElementNameConstraint, ElementNameVerifier > ElementName
Definition: elementname.h:84
Definition: outputjob.h:65
static ObjectSet set(const QSet< T > &set) noexcept
Definition: outputjob.h:113
ObjectSet(const ObjectSet &other)=default
void serialize(SExpression &root, const QString &key) const
Definition: outputjob.h:93
ObjectSet & operator=(const ObjectSet &rhs)=default
static ObjectSet all() noexcept
Definition: outputjob.h:109
bool operator!=(const ObjectSet< T > &rhs) const noexcept
Definition: outputjob.h:85
const QSet< T > & getSet() const noexcept
Definition: outputjob.h:92
bool mAll
Definition: outputjob.h:121
bool operator==(const ObjectSet< T > &rhs) const noexcept
Definition: outputjob.h:81
bool isDefault() const noexcept
Definition: outputjob.h:90
bool mDefault
Definition: outputjob.h:122
static ObjectSet onlyDefault() noexcept
Definition: outputjob.h:110
bool isAll() const noexcept
Definition: outputjob.h:89
QSet< T > mSet
Definition: outputjob.h:123
ObjectSet(bool all, bool onlyDefault, const QSet< T > &set)
Definition: outputjob.h:118
bool isCustom() const noexcept
Definition: outputjob.h:91
ObjectSet(const SExpression &node, const QString &childName)
Definition: outputjob.h:67
Definition: outputjob.h:182
static constexpr const char * tagname
Definition: outputjob.h:183