LibrePCB Developers Documentation
Loading...
Searching...
No Matches
workspacelibrarydb.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_WORKSPACELIBRARYDB_H
21#define LIBREPCB_CORE_WORKSPACELIBRARYDB_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../attribute/attribute.h"
27#include "../attribute/attributetype.h"
28#include "../fileio/filepath.h"
29#include "../library/resource.h"
30#include "../types/uuid.h"
31#include "../types/version.h"
32
33#include <QtCore>
34
35/*******************************************************************************
36 * Namespace / Forward Declarations
37 ******************************************************************************/
38class QSqlQuery;
39
40namespace librepcb {
41
42class Component;
43class ComponentCategory;
44class Device;
45class Package;
46class PackageCategory;
47class SQLiteDatabase;
48class Symbol;
49class WorkspaceLibraryScanner;
50
51/*******************************************************************************
52 * Class WorkspaceLibraryDb
53 ******************************************************************************/
54
58class WorkspaceLibraryDb final : public QObject {
59 Q_OBJECT
60
61public:
62 struct Part {
63 QString mpn;
64 QString manufacturer;
66
67 bool operator==(const Part& rhs) const noexcept {
68 return (mpn == rhs.mpn) && (manufacturer == rhs.manufacturer) &&
69 (attributes == rhs.attributes);
70 }
71 bool operator<(const Part& rhs) const noexcept {
72 if (mpn.isEmpty() != rhs.mpn.isEmpty()) {
73 return mpn.count() < rhs.mpn.count();
74 }
75 if (mpn != rhs.mpn) {
76 return mpn < rhs.mpn;
77 }
78 if (manufacturer != rhs.manufacturer) {
79 return manufacturer < rhs.manufacturer;
80 }
81 QCollator collator;
82 collator.setNumericMode(true);
83 collator.setCaseSensitivity(Qt::CaseInsensitive);
84 collator.setIgnorePunctuation(false);
85 for (int i = 0; i < std::max(attributes.count(), rhs.attributes.count());
86 ++i) {
87 auto a = attributes.value(i);
88 auto b = rhs.attributes.value(i);
89 if (a && (!b)) {
90 return false;
91 } else if ((!a) && b) {
92 return true;
93 } else if (a->getKey() != b->getKey()) {
94 return a->getKey() < b->getKey();
95 } else if (&a->getType() != &b->getType()) {
96 return a->getType().getName() < b->getType().getName();
97 } else if (a->getValueTr(true) != b->getValueTr(true)) {
98 return collator(a->getValueTr(true), b->getValueTr(true));
99 }
100 }
101 return false;
102 }
103 };
104
105 // Constructors / Destructor
108
117 explicit WorkspaceLibraryDb(const FilePath& librariesPath);
118 ~WorkspaceLibraryDb() noexcept;
119
120 // Getters
121
127 const FilePath& getFilePath() const noexcept { return mFilePath; }
128
134 bool isScanInProgress() const noexcept {
135 return getScanProgressPercent() < 100;
136 }
137
143 int getScanProgressPercent() const noexcept;
144
156 template <typename ElementType>
158 const std::optional<Uuid>& uuid = std::nullopt,
159 const FilePath& lib = FilePath()) const {
160 return getAll(getTable<ElementType>(), uuid, lib);
161 }
162
172 template <typename ElementType>
173 QHash<FilePath, Uuid> getAll(const FilePath& lib) const {
174 return getAll(getTable<ElementType>(), lib);
175 }
176
186 template <typename ElementType>
187 FilePath getLatest(const Uuid& uuid) const {
188 return getLatestVersionFilePath(getAll<ElementType>(uuid));
189 }
190
200 template <typename ElementType>
201 QList<Uuid> find(const QString& keyword) const;
202
212 QList<Uuid> findDevicesOfParts(const QString& keyword) const;
213
224 QList<Part> findPartsOfDevice(const Uuid& device,
225 const QString& keyword) const;
226
247 template <typename ElementType>
248 bool getTranslations(const FilePath& elemDir, const QStringList& localeOrder,
249 QString* name = nullptr, QString* description = nullptr,
250 QString* keywords = nullptr) const {
251 return getTranslations(getTable<ElementType>(), elemDir, localeOrder, name,
252 description, keywords);
253 }
254
271 template <typename ElementType>
272 bool getMetadata(const FilePath elemDir, Uuid* uuid = nullptr,
273 Version* version = nullptr,
274 bool* deprecated = nullptr) const {
275 return getMetadata(getTable<ElementType>(), elemDir, uuid, version,
276 deprecated);
277 }
278
292 bool getLibraryMetadata(const FilePath libDir, QPixmap* icon = nullptr,
293 QString* manufacturer = nullptr) const;
294
307 template <typename ElementType>
308 bool getCategoryMetadata(const FilePath catDir,
309 std::optional<Uuid>* parent = nullptr) const {
310 static_assert(std::is_same<ElementType, ComponentCategory>::value ||
311 std::is_same<ElementType, PackageCategory>::value,
312 "Unsupported ElementType");
313 return getCategoryMetadata(getTable<ElementType>(), catDir, parent);
314 }
315
328 bool getDeviceMetadata(const FilePath& devDir, Uuid* cmpUuid = nullptr,
329 Uuid* pkgUuid = nullptr) const;
330
344 template <typename ElementType>
345 QSet<Uuid> getChilds(const std::optional<Uuid>& parent) const {
346 static_assert(std::is_same<ElementType, ComponentCategory>::value ||
347 std::is_same<ElementType, PackageCategory>::value,
348 "Unsupported ElementType");
349 return getChilds(getTable<ElementType>(), parent);
350 }
351
369 template <typename ElementType>
370 QSet<Uuid> getByCategory(const std::optional<Uuid>& category,
371 int limit = -1) const {
372 static_assert(std::is_same<ElementType, Symbol>::value ||
373 std::is_same<ElementType, Package>::value ||
374 std::is_same<ElementType, Component>::value ||
375 std::is_same<ElementType, Device>::value,
376 "Unsupported ElementType");
377 return getByCategory(getTable<ElementType>(),
378 getCategoryTable<ElementType>(), category, limit);
379 }
380
391 template <typename ElementType>
392 QSet<Uuid> getCategoriesOf(const FilePath& elemDir) const {
393 static_assert(std::is_same<ElementType, Symbol>::value ||
394 std::is_same<ElementType, Package>::value ||
395 std::is_same<ElementType, Component>::value ||
396 std::is_same<ElementType, Device>::value,
397 "Unsupported ElementType");
398 return getCategoriesOf(getTable<ElementType>(),
399 getCategoryTable<ElementType>(), elemDir);
400 }
401
412 template <typename ElementType>
413 QSet<Uuid> getGenerated(const QString& generatedBy) const {
414 static_assert(std::is_same<ElementType, Symbol>::value ||
415 std::is_same<ElementType, Package>::value ||
416 std::is_same<ElementType, Component>::value ||
417 std::is_same<ElementType, Device>::value,
418 "Unsupported ElementType");
419 return getGenerated(getTable<ElementType>(), generatedBy);
420 }
421
431 template <typename ElementType>
432 ResourceList getResources(const FilePath elemDir) const {
433 static_assert(std::is_same<ElementType, Component>::value ||
434 std::is_same<ElementType, Device>::value,
435 "Unsupported ElementType");
436 return getResources(getTable<ElementType>(), elemDir);
437 }
438
446 QSet<Uuid> getComponentDevices(const Uuid& component) const;
447
455 QList<Part> getDeviceParts(const Uuid& device) const;
456
457 // General Methods
458
462 void startLibraryRescan() noexcept;
463
464 // Operator Overloadings
465 WorkspaceLibraryDb& operator=(const WorkspaceLibraryDb& rhs) = delete;
466
467signals:
469 void scanLibraryListUpdated(int libraryCount);
470 void scanProgressUpdate(int percent);
471 void scanSucceeded(int elementCount);
472 void scanFailed(QString errorMsg);
474 void scanInProgressChanged(bool inProgress);
475
476private:
477 // Private Methods
478 QMultiMap<Version, FilePath> getAll(const QString& elementsTable,
479 const std::optional<Uuid>& uuid,
480 const FilePath& lib) const;
481 QHash<FilePath, Uuid> getAll(const QString& elementsTable,
482 const FilePath& lib) const;
484 const QMultiMap<Version, FilePath>& list) const noexcept;
485 QList<Uuid> find(const QString& elementsTable, const QString& keyword) const;
486 bool getTranslations(const QString& elementsTable, const FilePath& elemDir,
487 const QStringList& localeOrder, QString* name,
488 QString* description, QString* keywords) const;
489 bool getMetadata(const QString& elementsTable, const FilePath elemDir,
490 Uuid* uuid, Version* version, bool* deprecated) const;
491 bool getCategoryMetadata(const QString& categoriesTable,
492 const FilePath catDir,
493 std::optional<Uuid>* parent) const;
494 AttributeList getPartAttributes(int partId) const;
495 QSet<Uuid> getChilds(const QString& categoriesTable,
496 const std::optional<Uuid>& categoryUuid) const;
497 QSet<Uuid> getByCategory(const QString& elementsTable,
498 const QString& categoryTable,
499 const std::optional<Uuid>& category,
500 int limit) const;
501 QSet<Uuid> getCategoriesOf(const QString& elementsTable,
502 const QString& categoryTable,
503 const FilePath& elemDir) const;
504 QSet<Uuid> getGenerated(const QString& elementsTable,
505 const QString& generatedBy) const;
506 ResourceList getResources(const QString& elementsTable,
507 const FilePath& elemDir) const;
508 static QSet<Uuid> getUuidSet(QSqlQuery& query);
509 int getDbVersion() const noexcept;
510 template <typename ElementType>
511 static QString getTable() noexcept;
512 template <typename ElementType>
513 static QString getCategoryTable() noexcept;
514
515 // Attributes
518 QScopedPointer<SQLiteDatabase> mDb;
520
521 // Constants
522 static const int sCurrentDbVersion = 7;
523};
524
525/*******************************************************************************
526 * End of File
527 ******************************************************************************/
528
529} // namespace librepcb
530
531#endif
This class represents absolute, well-formatted paths to files or directories.
Definition filepath.h:127
The SQLiteDatabase class.
Definition sqlitedatabase.h:43
int count() const noexcept
Definition serializableobjectlist.h:199
std::shared_ptr< T > value(int index) noexcept
Definition serializableobjectlist.h:254
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition uuid.h:56
The Version class represents a version number in the format "1.42.7".
Definition version.h:58
The WorkspaceLibraryDb class.
Definition workspacelibrarydb.h:58
ResourceList getResources(const FilePath elemDir) const
Get resources of a specific library element.
Definition workspacelibrarydb.h:432
WorkspaceLibraryDb(const WorkspaceLibraryDb &other)=delete
QSet< Uuid > getGenerated(const QString &generatedBy) const
Get elements generated by a given "generated_by" identifier.
Definition workspacelibrarydb.h:413
bool getLibraryMetadata(const FilePath libDir, QPixmap *icon=nullptr, QString *manufacturer=nullptr) const
Get additional metadata of a specific library.
Definition workspacelibrarydb.cpp:201
void scanProgressUpdate(int percent)
static const int sCurrentDbVersion
Definition workspacelibrarydb.h:522
~WorkspaceLibraryDb() noexcept
Definition workspacelibrarydb.cpp:106
QMultiMap< Version, FilePath > getAll(const std::optional< Uuid > &uuid=std::nullopt, const FilePath &lib=FilePath()) const
Get elements, optionally matching some criteria.
Definition workspacelibrarydb.h:157
FilePath getLatestVersionFilePath(const QMultiMap< Version, FilePath > &list) const noexcept
Definition workspacelibrarydb.cpp:369
bool getDeviceMetadata(const FilePath &devDir, Uuid *cmpUuid=nullptr, Uuid *pkgUuid=nullptr) const
Get additional metadata of a specific device.
Definition workspacelibrarydb.cpp:225
void scanFailed(QString errorMsg)
QList< Uuid > find(const QString &keyword) const
Find elements by keyword.
Definition workspacelibrarydb.cpp:118
QScopedPointer< SQLiteDatabase > mDb
The SQLite database.
Definition workspacelibrarydb.h:518
void scanInProgressChanged(bool inProgress)
void scanSucceeded(int elementCount)
const FilePath & getFilePath() const noexcept
Get the file path of the SQLite database.
Definition workspacelibrarydb.h:127
AttributeList getPartAttributes(int partId) const
Definition workspacelibrarydb.cpp:496
void scanLibraryListUpdated(int libraryCount)
FilePath getLatest(const Uuid &uuid) const
Get an element of a specific UUID and the highest version.
Definition workspacelibrarydb.h:187
QSet< Uuid > getChilds(const std::optional< Uuid > &parent) const
Get children categories of a specific category.
Definition workspacelibrarydb.h:345
bool getMetadata(const FilePath elemDir, Uuid *uuid=nullptr, Version *version=nullptr, bool *deprecated=nullptr) const
Get metadata of a specific element.
Definition workspacelibrarydb.h:272
static QString getTable() noexcept
Definition workspacelibrarydb.cpp:668
QList< Part > getDeviceParts(const Uuid &device) const
Get all parts of a specific device.
Definition workspacelibrarydb.cpp:263
QSet< Uuid > getByCategory(const std::optional< Uuid > &category, int limit=-1) const
Get elements of a specific category.
Definition workspacelibrarydb.h:370
void startLibraryRescan() noexcept
Rescan the whole library directory and update the SQLite database.
Definition workspacelibrarydb.cpp:286
bool getCategoryMetadata(const FilePath catDir, std::optional< Uuid > *parent=nullptr) const
Get additional metadata of a specific category.
Definition workspacelibrarydb.h:308
QSet< Uuid > getCategoriesOf(const FilePath &elemDir) const
Get all categories assigned to a particular library element.
Definition workspacelibrarydb.h:392
bool isScanInProgress() const noexcept
Check if there is currently a library scan in progress.
Definition workspacelibrarydb.h:134
static QString getCategoryTable() noexcept
Definition workspacelibrarydb.cpp:682
QScopedPointer< WorkspaceLibraryScanner > mLibraryScanner
Definition workspacelibrarydb.h:519
bool getTranslations(const FilePath &elemDir, const QStringList &localeOrder, QString *name=nullptr, QString *description=nullptr, QString *keywords=nullptr) const
Get translations of a specific element.
Definition workspacelibrarydb.h:248
int getScanProgressPercent() const noexcept
Get the current progress of the library rescan.
Definition workspacelibrarydb.cpp:113
static QSet< Uuid > getUuidSet(QSqlQuery &query)
Definition workspacelibrarydb.cpp:640
int getDbVersion() const noexcept
Definition workspacelibrarydb.cpp:648
QSet< Uuid > getComponentDevices(const Uuid &component) const
Get all devices of a specific component.
Definition workspacelibrarydb.cpp:252
const FilePath mLibrariesPath
Path to workspace libraries directory.
Definition workspacelibrarydb.h:516
const FilePath mFilePath
Path to the SQLite database file.
Definition workspacelibrarydb.h:517
QList< Uuid > findDevicesOfParts(const QString &keyword) const
Find parts by keyword.
Definition workspacelibrarydb.cpp:157
QHash< FilePath, Uuid > getAll(const FilePath &lib) const
Get all elements of a particular library.
Definition workspacelibrarydb.h:173
QList< Part > findPartsOfDevice(const Uuid &device, const QString &keyword) const
Find parts of device by keyword.
Definition workspacelibrarydb.cpp:179
The WorkspaceLibraryScanner class.
Definition workspacelibraryscanner.h:52
Definition occmodel.cpp:77
Definition uuid.h:186
Definition workspacelibrarydb.h:62
QString mpn
Definition workspacelibrarydb.h:63
bool operator==(const Part &rhs) const noexcept
Definition workspacelibrarydb.h:67
QString manufacturer
Definition workspacelibrarydb.h:64
bool operator<(const Part &rhs) const noexcept
Definition workspacelibrarydb.h:71
AttributeList attributes
Definition workspacelibrarydb.h:65