File: GameListModel.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 28,976 kB
  • ctags: 35,666
  • sloc: cpp: 213,139; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (103 lines) | stat: -rw-r--r-- 2,737 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "DolphinQt2/Resources.h"
#include "DolphinQt2/GameList/GameListModel.h"

GameListModel::GameListModel(QObject* parent)
	: QAbstractTableModel(parent)
{
	connect(&m_tracker, &GameTracker::GameLoaded, this, &GameListModel::UpdateGame);
	connect(&m_tracker, &GameTracker::GameRemoved, this, &GameListModel::RemoveGame);
	connect(this, &GameListModel::DirectoryAdded, &m_tracker, &GameTracker::AddDirectory);
	connect(this, &GameListModel::DirectoryRemoved, &m_tracker, &GameTracker::RemoveDirectory);
}

QVariant GameListModel::data(const QModelIndex& index, int role) const
{
	if (!index.isValid())
		return QVariant();

	QSharedPointer<GameFile> game = m_games[index.row()];
	if (role == Qt::DisplayRole)
	{
		switch (index.column())
		{
		case COL_PLATFORM:    return game->GetPlatform();
		case COL_BANNER:      return game->GetBanner();
		case COL_TITLE:       return game->GetLongName();
		case COL_ID:          return game->GetUniqueID();
		case COL_DESCRIPTION: return game->GetDescription();
		case COL_MAKER:       return game->GetCompany();
		case COL_SIZE:        return game->GetFileSize();
		case COL_COUNTRY:     return game->GetCountry();
		case COL_RATING:      return game->GetRating();
		}
	}
	return QVariant();
}

QVariant GameListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (orientation == Qt::Vertical || role != Qt::DisplayRole)
		return QVariant();

	switch (section)
	{
	case COL_TITLE:       return tr("Title");
	case COL_ID:          return tr("ID");
	case COL_BANNER:      return tr("Banner");
	case COL_DESCRIPTION: return tr("Description");
	case COL_MAKER:       return tr("Maker");
	case COL_SIZE:        return tr("Size");
	case COL_RATING:      return tr("Quality");
	}
	return QVariant();
}

int GameListModel::rowCount(const QModelIndex& parent) const
{
	if (parent.isValid())
		return 0;
	return m_games.size();
}

int GameListModel::columnCount(const QModelIndex& parent) const
{
	return NUM_COLS;
}

void GameListModel::UpdateGame(QSharedPointer<GameFile> game)
{
	QString path = game->GetPath();

	int entry = FindGame(path);
	if (entry < 0)
		entry = m_games.size();

	beginInsertRows(QModelIndex(), entry, entry);
	m_games.insert(entry, game);
	endInsertRows();
}

void GameListModel::RemoveGame(const QString& path)
{
	int entry = FindGame(path);
	if (entry < 0)
		return;

	beginRemoveRows(QModelIndex(), entry, entry);
	m_games.removeAt(entry);
	endRemoveRows();
}

int GameListModel::FindGame(const QString& path) const
{
	for (int i = 0; i < m_games.size(); i++)
	{
		if (m_games[i]->GetPath() == path)
			return i;
	}
	return -1;
}