LibrePCB Developers Documentation
partinformationprovider.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_EDITOR_PARTINFORMATIONPROVIDER_H
21#define LIBREPCB_EDITOR_PARTINFORMATIONPROVIDER_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
27#include <optional/tl/optional.hpp>
28
29#include <QtCore>
30#include <QtGui>
31
32/*******************************************************************************
33 * Namespace / Forward Declarations
34 ******************************************************************************/
35namespace librepcb {
36
37class ApiEndpoint;
38class SExpression;
39
40namespace editor {
41
42/*******************************************************************************
43 * Class PartInformationProvider
44 ******************************************************************************/
45
52class PartInformationProvider final : public QObject {
53 Q_OBJECT
54
55public:
56 // Types
57 struct Part {
58 QString mpn;
59 QString manufacturer;
60 inline bool operator==(const Part& rhs) const noexcept {
61 return (mpn == rhs.mpn) && (manufacturer == rhs.manufacturer);
62 }
63 inline bool operator<(const Part& rhs) const noexcept {
64 if (mpn != rhs.mpn) {
65 return mpn < rhs.mpn;
66 } else {
67 return manufacturer < rhs.manufacturer;
68 }
69 }
70 };
71 struct PartResource {
72 QString name;
73 QString mediaType;
74 QUrl url;
75 };
77 qint64 timestamp; // Seconds since epoch
78 QString mpn;
79 QString manufacturer;
81 QUrl productUrl; // Empty if N/A
82 QUrl pictureUrl; // Empty if N/A
83 QUrl pricingUrl; // Empty if N/A
84 QString status; // Empty if N/A
85 tl::optional<int> availability; // nullopt if N/A
86 QMap<int, qreal> prices; // Empty if N/A
87 QVector<PartResource> resources; // Empty if N/A
88
89 QString getStatusTr() const noexcept;
90 QString getStatusColorName() const noexcept;
91 QString getAvailabilityTr() const noexcept;
92 QString getAvailabilityColorName() const noexcept;
93 qreal getPrice(int quantity = 1) const noexcept;
94 QString getPriceStr(int quantity = 1, const char* prefix = "$ ",
95 const char* suffix = "") const noexcept;
96 static QString formatQuantity(const QLocale& locale, int qty) noexcept;
97
103 void serialize(SExpression& root) const;
104
105 void load(const SExpression& node);
106 };
107
108 // Constructors / Destructor
110 delete;
111 explicit PartInformationProvider(QObject* parent = nullptr) noexcept;
112 ~PartInformationProvider() noexcept;
113
114 // Getters
115 bool isOperational() const noexcept;
116 const QString& getProviderName() const noexcept { return mProviderName; }
117 const QUrl& getProviderUrl() const noexcept { return mProviderUrl; }
118 const QUrl& getProviderLogoUrl() const noexcept { return mProviderLogoUrl; }
119 const QPixmap getProviderLogo() const noexcept { return mProviderLogo; }
120 const QUrl& getInfoUrl() const noexcept { return mInfoUrl; }
121
122 // Setters
123 void setCacheDir(const FilePath& dir) noexcept;
124 void setApiEndpoint(const QUrl& url) noexcept;
125
126 // General Methods
127 bool startOperation(int timeoutMs = 0) noexcept;
128 std::shared_ptr<PartInformation> getPartInfo(const Part& part) noexcept;
129 std::shared_ptr<PartInformation> waitForPartInfo(const Part& part,
130 int timeoutMs) noexcept;
131 bool isOngoing(const Part& part) const noexcept;
132 void scheduleRequest(const Part& part) noexcept;
133 void requestScheduledParts() noexcept;
134
135 // Static Methods
137 static PartInformationProvider obj;
138 return obj;
139 }
140
141 // Operator Overloadings
143 const PartInformationProvider& rhs) noexcept = delete;
144
145signals:
149
150private: // Methods
151 void reset() noexcept;
152 void requestStatus() noexcept;
153 void statusReceived(const QJsonObject& json) noexcept;
154 void errorWhileFetchingStatus(const QString& errorMsg) noexcept;
155 void partsInformationReceived(const QJsonObject& json) noexcept;
156 void errorWhileFetchingPartsInformation(const QString& errorMsg) noexcept;
157 void removeOutdatedInformation() noexcept;
158 void loadCacheFromDisk() noexcept;
159 void saveCacheToDisk() noexcept;
160
161private: // Data
162 // Configuration
164 QScopedPointer<ApiEndpoint> mEndpoint;
165
166 // Error handling
169
170 // Status request state
177 QUrl mInfoUrl;
180
181 // Query request state
184
185 // Cache
186 QMap<Part, std::shared_ptr<PartInformation>> mCache; // Sorted for file I/O!
188};
189
190/*******************************************************************************
191 * Non-Member Functions
192 ******************************************************************************/
193
194inline uint qHash(const PartInformationProvider::Part& key,
195 uint seed = 0) noexcept {
196 return ::qHash(qMakePair(key.mpn, key.manufacturer), seed);
197}
198
199/*******************************************************************************
200 * End of File
201 ******************************************************************************/
202
203} // namespace editor
204} // namespace librepcb
205
207
208#endif
Access to a LibrePCB API endpoint.
Definition: apiendpoint.h:42
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129
The Part class.
Definition: part.h:46
The SExpression class.
Definition: sexpression.h:69
Parts information provider & cache.
Definition: partinformationprovider.h:52
bool isOngoing(const Part &part) const noexcept
Definition: partinformationprovider.cpp:344
QString mProviderName
Valid only if mStatusReceived is true
Definition: partinformationprovider.h:173
void reset() noexcept
Definition: partinformationprovider.cpp:378
const QString & getProviderName() const noexcept
Definition: partinformationprovider.h:116
qint64 mStatusRequestedTimestamp
Definition: partinformationprovider.h:171
const QUrl & getInfoUrl() const noexcept
Definition: partinformationprovider.h:120
bool mStatusReceived
Definition: partinformationprovider.h:172
static PartInformationProvider & instance() noexcept
Definition: partinformationprovider.h:136
bool startOperation(int timeoutMs=0) noexcept
Definition: partinformationprovider.cpp:316
std::shared_ptr< PartInformation > waitForPartInfo(const Part &part, int timeoutMs) noexcept
Definition: partinformationprovider.cpp:333
QMap< Part, std::shared_ptr< PartInformation > > mCache
Definition: partinformationprovider.h:186
void requestScheduledParts() noexcept
Definition: partinformationprovider.cpp:354
int mErrorCounter
Definition: partinformationprovider.h:167
void errorWhileFetchingPartsInformation(const QString &errorMsg) noexcept
Definition: partinformationprovider.cpp:499
const QUrl & getProviderUrl() const noexcept
Definition: partinformationprovider.h:117
QUrl mQueryUrl
Valid only if mStatusReceived is true
Definition: partinformationprovider.h:178
bool mCacheModified
Definition: partinformationprovider.h:187
void errorWhileFetchingStatus(const QString &errorMsg) noexcept
Definition: partinformationprovider.cpp:441
void loadCacheFromDisk() noexcept
Definition: partinformationprovider.cpp:525
void scheduleRequest(const Part &part) noexcept
Definition: partinformationprovider.cpp:348
const QPixmap getProviderLogo() const noexcept
Definition: partinformationprovider.h:119
void setCacheDir(const FilePath &dir) noexcept
Definition: partinformationprovider.cpp:286
QUrl mInfoUrl
Valid only if mStatusReceived is true
Definition: partinformationprovider.h:177
QPixmap mProviderLogo
Requested asynchronously.
Definition: partinformationprovider.h:176
QUrl mProviderLogoUrl
Valid only if mStatusReceived is true
Definition: partinformationprovider.h:175
void partsInformationReceived(const QJsonObject &json) noexcept
Definition: partinformationprovider.cpp:453
void requestStatus() noexcept
Definition: partinformationprovider.cpp:395
void setApiEndpoint(const QUrl &url) noexcept
Definition: partinformationprovider.cpp:291
void saveCacheToDisk() noexcept
Definition: partinformationprovider.cpp:551
const QUrl & getProviderLogoUrl() const noexcept
Definition: partinformationprovider.h:118
QUrl mProviderUrl
Valid only if mStatusReceived is true
Definition: partinformationprovider.h:174
void statusReceived(const QJsonObject &json) noexcept
Definition: partinformationprovider.cpp:404
bool mDisabledDueToErrors
Definition: partinformationprovider.h:168
QScopedPointer< ApiEndpoint > mEndpoint
Definition: partinformationprovider.h:164
bool isOperational() const noexcept
Definition: partinformationprovider.cpp:277
PartInformationProvider & operator=(const PartInformationProvider &rhs) noexcept=delete
FilePath mCacheFp
Definition: partinformationprovider.h:163
std::shared_ptr< PartInformation > getPartInfo(const Part &part) noexcept
Definition: partinformationprovider.cpp:328
QSet< Part > mRequestedParts
Definition: partinformationprovider.h:183
int mQueryMaxPartCount
Valid only if mStatusReceived is true
Definition: partinformationprovider.h:179
QVector< Part > mScheduledParts
Definition: partinformationprovider.h:182
void removeOutdatedInformation() noexcept
Definition: partinformationprovider.cpp:511
uint qHash(const PartInformationProvider::Part &key, uint seed=0) noexcept
Definition: partinformationprovider.h:194
QtCompat::Hash qHash(const EditorWidgetBase::Feature &feature, QtCompat::Hash seed=0) noexcept
Definition: editorwidgetbase.h:274
Definition: occmodel.cpp:77
Definition: partinformationprovider.h:57
QString mpn
Definition: partinformationprovider.h:58
bool operator==(const Part &rhs) const noexcept
Definition: partinformationprovider.h:60
QString manufacturer
Definition: partinformationprovider.h:59
bool operator<(const Part &rhs) const noexcept
Definition: partinformationprovider.h:63
Definition: partinformationprovider.h:76
QString mpn
Definition: partinformationprovider.h:78
QVector< PartResource > resources
Definition: partinformationprovider.h:87
QString getPriceStr(int quantity=1, const char *prefix="$ ", const char *suffix="") const noexcept
Definition: partinformationprovider.cpp:125
QString getAvailabilityColorName() const noexcept
Definition: partinformationprovider.cpp:95
qint64 timestamp
Definition: partinformationprovider.h:77
QUrl pricingUrl
Definition: partinformationprovider.h:83
QUrl pictureUrl
Definition: partinformationprovider.h:82
int results
Definition: partinformationprovider.h:80
void load(const SExpression &node)
Definition: partinformationprovider.cpp:196
QString manufacturer
Definition: partinformationprovider.h:79
static QString formatQuantity(const QLocale &locale, int qty) noexcept
Definition: partinformationprovider.cpp:136
void serialize(SExpression &root) const
Serialize into librepcb::SExpression node.
Definition: partinformationprovider.cpp:151
qreal getPrice(int quantity=1) const noexcept
Definition: partinformationprovider.cpp:114
QString getStatusTr() const noexcept
Definition: partinformationprovider.cpp:44
QString status
Definition: partinformationprovider.h:84
QString getAvailabilityTr() const noexcept
Definition: partinformationprovider.cpp:71
QMap< int, qreal > prices
Definition: partinformationprovider.h:86
QUrl productUrl
Definition: partinformationprovider.h:81
QString getStatusColorName() const noexcept
Definition: partinformationprovider.cpp:60
tl::optional< int > availability
Definition: partinformationprovider.h:85
Definition: partinformationprovider.h:71
QUrl url
Definition: partinformationprovider.h:74
QString name
Definition: partinformationprovider.h:72
QString mediaType
Definition: partinformationprovider.h:73