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_LIBRARY_FOOTPRINT_H
21 #define LIBREPCB_LIBRARY_FOOTPRINT_H
22 
23 /*******************************************************************************
24  *  Includes
25  ******************************************************************************/
26 #include "footprintpad.h"
27 
28 #include <librepcb/common/fileio/cmd/cmdlistelementinsert.h>
29 #include <librepcb/common/fileio/cmd/cmdlistelementremove.h>
30 #include <librepcb/common/fileio/cmd/cmdlistelementsswap.h>
31 #include <librepcb/common/fileio/serializablekeyvaluemap.h>
32 #include <librepcb/common/fileio/serializableobjectlist.h>
33 #include <librepcb/common/geometry/circle.h>
34 #include <librepcb/common/geometry/hole.h>
35 #include <librepcb/common/geometry/polygon.h>
36 #include <librepcb/common/geometry/stroketext.h>
37 
38 #include <QtCore>
39 
40 /*******************************************************************************
41  *  Namespace / Forward Declarations
42  ******************************************************************************/
43 namespace librepcb {
44 namespace library {
45 
46 class FootprintGraphicsItem;
47 
48 /*******************************************************************************
49  *  Class Footprint
50  ******************************************************************************/
51 
52 /**
53  * @brief The Footprint class represents one footprint variant of a package
54  *
55  * Following information is considered as the "interface" of a footprint and
56  * must therefore never be changed:
57  *  - UUID
58  *  - Footprint pads (neither adding nor removing pads is allowed)
59  *    - UUID
60  */
61 class Footprint final : public SerializableObject {
62   Q_DECLARE_TR_FUNCTIONS(Footprint)
63 
64 public:
65   // Signals
66   enum class Event {
67     UuidChanged,
68     NamesEdited,
69     DescriptionsEdited,
70     PadsEdited,
71     PolygonsEdited,
72     CirclesEdited,
73     StrokeTextsEdited,
74     HolesEdited,
75   };
76   Signal<Footprint, Event> onEdited;
77   typedef Slot<Footprint, Event> OnEditedSlot;
78 
79   // Constructors / Destructor
80   Footprint() = delete;
81   Footprint(const Footprint& other) noexcept;
82   Footprint(const Uuid& uuid, const ElementName& name_en_US,
83             const QString& description_en_US);
84   Footprint(const SExpression& node, const Version& fileFormat);
85   ~Footprint() noexcept;
86 
87   // Getters: General
getUuid()88   const Uuid& getUuid() const noexcept { return mUuid; }
getNames()89   LocalizedNameMap& getNames() noexcept { return mNames; }
getNames()90   const LocalizedNameMap& getNames() const noexcept { return mNames; }
getDescriptions()91   LocalizedDescriptionMap& getDescriptions() noexcept { return mDescriptions; }
getDescriptions()92   const LocalizedDescriptionMap& getDescriptions() const noexcept {
93     return mDescriptions;
94   }
95 
96   // Getters: Geometry
getPads()97   const FootprintPadList& getPads() const noexcept { return mPads; }
getPads()98   FootprintPadList& getPads() noexcept { return mPads; }
getPolygons()99   const PolygonList& getPolygons() const noexcept { return mPolygons; }
getPolygons()100   PolygonList& getPolygons() noexcept { return mPolygons; }
getCircles()101   const CircleList& getCircles() const noexcept { return mCircles; }
getCircles()102   CircleList& getCircles() noexcept { return mCircles; }
getStrokeTexts()103   const StrokeTextList& getStrokeTexts() const noexcept { return mStrokeTexts; }
getStrokeTexts()104   StrokeTextList& getStrokeTexts() noexcept { return mStrokeTexts; }
getHoles()105   const HoleList& getHoles() const noexcept { return mHoles; }
getHoles()106   HoleList& getHoles() noexcept { return mHoles; }
107 
108   // General Methods
109   void setStrokeFontForAllTexts(const StrokeFont* font) noexcept;
110   void registerGraphicsItem(FootprintGraphicsItem& item) noexcept;
111   void unregisterGraphicsItem(FootprintGraphicsItem& item) noexcept;
112 
113   // General Methods
114 
115   /// @copydoc librepcb::SerializableObject::serialize()
116   void serialize(SExpression& root) const override;
117 
118   // Operator Overloadings
119   bool operator==(const Footprint& rhs) const noexcept;
120   bool operator!=(const Footprint& rhs) const noexcept {
121     return !(*this == rhs);
122   }
123   Footprint& operator=(const Footprint& rhs) noexcept;
124 
125 private:  // Methods
126   void namesEdited(const LocalizedNameMap& names, const QString& key,
127                    LocalizedNameMap::Event event) noexcept;
128   void descriptionsEdited(const LocalizedDescriptionMap& names,
129                           const QString& key,
130                           LocalizedDescriptionMap::Event event) noexcept;
131   void padsEdited(const FootprintPadList& list, int index,
132                   const std::shared_ptr<const FootprintPad>& pad,
133                   FootprintPadList::Event event) noexcept;
134   void polygonsEdited(const PolygonList& list, int index,
135                       const std::shared_ptr<const Polygon>& polygon,
136                       PolygonList::Event event) noexcept;
137   void circlesEdited(const CircleList& list, int index,
138                      const std::shared_ptr<const Circle>& circle,
139                      CircleList::Event event) noexcept;
140   void strokeTextsEdited(const StrokeTextList& list, int index,
141                          const std::shared_ptr<const StrokeText>& text,
142                          StrokeTextList::Event event) noexcept;
143   void holesEdited(const HoleList& list, int index,
144                    const std::shared_ptr<const Hole>& hole,
145                    HoleList::Event event) noexcept;
146 
147 private:  // Data
148   Uuid mUuid;
149   LocalizedNameMap mNames;
150   LocalizedDescriptionMap mDescriptions;
151   FootprintPadList mPads;
152   PolygonList mPolygons;
153   CircleList mCircles;
154   StrokeTextList mStrokeTexts;
155   HoleList mHoles;
156 
157   const StrokeFont* mStrokeFont;
158   FootprintGraphicsItem* mRegisteredGraphicsItem;
159 
160   // Slots
161   LocalizedNameMap::OnEditedSlot mNamesEditedSlot;
162   LocalizedDescriptionMap::OnEditedSlot mDescriptionsEditedSlot;
163   FootprintPadList::OnEditedSlot mPadsEditedSlot;
164   PolygonList::OnEditedSlot mPolygonsEditedSlot;
165   CircleList::OnEditedSlot mCirclesEditedSlot;
166   StrokeTextList::OnEditedSlot mStrokeTextsEditedSlot;
167   HoleList::OnEditedSlot mHolesEditedSlot;
168 };
169 
170 /*******************************************************************************
171  *  Class FootprintList
172  ******************************************************************************/
173 
174 struct FootprintListNameProvider {
175   static constexpr const char* tagname = "footprint";
176 };
177 using FootprintList =
178     SerializableObjectList<Footprint, FootprintListNameProvider,
179                            Footprint::Event>;
180 using CmdFootprintInsert =
181     CmdListElementInsert<Footprint, FootprintListNameProvider,
182                          Footprint::Event>;
183 using CmdFootprintRemove =
184     CmdListElementRemove<Footprint, FootprintListNameProvider,
185                          Footprint::Event>;
186 using CmdFootprintsSwap =
187     CmdListElementsSwap<Footprint, FootprintListNameProvider, Footprint::Event>;
188 
189 /*******************************************************************************
190  *  End of File
191  ******************************************************************************/
192 
193 }  // namespace library
194 }  // namespace librepcb
195 
196 #endif  // LIBREPCB_LIBRARY_FOOTPRINT_H
197