28 games_ = std::move(
games);
42void GameListModel::rebuildIdIndex()
45 idToRow_.reserve(games_.size());
46 for (
size_t i = 0; i < games_.size(); ++i)
49 idToRow_[games_[i]->id] =
static_cast<int>(i);
55 auto it = idToRow_.find(gameId);
56 return (it != idToRow_.end()) ? it->second : -1;
61 if (row < 0 || row >=
static_cast<int>(games_.size()))
63 return games_[
static_cast<size_t>(row)];
72 if (row < 0 || row >=
static_cast<int>(games_.size()))
74 const QModelIndex idx = index(row, 0);
75 emit dataChanged(idx, idx);
91 icons_[gameId] = icon;
96 const QModelIndex idx = index(row, 0);
97 emit dataChanged(idx, idx, {Qt::DecorationRole});
107 if (parent.isValid())
109 return static_cast<int>(games_.size());
114 if (!index.isValid())
115 return Qt::NoItemFlags;
116 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
121 if (!index.isValid())
123 const int row = index.row();
124 if (row < 0 || row >=
static_cast<int>(games_.size()))
126 const Game* game = games_[
static_cast<size_t>(row)];
129 return valueForRole(*game, role);
132QVariant GameListModel::valueForRole(
const Game& game,
int role)
const
136 case Qt::DisplayRole:
137 return QString::fromStdString(game.
name);
139 case Qt::DecorationRole:
141 auto it = icons_.find(game.
id);
142 if (it != icons_.end())
147 case Qt::ToolTipRole:
149 QString tip = QString::fromStdString(game.
name);
155 tip += QStringLiteral(
"\n") +
158 tip += QStringLiteral(
"\n") +
170 case Qt::AccessibleTextRole:
171 return QString::fromStdString(game.
name);
172 case Qt::AccessibleDescriptionRole:
181 parts << QString::fromStdString(game.
name);
183 parts << tr(
"source %1").arg(QString::fromStdString(game.
getSourceName()));
185 parts << tr(
"runner %1").arg(QString::fromStdString(game.
getRunnerName()));
187 parts << (installed ? tr(
"installed") : tr(
"not installed"));
190 const int hours = static_cast<int>(game.statistics.totalPlayTimeSeconds / 3600);
196 parts << tr(
"%n hour(s) played", nullptr, hours);
200 parts << tr(
"favorite");
201 return parts.join(QStringLiteral(
", "));
205 return QString::fromStdString(game.
id);
219 return isRunning_ ? isRunning_(game.
id) : game.isRunning();
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.
GameMetadata metadata
Descriptive information and media.
std::string getPlatformName() const
Get human-readable platform name.
bool isFavorite
Is this marked as a favorite game?
std::string name
Display name shown in UI.
std::string getSourceName() const
Get human-readable source name.
std::string id
Unique identifier (auto-generated or user-specified).
GameStatistics statistics
Play statistics and usage tracking.
uint64_t installSizeBytes
Installation size in bytes.
std::string getRunnerName() const
Get human-readable runner name.
@ GameCloudSaveEnabledRole
@ GameDownloadProgressRole
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.
double totalPlayTimeSeconds
Total play time in seconds (for all sessions).
time_t lastPlayed
Unix timestamp of last play session.
int timesLaunched
Number of times game was launched.