game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
game_list_model.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/game_list_model.cpp
4// ==============================================================================
5// GameListModel implementation. Mostly read-through to Game* fields.
6// ==============================================================================
7
9
10#include "integrations/steam_integration.h" // SteamStateFlags
11#include "models/game.h"
12#include "widgets/game_list_delegate.h" // GameDataRole enum
13
14#include <QString>
15#include <QStringList>
16
17GameListModel::GameListModel(QObject* parent) : QAbstractListModel(parent) {}
18
20
21// ==============================================================================
22// Population
23// ==============================================================================
24
25void GameListModel::setGames(std::vector<const Game*> games)
26{
27 beginResetModel();
28 games_ = std::move(games);
29 rebuildIdIndex();
30 endResetModel();
31}
32
34{
35 beginResetModel();
36 games_.clear();
37 idToRow_.clear();
38 icons_.clear();
39 endResetModel();
40}
41
42void GameListModel::rebuildIdIndex()
43{
44 idToRow_.clear();
45 idToRow_.reserve(games_.size());
46 for (size_t i = 0; i < games_.size(); ++i)
47 {
48 if (games_[i])
49 idToRow_[games_[i]->id] = static_cast<int>(i);
50 }
51}
52
53int GameListModel::rowForGameId(const std::string& gameId) const
54{
55 auto it = idToRow_.find(gameId);
56 return (it != idToRow_.end()) ? it->second : -1;
57}
58
59const Game* GameListModel::gameAt(int row) const
60{
61 if (row < 0 || row >= static_cast<int>(games_.size()))
62 return nullptr;
63 return games_[static_cast<size_t>(row)];
64}
65
66// ==============================================================================
67// Per-row updates
68// ==============================================================================
69
71{
72 if (row < 0 || row >= static_cast<int>(games_.size()))
73 return;
74 const QModelIndex idx = index(row, 0);
75 emit dataChanged(idx, idx); // all roles
76}
77
78void GameListModel::updateGameById(const std::string& gameId)
79{
80 const int row = rowForGameId(gameId);
81 if (row >= 0)
82 updateRow(row);
83}
84
85// ==============================================================================
86// Icon cache
87// ==============================================================================
88
89void GameListModel::setIcon(const std::string& gameId, const QIcon& icon)
90{
91 icons_[gameId] = icon;
92 // Emit dataChanged so the view refreshes the DecorationRole only.
93 const int row = rowForGameId(gameId);
94 if (row >= 0)
95 {
96 const QModelIndex idx = index(row, 0);
97 emit dataChanged(idx, idx, {Qt::DecorationRole});
98 }
99}
100
101// ==============================================================================
102// QAbstractListModel
103// ==============================================================================
104
105int GameListModel::rowCount(const QModelIndex& parent) const
106{
107 if (parent.isValid())
108 return 0; // flat list
109 return static_cast<int>(games_.size());
110}
111
112Qt::ItemFlags GameListModel::flags(const QModelIndex& index) const
113{
114 if (!index.isValid())
115 return Qt::NoItemFlags;
116 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
117}
118
119QVariant GameListModel::data(const QModelIndex& index, int role) const
120{
121 if (!index.isValid())
122 return {};
123 const int row = index.row();
124 if (row < 0 || row >= static_cast<int>(games_.size()))
125 return {};
126 const Game* game = games_[static_cast<size_t>(row)];
127 if (!game)
128 return {};
129 return valueForRole(*game, role);
130}
131
132QVariant GameListModel::valueForRole(const Game& game, int role) const
133{
134 switch (role)
135 {
136 case Qt::DisplayRole:
137 return QString::fromStdString(game.name);
138
139 case Qt::DecorationRole:
140 {
141 auto it = icons_.find(game.id);
142 if (it != icons_.end())
143 return it->second;
144 return {};
145 }
146
147 case Qt::ToolTipRole:
148 {
149 QString tip = QString::fromStdString(game.name);
150 // Each line goes through tr() with the dynamic value as %1 so
151 // translators can reorder / re-punctuate per-language. The field
152 // labels ("Developer:", "Released:") MUST be inside tr(); without
153 // it lupdate can't extract them and they remain English.
154 if (!game.metadata.developer.empty())
155 tip += QStringLiteral("\n") +
156 tr("Developer: %1").arg(QString::fromStdString(game.metadata.developer));
157 if (game.metadata.releaseYear > 0)
158 tip += QStringLiteral("\n") +
159 tr("Released: %1").arg(QString::number(game.metadata.releaseYear));
160 return tip;
161 }
162
163 // Accessibility roles per the Qt model/view contract
164 // (https://doc.qt.io/qt-6/qabstractitemmodel.html#data). Without these,
165 // a screen reader walking the QListView speaks only the DisplayRole
166 // and has no information about runner/source/playtime context. Returning
167 // both AccessibleTextRole (the spoken label) and AccessibleDescriptionRole
168 // (the longer "what is this") closes the gap surfaced in the 2026-05
169 // accessibility audit.
170 case Qt::AccessibleTextRole:
171 return QString::fromStdString(game.name);
172 case Qt::AccessibleDescriptionRole:
173 {
174 // Build the description as a list of localised fragments, then join
175 // with ", " (also localised — some languages prefer a different
176 // separator). Each tr() call carries a %1 / %n placeholder so the
177 // translator can reorder. The plural-aware tr(..., n) form is used
178 // for hour count to handle languages with multiple plural categories
179 // per https://doc.qt.io/qt-6/i18n-plural-rules.html.
180 QStringList parts;
181 parts << QString::fromStdString(game.name);
182 if (!game.getSourceName().empty())
183 parts << tr("source %1").arg(QString::fromStdString(game.getSourceName()));
184 if (!game.getRunnerName().empty())
185 parts << tr("runner %1").arg(QString::fromStdString(game.getRunnerName()));
186 const bool installed = game.metadata.isSteamGame ? game.metadata.steamInstalled : true;
187 parts << (installed ? tr("installed") : tr("not installed"));
188 if (game.statistics.totalPlayTimeSeconds > 0)
189 {
190 const int hours = static_cast<int>(game.statistics.totalPlayTimeSeconds / 3600);
191 if (hours > 0)
192 {
193 // tr(text, disambiguation, n) selects the correct plural
194 // form for the target language. `%n` is the standard Qt
195 // placeholder for the count.
196 parts << tr("%n hour(s) played", nullptr, hours);
197 }
198 }
199 if (game.isFavorite)
200 parts << tr("favorite");
201 return parts.join(QStringLiteral(", "));
202 }
203
204 case GameIdRole:
205 return QString::fromStdString(game.id);
206 case GameSourceRole:
207 return QString::fromStdString(game.getSourceName());
208 case GameRunnerRole:
209 return QString::fromStdString(game.getRunnerName());
210 case GamePlatformRole:
211 return QString::fromStdString(game.getPlatformName());
212 case GamePlayTimeRole:
213 return static_cast<int>(game.statistics.totalPlayTimeSeconds);
215 return static_cast<qint64>(game.statistics.lastPlayed);
217 return game.statistics.timesLaunched;
219 return isRunning_ ? isRunning_(game.id) : game.isRunning();
221 return game.isFavorite;
223 return QString::fromStdString(game.metadata.protondbTier);
224 case GameIsSteamRole:
225 return game.metadata.isSteamGame;
227 return game.metadata.isSteamGame ? game.metadata.steamInstalled : true;
229 return game.metadata.isSteamGame && game.metadata.steamAppId > 0 &&
232 return game.metadata.isSteamGame &&
236 {
238 {
239 return static_cast<double>(game.metadata.steamBytesDownloaded) /
240 static_cast<double>(game.metadata.steamBytesToDownload);
241 }
242 return 0.0;
243 }
245 return game.metadata.cloudSave.enabled;
247 return static_cast<qint64>(game.installSizeBytes);
249 return game.metadata.steamAppId;
250
251 default:
252 return {};
253 }
254}
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
~GameListModel() override
const Game * gameAt(int row) const
Get the Game* at a given row. Returns nullptr if out of range.
void clear()
Clear all rows.
void updateRow(int row)
Emit dataChanged for a single row so the delegate repaints.
const std::vector< const Game * > & games() const
Get the raw game list (O(1)).
void updateGameById(const std::string &gameId)
Find the row for gameId and emit dataChanged for it. No-op if missing.
void setIcon(const std::string &gameId, const QIcon &icon)
Stash a pre-scaled QIcon for a given gameId.
void setGames(std::vector< const Game * > games)
Replace the model's game list in one shot.
GameListModel(QObject *parent=nullptr)
int rowForGameId(const std::string &gameId) const
O(1) gameId -> row lookup. Returns -1 if not found.
Qt::ItemFlags flags(const QModelIndex &index) const override
Complete representation of a game in the library.
Definition game.h:767
GameMetadata metadata
Descriptive information and media.
Definition game.h:785
std::string getPlatformName() const
Get human-readable platform name.
Definition game.cpp:68
bool isFavorite
Is this marked as a favorite game?
Definition game.h:803
std::string name
Display name shown in UI.
Definition game.h:774
std::string getSourceName() const
Get human-readable source name.
Definition game.cpp:25
std::string id
Unique identifier (auto-generated or user-specified).
Definition game.h:773
GameStatistics statistics
Play statistics and usage tracking.
Definition game.h:786
uint64_t installSizeBytes
Installation size in bytes.
Definition game.h:820
std::string getRunnerName() const
Get human-readable runner name.
Definition game.cpp:49
@ GameRunnerRole
@ GameProtonDBTierRole
@ GameNeedsUpdateRole
@ GameCloudSaveEnabledRole
@ GameIdRole
@ GameIsSteamRole
@ GamePlatformRole
@ GameSourceRole
@ GameInstallSizeRole
@ GameIsDownloadingRole
@ GameIsRunningRole
@ GameLastPlayedRole
@ GameDownloadProgressRole
@ GameIsInstalledRole
@ GameLaunchCountRole
@ GamePlayTimeRole
@ GameSteamAppIdRole
@ GameIsFavoriteRole
bool needsUpdate(uint32_t flags)
Check if a game needs an update.
bool isDownloading(uint32_t flags)
Check if a game is currently downloading or updating.
bool enabled
Cloud saves enabled for this game.
Definition game.h:518
bool isSteamGame
True if imported from Steam.
Definition game.h:574
bool steamInstalled
True if Steam game is installed (set during Steam import).
Definition game.h:576
uint64_t steamBytesDownloaded
Bytes downloaded so far.
Definition game.h:588
int steamAppId
Steam App ID (0 if not a Steam game).
Definition game.h:575
uint32_t steamStateFlags
Steam StateFlags from appmanifest.
Definition game.h:587
CloudSaveInfo cloudSave
Steam Cloud save information.
Definition game.h:596
std::string developer
Developer/studio name.
Definition game.h:568
uint64_t steamBytesToDownload
Total bytes to download.
Definition game.h:589
int releaseYear
Year the game was released.
Definition game.h:570
std::string protondbTier
Cached tier: "platinum", "gold", "silver", "bronze", "borked", "native".
Definition game.h:601
bool steamIsDownloading
Currently downloading/updating.
Definition game.h:590
double totalPlayTimeSeconds
Total play time in seconds (for all sessions).
Definition game.h:714
time_t lastPlayed
Unix timestamp of last play session.
Definition game.h:715
int timesLaunched
Number of times game was launched.
Definition game.h:713