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 <QSize>
25#include <QFutureWatcher>
26#include <QAbstractTableModel>
27#include "ores.qt/AbstractClientModel.hpp"
28#include "ores.qt/ClientManager.hpp"
29#include "ores.qt/RecencyPulseManager.hpp"
30#include "ores.qt/RecencyTracker.hpp"
31#include "ores.qt/ColumnMetadata.hpp"
32#include "ores.logging/make_logger.hpp"
33#include "ores.refdata.api/domain/country.hpp"
34
35namespace ores::qt {
36
37class ImageCache;
38
46 Q_OBJECT
47
48private:
49 inline static std::string_view logger_name =
50 "ores.qt.client_country_model";
51
52 [[nodiscard]] static auto& lg() {
53 using namespace ores::logging;
54 static auto instance = make_logger(logger_name);
55 return instance;
56 }
57
58public:
62 enum Column {
63 Alpha2Code, // ISO 3166-1 alpha-2 code
64 Alpha3Code, // ISO 3166-1 alpha-3 code
65 Name, // Short name
66 NumericCode, // ISO 3166-1 numeric code
67 OfficialName, // Official name
68 Version, // Version number
69 ModifiedBy, // Username who recorded
70 RecordedAt, // Timestamp when recorded
71 ColumnCount // Must be last
72 };
73
79 static constexpr std::size_t kColumnCount = std::size_t(ColumnCount);
80 static constexpr std::array<ColumnMetadata, kColumnCount> kColumns = {{
81 {
82 .column = Alpha2Code,
83 .header = std::string_view("Alpha-2"),
85 .hidden_by_default = false,
86 .default_width = kColumnWidthAuto
87 },
88 {
89 .column = Alpha3Code,
90 .header = std::string_view("Alpha-3"),
92 .hidden_by_default = true,
93 .default_width = 80
94 },
95 {
96 .column = Name,
97 .header = std::string_view("Name"),
99 .hidden_by_default = false,
100 .default_width = kColumnWidthAuto
101 },
102 {
103 .column = NumericCode,
104 .header = std::string_view("Numeric"),
106 .hidden_by_default = true,
107 .default_width = 70
108 },
109 {
110 .column = OfficialName,
111 .header = std::string_view("Official Name"),
113 .hidden_by_default = true,
114 .default_width = kColumnWidthAuto
115 },
116 {
117 .column = Version,
118 .header = std::string_view("Version"),
120 .hidden_by_default = false,
121 .default_width = 70
122 },
123 {
124 .column = ModifiedBy,
125 .header = std::string_view("Modified By"),
127 .hidden_by_default = false,
128 .default_width = kColumnWidthAuto
129 },
130 {
131 .column = RecordedAt,
132 .header = std::string_view("Recorded At"),
134 .hidden_by_default = false,
135 .default_width = kColumnWidthAuto
136 }
137 }};
138
142 inline static const QSize kDefaultWindowSize = {900, 600};
143
147 static constexpr std::string_view kSettingsGroup = "CountryListWindow";
148
152 static std::vector<column_style> const& columnStyles() {
153 static std::vector<column_style> const kStylesVector = []() {
154 std::vector<column_style> result;
155 result.reserve(kColumnCount);
156 for (std::size_t i = 0; i < kColumnCount; ++i)
157 result.push_back(kColumns[i].style);
158 return result;
159 }();
160 return kStylesVector;
161 }
162
166 static QVector<int> defaultHiddenColumns() {
167 static QVector<int> const result =
168 ::ores::qt::defaultHiddenColumns<kColumnCount>(kColumns);
169 return result;
170 }
171
172 explicit ClientCountryModel(ClientManager* clientManager,
173 ImageCache* imageCache,
174 QObject* parent = nullptr);
175 ~ClientCountryModel() override = default;
176
177 // QAbstractTableModel interface
178 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
179 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
180 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
181 QVariant headerData(int section, Qt::Orientation orientation,
182 int role = Qt::DisplayRole) const override;
183
189 void refresh(bool replace = true);
190
197 void load_page(std::uint32_t offset, std::uint32_t limit);
198
205 const refdata::domain::country* getCountry(int row) const;
206
212 std::vector<refdata::domain::country> getCountries() const;
213
217 std::uint32_t page_size() const { return page_size_; }
218
224 void set_page_size(std::uint32_t size);
225
229 std::uint32_t total_available_count() const { return total_available_count_; }
230
231signals:
240private slots:
241 void onCountriesLoaded();
242 void onPulseStateChanged(bool isOn);
243 void onPulsingComplete();
244
245private:
246 QVariant recency_foreground_color(const std::string& alpha2_code) const;
247
248 struct FetchResult {
249 bool success;
250 std::vector<refdata::domain::country> countries;
251 std::uint32_t total_available_count;
252 QString error_message;
253 QString error_details;
254 };
255
256 void fetch_countries(std::uint32_t offset, std::uint32_t limit);
257
258 ClientManager* clientManager_;
259 ImageCache* imageCache_;
260 std::vector<refdata::domain::country> countries_;
261 QFutureWatcher<FetchResult>* watcher_;
262 std::uint32_t page_size_{100};
263 std::uint32_t total_available_count_{0};
264 bool is_fetching_{false};
265
266 // Recency highlighting
267 using CountryKeyExtractor = std::string(*)(const refdata::domain::country&);
268 RecencyTracker<refdata::domain::country, CountryKeyExtractor> recencyTracker_;
269 RecencyPulseManager* pulseManager_;
270};
271
272}
273
274#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:29
constexpr int kColumnWidthAuto
Sentinel value for column default width meaning "auto-size to contents".
Definition ColumnMetadata.hpp:37
@ mono_center
Monospace, centered.
@ text_left
Proportional font, left-aligned (default).
@ mono_bold_left
Monospace bold, left-aligned.
@ mono_bold_center
Monospace bold, centered.
@ mono_left
Monospace, left-aligned.
Base class for all client-side entity models.
Definition AbstractClientModel.hpp:36
Model for displaying countries fetched from the server.
Definition ClientCountryModel.hpp:45
static constexpr std::string_view kSettingsGroup
Settings group name for persisting window and column state.
Definition ClientCountryModel.hpp:147
void set_page_size(std::uint32_t size)
Set the page size for pagination.
Definition ClientCountryModel.cpp:297
static constexpr std::size_t kColumnCount
Column metadata: header text, style, visibility, and width.
Definition ClientCountryModel.hpp:79
static const QSize kDefaultWindowSize
Default window size for the country list window.
Definition ClientCountryModel.hpp:142
static std::vector< column_style > const & columnStyles()
Returns a static vector of column styles (built once per process).
Definition ClientCountryModel.hpp:152
std::uint32_t total_available_count() const
Get the total number of records available on the server.
Definition ClientCountryModel.hpp:229
std::vector< refdata::domain::country > getCountries() const
Get all countries.
Definition ClientCountryModel.cpp:293
static QVector< int > defaultHiddenColumns()
Returns a static QVector of hidden column indices (built once per process).
Definition ClientCountryModel.hpp:166
const refdata::domain::country * getCountry(int row) const
Get country at the specified row.
Definition ClientCountryModel.cpp:286
void refresh(bool replace=true)
Refresh country data from server asynchronously.
Definition ClientCountryModel.cpp:156
void load_page(std::uint32_t offset, std::uint32_t limit)
Load a specific page of country data.
Definition ClientCountryModel.cpp:181
std::uint32_t page_size() const
Get the page size used for pagination.
Definition ClientCountryModel.hpp:217
Column
Enumeration of table columns for type-safe column access.
Definition ClientCountryModel.hpp:62
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
Represents a country using ISO 3166-1 standard codes.
Definition country.hpp:34