game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
game_list_model.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/game_list_model.h
4// ==============================================================================
5// QAbstractListModel wrapping a vector of Game* for the main library view.
6// ==============================================================================
7// Ownership:
8// - Games are owned by core::GameLibraryController. This model holds only
9// non-owning pointers. Callers (MainWindow) must ensure Game* lifetimes
10// exceed the model's view of them (which is naturally true because
11// setGames() is called from the GUI thread after each library change).
12//
13// Threading:
14// - Model is GUI-thread-only (Qt's model/view constraint). All mutations
15// must be called on the GUI thread.
16//
17// Roles:
18// - Qt::DisplayRole: game name
19// - Qt::DecorationRole: game icon (QIcon)
20// - Qt::ToolTipRole: multi-line hover text
21// - GameIdRole .. GameSteamAppIdRole (from game_list_delegate.h): all
22// per-game metadata that the delegate reads.
23// ==============================================================================
24
25#pragma once
26
27#include <QAbstractListModel>
28#include <QIcon>
29#include <functional>
30#include <string>
31#include <unordered_map>
32#include <vector>
33
34class Game;
35
36class GameListModel : public QAbstractListModel
37{
38 Q_OBJECT
39
40 public:
41 explicit GameListModel(QObject* parent = nullptr);
42 ~GameListModel() override;
43
44 // ==========================================================================
45 // Population
46 // ==========================================================================
47
48 /// Replace the model's game list in one shot. Triggers full reset.
49 /// Game* pointers must remain valid until the next setGames() or clear().
50 void setGames(std::vector<const Game*> games);
51
52 /// Clear all rows.
53 void clear();
54
55 /// Get the raw game list (O(1)).
56 const std::vector<const Game*>& games() const
57 {
58 return games_;
59 }
60
61 // ==========================================================================
62 // Row <-> id helpers
63 // ==========================================================================
64
65 /// O(1) gameId -> row lookup. Returns -1 if not found.
66 int rowForGameId(const std::string& gameId) const;
67
68 /// Get the Game* at a given row. Returns nullptr if out of range.
69 const Game* gameAt(int row) const;
70
71 // ==========================================================================
72 // Per-row updates
73 // ==========================================================================
74
75 /// Emit dataChanged for a single row so the delegate repaints.
76 /// Use this when backing Game* data has changed but the row list hasn't.
77 void updateRow(int row);
78
79 /// Find the row for `gameId` and emit dataChanged for it. No-op if missing.
80 void updateGameById(const std::string& gameId);
81
82 // ==========================================================================
83 // Icon cache (optional — populated by MainWindow via async loader)
84 // ==========================================================================
85
86 /// Stash a pre-scaled QIcon for a given gameId. Returned via
87 /// Qt::DecorationRole on the corresponding row. Takes no ownership —
88 /// QIcon is implicitly shared.
89 void setIcon(const std::string& gameId, const QIcon& icon);
90
91 // ==========================================================================
92 // External-state accessors (for roles that aren't on Game directly)
93 // ==========================================================================
94
95 using IsRunningFn = std::function<bool(const std::string& gameId)>;
96
97 /// Install a callback the model queries to answer GameIsRunningRole.
98 /// Routing through a callback avoids a hard dependency on
99 /// GameLaunchController in the model. The callback is invoked on the
100 /// GUI thread during paint — keep it O(1) (map lookup).
102 {
103 isRunning_ = std::move(accessor);
104 }
105
106 // ==========================================================================
107 // QAbstractListModel API
108 // ==========================================================================
109
110 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
111 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
112 Qt::ItemFlags flags(const QModelIndex& index) const override;
113
114 private:
115 void rebuildIdIndex();
116 QVariant valueForRole(const Game& game, int role) const;
117
118 std::vector<const Game*> games_;
119 std::unordered_map<std::string, int> idToRow_;
120 std::unordered_map<std::string, QIcon> icons_;
121 IsRunningFn isRunning_;
122};
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
std::function< bool(const std::string &gameId)> IsRunningFn
int rowCount(const QModelIndex &parent=QModelIndex()) const override
~GameListModel() override
void setIsRunningAccessor(IsRunningFn accessor)
Install a callback the model queries to answer GameIsRunningRole.
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