a-la-karte/src/gamemodel.h
Marco Allegretti 58f69e6717 Fix library persistence and duplicate handling
Ensure persistence operates on the full game list, not filtered rows.
Deduplicate imported entries by stable game ID to prevent missing games
and regressions when importing from multiple sources.
2026-01-20 00:13:11 +01:00

110 lines
3.1 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2024 A-La-Karte Contributors
#pragma once
#include <QAbstractListModel>
#include <QQmlEngine>
#include <QSortFilterProxyModel>
#include "game.h"
class GameModel : public QAbstractListModel
{
Q_OBJECT
QML_ELEMENT
Q_PROPERTY(int count READ count NOTIFY countChanged)
Q_PROPERTY(QString filterText READ filterText WRITE setFilterText NOTIFY filterTextChanged)
Q_PROPERTY(QString filterPlatform READ filterPlatform WRITE setFilterPlatform NOTIFY filterPlatformChanged)
Q_PROPERTY(bool showHidden READ showHidden WRITE setShowHidden NOTIFY showHiddenChanged)
Q_PROPERTY(bool favoritesOnly READ favoritesOnly WRITE setFavoritesOnly NOTIFY favoritesOnlyChanged)
Q_PROPERTY(SortMode sortMode READ sortMode WRITE setSortMode NOTIFY sortModeChanged)
public:
enum Roles {
IdRole = Qt::UserRole + 1,
NameRole,
DescriptionRole,
DeveloperRole,
PublisherRole,
CoverUrlRole,
IconUrlRole,
LaunchCommandRole,
PlatformRole,
PlatformIdRole,
LastPlayedRole,
PlayTimeRole,
FavoriteRole,
HiddenRole,
InstalledRole,
RunningRole,
GameObjectRole
};
Q_ENUM(Roles)
enum SortMode {
SortByName,
SortByLastPlayed,
SortByPlayTime,
SortByPlatform,
SortByRecent
};
Q_ENUM(SortMode)
explicit GameModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
QHash<int, QByteArray> roleNames() const override;
int count() const;
QString filterText() const;
void setFilterText(const QString &text);
QString filterPlatform() const;
void setFilterPlatform(const QString &platform);
bool showHidden() const;
void setShowHidden(bool show);
bool favoritesOnly() const;
void setFavoritesOnly(bool favorites);
SortMode sortMode() const;
void setSortMode(SortMode mode);
Q_INVOKABLE void addGame(Game *game);
Q_INVOKABLE void removeGame(const QString &id);
Q_INVOKABLE Game *gameAt(int index) const;
Q_INVOKABLE Game *gameById(const QString &id) const;
Q_INVOKABLE void clear();
Q_INVOKABLE QStringList platforms() const;
QList<Game *> allGames() const;
Q_SIGNALS:
void countChanged();
void filterTextChanged();
void filterPlatformChanged();
void showHiddenChanged();
void favoritesOnlyChanged();
void sortModeChanged();
private:
QList<Game *> m_games;
QList<Game *> m_filteredGames;
QString m_filterText;
QString m_filterPlatform;
bool m_showHidden = false;
bool m_favoritesOnly = false;
SortMode m_sortMode = SortByName;
void applyFilter();
void applySort();
bool matchesFilter(Game *game) const;
};