ORE Studio 0.0.4
Loading...
Searching...
No Matches
ClientCountryModel.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 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_COUNTRY_MODEL_HPP
21#define ORES_QT_CLIENT_COUNTRY_MODEL_HPP
22
23#include <vector>
24#include <QFutureWatcher>
25#include <QAbstractTableModel>
26#include "ores.qt/ClientManager.hpp"
27#include "ores.qt/RecencyPulseManager.hpp"
28#include "ores.qt/RecencyTracker.hpp"
29#include "ores.logging/make_logger.hpp"
30#include "ores.refdata/domain/country.hpp"
31
32namespace ores::qt {
33
34class ImageCache;
35
42class ClientCountryModel final : public QAbstractTableModel {
43 Q_OBJECT
44
45private:
46 inline static std::string_view logger_name =
47 "ores.qt.client_country_model";
48
49 [[nodiscard]] static auto& lg() {
50 using namespace ores::logging;
51 static auto instance = make_logger(logger_name);
52 return instance;
53 }
54
55public:
59 enum Column {
60 Flag, // Country flag icon
61 Name, // Short name
62 Alpha2Code, // ISO 3166-1 alpha-2 code
63 Alpha3Code, // ISO 3166-1 alpha-3 code
64 NumericCode, // ISO 3166-1 numeric code
65 OfficialName, // Official name
66 Version, // Version number
67 RecordedBy, // Username who recorded
68 RecordedAt, // Timestamp when recorded
69 ColumnCount // Must be last
70 };
71
72 explicit ClientCountryModel(ClientManager* clientManager,
73 ImageCache* imageCache,
74 QObject* parent = nullptr);
75 ~ClientCountryModel() override = default;
76
77 // QAbstractTableModel interface
78 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
79 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
80 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
81 QVariant headerData(int section, Qt::Orientation orientation,
82 int role = Qt::DisplayRole) const override;
83
89 void refresh(bool replace = true);
90
97 void load_page(std::uint32_t offset, std::uint32_t limit);
98
102 bool canFetchMore(const QModelIndex& parent = QModelIndex()) const override;
103
107 void fetchMore(const QModelIndex& parent = QModelIndex()) override;
108
115 const refdata::domain::country* getCountry(int row) const;
116
122 std::vector<refdata::domain::country> getCountries() const;
123
127 std::uint32_t page_size() const { return page_size_; }
128
134 void set_page_size(std::uint32_t size);
135
139 std::uint32_t total_available_count() const { return total_available_count_; }
140
141signals:
146
150 void loadError(const QString& error_message, const QString& details = {});
151
152private slots:
153 void onCountriesLoaded();
154 void onPulseStateChanged(bool isOn);
155 void onPulsingComplete();
156
157private:
158 QVariant recency_foreground_color(const std::string& alpha2_code) const;
159
160 struct FetchResult {
161 bool success;
162 std::vector<refdata::domain::country> countries;
163 std::uint32_t total_available_count;
164 QString error_message;
165 QString error_details;
166 };
167
168 void fetch_countries(std::uint32_t offset, std::uint32_t limit);
169
170 ClientManager* clientManager_;
171 ImageCache* imageCache_;
172 std::vector<refdata::domain::country> countries_;
173 QFutureWatcher<FetchResult>* watcher_;
174 std::uint32_t page_size_{100};
175 std::uint32_t total_available_count_{0};
176 bool is_fetching_{false};
177
178 // Recency highlighting
179 using CountryKeyExtractor = std::string(*)(const refdata::domain::country&);
180 RecencyTracker<refdata::domain::country, CountryKeyExtractor> recencyTracker_;
181 RecencyPulseManager* pulseManager_;
182};
183
184}
185
186#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 countries fetched from the server.
Definition ClientCountryModel.hpp:42
bool canFetchMore(const QModelIndex &parent=QModelIndex()) const override
Check if more data can be fetched from the server.
Definition ClientCountryModel.cpp:332
void fetchMore(const QModelIndex &parent=QModelIndex()) override
Fetch the next page of data from the server.
Definition ClientCountryModel.cpp:348
void set_page_size(std::uint32_t size)
Set the page size for pagination.
Definition ClientCountryModel.cpp:356
std::uint32_t total_available_count() const
Get the total number of records available on the server.
Definition ClientCountryModel.hpp:139
std::vector< refdata::domain::country > getCountries() const
Get all countries.
Definition ClientCountryModel.cpp:328
void dataLoaded()
Emitted when data has been successfully loaded.
const refdata::domain::country * getCountry(int row) const
Get country at the specified row.
Definition ClientCountryModel.cpp:321
void refresh(bool replace=true)
Refresh country data from server asynchronously.
Definition ClientCountryModel.cpp:161
void load_page(std::uint32_t offset, std::uint32_t limit)
Load a specific page of country data.
Definition ClientCountryModel.cpp:190
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 ClientCountryModel.hpp:127
Column
Enumeration of table columns for type-safe column access.
Definition ClientCountryModel.hpp:59
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 country using ISO 3166-1 standard codes.
Definition country.hpp:33