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 /*******************************************************************************
21  *  Includes
22  ******************************************************************************/
23 #include "componentsignal.h"
24 
25 #include <QtCore>
26 
27 /*******************************************************************************
28  *  Namespace
29  ******************************************************************************/
30 namespace librepcb {
31 namespace library {
32 
33 /*******************************************************************************
34  *  Constructors / Destructor
35  ******************************************************************************/
36 
ComponentSignal(const ComponentSignal & other)37 ComponentSignal::ComponentSignal(const ComponentSignal& other) noexcept
38   : onEdited(*this),
39     mUuid(other.mUuid),
40     mName(other.mName),
41     mRole(other.mRole),
42     mForcedNetName(other.mForcedNetName),
43     mIsRequired(other.mIsRequired),
44     mIsNegated(other.mIsNegated),
45     mIsClock(other.mIsClock) {
46 }
47 
ComponentSignal(const Uuid & uuid,const CircuitIdentifier & name,const SignalRole & role,const QString & forcedNetName,bool isRequired,bool isNegated,bool isClock)48 ComponentSignal::ComponentSignal(const Uuid& uuid,
49                                  const CircuitIdentifier& name,
50                                  const SignalRole& role,
51                                  const QString& forcedNetName, bool isRequired,
52                                  bool isNegated, bool isClock) noexcept
53   : onEdited(*this),
54     mUuid(uuid),
55     mName(name),
56     mRole(role),
57     mForcedNetName(forcedNetName),
58     mIsRequired(isRequired),
59     mIsNegated(isNegated),
60     mIsClock(isClock) {
61 }
62 
ComponentSignal(const SExpression & node,const Version & fileFormat)63 ComponentSignal::ComponentSignal(const SExpression& node,
64                                  const Version& fileFormat)
65   : onEdited(*this),
66     mUuid(deserialize<Uuid>(node.getChild("@0"), fileFormat)),
67     mName(deserialize<CircuitIdentifier>(node.getChild("name/@0"), fileFormat)),
68     mRole(deserialize<SignalRole>(node.getChild("role/@0"), fileFormat)),
69     mForcedNetName(node.getChild("forced_net/@0").getValue()),
70     mIsRequired(deserialize<bool>(node.getChild("required/@0"), fileFormat)),
71     mIsNegated(deserialize<bool>(node.getChild("negated/@0"), fileFormat)),
72     mIsClock(deserialize<bool>(node.getChild("clock/@0"), fileFormat)) {
73 }
74 
~ComponentSignal()75 ComponentSignal::~ComponentSignal() noexcept {
76 }
77 
78 /*******************************************************************************
79  *  Setters
80  ******************************************************************************/
81 
setName(const CircuitIdentifier & name)82 bool ComponentSignal::setName(const CircuitIdentifier& name) noexcept {
83   if (name == mName) {
84     return false;
85   }
86 
87   mName = name;
88   onEdited.notify(Event::NameChanged);
89   return true;
90 }
91 
setRole(const SignalRole & role)92 bool ComponentSignal::setRole(const SignalRole& role) noexcept {
93   if (role == mRole) {
94     return false;
95   }
96 
97   mRole = role;
98   onEdited.notify(Event::RoleChanged);
99   return true;
100 }
101 
setForcedNetName(const QString & name)102 bool ComponentSignal::setForcedNetName(const QString& name) noexcept {
103   if (name == mForcedNetName) {
104     return false;
105   }
106 
107   mForcedNetName = name;
108   onEdited.notify(Event::ForcedNetNameChanged);
109   return true;
110 }
111 
setIsRequired(bool required)112 bool ComponentSignal::setIsRequired(bool required) noexcept {
113   if (required == mIsRequired) {
114     return false;
115   }
116 
117   mIsRequired = required;
118   onEdited.notify(Event::IsRequiredChanged);
119   return true;
120 }
121 
setIsNegated(bool negated)122 bool ComponentSignal::setIsNegated(bool negated) noexcept {
123   if (negated == mIsNegated) {
124     return false;
125   }
126 
127   mIsNegated = negated;
128   onEdited.notify(Event::IsNegatedChanged);
129   return true;
130 }
131 
setIsClock(bool clock)132 bool ComponentSignal::setIsClock(bool clock) noexcept {
133   if (clock == mIsClock) {
134     return false;
135   }
136 
137   mIsClock = clock;
138   onEdited.notify(Event::IsClockChanged);
139   return true;
140 }
141 
142 /*******************************************************************************
143  *  General Methods
144  ******************************************************************************/
145 
serialize(SExpression & root) const146 void ComponentSignal::serialize(SExpression& root) const {
147   root.appendChild(mUuid);
148   root.appendChild("name", mName, false);
149   root.appendChild("role", mRole, false);
150   root.appendChild("required", mIsRequired, true);
151   root.appendChild("negated", mIsNegated, false);
152   root.appendChild("clock", mIsClock, false);
153   root.appendChild("forced_net", mForcedNetName, false);
154 }
155 
156 /*******************************************************************************
157  *  Operator Overloadings
158  ******************************************************************************/
159 
operator ==(const ComponentSignal & rhs) const160 bool ComponentSignal::operator==(const ComponentSignal& rhs) const noexcept {
161   if (mUuid != rhs.mUuid) return false;
162   if (mName != rhs.mName) return false;
163   if (mRole != rhs.mRole) return false;
164   if (mForcedNetName != rhs.mForcedNetName) return false;
165   if (mIsRequired != rhs.mIsRequired) return false;
166   if (mIsNegated != rhs.mIsNegated) return false;
167   if (mIsClock != rhs.mIsClock) return false;
168   return true;
169 }
170 
operator =(const ComponentSignal & rhs)171 ComponentSignal& ComponentSignal::operator=(
172     const ComponentSignal& rhs) noexcept {
173   if (mUuid != rhs.mUuid) {
174     mUuid = rhs.mUuid;
175     onEdited.notify(Event::UuidChanged);
176   }
177   setName(rhs.mName);
178   setRole(rhs.mRole);
179   setForcedNetName(rhs.mForcedNetName);
180   setIsRequired(rhs.mIsRequired);
181   setIsNegated(rhs.mIsNegated);
182   setIsClock(rhs.mIsClock);
183   return *this;
184 }
185 
186 /*******************************************************************************
187  *  End of File
188  ******************************************************************************/
189 
190 }  // namespace library
191 }  // namespace librepcb
192