20#ifndef ORES_QT_IMAGE_CACHE_HPP
21#define ORES_QT_IMAGE_CACHE_HPP
24#include <unordered_map>
25#include <unordered_set>
28#include <QFutureWatcher>
29#include "ores.qt/ClientManager.hpp"
30#include "ores.logging/make_logger.hpp"
31#include "ores.assets.api/domain/image.hpp"
32#include "ores.assets.api/messaging/assets_protocol.hpp"
57 inline static std::string_view logger_name =
"ores.qt.image_cache";
59 [[nodiscard]]
static auto& lg() {
61 static auto instance = make_logger(logger_name);
96 QIcon
getIcon(
const std::string& image_id);
104 bool hasIcon(
const std::string& image_id)
const;
140 return available_images_;
163 void setCurrencyImage(
const std::string& iso_code,
const std::string& image_id,
164 const std::string& assigned_by);
173 void setCountryImage(
const std::string& alpha2_code,
const std::string& image_id,
174 const std::string& assigned_by);
255 void countryImageSet(
const QString& alpha2_code,
bool success,
const QString& message);
258 void onCurrencyImageIdsLoaded();
259 void onCountryImageIdsLoaded();
260 void onBusinessCentreMappingLoaded();
261 void onImagesLoaded();
262 void onImageListLoaded();
263 void onSingleImageLoaded();
264 void onCurrencyImageSet();
265 void onCountryImageSet();
266 void onAllAvailableImagesLoaded();
267 void onIncrementalChangesLoaded();
276 static QIcon svgToIcon(
const std::string& svg_data);
283 void loadImageById(
const std::string& image_id);
288 void loadCurrencyImageIds();
293 void loadCountryImageIds();
298 void loadImagesByIds(
const std::vector<std::string>& image_ids);
303 void loadBusinessCentreMapping();
310 void loadIncrementalChanges();
312 struct ImageIdsResult {
314 std::vector<std::string> image_ids;
316 std::unordered_map<std::string, std::string> code_to_image_id;
319 struct BusinessCentreMappingResult {
321 std::unordered_map<std::string, std::string> bc_to_country;
324 struct ImagesResult {
326 std::vector<assets::domain::image> images;
327 int failed_batches{0};
337 static ImagesResult fetchImagesInBatches(
338 ClientManager* clientManager,
339 const std::vector<std::string>& image_ids);
341 struct ImageListResult {
343 std::vector<assets::messaging::image_info> images;
346 struct SingleImageResult {
348 std::string image_id;
352 struct SetCurrencyImageResult {
354 std::string iso_code;
358 struct SetCountryImageResult {
360 std::string alpha2_code;
364 ClientManager* clientManager_;
367 std::unordered_map<std::string, std::string> image_svg_cache_;
370 std::unordered_map<std::string, QIcon> image_icons_;
373 bool is_loading_images_{
false};
374 bool is_loading_all_available_{
false};
375 bool load_all_in_progress_{
false};
378 std::vector<std::string> pending_image_ids_;
380 QFutureWatcher<ImageIdsResult>* currency_ids_watcher_;
381 QFutureWatcher<ImageIdsResult>* country_ids_watcher_;
382 QFutureWatcher<ImageIdsResult>* incremental_changes_watcher_;
383 QFutureWatcher<ImagesResult>* images_watcher_;
384 QFutureWatcher<ImageListResult>* image_list_watcher_;
385 QFutureWatcher<SingleImageResult>* single_image_watcher_;
386 QFutureWatcher<SetCurrencyImageResult>* set_currency_image_watcher_;
387 QFutureWatcher<SetCountryImageResult>* set_country_image_watcher_;
388 QFutureWatcher<ImagesResult>* all_available_watcher_;
391 std::vector<assets::messaging::image_info> available_images_;
394 std::unordered_set<std::string> pending_image_requests_;
397 std::optional<std::chrono::system_clock::time_point> last_load_time_;
400 std::unordered_map<std::string, std::string> currency_iso_to_image_id_;
401 std::unordered_map<std::string, std::string> country_alpha2_to_image_id_;
402 std::unordered_map<std::string, std::string> bc_code_to_country_alpha2_;
404 QFutureWatcher<BusinessCentreMappingResult>* bc_mapping_watcher_;
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:29
Represents a dynamically loaded image (typically SVG).
Definition image.hpp:34
Manages the lifecycle of the NATS client and login state.
Definition ClientManager.hpp:109
Cache for dynamically loaded images (flags, icons) from the server.
Definition ImageCache.hpp:53
QIcon getNoFlagIcon() const
Get the icon for the "no-flag" placeholder.
Definition ImageCache.cpp:847
void loadImageList()
Load list of all available images from the server.
Definition ImageCache.cpp:713
void imageLoaded(const QString &image_id)
Emitted when a single image has been loaded.
std::size_t cachedIconCount() const
Get the number of cached images.
Definition ImageCache.hpp:109
void countryImageSet(const QString &alpha2_code, bool success, const QString &message)
Emitted when country image assignment is complete.
void allLoaded()
Emitted when all data has been loaded (after loadAll()).
QIcon getCurrencyFlagIcon(const std::string &iso_code)
Get flag icon for a currency by its ISO code.
Definition ImageCache.cpp:367
bool hasIcon(const std::string &image_id) const
Check if an image is cached.
Definition ImageCache.cpp:582
void setCountryImage(const std::string &alpha2_code, const std::string &image_id, const std::string &assigned_by)
Set or remove a country's image association.
Definition ImageCache.cpp:959
const std::vector< assets::messaging::image_info > & availableImages() const
Get the list of available images.
Definition ImageCache.hpp:139
bool isLoading() const
Check if images are currently being loaded.
Definition ImageCache.hpp:114
bool hasImageList() const
Check if image list has been loaded.
Definition ImageCache.hpp:146
QIcon getCountryFlagIcon(const std::string &alpha2_code)
Get flag icon for a country by its alpha-2 code.
Definition ImageCache.cpp:375
std::string getNoFlagImageId() const
Get the image ID for the "no-flag" placeholder.
Definition ImageCache.cpp:837
QIcon getBusinessCentreFlagIcon(const std::string &bc_code)
Get flag icon for a business centre by its code.
Definition ImageCache.cpp:383
void loadAllAvailableImages()
Load all available images from the image list.
Definition ImageCache.cpp:761
void setCurrencyImage(const std::string &iso_code, const std::string &image_id, const std::string &assigned_by)
Set or remove a currency's image association.
Definition ImageCache.cpp:871
void imageListLoaded()
Emitted when image list has been loaded.
void loadAll()
Preload images for current currencies and countries.
Definition ImageCache.cpp:79
void clear()
Clear all caches and reset load state.
Definition ImageCache.cpp:133
void loadError(const QString &error_message)
Emitted when an error occurs during loading.
void imagesLoaded()
Emitted when images have been loaded.
void currencyImageSet(const QString &iso_code, bool success, const QString &message)
Emitted when currency image assignment is complete.
QIcon getIcon(const std::string &image_id)
Get icon for an image by its UUID.
Definition ImageCache.cpp:562
void allAvailableImagesLoaded()
Emitted when all available images have been loaded.
void reload()
Clear all caches and reload images.
Definition ImageCache.cpp:94