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