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_PROJECT_PROJECTLIBRARY_H
21 #define LIBREPCB_PROJECT_PROJECTLIBRARY_H
22 
23 /*******************************************************************************
24  *  Includes
25  ******************************************************************************/
26 #include <librepcb/common/exceptions.h>
27 #include <librepcb/common/fileio/transactionaldirectory.h>
28 #include <librepcb/common/uuid.h>
29 
30 #include <QtCore>
31 
32 #include <memory>
33 
34 /*******************************************************************************
35  *  Namespace / Forward Declarations
36  ******************************************************************************/
37 namespace librepcb {
38 
39 namespace library {
40 class LibraryBaseElement;
41 class Symbol;
42 class Package;
43 class Component;
44 class Device;
45 }  // namespace library
46 
47 namespace project {
48 
49 class Project;
50 
51 /*******************************************************************************
52  *  Class ProjectLibrary
53  ******************************************************************************/
54 
55 /**
56  * @brief The ProjectLibrary class
57  */
58 class ProjectLibrary final : public QObject {
59   Q_OBJECT
60 
61 public:
62   // Constructors / Destructor
63   ProjectLibrary() = delete;
64   ProjectLibrary(const ProjectLibrary& other) = delete;
65   ProjectLibrary(std::unique_ptr<TransactionalDirectory> directory);
66   ~ProjectLibrary() noexcept;
67 
68   // Getters: Library Elements
getSymbols()69   const QHash<Uuid, library::Symbol*>& getSymbols() const noexcept {
70     return mSymbols;
71   }
getPackages()72   const QHash<Uuid, library::Package*>& getPackages() const noexcept {
73     return mPackages;
74   }
getComponents()75   const QHash<Uuid, library::Component*>& getComponents() const noexcept {
76     return mComponents;
77   }
getDevices()78   const QHash<Uuid, library::Device*>& getDevices() const noexcept {
79     return mDevices;
80   }
getSymbol(const Uuid & uuid)81   library::Symbol* getSymbol(const Uuid& uuid) const noexcept {
82     return mSymbols.value(uuid);
83   }
getPackage(const Uuid & uuid)84   library::Package* getPackage(const Uuid& uuid) const noexcept {
85     return mPackages.value(uuid);
86   }
getComponent(const Uuid & uuid)87   library::Component* getComponent(const Uuid& uuid) const noexcept {
88     return mComponents.value(uuid);
89   }
getDevice(const Uuid & uuid)90   library::Device* getDevice(const Uuid& uuid) const noexcept {
91     return mDevices.value(uuid);
92   }
93 
94   // Getters: Special Queries
95   QHash<Uuid, library::Device*> getDevicesOfComponent(
96       const Uuid& compUuid) const noexcept;
97 
98   // Add/Remove Methods
99   void addSymbol(library::Symbol& s);
100   void addPackage(library::Package& p);
101   void addComponent(library::Component& c);
102   void addDevice(library::Device& d);
103   void removeSymbol(library::Symbol& s);
104   void removePackage(library::Package& p);
105   void removeComponent(library::Component& c);
106   void removeDevice(library::Device& d);
107 
108   // General Methods
109   void save();
110 
111   // Operator Overloadings
112   ProjectLibrary& operator=(const ProjectLibrary& rhs) = delete;
113 
114 private:
115   // Private Methods
116   template <typename ElementType>
117   void loadElements(const QString& dirname, const QString& type,
118                     QHash<Uuid, ElementType*>& elementList);
119   template <typename ElementType>
120   void addElement(ElementType& element, QHash<Uuid, ElementType*>& elementList);
121   template <typename ElementType>
122   void removeElement(ElementType& element,
123                      QHash<Uuid, ElementType*>& elementList);
124 
125   // General
126   std::unique_ptr<TransactionalDirectory> mDirectory;
127 
128   // The currently added library elements
129   QHash<Uuid, library::Symbol*> mSymbols;
130   QHash<Uuid, library::Package*> mPackages;
131   QHash<Uuid, library::Component*> mComponents;
132   QHash<Uuid, library::Device*> mDevices;
133 
134   QSet<library::LibraryBaseElement*> mAllElements;
135   QSet<library::LibraryBaseElement*> mElementsToUpgrade;
136 };
137 
138 /*******************************************************************************
139  *  End of File
140  ******************************************************************************/
141 
142 }  // namespace project
143 }  // namespace librepcb
144 
145 #endif  // LIBREPCB_PROJECT_PROJECTLIBRARY_H
146