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 <chrono>
26#include <QFutureWatcher>
27#include <QAbstractTableModel>
28#include <QTimer>
29#include "ores.qt/ClientManager.hpp"
30#include "ores.utility/log/make_logger.hpp"
31#include "ores.risk/domain/currency.hpp"
32
33namespace ores::qt {
34
41class ClientCurrencyModel final : public QAbstractTableModel {
42 Q_OBJECT
43
44private:
45 inline static std::string_view logger_name =
46 "ores.qt.client_currency_model";
47
48 [[nodiscard]] static auto& lg() {
49 using namespace ores::utility::log;
50 static auto instance = make_logger(logger_name);
51 return instance;
52 }
53
54public:
61 enum Column {
62 CurrencyName,
63 IsoCode,
64 Version,
65 NumericCode,
66 Symbol,
67 FractionSymbol,
68 FractionsPerUnit,
69 RoundingType,
70 RoundingPrecision,
71 Format,
72 CurrencyType,
73 RecordedBy,
74 RecordedAt,
75 ColumnCount // Must be last - represents total number of columns
76 };
77
78 explicit ClientCurrencyModel(ClientManager* clientManager,
79 QObject* parent = nullptr);
80 ~ClientCurrencyModel() override = default;
81
82 // QAbstractTableModel interface
83 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
84 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
85 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
86 QVariant headerData(int section, Qt::Orientation orientation,
87 int role = Qt::DisplayRole) const override;
88
99 void refresh(bool replace = true);
100
106 bool canFetchMore(const QModelIndex& parent = QModelIndex()) const override;
107
114 void fetchMore(const QModelIndex& parent = QModelIndex()) override;
115
122 const risk::domain::currency* getCurrency(int row) const;
123
129 std::vector<risk::domain::currency> getCurrencies() const;
130
136 std::uint32_t page_size() const { return page_size_; }
137
143 void set_page_size(std::uint32_t size);
144
150 std::uint32_t total_available_count() const { return total_available_count_; }
151
152signals:
157
161 void loadError(const QString& error_message);
162
163private slots:
164 void onCurrenciesLoaded();
165 void onPulseTimerTimeout();
166
167private:
177 QVariant recency_foreground_color(const std::string& iso_code) const;
178
182 void update_recent_currencies();
183
184 struct FetchResult {
185 bool success;
186 std::vector<risk::domain::currency> currencies;
187 std::uint32_t total_available_count;
188 };
189
190 using FutureWatcherResult = FetchResult;
191
192 ClientManager* clientManager_;
193 std::vector<risk::domain::currency> currencies_;
194 QFutureWatcher<FutureWatcherResult>* watcher_;
195 std::uint32_t page_size_{100};
196 std::uint32_t total_available_count_{0};
197 bool is_fetching_{false};
198
199 // Recency tracking for row coloring based on valid_from date
200 std::unordered_set<std::string> recent_iso_codes_; // ISO codes newer than last reload
201 QDateTime last_reload_time_; // Timestamp of last reload (for comparison)
202 QTimer* pulse_timer_;
203 bool pulse_state_{false}; // Toggle for pulsing effect
204 int pulse_count_{0};
205 static constexpr int max_pulse_cycles_{6}; // 6 pulses (3 seconds at 500ms interval)
206 static constexpr int pulse_interval_ms_{500}; // Pulse toggle interval in milliseconds
207};
208
209}
210
211#endif
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:35
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Model for displaying currencies fetched from the server via client.
Definition ClientCurrencyModel.hpp:41
bool canFetchMore(const QModelIndex &parent=QModelIndex()) const override
Check if more data can be fetched from the server.
Definition ClientCurrencyModel.cpp:300
void fetchMore(const QModelIndex &parent=QModelIndex()) override
Fetch the next page of data from the server.
Definition ClientCurrencyModel.cpp:321
std::vector< risk::domain::currency > getCurrencies() const
Get all currencies.
Definition ClientCurrencyModel.cpp:296
void set_page_size(std::uint32_t size)
Set the page size for pagination.
Definition ClientCurrencyModel.cpp:329
std::uint32_t total_available_count() const
Get the total number of records available on the server.
Definition ClientCurrencyModel.hpp:150
const risk::domain::currency * getCurrency(int row) const
Get currency at the specified row.
Definition ClientCurrencyModel.cpp:289
void dataLoaded()
Emitted when data has been successfully loaded.
void refresh(bool replace=true)
Refresh currency data from server asynchronously.
Definition ClientCurrencyModel.cpp:127
std::uint32_t page_size() const
Get the page size used for pagination.
Definition ClientCurrencyModel.hpp:136
void loadError(const QString &error_message)
Emitted when an error occurs during data loading.
Column
Enumeration of table columns for type-safe column access.
Definition ClientCurrencyModel.hpp:61
Manages the lifecycle of the network client and IO context.
Definition ClientManager.hpp:56
Represents a currency with its metadata and formatting rules.
Definition currency.hpp:30