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 Organization;
46class Package;
47class PackageCategory;
48class SQLiteDatabase;
49class Symbol;
50class WorkspaceLibraryScanner;
51
52/*******************************************************************************
53 * Class WorkspaceLibraryDb
54 ******************************************************************************/
55
59class WorkspaceLibraryDb final : public QObject {
60 Q_OBJECT
61
62public:
63 struct Part {
64 QString mpn;
65 QString manufacturer;
67
68 bool operator==(const Part& rhs) const noexcept {
69 return (mpn == rhs.mpn) && (manufacturer == rhs.manufacturer) &&
70 (attributes == rhs.attributes);
71 }
72 bool operator<(const Part& rhs) const noexcept {
73 if (mpn.isEmpty() != rhs.mpn.isEmpty()) {
74 return mpn.count() < rhs.mpn.count();
75 }
76 if (mpn != rhs.mpn) {
77 return mpn < rhs.mpn;
78 }
79 if (manufacturer != rhs.manufacturer) {
80 return manufacturer < rhs.manufacturer;
81 }
82 QCollator collator;
83 collator.setNumericMode(true);
84 collator.setCaseSensitivity(Qt::CaseInsensitive);
85 collator.setIgnorePunctuation(false);
86 for (int i = 0; i < std::max(attributes.count(), rhs.attributes.count());
87 ++i) {
88 auto a = attributes.value(i);
89 auto b = rhs.attributes.value(i);
90 if (a && (!b)) {
91 return false;
92 } else if ((!a) && b) {
93 return true;
94 } else if (a->getKey() != b->getKey()) {
95 return a->getKey() < b->getKey();
96 } else if (&a->getType() != &b->getType()) {
97 return a->getType().getName() < b->getType().getName();
98 } else if (a->getValueTr(true) != b->getValueTr(true)) {
99 return collator(a->getValueTr(true), b->getValueTr(true));
100 }
101 }
102 return false;
103 }
104 };
107 QString name;
108 QString description;
109 QUrl url;
111 };
112 enum class OutputJobKind : int {
113 Pcb = 1,
114 Assembly = 2,
115 User = 99,
116 };
117 struct OutputJob {
120 QString type;
121 QString name;
122 };
124 int id;
127 QString name;
128 QString description;
130 QPixmap logo;
131 QUrl url;
132 QString country;
133 QStringList fabs;
134 QStringList shipping;
137 QList<PcbDesignRules> pcbDesignRules;
138 QList<OutputJob> outputJobs;
139 };
140
141 // Constructors / Destructor
144
153 explicit WorkspaceLibraryDb(const FilePath& librariesPath);
154 ~WorkspaceLibraryDb() noexcept;
155
156 // Getters
157
163 const FilePath& getFilePath() const noexcept { return mFilePath; }
164
170 bool isScanInProgress() const noexcept {
171 return getScanProgressPercent() < 100;
172 }
173
179 int getScanProgressPercent() const noexcept;
180
192 template <typename ElementType>
194 const std::optional<Uuid>& uuid = std::nullopt,
195 const FilePath& lib = FilePath()) const {
196 return getAll(getTable<ElementType>(), uuid, lib);
197 }
198
208 template <typename ElementType>
209 QHash<FilePath, Uuid> getAll(const FilePath& lib) const {
210 return getAll(getTable<ElementType>(), lib);
211 }
212
222 template <typename ElementType>
223 FilePath getLatest(const Uuid& uuid) const {
224 return getLatestVersionFilePath(getAll<ElementType>(uuid));
225 }
226
236 template <typename ElementType>
237 QList<Uuid> find(const QString& keyword) const;
238
248 QList<Uuid> findDevicesOfParts(const QString& keyword) const;
249
260 QList<Part> findPartsOfDevice(const Uuid& device,
261 const QString& keyword) const;
262
283 template <typename ElementType>
284 bool getTranslations(const FilePath& elemDir, const QStringList& localeOrder,
285 QString* name = nullptr, QString* description = nullptr,
286 QString* keywords = nullptr) const {
287 return getTranslations(getTable<ElementType>(), elemDir, localeOrder, name,
288 description, keywords);
289 }
290
307 template <typename ElementType>
308 bool getMetadata(const FilePath elemDir, Uuid* uuid = nullptr,
309 Version* version = nullptr,
310 bool* deprecated = nullptr) const {
311 return getMetadata(getTable<ElementType>(), elemDir, uuid, version,
312 deprecated);
313 }
314
328 bool getLibraryMetadata(const FilePath libDir, QPixmap* icon = nullptr,
329 QString* manufacturer = nullptr) const;
330
343 template <typename ElementType>
344 bool getCategoryMetadata(const FilePath catDir,
345 std::optional<Uuid>* parent = nullptr) const {
346 static_assert(std::is_same<ElementType, ComponentCategory>::value ||
347 std::is_same<ElementType, PackageCategory>::value,
348 "Unsupported ElementType");
349 return getCategoryMetadata(getTable<ElementType>(), catDir, parent);
350 }
351
364 bool getDeviceMetadata(const FilePath& devDir, Uuid* cmpUuid = nullptr,
365 Uuid* pkgUuid = nullptr) const;
366
380 template <typename ElementType>
381 QSet<Uuid> getChilds(const std::optional<Uuid>& parent) const {
382 static_assert(std::is_same<ElementType, ComponentCategory>::value ||
383 std::is_same<ElementType, PackageCategory>::value,
384 "Unsupported ElementType");
385 return getChilds(getTable<ElementType>(), parent);
386 }
387
405 template <typename ElementType>
406 QSet<Uuid> getByCategory(const std::optional<Uuid>& category,
407 int limit = -1) const {
408 static_assert(std::is_same<ElementType, Symbol>::value ||
409 std::is_same<ElementType, Package>::value ||
410 std::is_same<ElementType, Component>::value ||
411 std::is_same<ElementType, Device>::value,
412 "Unsupported ElementType");
413 return getByCategory(getTable<ElementType>(),
414 getCategoryTable<ElementType>(), category, limit);
415 }
416
427 template <typename ElementType>
428 QSet<Uuid> getCategoriesOf(const FilePath& elemDir) const {
429 static_assert(std::is_same<ElementType, Symbol>::value ||
430 std::is_same<ElementType, Package>::value ||
431 std::is_same<ElementType, Component>::value ||
432 std::is_same<ElementType, Device>::value,
433 "Unsupported ElementType");
434 return getCategoriesOf(getTable<ElementType>(),
435 getCategoryTable<ElementType>(), elemDir);
436 }
437
448 template <typename ElementType>
449 QSet<Uuid> getGenerated(const QString& generatedBy) const {
450 static_assert(std::is_same<ElementType, Symbol>::value ||
451 std::is_same<ElementType, Package>::value ||
452 std::is_same<ElementType, Component>::value ||
453 std::is_same<ElementType, Device>::value,
454 "Unsupported ElementType");
455 return getGenerated(getTable<ElementType>(), generatedBy);
456 }
457
467 template <typename ElementType>
468 ResourceList getResources(const FilePath elemDir) const {
469 static_assert(std::is_same<ElementType, Component>::value ||
470 std::is_same<ElementType, Device>::value,
471 "Unsupported ElementType");
472 return getResources(getTable<ElementType>(), elemDir);
473 }
474
482 QSet<Uuid> getComponentDevices(const Uuid& component) const;
483
491 QList<Part> getDeviceParts(const Uuid& device) const;
492
502 QList<Organization> getAllLatestOrganizations(const QStringList& localeOrder,
503 bool pcbDesignRules,
504 bool outputJobs) const;
505
506 // General Methods
507
511 void resetAndRescan() noexcept;
512
516 void startLibraryRescan() noexcept;
517
524 bool cancelLibraryRescan() noexcept;
525
526 // Operator Overloadings
527 WorkspaceLibraryDb& operator=(const WorkspaceLibraryDb& rhs) = delete;
528
529signals:
531 void scanLibraryListUpdated(int libraryCount);
532 void scanProgressUpdate(int percent);
533 void scanSucceeded(int elementCount);
534 void scanFailed(QString errorMsg);
536 void scanInProgressChanged(bool inProgress);
537
538private:
539 // Private Methods
540 void reset();
541 QMultiMap<Version, FilePath> getAll(const QString& elementsTable,
542 const std::optional<Uuid>& uuid,
543 const FilePath& lib) const;
544 QHash<FilePath, Uuid> getAll(const QString& elementsTable,
545 const FilePath& lib) const;
547 const QMultiMap<Version, FilePath>& list) const noexcept;
548 QList<Uuid> find(const QString& elementsTable, const QString& keyword) const;
549 bool getTranslations(const QString& elementsTable, const FilePath& elemDir,
550 const QStringList& localeOrder, QString* name,
551 QString* description, QString* keywords) const;
552 bool getMetadata(const QString& elementsTable, const FilePath elemDir,
553 Uuid* uuid, Version* version, bool* deprecated) const;
554 bool getCategoryMetadata(const QString& categoriesTable,
555 const FilePath catDir,
556 std::optional<Uuid>* parent) const;
557 AttributeList getPartAttributes(int partId) const;
558 QSet<Uuid> getChilds(const QString& categoriesTable,
559 const std::optional<Uuid>& categoryUuid) const;
560 QSet<Uuid> getByCategory(const QString& elementsTable,
561 const QString& categoryTable,
562 const std::optional<Uuid>& category,
563 int limit) const;
564 QSet<Uuid> getCategoriesOf(const QString& elementsTable,
565 const QString& categoryTable,
566 const FilePath& elemDir) const;
567 QSet<Uuid> getGenerated(const QString& elementsTable,
568 const QString& generatedBy) const;
569 ResourceList getResources(const QString& elementsTable,
570 const FilePath& elemDir) const;
571 static QSet<Uuid> getUuidSet(QSqlQuery& query);
572 int getDbVersion() const noexcept;
573 template <typename ElementType>
574 static QString getTable() noexcept;
575 template <typename ElementType>
576 static QString getCategoryTable() noexcept;
577
578 // Attributes
581 QScopedPointer<SQLiteDatabase> mDb;
583
584 // Constants
585 static const int sCurrentDbVersion = 8;
586};
587
588/*******************************************************************************
589 * End of File
590 ******************************************************************************/
591
592} // namespace librepcb
593
594#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:59
ResourceList getResources(const FilePath elemDir) const
Get resources of a specific library element.
Definition workspacelibrarydb.h:468
WorkspaceLibraryDb(const WorkspaceLibraryDb &other)=delete
QSet< Uuid > getGenerated(const QString &generatedBy) const
Get elements generated by a given "generated_by" identifier.
Definition workspacelibrarydb.h:449
bool getLibraryMetadata(const FilePath libDir, QPixmap *icon=nullptr, QString *manufacturer=nullptr) const
Get additional metadata of a specific library.
Definition workspacelibrarydb.cpp:197
bool cancelLibraryRescan() noexcept
Abort the library rescan (if running)
Definition workspacelibrarydb.cpp:409
void scanProgressUpdate(int percent)
static const int sCurrentDbVersion
Definition workspacelibrarydb.h:585
~WorkspaceLibraryDb() noexcept
Definition workspacelibrarydb.cpp:102
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:193
FilePath getLatestVersionFilePath(const QMultiMap< Version, FilePath > &list) const noexcept
Definition workspacelibrarydb.cpp:501
bool getDeviceMetadata(const FilePath &devDir, Uuid *cmpUuid=nullptr, Uuid *pkgUuid=nullptr) const
Get additional metadata of a specific device.
Definition workspacelibrarydb.cpp:221
void resetAndRescan() noexcept
Re-initialize database and start new rescan.
Definition workspacelibrarydb.cpp:396
void scanFailed(QString errorMsg)
QList< Uuid > find(const QString &keyword) const
Find elements by keyword.
Definition workspacelibrarydb.cpp:114
QScopedPointer< SQLiteDatabase > mDb
The SQLite database.
Definition workspacelibrarydb.h:581
void scanInProgressChanged(bool inProgress)
void scanSucceeded(int elementCount)
const FilePath & getFilePath() const noexcept
Get the file path of the SQLite database.
Definition workspacelibrarydb.h:163
AttributeList getPartAttributes(int partId) const
Definition workspacelibrarydb.cpp:628
void scanLibraryListUpdated(int libraryCount)
FilePath getLatest(const Uuid &uuid) const
Get an element of a specific UUID and the highest version.
Definition workspacelibrarydb.h:223
QSet< Uuid > getChilds(const std::optional< Uuid > &parent) const
Get children categories of a specific category.
Definition workspacelibrarydb.h:381
bool getMetadata(const FilePath elemDir, Uuid *uuid=nullptr, Version *version=nullptr, bool *deprecated=nullptr) const
Get metadata of a specific element.
Definition workspacelibrarydb.h:308
QList< Part > getDeviceParts(const Uuid &device) const
Get all parts of a specific device.
Definition workspacelibrarydb.cpp:259
QSet< Uuid > getByCategory(const std::optional< Uuid > &category, int limit=-1) const
Get elements of a specific category.
Definition workspacelibrarydb.h:406
void startLibraryRescan() noexcept
Rescan the whole library directory and update the SQLite database.
Definition workspacelibrarydb.cpp:405
OutputJobKind
Definition workspacelibrarydb.h:112
bool getCategoryMetadata(const FilePath catDir, std::optional< Uuid > *parent=nullptr) const
Get additional metadata of a specific category.
Definition workspacelibrarydb.h:344
QSet< Uuid > getCategoriesOf(const FilePath &elemDir) const
Get all categories assigned to a particular library element.
Definition workspacelibrarydb.h:428
bool isScanInProgress() const noexcept
Check if there is currently a library scan in progress.
Definition workspacelibrarydb.h:170
static QString getCategoryTable() noexcept
Definition workspacelibrarydb.cpp:815
QScopedPointer< WorkspaceLibraryScanner > mLibraryScanner
Definition workspacelibrarydb.h:582
QList< Organization > getAllLatestOrganizations(const QStringList &localeOrder, bool pcbDesignRules, bool outputJobs) const
Get all organizations (only the latest version of each)
Definition workspacelibrarydb.cpp:279
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:284
int getScanProgressPercent() const noexcept
Get the current progress of the library rescan.
Definition workspacelibrarydb.cpp:109
static QSet< Uuid > getUuidSet(QSqlQuery &query)
Definition workspacelibrarydb.cpp:772
void reset()
Definition workspacelibrarydb.cpp:417
int getDbVersion() const noexcept
Definition workspacelibrarydb.cpp:780
static QString getTable() noexcept
Definition workspacelibrarydb.cpp:800
QSet< Uuid > getComponentDevices(const Uuid &component) const
Get all devices of a specific component.
Definition workspacelibrarydb.cpp:248
const FilePath mLibrariesPath
Path to workspace libraries directory.
Definition workspacelibrarydb.h:579
const FilePath mFilePath
Path to the SQLite database file.
Definition workspacelibrarydb.h:580
QList< Uuid > findDevicesOfParts(const QString &keyword) const
Find parts by keyword.
Definition workspacelibrarydb.cpp:153
QHash< FilePath, Uuid > getAll(const FilePath &lib) const
Get all elements of a particular library.
Definition workspacelibrarydb.h:209
QList< Part > findPartsOfDevice(const Uuid &device, const QString &keyword) const
Find parts of device by keyword.
Definition workspacelibrarydb.cpp:175
The WorkspaceLibraryScanner class.
Definition workspacelibraryscanner.h:55
Definition occmodel.cpp:77
Definition uuid.h:186
Definition workspacelibrarydb.h:123
QStringList fabs
Definition workspacelibrarydb.h:133
QUrl url
Definition workspacelibrarydb.h:131
QList< OutputJob > outputJobs
Definition workspacelibrarydb.h:138
FilePath elemDir
Definition workspacelibrarydb.h:125
Uuid uuid
Definition workspacelibrarydb.h:126
QStringList shipping
Definition workspacelibrarydb.h:134
bool isSponsor
Definition workspacelibrarydb.h:135
int id
Definition workspacelibrarydb.h:124
QString description
Definition workspacelibrarydb.h:128
QList< PcbDesignRules > pcbDesignRules
Definition workspacelibrarydb.h:137
QString country
Definition workspacelibrarydb.h:132
QString name
Definition workspacelibrarydb.h:127
Version version
Definition workspacelibrarydb.h:129
int priority
Definition workspacelibrarydb.h:136
QPixmap logo
Definition workspacelibrarydb.h:130
Definition workspacelibrarydb.h:117
Uuid uuid
Definition workspacelibrarydb.h:119
QString type
Definition workspacelibrarydb.h:120
QString name
Definition workspacelibrarydb.h:121
OutputJobKind kind
Definition workspacelibrarydb.h:118
Definition workspacelibrarydb.h:63
QString mpn
Definition workspacelibrarydb.h:64
bool operator==(const Part &rhs) const noexcept
Definition workspacelibrarydb.h:68
QString manufacturer
Definition workspacelibrarydb.h:65
bool operator<(const Part &rhs) const noexcept
Definition workspacelibrarydb.h:72
AttributeList attributes
Definition workspacelibrarydb.h:66
Definition workspacelibrarydb.h:105
QUrl url
Definition workspacelibrarydb.h:109
Uuid uuid
Definition workspacelibrarydb.h:106
int maxLayerCount
Definition workspacelibrarydb.h:110
QString description
Definition workspacelibrarydb.h:108
QString name
Definition workspacelibrarydb.h:107