ORE Studio 0.0.4
Loading...
Searching...
No Matches
ClientCurrencyModel.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2024 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_QT_CLIENT_CURRENCY_MODEL_HPP
21#define ORES_QT_CLIENT_CURRENCY_MODEL_HPP
22
23#include <vector>
24#include <unordered_set>
25#include <QFutureWatcher>
26#include <QAbstractTableModel>
27#include "ores.qt/ClientManager.hpp"
28#include "ores.qt/RecencyPulseManager.hpp"
29#include "ores.qt/RecencyTracker.hpp"
30#include "ores.logging/make_logger.hpp"
31#include "ores.refdata/domain/currency.hpp"
32
33namespace ores::qt {
34
35class ImageCache;
36
43class ClientCurrencyModel final : public QAbstractTableModel {
44 Q_OBJECT
45
46private:
47 inline static std::string_view logger_name =
48 "ores.qt.client_currency_model";
49
50 [[nodiscard]] static auto& lg() {
51 using namespace ores::logging;
52 static auto instance = make_logger(logger_name);
53 return instance;
54 }
55
56public:
63 enum Column {
64 Flag, // Currency flag icon
65 CurrencyName,
66 IsoCode,
67 Version,
68 NumericCode,
69 Symbol,
70 FractionSymbol,
71 FractionsPerUnit,
72 RoundingType,
73 RoundingPrecision,
74 Format,
75 CurrencyType,
76 RecordedBy,
77 RecordedAt,
78 ColumnCount // Must be last - represents total number of columns
79 };
80
81 explicit ClientCurrencyModel(ClientManager* clientManager,
82 ImageCache* imageCache,
83 QObject* parent = nullptr);
84 ~ClientCurrencyModel() override = default;
85
86 // QAbstractTableModel interface
87 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
88 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
89 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
90 QVariant headerData(int section, Qt::Orientation orientation,
91 int role = Qt::DisplayRole) const override;
92
103 void refresh(bool replace = true);
104
114 void load_page(std::uint32_t offset, std::uint32_t limit);
115
121 bool canFetchMore(const QModelIndex& parent = QModelIndex()) const override;
122
129 void fetchMore(const QModelIndex& parent = QModelIndex()) override;
130
137 const refdata::domain::currency* getCurrency(int row) const;
138
144 std::vector<refdata::domain::currency> getCurrencies() const;
145
151 std::uint32_t page_size() const { return page_size_; }
152
158 void set_page_size(std::uint32_t size);
159
165 std::uint32_t total_available_count() const { return total_available_count_; }
166
175 void add_synthetic_currencies(std::vector<refdata::domain::currency> currencies);
176
183 bool is_synthetic(const std::string& iso_code) const;
184
192 void mark_as_saved(const std::string& iso_code);
193
200
201signals:
206
210 void loadError(const QString& error_message, const QString& details = {});
211
212private slots:
213 void onCurrenciesLoaded();
214 void onPulseStateChanged(bool isOn);
215 void onPulsingComplete();
216
217private:
229 QVariant foreground_color(const std::string& iso_code) const;
230
231 struct FetchResult {
232 bool success;
233 std::vector<refdata::domain::currency> currencies;
234 std::uint32_t total_available_count;
235 QString error_message;
236 QString error_details;
237 };
238
239 using FutureWatcherResult = FetchResult;
240
241 ClientManager* clientManager_;
242 ImageCache* imageCache_;
243 std::vector<refdata::domain::currency> currencies_;
244 QFutureWatcher<FutureWatcherResult>* watcher_;
245 std::uint32_t page_size_{100};
246 std::uint32_t total_available_count_{0};
247 bool is_fetching_{false};
248
249 // Recency highlighting
250 using CurrencyKeyExtractor = std::string(*)(const refdata::domain::currency&);
251 RecencyTracker<refdata::domain::currency, CurrencyKeyExtractor> recencyTracker_;
252 RecencyPulseManager* pulseManager_;
253
257 void fetch_currencies(std::uint32_t offset, std::uint32_t limit);
258
259 // Synthetic currency tracking (generated but not yet saved)
260 std::unordered_set<std::string> synthetic_iso_codes_;
261};
262
263}
264
265#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:35
Model for displaying currencies fetched from the server via client.
Definition ClientCurrencyModel.hpp:43
bool canFetchMore(const QModelIndex &parent=QModelIndex()) const override
Check if more data can be fetched from the server.
Definition ClientCurrencyModel.cpp:348
void fetchMore(const QModelIndex &parent=QModelIndex()) override
Fetch the next page of data from the server.
Definition ClientCurrencyModel.cpp:364
void set_page_size(std::uint32_t size)
Set the page size for pagination.
Definition ClientCurrencyModel.cpp:372
void clear_synthetic_markers()
Clear all synthetic currency markers.
Definition ClientCurrencyModel.cpp:446
void add_synthetic_currencies(std::vector< refdata::domain::currency > currencies)
Add synthetic (generated) currencies to the model.
Definition ClientCurrencyModel.cpp:399
void mark_as_saved(const std::string &iso_code)
Mark a synthetic currency as saved (no longer synthetic).
Definition ClientCurrencyModel.cpp:426
std::uint32_t total_available_count() const
Get the total number of records available on the server.
Definition ClientCurrencyModel.hpp:165
const refdata::domain::currency * getCurrency(int row) const
Get currency at the specified row.
Definition ClientCurrencyModel.cpp:337
void dataLoaded()
Emitted when data has been successfully loaded.
std::vector< refdata::domain::currency > getCurrencies() const
Get all currencies.
Definition ClientCurrencyModel.cpp:344
void refresh(bool replace=true)
Refresh currency data from server asynchronously.
Definition ClientCurrencyModel.cpp:169
void load_page(std::uint32_t offset, std::uint32_t limit)
Load a specific page of currency data.
Definition ClientCurrencyModel.cpp:202
void loadError(const QString &error_message, const QString &details={})
Emitted when an error occurs during data loading.
std::uint32_t page_size() const
Get the page size used for pagination.
Definition ClientCurrencyModel.hpp:151
Column
Enumeration of table columns for type-safe column access.
Definition ClientCurrencyModel.hpp:63
bool is_synthetic(const std::string &iso_code) const
Check if a currency is synthetic (generated but not saved).
Definition ClientCurrencyModel.cpp:422
Manages the lifecycle of the network client and IO context.
Definition ClientManager.hpp:90
Cache for dynamically loaded images (flags, icons) from the server.
Definition ImageCache.hpp:52
Represents a currency with its metadata and formatting rules.
Definition currency.hpp:33