game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
sidebar_models.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/sidebar_models.h
4// ==============================================================================
5// QAbstractListModel subclasses backing the four sidebar filter lists.
6// Introduced 2026-04-19 as the QListView migration part of the Qt 6
7// modernization pass. Previously these lists were QListWidget; now they're
8// QListView + model so filter/sort proxies can be composed, icons are
9// recomputed only on theme change, and the logic is unit-testable.
10//
11// Each model owns a tiny value-type vector of "Entry" rows. Repopulation is
12// done via a single rebuild(...) call which uses beginResetModel /
13// endResetModel — the row counts here are small (≤ ~15) so incremental row
14// insertion doesn't save anything meaningful.
15//
16// Custom roles:
17// IdRole — the "payload" data UserRole carried before: an int for
18// SourceFilterModel, a QString for the others. All four
19// models report this at Qt::UserRole for backwards-compat
20// so any remaining callers that queried UserRole directly
21// keep working.
22// ==============================================================================
23
24#pragma once
25
26#include "models/game.h" // GameSource enum
27
28#include <QAbstractListModel>
29#include <QIcon>
30#include <QString>
31#include <vector>
32
33// Forward
34struct Game;
35class ThemeManager;
36
37// Shared "Entry" row for all four models. Keeps the boilerplate minimal.
39{
40 QString display; ///< "Steam (12)" / "GE-Proton9-26 (3)"
41 QString tooltip; ///< Hover description
42 QIcon icon; ///< Pre-rendered glyph in the accent/secondary colour
43 // Payload. Exactly one is meaningful per model:
44 QString stringId; ///< Runner version, collection name, or "" for "All"
45 int intId = 0; ///< Source enum value or category row
46};
47
48// Qt::UserRole → we re-expose the payload here so older callers that queried
49// UserRole during the QListWidget era continue to work.
50inline constexpr int SidebarIdRole = Qt::UserRole + 1;
51
52// ============================================================================
53// CategoryModel — fixed 7 rows (All, Favorites, Recently Played, Installed,
54// Not Installed, Updates Available, Running). Counts are mutated via
55// setCounts() which emits dataChanged for rows whose text changed.
56// ============================================================================
57
58class CategoryModel : public QAbstractListModel
59{
60 Q_OBJECT
61 public:
62 explicit CategoryModel(ThemeManager* themeManager, QObject* parent = nullptr);
63
64 // QAbstractListModel overrides
65 int rowCount(const QModelIndex& parent = {}) const override;
66 QVariant data(const QModelIndex& index, int role) const override;
67
68 // Re-render the 7 glyphs in the current accent/secondary theme colours.
69 // Call on construction and on ThemeManager::themeChanged.
70 void rebuildIcons();
71
72 // Refresh each row's count-in-parens. Emits dataChanged only for rows
73 // whose display text actually changed.
74 void setCounts(int all, int favorites, int recentlyPlayed, int installed, int notInstalled,
75 int updatesAvailable, int running);
76
77 // Convenience for the command palette: find the row whose display text
78 // contains `partial`. Returns -1 on no match.
79 int rowContaining(const QString& partial) const;
80
81 private:
82 struct Row
83 {
84 QString label;
85 QString tooltip;
86 QIcon icon;
87 int count = 0;
88 };
89 std::vector<Row> rows_;
90 ThemeManager* themeManager_ = nullptr;
91};
92
93// ============================================================================
94// SourceFilterModel — dynamic: "All" + one row per GameSource with games.
95// Payload at SidebarIdRole/UserRole is the GameSource enum value (int);
96// "All" carries -1.
97// ============================================================================
98
99class SourceFilterModel : public QAbstractListModel
100{
101 Q_OBJECT
102 public:
103 explicit SourceFilterModel(ThemeManager* themeManager, QObject* parent = nullptr);
104
105 int rowCount(const QModelIndex& parent = {}) const override;
106 QVariant data(const QModelIndex& index, int role) const override;
107
108 // Rebuild the row vector from all-games: "All (N)" followed by the
109 // sources that have at least one game, in a fixed order.
110 void rebuild(const std::vector<const Game*>& allGames);
111
112 // Find the row whose SourceIdRole equals `value` (or -1 for "All").
113 // Returns -1 if not present.
114 int rowForSourceValue(int value) const;
115
116 private:
117 std::vector<SidebarEntry> entries_;
118 ThemeManager* themeManager_ = nullptr;
119};
120
121// ============================================================================
122// RunnerFilterModel — "All" + "Native" (if present) + sorted Proton versions
123// + sorted Wine versions + other runner versions. Payload is the version
124// string ("" for "All", "native" for Native).
125// ============================================================================
126
127class RunnerFilterModel : public QAbstractListModel
128{
129 Q_OBJECT
130 public:
131 explicit RunnerFilterModel(ThemeManager* themeManager, QObject* parent = nullptr);
132
133 int rowCount(const QModelIndex& parent = {}) const override;
134 QVariant data(const QModelIndex& index, int role) const override;
135
136 void rebuild(const std::vector<const Game*>& allGames);
137
138 int rowForVersion(const QString& version) const;
139
140 private:
141 std::vector<SidebarEntry> entries_;
142 ThemeManager* themeManager_ = nullptr;
143};
144
145// ============================================================================
146// CollectionsModel — "All" + every collection returned by the library
147// controller. Payload is the collection name ("" for "All").
148// ============================================================================
149
150class CollectionsModel : public QAbstractListModel
151{
152 Q_OBJECT
153 public:
154 explicit CollectionsModel(ThemeManager* themeManager, QObject* parent = nullptr);
155
156 int rowCount(const QModelIndex& parent = {}) const override;
157 QVariant data(const QModelIndex& index, int role) const override;
158
159 void rebuild(const std::vector<std::string>& collectionNames,
160 const std::vector<const Game*>& allGames, int totalGameCount);
161
162 int rowForCollection(const QString& name) const;
163
164 private:
165 std::vector<SidebarEntry> entries_;
166 ThemeManager* themeManager_ = nullptr;
167};
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
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
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)"