game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
sidebar_models.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/sidebar_models.cpp
4// ==============================================================================
5
7
8#include "icon_font.h"
9#include "models/game.h"
10#include "theme_manager.h"
11
12#include <algorithm>
13#include <map>
14#include <unordered_map>
15#include <utility>
16
17namespace
18{
19
20// Per-model icon size (matches the legacy setIconSize(QSize(16,16))).
21constexpr int kIconPx = 16;
22
23QString qs(const std::string& s)
24{
25 return QString::fromStdString(s);
26}
27
28} // namespace
29
30// ============================================================================
31// CategoryModel
32// ============================================================================
33
34CategoryModel::CategoryModel(ThemeManager* themeManager, QObject* parent)
35 : QAbstractListModel(parent), themeManager_(themeManager)
36{
37 rows_ = {
38 {QStringLiteral("All"), QStringLiteral("Show all games in your library"), {}, 0},
39 {QStringLiteral("Favorites"),
40 QStringLiteral("Show games you've marked as favorites"),
41 {},
42 0},
43 {QStringLiteral("Recently Played"),
44 QStringLiteral("Show games sorted by last played date"),
45 {},
46 0},
47 {QStringLiteral("Installed"), QStringLiteral("Show only installed games"), {}, 0},
48 {QStringLiteral("Not Installed"),
49 QStringLiteral("Show games not currently installed"),
50 {},
51 0},
52 {QStringLiteral("Updates Available"),
53 QStringLiteral("Show games with updates ready to download"),
54 {},
55 0},
56 {QStringLiteral("Running"), QStringLiteral("Show currently running games"), {}, 0},
57 };
59}
60
61int CategoryModel::rowCount(const QModelIndex& parent) const
62{
63 if (parent.isValid())
64 return 0;
65 return static_cast<int>(rows_.size());
66}
67
68QVariant CategoryModel::data(const QModelIndex& index, int role) const
69{
70 if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(rows_.size()))
71 return {};
72 const Row& r = rows_[static_cast<size_t>(index.row())];
73 switch (role)
74 {
75 case Qt::DisplayRole:
76 return QString("%1 (%2)").arg(r.label).arg(r.count);
77 case Qt::DecorationRole:
78 return r.icon;
79 case Qt::ToolTipRole:
80 return r.tooltip;
81 case Qt::UserRole:
82 case SidebarIdRole:
83 return index.row(); // category index == row
84 default:
85 return {};
86 }
87}
88
90{
91 const ThemeColors& c = themeManager_->colors();
92 // Fixed icon mapping for the 7 rows.
93 static const char32_t kIcons[] = {
96 };
97 for (size_t i = 0; i < rows_.size() && i < std::size(kIcons); ++i)
98 rows_[i].icon = IconFont::icon(kIcons[i], kIconPx, c.textSecondary, c.accent);
99
100 // Icons changed for every row — emit a bulk dataChanged on DecorationRole.
101 if (!rows_.empty())
102 {
103 emit dataChanged(index(0), index(static_cast<int>(rows_.size()) - 1), {Qt::DecorationRole});
104 }
105}
106
107void CategoryModel::setCounts(int all, int favorites, int recentlyPlayed, int installed,
108 int notInstalled, int updatesAvailable, int running)
109{
110 const int newCounts[] = {all, favorites, recentlyPlayed, installed,
111 notInstalled, updatesAvailable, running};
112 for (size_t i = 0; i < rows_.size() && i < std::size(newCounts); ++i)
113 {
114 if (rows_[i].count != newCounts[i])
115 {
116 rows_[i].count = newCounts[i];
117 emit dataChanged(index(static_cast<int>(i)), index(static_cast<int>(i)),
118 {Qt::DisplayRole});
119 }
120 }
121}
122
123int CategoryModel::rowContaining(const QString& partial) const
124{
125 for (size_t i = 0; i < rows_.size(); ++i)
126 {
127 if (rows_[i].label.contains(partial, Qt::CaseInsensitive))
128 return static_cast<int>(i);
129 }
130 return -1;
131}
132
133// ============================================================================
134// SourceFilterModel
135// ============================================================================
136
138 : QAbstractListModel(parent), themeManager_(themeManager)
139{
140}
141
142int SourceFilterModel::rowCount(const QModelIndex& parent) const
143{
144 if (parent.isValid())
145 return 0;
146 return static_cast<int>(entries_.size());
147}
148
149QVariant SourceFilterModel::data(const QModelIndex& index, int role) const
150{
151 if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(entries_.size()))
152 return {};
153 const SidebarEntry& e = entries_[static_cast<size_t>(index.row())];
154 switch (role)
155 {
156 case Qt::DisplayRole:
157 return e.display;
158 case Qt::DecorationRole:
159 return e.icon;
160 case Qt::ToolTipRole:
161 return e.tooltip;
162 case Qt::UserRole:
163 case SidebarIdRole:
164 return e.intId;
165 default:
166 return {};
167 }
168}
169
170void SourceFilterModel::rebuild(const std::vector<const Game*>& allGames)
171{
172 struct Def
173 {
174 GameSource source;
175 char32_t faIcon;
176 QString label;
177 QString tooltip;
178 };
179 static const std::vector<Def> defs = {
180 {GameSource::Steam, FA::Steam, QStringLiteral("Steam"),
181 QStringLiteral("Games from Steam library")},
182 {GameSource::Local, FA::Folder, QStringLiteral("Local"),
183 QStringLiteral("Manually added local games")},
184 {GameSource::GOG, FA::CompactDisc, QStringLiteral("GOG"),
185 QStringLiteral("Games from GOG Galaxy")},
186 {GameSource::Epic, FA::Gamepad, QStringLiteral("Epic"),
187 QStringLiteral("Games from Epic Games Store")},
188 {GameSource::Lutris, FA::WineGlass, QStringLiteral("Lutris"),
189 QStringLiteral("Games configured via Lutris")},
190 {GameSource::Itch, FA::ItchIo, QStringLiteral("Itch.io"),
191 QStringLiteral("Games from itch.io")},
192 {GameSource::Flatpak, FA::BoxOpen, QStringLiteral("Flatpak"),
193 QStringLiteral("Flatpak-packaged games")},
194 };
195
196 std::map<GameSource, int> sourceCounts;
197 for (const Game* g : allGames)
198 {
199 if (g)
200 sourceCounts[g->source]++;
201 }
202
203 const ThemeColors& c = themeManager_->colors();
204
205 beginResetModel();
206 entries_.clear();
207
208 SidebarEntry allEntry;
209 allEntry.display = QString("All (%1)").arg(allGames.size());
210 allEntry.tooltip = QString("Show all %1 games").arg(allGames.size());
211 allEntry.icon = IconFont::icon(FA::LayerGroup, kIconPx, c.textSecondary, c.accent);
212 allEntry.intId = -1;
213 entries_.push_back(std::move(allEntry));
214
215 for (const Def& d : defs)
216 {
217 auto it = sourceCounts.find(d.source);
218 if (it == sourceCounts.end() || it->second <= 0)
219 continue;
220 SidebarEntry e;
221 e.display = QString("%1 (%2)").arg(d.label).arg(it->second);
222 e.tooltip = QString("%1 (%2 games)").arg(d.tooltip).arg(it->second);
223 e.icon = IconFont::icon(d.faIcon, kIconPx, c.textSecondary, c.accent);
224 e.intId = static_cast<int>(d.source);
225 entries_.push_back(std::move(e));
226 }
227
228 endResetModel();
229}
230
232{
233 for (size_t i = 0; i < entries_.size(); ++i)
234 {
235 if (entries_[i].intId == value)
236 return static_cast<int>(i);
237 }
238 return -1;
239}
240
241// ============================================================================
242// RunnerFilterModel
243// ============================================================================
244
246 : QAbstractListModel(parent), themeManager_(themeManager)
247{
248}
249
250int RunnerFilterModel::rowCount(const QModelIndex& parent) const
251{
252 if (parent.isValid())
253 return 0;
254 return static_cast<int>(entries_.size());
255}
256
257QVariant RunnerFilterModel::data(const QModelIndex& index, int role) const
258{
259 if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(entries_.size()))
260 return {};
261 const SidebarEntry& e = entries_[static_cast<size_t>(index.row())];
262 switch (role)
263 {
264 case Qt::DisplayRole:
265 return e.display;
266 case Qt::DecorationRole:
267 return e.icon;
268 case Qt::ToolTipRole:
269 return e.tooltip;
270 case Qt::UserRole:
271 case SidebarIdRole:
272 return e.stringId;
273 default:
274 return {};
275 }
276}
277
278void RunnerFilterModel::rebuild(const std::vector<const Game*>& allGames)
279{
280 std::map<std::string, int> runnerCounts;
281 int nativeCount = 0;
282 int totalCount = 0;
283
284 for (const Game* game : allGames)
285 {
286 if (!game)
287 continue;
288 totalCount++;
289 bool isNative = game->runner == GameRunner::Native ||
290 game->runnerConfig.runnerType == "native" ||
291 game->runnerConfig.runnerVersion == "native";
292 if (isNative)
293 {
294 nativeCount++;
295 continue;
296 }
297
298 std::string version = game->runnerConfig.runnerVersion;
299 if (version.empty())
300 version = game->runnerConfig.runnerType;
301 if (version.empty() && !game->runnerConfig.protonVersion.empty() &&
302 game->runnerConfig.protonVersion != "Native Linux")
303 version = game->runnerConfig.protonVersion;
304 if (!version.empty() && version != "native")
305 runnerCounts[version]++;
306 }
307
308 const ThemeColors& c = themeManager_->colors();
309
310 beginResetModel();
311 entries_.clear();
312
313 // "All" row
314 {
315 SidebarEntry e;
316 e.display = QString("All (%1)").arg(totalCount);
317 e.tooltip = QString("Show all %1 games").arg(totalCount);
319 e.stringId = QString();
320 entries_.push_back(std::move(e));
321 }
322
323 if (nativeCount > 0)
324 {
325 SidebarEntry e;
326 e.display = QString("Native (%1)").arg(nativeCount);
327 e.tooltip = QString("Native Linux games (%1)").arg(nativeCount);
329 e.stringId = QStringLiteral("native");
330 entries_.push_back(std::move(e));
331 }
332
333 std::vector<std::pair<std::string, int>> proton, wine, other;
334 for (const auto& [version, count] : runnerCounts)
335 {
336 QString lower = qs(version).toLower();
337 if (lower.contains(QStringLiteral("proton")) || lower.contains(QStringLiteral("umu")))
338 proton.push_back({version, count});
339 else if (lower.contains(QStringLiteral("wine")))
340 wine.push_back({version, count});
341 else if (version != "native")
342 other.push_back({version, count});
343 }
344 auto byName = [](const auto& a, const auto& b) { return a.first < b.first; };
345 std::sort(proton.begin(), proton.end(), byName);
346 std::sort(wine.begin(), wine.end(), byName);
347 std::sort(other.begin(), other.end(), byName);
348
349 auto emitGroup = [&](const std::vector<std::pair<std::string, int>>& group, char32_t icon,
350 const QString& prefix)
351 {
352 for (const auto& [version, count] : group)
353 {
354 SidebarEntry e;
355 e.display = QString("%1 (%2)").arg(qs(version)).arg(count);
356 e.tooltip = prefix.isEmpty()
357 ? QString("%1 (%2 games)").arg(qs(version)).arg(count)
358 : QString("%1: %2 (%3 games)").arg(prefix, qs(version)).arg(count);
359 e.icon = IconFont::icon(icon, kIconPx, c.textSecondary, c.accent);
360 e.stringId = qs(version);
361 entries_.push_back(std::move(e));
362 }
363 };
364 emitGroup(proton, FA::Atom, QStringLiteral("Proton"));
365 emitGroup(wine, FA::WineGlass, QStringLiteral("Wine"));
366 emitGroup(other, FA::Gamepad, QString());
367
368 endResetModel();
369}
370
371int RunnerFilterModel::rowForVersion(const QString& version) const
372{
373 for (size_t i = 0; i < entries_.size(); ++i)
374 {
375 if (entries_[i].stringId == version)
376 return static_cast<int>(i);
377 }
378 return -1;
379}
380
381// ============================================================================
382// CollectionsModel
383// ============================================================================
384
386 : QAbstractListModel(parent), themeManager_(themeManager)
387{
388}
389
390int CollectionsModel::rowCount(const QModelIndex& parent) const
391{
392 if (parent.isValid())
393 return 0;
394 return static_cast<int>(entries_.size());
395}
396
397QVariant CollectionsModel::data(const QModelIndex& index, int role) const
398{
399 if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(entries_.size()))
400 return {};
401 const SidebarEntry& e = entries_[static_cast<size_t>(index.row())];
402 switch (role)
403 {
404 case Qt::DisplayRole:
405 return e.display;
406 case Qt::DecorationRole:
407 return e.icon;
408 case Qt::ToolTipRole:
409 return e.tooltip;
410 case Qt::UserRole:
411 case SidebarIdRole:
412 return e.stringId;
413 default:
414 return {};
415 }
416}
417
418void CollectionsModel::rebuild(const std::vector<std::string>& collectionNames,
419 const std::vector<const Game*>& allGames, int totalGameCount)
420{
421 const ThemeColors& c = themeManager_->colors();
422
423 std::unordered_map<std::string, int> counts;
424 for (const Game* g : allGames)
425 {
426 if (!g)
427 continue;
428 for (const auto& col : g->collections)
429 counts[col]++;
430 }
431
432 beginResetModel();
433 entries_.clear();
434
435 // "All" row.
436 {
437 SidebarEntry e;
438 e.display = QString("All (%1)").arg(totalGameCount);
439 e.tooltip = QStringLiteral("Show all games without collection filter");
441 e.stringId = QString();
442 entries_.push_back(std::move(e));
443 }
444
445 for (const std::string& name : collectionNames)
446 {
447 auto it = counts.find(name);
448 int count = it == counts.end() ? 0 : it->second;
449 SidebarEntry e;
450 e.display = QString("%1 (%2)").arg(qs(name)).arg(count);
451 e.tooltip = QString("Show %1 games in '%2' collection").arg(count).arg(qs(name));
453 e.stringId = qs(name);
454 entries_.push_back(std::move(e));
455 }
456
457 endResetModel();
458}
459
460int CollectionsModel::rowForCollection(const QString& name) const
461{
462 for (size_t i = 0; i < entries_.size(); ++i)
463 {
464 if (entries_[i].stringId == name)
465 return static_cast<int>(i);
466 }
467 return -1;
468}
void setCounts(int all, int favorites, int recentlyPlayed, int installed, int notInstalled, int updatesAvailable, int running)
CategoryModel(ThemeManager *themeManager, QObject *parent=nullptr)
int rowCount(const QModelIndex &parent={}) const override
int rowContaining(const QString &partial) const
QVariant data(const QModelIndex &index, int role) const override
QVariant data(const QModelIndex &index, int role) const override
int rowCount(const QModelIndex &parent={}) const override
int rowForCollection(const QString &name) const
CollectionsModel(ThemeManager *themeManager, QObject *parent=nullptr)
void rebuild(const std::vector< std::string > &collectionNames, const std::vector< const Game * > &allGames, int totalGameCount)
Complete representation of a game in the library.
Definition game.h:767
static QIcon icon(char32_t codepoint, int size, const QColor &color)
Render a FontAwesome glyph as a QIcon at the given size and color.
Definition icon_font.h:119
void rebuild(const std::vector< const Game * > &allGames)
int rowCount(const QModelIndex &parent={}) const override
int rowForVersion(const QString &version) const
QVariant data(const QModelIndex &index, int role) const override
RunnerFilterModel(ThemeManager *themeManager, QObject *parent=nullptr)
void rebuild(const std::vector< const Game * > &allGames)
int rowForSourceValue(int value) const
int rowCount(const QModelIndex &parent={}) const override
SourceFilterModel(ThemeManager *themeManager, QObject *parent=nullptr)
QVariant data(const QModelIndex &index, int role) const override
GameSource
Where the game was acquired from (store/service/local).
Definition game.h:39
@ Steam
Steam store/platform.
Definition game.h:41
@ Local
Manually added game (not from a store).
Definition game.h:40
@ Itch
itch.io indie games
Definition game.h:44
@ Lutris
Lutris community scripts.
Definition game.h:45
@ GOG
GOG.com (DRM-free games).
Definition game.h:42
@ Flatpak
Flatpak package.
Definition game.h:46
@ Epic
Epic Games Store.
Definition game.h:43
@ Native
Native Linux executable (no compatibility layer).
Definition game.h:82
constexpr char32_t ClockRotateLeft
Definition icon_font.h:30
constexpr char32_t HardDrive
Definition icon_font.h:31
constexpr char32_t FolderOpen
Definition icon_font.h:51
constexpr char32_t CompactDisc
Definition icon_font.h:39
constexpr char32_t CircleUp
Definition icon_font.h:33
constexpr char32_t CirclePlay
Definition icon_font.h:34
constexpr char32_t Atom
Definition icon_font.h:48
constexpr char32_t BoxOpen
Definition icon_font.h:43
constexpr char32_t Cloud
Definition icon_font.h:32
constexpr char32_t Gamepad
Definition icon_font.h:40
constexpr char32_t Star
Definition icon_font.h:29
constexpr char32_t WineGlass
Definition icon_font.h:38
constexpr char32_t LayerGroup
Definition icon_font.h:46
constexpr char32_t ItchIo
Definition icon_font.h:42
constexpr char32_t Bookmark
Definition icon_font.h:52
constexpr char32_t Linux
Definition icon_font.h:47
constexpr char32_t TableCellsLarge
Definition icon_font.h:28
constexpr char32_t Steam
Definition icon_font.h:37
constexpr char32_t Folder
Definition icon_font.h:41
QString qs(const std::string &s)
constexpr int SidebarIdRole
int intId
Source enum value or category row.
QString tooltip
Hover description.
QIcon icon
Pre-rendered glyph in the accent/secondary colour.
QString stringId
Runner version, collection name, or "" for "All".
QString display
"Steam (12)" / "GE-Proton9-26 (3)"
QColor textSecondary