Reformat all the things. Have fun with merge conflicts.

This commit is contained in:
Pierre Bourdon
2016-06-24 10:43:46 +02:00
parent 2115e8a4a6
commit 3570c7f03a
1116 changed files with 187405 additions and 180344 deletions

View File

@ -13,193 +13,190 @@
#include "Common/FileUtil.h"
#include "Core/ConfigManager.h"
#include "DiscIO/VolumeCreator.h"
#include "DolphinQt2/GameList/GameFile.h"
#include "DolphinQt2/Resources.h"
#include "DolphinQt2/Settings.h"
#include "DolphinQt2/GameList/GameFile.h"
static const int CACHE_VERSION = 13; // Last changed in PR #3261
static const int CACHE_VERSION = 13; // Last changed in PR #3261
static const int DATASTREAM_VERSION = QDataStream::Qt_5_5;
static QMap<DiscIO::IVolume::ELanguage, QString> ConvertLanguageMap(
const std::map<DiscIO::IVolume::ELanguage, std::string>& map)
static QMap<DiscIO::IVolume::ELanguage, QString>
ConvertLanguageMap(const std::map<DiscIO::IVolume::ELanguage, std::string>& map)
{
QMap<DiscIO::IVolume::ELanguage, QString> result;
for (auto entry : map)
result.insert(entry.first, QString::fromStdString(entry.second).trimmed());
return result;
QMap<DiscIO::IVolume::ELanguage, QString> result;
for (auto entry : map)
result.insert(entry.first, QString::fromStdString(entry.second).trimmed());
return result;
}
GameFile::GameFile(const QString& path) : m_path(path)
{
m_valid = false;
m_valid = false;
if (!LoadFileInfo(path))
return;
if (!LoadFileInfo(path))
return;
if (!TryLoadCache())
{
if (TryLoadVolume())
{
LoadState();
}
else if (!TryLoadElfDol())
{
return;
}
}
if (!TryLoadCache())
{
if (TryLoadVolume())
{
LoadState();
}
else if (!TryLoadElfDol())
{
return;
}
}
m_valid = true;
m_valid = true;
}
QString GameFile::GetCacheFileName() const
{
QString folder = QString::fromStdString(File::GetUserPath(D_CACHE_IDX));
// Append a hash of the full path to prevent name clashes between
// files with the same names in different folders.
QString hash = QString::fromUtf8(
QCryptographicHash::hash(m_path.toUtf8(),
QCryptographicHash::Md5).toHex());
return folder + m_file_name + hash;
QString folder = QString::fromStdString(File::GetUserPath(D_CACHE_IDX));
// Append a hash of the full path to prevent name clashes between
// files with the same names in different folders.
QString hash =
QString::fromUtf8(QCryptographicHash::hash(m_path.toUtf8(), QCryptographicHash::Md5).toHex());
return folder + m_file_name + hash;
}
void GameFile::ReadBanner(const DiscIO::IVolume& volume)
{
int width, height;
std::vector<u32> buffer = volume.GetBanner(&width, &height);
QImage banner(width, height, QImage::Format_RGB888);
for (int i = 0; i < width * height; i++)
{
int x = i % width, y = i / width;
banner.setPixel(x, y, qRgb((buffer[i] & 0xFF0000) >> 16,
(buffer[i] & 0x00FF00) >> 8,
(buffer[i] & 0x0000FF) >> 0));
}
int width, height;
std::vector<u32> buffer = volume.GetBanner(&width, &height);
QImage banner(width, height, QImage::Format_RGB888);
for (int i = 0; i < width * height; i++)
{
int x = i % width, y = i / width;
banner.setPixel(x, y, qRgb((buffer[i] & 0xFF0000) >> 16, (buffer[i] & 0x00FF00) >> 8,
(buffer[i] & 0x0000FF) >> 0));
}
if (!banner.isNull())
m_banner = QPixmap::fromImage(banner);
else
m_banner = Resources::GetMisc(Resources::BANNER_MISSING);
if (!banner.isNull())
m_banner = QPixmap::fromImage(banner);
else
m_banner = Resources::GetMisc(Resources::BANNER_MISSING);
}
bool GameFile::LoadFileInfo(const QString& path)
{
QFileInfo info(path);
if (!info.exists() || !info.isReadable())
return false;
QFileInfo info(path);
if (!info.exists() || !info.isReadable())
return false;
m_file_name = info.fileName();
m_extension = info.suffix();
m_folder = info.dir().dirName();
m_last_modified = info.lastModified();
m_size = info.size();
m_file_name = info.fileName();
m_extension = info.suffix();
m_folder = info.dir().dirName();
m_last_modified = info.lastModified();
m_size = info.size();
return true;
return true;
}
void GameFile::LoadState()
{
IniFile ini = SConfig::LoadGameIni(m_unique_id.toStdString(), m_revision);
std::string issues_temp;
ini.GetIfExists("EmuState", "EmulationStateId", &m_rating);
ini.GetIfExists("EmuState", "EmulationIssues", &issues_temp);
m_issues = QString::fromStdString(issues_temp);
IniFile ini = SConfig::LoadGameIni(m_unique_id.toStdString(), m_revision);
std::string issues_temp;
ini.GetIfExists("EmuState", "EmulationStateId", &m_rating);
ini.GetIfExists("EmuState", "EmulationIssues", &issues_temp);
m_issues = QString::fromStdString(issues_temp);
}
bool GameFile::IsElfOrDol()
{
return m_extension == QStringLiteral("elf") ||
m_extension == QStringLiteral("dol");
return m_extension == QStringLiteral("elf") || m_extension == QStringLiteral("dol");
}
bool GameFile::TryLoadCache()
{
QFile cache(GetCacheFileName());
if (!cache.exists())
return false;
if (!cache.open(QIODevice::ReadOnly))
return false;
if (QFileInfo(cache).lastModified() < m_last_modified)
return false;
QFile cache(GetCacheFileName());
if (!cache.exists())
return false;
if (!cache.open(QIODevice::ReadOnly))
return false;
if (QFileInfo(cache).lastModified() < m_last_modified)
return false;
QDataStream in(&cache);
in.setVersion(DATASTREAM_VERSION);
QDataStream in(&cache);
in.setVersion(DATASTREAM_VERSION);
int cache_version;
in >> cache_version;
if (cache_version != CACHE_VERSION)
return false;
int cache_version;
in >> cache_version;
if (cache_version != CACHE_VERSION)
return false;
return false;
return false;
}
bool GameFile::TryLoadVolume()
{
QSharedPointer<DiscIO::IVolume> volume(
DiscIO::CreateVolumeFromFilename(m_path.toStdString()).release());
if (volume == nullptr)
return false;
QSharedPointer<DiscIO::IVolume> volume(
DiscIO::CreateVolumeFromFilename(m_path.toStdString()).release());
if (volume == nullptr)
return false;
m_unique_id = QString::fromStdString(volume->GetUniqueID());
m_maker_id = QString::fromStdString(volume->GetMakerID());
m_revision = volume->GetRevision();
m_internal_name = QString::fromStdString(volume->GetInternalName());
m_short_names = ConvertLanguageMap(volume->GetNames(false));
m_long_names = ConvertLanguageMap(volume->GetNames(true));
m_descriptions = ConvertLanguageMap(volume->GetDescriptions());
m_company = QString::fromStdString(volume->GetCompany());
m_disc_number = volume->GetDiscNumber();
m_platform = volume->GetVolumeType();
m_country = volume->GetCountry();
m_blob_type = volume->GetBlobType();
m_raw_size = volume->GetRawSize();
m_unique_id = QString::fromStdString(volume->GetUniqueID());
m_maker_id = QString::fromStdString(volume->GetMakerID());
m_revision = volume->GetRevision();
m_internal_name = QString::fromStdString(volume->GetInternalName());
m_short_names = ConvertLanguageMap(volume->GetNames(false));
m_long_names = ConvertLanguageMap(volume->GetNames(true));
m_descriptions = ConvertLanguageMap(volume->GetDescriptions());
m_company = QString::fromStdString(volume->GetCompany());
m_disc_number = volume->GetDiscNumber();
m_platform = volume->GetVolumeType();
m_country = volume->GetCountry();
m_blob_type = volume->GetBlobType();
m_raw_size = volume->GetRawSize();
if (m_company.isEmpty() && m_unique_id.size() >= 6)
m_company = QString::fromStdString(
DiscIO::GetCompanyFromID(m_unique_id.mid(4, 2).toStdString()));
if (m_company.isEmpty() && m_unique_id.size() >= 6)
m_company =
QString::fromStdString(DiscIO::GetCompanyFromID(m_unique_id.mid(4, 2).toStdString()));
ReadBanner(*volume);
ReadBanner(*volume);
SaveCache();
return true;
SaveCache();
return true;
}
bool GameFile::TryLoadElfDol()
{
if (!IsElfOrDol())
return false;
if (!IsElfOrDol())
return false;
m_revision = 0;
m_long_names[DiscIO::IVolume::LANGUAGE_ENGLISH] = m_file_name;
m_platform = DiscIO::IVolume::ELF_DOL;
m_country = DiscIO::IVolume::COUNTRY_UNKNOWN;
m_blob_type = DiscIO::BlobType::DIRECTORY;
m_raw_size = m_size;
m_banner = Resources::GetMisc(Resources::BANNER_MISSING);
m_rating = 0;
m_revision = 0;
m_long_names[DiscIO::IVolume::LANGUAGE_ENGLISH] = m_file_name;
m_platform = DiscIO::IVolume::ELF_DOL;
m_country = DiscIO::IVolume::COUNTRY_UNKNOWN;
m_blob_type = DiscIO::BlobType::DIRECTORY;
m_raw_size = m_size;
m_banner = Resources::GetMisc(Resources::BANNER_MISSING);
m_rating = 0;
return true;
return true;
}
void GameFile::SaveCache()
{
// TODO
// TODO
}
QString GameFile::GetLanguageString(const QMap<DiscIO::IVolume::ELanguage, QString>& m) const
{
// Try the settings language, then English, then just pick one.
if (m.isEmpty())
return QString();
// Try the settings language, then English, then just pick one.
if (m.isEmpty())
return QString();
bool wii = m_platform != DiscIO::IVolume::GAMECUBE_DISC;
DiscIO::IVolume::ELanguage current_lang;
if (wii)
current_lang = Settings().GetWiiSystemLanguage();
else
current_lang = Settings().GetGCSystemLanguage();
bool wii = m_platform != DiscIO::IVolume::GAMECUBE_DISC;
DiscIO::IVolume::ELanguage current_lang;
if (wii)
current_lang = Settings().GetWiiSystemLanguage();
else
current_lang = Settings().GetGCSystemLanguage();
if (m.contains(current_lang))
return m[current_lang];
if (m.contains(DiscIO::IVolume::LANGUAGE_ENGLISH))
return m[DiscIO::IVolume::LANGUAGE_ENGLISH];
return m.first();
if (m.contains(current_lang))
return m[current_lang];
if (m.contains(DiscIO::IVolume::LANGUAGE_ENGLISH))
return m[DiscIO::IVolume::LANGUAGE_ENGLISH];
return m.first();
}

View File

@ -15,87 +15,71 @@
class GameFile final
{
public:
explicit GameFile(const QString& path);
bool IsValid() const { return m_valid; }
// These will be properly initialized before we try to load the file.
QString GetPath() const { return m_path; }
QString GetFileName() const { return m_file_name; }
QString GetExtension() const { return m_extension; }
QString GetFolder() const { return m_folder; }
qint64 GetFileSize() const { return m_size; }
// The rest will not.
QString GetUniqueID() const { return m_unique_id; }
QString GetMakerID() const { return m_maker_id; }
u16 GetRevision() const { return m_revision; }
QString GetInternalName() const { return m_internal_name; }
QString GetCompany() const { return m_company; }
u8 GetDiscNumber() const { return m_disc_number; }
u64 GetRawSize() const { return m_raw_size; }
QPixmap GetBanner() const { return m_banner; }
QString GetIssues() const { return m_issues; }
int GetRating() const { return m_rating; }
DiscIO::IVolume::EPlatform GetPlatform() const { return m_platform; }
DiscIO::IVolume::ECountry GetCountry() const { return m_country; }
DiscIO::BlobType GetBlobType() const { return m_blob_type; }
QString GetShortName() const { return GetLanguageString(m_short_names); }
QString GetShortName(DiscIO::IVolume::ELanguage lang) const
{
return m_short_names[lang];
}
QString GetLongName() const { return GetLanguageString(m_long_names); }
QString GetLongName(DiscIO::IVolume::ELanguage lang) const
{
return m_long_names[lang];
}
QString GetDescription() const { return GetLanguageString(m_descriptions); }
QString GetDescription(DiscIO::IVolume::ELanguage lang) const
{
return m_descriptions[lang];
}
explicit GameFile(const QString& path);
bool IsValid() const { return m_valid; }
// These will be properly initialized before we try to load the file.
QString GetPath() const { return m_path; }
QString GetFileName() const { return m_file_name; }
QString GetExtension() const { return m_extension; }
QString GetFolder() const { return m_folder; }
qint64 GetFileSize() const { return m_size; }
// The rest will not.
QString GetUniqueID() const { return m_unique_id; }
QString GetMakerID() const { return m_maker_id; }
u16 GetRevision() const { return m_revision; }
QString GetInternalName() const { return m_internal_name; }
QString GetCompany() const { return m_company; }
u8 GetDiscNumber() const { return m_disc_number; }
u64 GetRawSize() const { return m_raw_size; }
QPixmap GetBanner() const { return m_banner; }
QString GetIssues() const { return m_issues; }
int GetRating() const { return m_rating; }
DiscIO::IVolume::EPlatform GetPlatform() const { return m_platform; }
DiscIO::IVolume::ECountry GetCountry() const { return m_country; }
DiscIO::BlobType GetBlobType() const { return m_blob_type; }
QString GetShortName() const { return GetLanguageString(m_short_names); }
QString GetShortName(DiscIO::IVolume::ELanguage lang) const { return m_short_names[lang]; }
QString GetLongName() const { return GetLanguageString(m_long_names); }
QString GetLongName(DiscIO::IVolume::ELanguage lang) const { return m_long_names[lang]; }
QString GetDescription() const { return GetLanguageString(m_descriptions); }
QString GetDescription(DiscIO::IVolume::ELanguage lang) const { return m_descriptions[lang]; }
private:
DiscIO::IVolume::ELanguage GetDefaultLanguage() const;
QString GetLanguageString(const QMap<DiscIO::IVolume::ELanguage, QString>& m) const;
DiscIO::IVolume::ELanguage GetDefaultLanguage() const;
QString GetLanguageString(const QMap<DiscIO::IVolume::ELanguage, QString>& m) const;
QString GetCacheFileName() const;
void ReadBanner(const DiscIO::IVolume& volume);
bool LoadFileInfo(const QString& path);
void LoadState();
bool IsElfOrDol();
bool TryLoadElfDol();
bool TryLoadCache();
bool TryLoadVolume();
void SaveCache();
QString GetCacheFileName() const;
void ReadBanner(const DiscIO::IVolume& volume);
bool LoadFileInfo(const QString& path);
void LoadState();
bool IsElfOrDol();
bool TryLoadElfDol();
bool TryLoadCache();
bool TryLoadVolume();
void SaveCache();
bool m_valid;
QString m_path;
QString m_file_name;
QString m_extension;
QString m_folder;
QDateTime m_last_modified;
qint64 m_size = 0;
bool m_valid;
QString m_path;
QString m_file_name;
QString m_extension;
QString m_folder;
QDateTime m_last_modified;
qint64 m_size = 0;
QString m_unique_id;
QString m_maker_id;
u16 m_revision = 0;
QString m_internal_name;
QMap<DiscIO::IVolume::ELanguage, QString> m_short_names;
QMap<DiscIO::IVolume::ELanguage, QString> m_long_names;
QMap<DiscIO::IVolume::ELanguage, QString> m_descriptions;
QString m_company;
u8 m_disc_number = 0;
DiscIO::IVolume::EPlatform m_platform;
DiscIO::IVolume::ECountry m_country;
DiscIO::BlobType m_blob_type;
u64 m_raw_size = 0;
QPixmap m_banner;
QString m_issues;
int m_rating = 0;
QString m_unique_id;
QString m_maker_id;
u16 m_revision = 0;
QString m_internal_name;
QMap<DiscIO::IVolume::ELanguage, QString> m_short_names;
QMap<DiscIO::IVolume::ELanguage, QString> m_long_names;
QMap<DiscIO::IVolume::ELanguage, QString> m_descriptions;
QString m_company;
u8 m_disc_number = 0;
DiscIO::IVolume::EPlatform m_platform;
DiscIO::IVolume::ECountry m_country;
DiscIO::BlobType m_blob_type;
u64 m_raw_size = 0;
QPixmap m_banner;
QString m_issues;
int m_rating = 0;
};

View File

@ -8,167 +8,167 @@
#include <QMenu>
#include <QUrl>
#include "DolphinQt2/Settings.h"
#include "DolphinQt2/GameList/GameList.h"
#include "DolphinQt2/GameList/ListProxyModel.h"
#include "DolphinQt2/GameList/TableDelegate.h"
#include "DolphinQt2/Settings.h"
GameList::GameList(QWidget* parent): QStackedWidget(parent)
GameList::GameList(QWidget* parent) : QStackedWidget(parent)
{
m_model = new GameListModel(this);
m_table_proxy = new QSortFilterProxyModel(this);
m_table_proxy->setSourceModel(m_model);
m_list_proxy = new ListProxyModel(this);
m_list_proxy->setSourceModel(m_model);
m_model = new GameListModel(this);
m_table_proxy = new QSortFilterProxyModel(this);
m_table_proxy->setSourceModel(m_model);
m_list_proxy = new ListProxyModel(this);
m_list_proxy->setSourceModel(m_model);
m_delegate = new TableDelegate(this);
m_delegate = new TableDelegate(this);
MakeTableView();
MakeListView();
MakeEmptyView();
MakeTableView();
MakeListView();
MakeEmptyView();
connect(m_table, &QTableView::doubleClicked, this, &GameList::GameSelected);
connect(m_list, &QListView::doubleClicked, this, &GameList::GameSelected);
connect(this, &GameList::DirectoryAdded, m_model, &GameListModel::DirectoryAdded);
connect(this, &GameList::DirectoryRemoved, m_model, &GameListModel::DirectoryRemoved);
connect(m_model, &QAbstractItemModel::rowsInserted, this, &GameList::ConsiderViewChange);
connect(m_model, &QAbstractItemModel::rowsRemoved, this, &GameList::ConsiderViewChange);
connect(m_table, &QTableView::doubleClicked, this, &GameList::GameSelected);
connect(m_list, &QListView::doubleClicked, this, &GameList::GameSelected);
connect(this, &GameList::DirectoryAdded, m_model, &GameListModel::DirectoryAdded);
connect(this, &GameList::DirectoryRemoved, m_model, &GameListModel::DirectoryRemoved);
connect(m_model, &QAbstractItemModel::rowsInserted, this, &GameList::ConsiderViewChange);
connect(m_model, &QAbstractItemModel::rowsRemoved, this, &GameList::ConsiderViewChange);
addWidget(m_table);
addWidget(m_list);
addWidget(m_empty);
m_prefer_table = Settings().GetPreferredView();
ConsiderViewChange();
addWidget(m_table);
addWidget(m_list);
addWidget(m_empty);
m_prefer_table = Settings().GetPreferredView();
ConsiderViewChange();
}
void GameList::MakeTableView()
{
m_table = new QTableView(this);
m_table->setModel(m_table_proxy);
m_table->setItemDelegate(m_delegate);
m_table->setSelectionMode(QAbstractItemView::SingleSelection);
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setAlternatingRowColors(true);
m_table->setShowGrid(false);
m_table->setSortingEnabled(true);
m_table->setCurrentIndex(QModelIndex());
m_table->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_table, &QTableView::customContextMenuRequested, this, &GameList::ShowContextMenu);
m_table = new QTableView(this);
m_table->setModel(m_table_proxy);
m_table->setItemDelegate(m_delegate);
m_table->setSelectionMode(QAbstractItemView::SingleSelection);
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setAlternatingRowColors(true);
m_table->setShowGrid(false);
m_table->setSortingEnabled(true);
m_table->setCurrentIndex(QModelIndex());
m_table->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_table, &QTableView::customContextMenuRequested, this, &GameList::ShowContextMenu);
// TODO load from config
m_table->setColumnHidden(GameListModel::COL_PLATFORM, false);
m_table->setColumnHidden(GameListModel::COL_ID, true);
m_table->setColumnHidden(GameListModel::COL_BANNER, false);
m_table->setColumnHidden(GameListModel::COL_TITLE, false);
m_table->setColumnHidden(GameListModel::COL_DESCRIPTION, true);
m_table->setColumnHidden(GameListModel::COL_MAKER, false);
m_table->setColumnHidden(GameListModel::COL_SIZE, false);
m_table->setColumnHidden(GameListModel::COL_COUNTRY, false);
m_table->setColumnHidden(GameListModel::COL_RATING, false);
// TODO load from config
m_table->setColumnHidden(GameListModel::COL_PLATFORM, false);
m_table->setColumnHidden(GameListModel::COL_ID, true);
m_table->setColumnHidden(GameListModel::COL_BANNER, false);
m_table->setColumnHidden(GameListModel::COL_TITLE, false);
m_table->setColumnHidden(GameListModel::COL_DESCRIPTION, true);
m_table->setColumnHidden(GameListModel::COL_MAKER, false);
m_table->setColumnHidden(GameListModel::COL_SIZE, false);
m_table->setColumnHidden(GameListModel::COL_COUNTRY, false);
m_table->setColumnHidden(GameListModel::COL_RATING, false);
QHeaderView* hor_header = m_table->horizontalHeader();
hor_header->setSectionResizeMode(GameListModel::COL_PLATFORM, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_COUNTRY, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_ID, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_BANNER, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_TITLE, QHeaderView::Stretch);
hor_header->setSectionResizeMode(GameListModel::COL_MAKER, QHeaderView::Stretch);
hor_header->setSectionResizeMode(GameListModel::COL_SIZE, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_DESCRIPTION, QHeaderView::Stretch);
hor_header->setSectionResizeMode(GameListModel::COL_RATING, QHeaderView::ResizeToContents);
QHeaderView* hor_header = m_table->horizontalHeader();
hor_header->setSectionResizeMode(GameListModel::COL_PLATFORM, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_COUNTRY, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_ID, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_BANNER, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_TITLE, QHeaderView::Stretch);
hor_header->setSectionResizeMode(GameListModel::COL_MAKER, QHeaderView::Stretch);
hor_header->setSectionResizeMode(GameListModel::COL_SIZE, QHeaderView::ResizeToContents);
hor_header->setSectionResizeMode(GameListModel::COL_DESCRIPTION, QHeaderView::Stretch);
hor_header->setSectionResizeMode(GameListModel::COL_RATING, QHeaderView::ResizeToContents);
QHeaderView* ver_header = m_table->verticalHeader();
ver_header->setSectionResizeMode(QHeaderView::ResizeToContents);
QHeaderView* ver_header = m_table->verticalHeader();
ver_header->setSectionResizeMode(QHeaderView::ResizeToContents);
}
void GameList::MakeEmptyView()
{
m_empty = new QLabel(this);
m_empty->setText(tr("Dolphin did not find any game files.\n"
"Open the Paths dialog to add game folders."));
m_empty->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
m_empty = new QLabel(this);
m_empty->setText(tr("Dolphin did not find any game files.\n"
"Open the Paths dialog to add game folders."));
m_empty->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
}
void GameList::MakeListView()
{
m_list = new QListView(this);
m_list->setModel(m_list_proxy);
m_list->setViewMode(QListView::IconMode);
m_list->setResizeMode(QListView::Adjust);
m_list->setUniformItemSizes(true);
m_list->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_list, &QTableView::customContextMenuRequested, this, &GameList::ShowContextMenu);
m_list = new QListView(this);
m_list->setModel(m_list_proxy);
m_list->setViewMode(QListView::IconMode);
m_list->setResizeMode(QListView::Adjust);
m_list->setUniformItemSizes(true);
m_list->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_list, &QTableView::customContextMenuRequested, this, &GameList::ShowContextMenu);
}
void GameList::ShowContextMenu(const QPoint&)
{
QMenu* menu = new QMenu(this);
menu->addAction(tr("Properties"));
menu->addAction(tr("Open Wiki Page"), this, SLOT(OpenWiki()));
menu->addAction(tr("Set as Default ISO"), this, SLOT(SetDefaultISO()));
menu->exec(QCursor::pos());
QMenu* menu = new QMenu(this);
menu->addAction(tr("Properties"));
menu->addAction(tr("Open Wiki Page"), this, SLOT(OpenWiki()));
menu->addAction(tr("Set as Default ISO"), this, SLOT(SetDefaultISO()));
menu->exec(QCursor::pos());
}
void GameList::OpenWiki()
{
QString game_id = GameFile(GetSelectedGame()).GetUniqueID();
QString url = QStringLiteral("https://wiki.dolphin-emu.org/index.php?title=").append(game_id);
QDesktopServices::openUrl(QUrl(url));
QString game_id = GameFile(GetSelectedGame()).GetUniqueID();
QString url = QStringLiteral("https://wiki.dolphin-emu.org/index.php?title=").append(game_id);
QDesktopServices::openUrl(QUrl(url));
}
void GameList::SetDefaultISO()
{
Settings().SetDefaultGame(GetSelectedGame());
Settings().SetDefaultGame(GetSelectedGame());
}
QString GameList::GetSelectedGame() const
{
QAbstractItemView* view;
QSortFilterProxyModel* proxy;
if (currentWidget() == m_table)
{
view = m_table;
proxy = m_table_proxy;
}
else
{
view = m_list;
proxy = m_list_proxy;
}
QItemSelectionModel* sel_model = view->selectionModel();
if (sel_model->hasSelection())
{
QModelIndex model_index = proxy->mapToSource(sel_model->selectedIndexes()[0]);
return m_model->GetPath(model_index.row());
}
return QStringLiteral("");
QAbstractItemView* view;
QSortFilterProxyModel* proxy;
if (currentWidget() == m_table)
{
view = m_table;
proxy = m_table_proxy;
}
else
{
view = m_list;
proxy = m_list_proxy;
}
QItemSelectionModel* sel_model = view->selectionModel();
if (sel_model->hasSelection())
{
QModelIndex model_index = proxy->mapToSource(sel_model->selectedIndexes()[0]);
return m_model->GetPath(model_index.row());
}
return QStringLiteral("");
}
void GameList::SetPreferredView(bool table)
{
m_prefer_table = table;
Settings().SetPreferredView(table);
ConsiderViewChange();
m_prefer_table = table;
Settings().SetPreferredView(table);
ConsiderViewChange();
}
void GameList::ConsiderViewChange()
{
if (m_model->rowCount(QModelIndex()) > 0)
{
if (m_prefer_table)
setCurrentWidget(m_table);
else
setCurrentWidget(m_list);
}
else
{
setCurrentWidget(m_empty);
}
if (m_model->rowCount(QModelIndex()) > 0)
{
if (m_prefer_table)
setCurrentWidget(m_table);
else
setCurrentWidget(m_list);
}
else
{
setCurrentWidget(m_empty);
}
}
void GameList::keyReleaseEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Return)
emit GameSelected();
else
QStackedWidget::keyReleaseEvent(event);
if (event->key() == Qt::Key_Return)
emit GameSelected();
else
QStackedWidget::keyReleaseEvent(event);
}

View File

@ -17,44 +17,44 @@ class TableDelegate;
class GameList final : public QStackedWidget
{
Q_OBJECT
Q_OBJECT
public:
explicit GameList(QWidget* parent = nullptr);
QString GetSelectedGame() const;
explicit GameList(QWidget* parent = nullptr);
QString GetSelectedGame() const;
public slots:
void SetTableView() { SetPreferredView(true); }
void SetListView() { SetPreferredView(false); }
void SetViewColumn(int col, bool view) { m_table->setColumnHidden(col, !view); }
void SetTableView() { SetPreferredView(true); }
void SetListView() { SetPreferredView(false); }
void SetViewColumn(int col, bool view) { m_table->setColumnHidden(col, !view); }
private slots:
void ShowContextMenu(const QPoint&);
void OpenWiki();
void SetDefaultISO();
void ShowContextMenu(const QPoint&);
void OpenWiki();
void SetDefaultISO();
signals:
void GameSelected();
void DirectoryAdded(const QString& dir);
void DirectoryRemoved(const QString& dir);
void GameSelected();
void DirectoryAdded(const QString& dir);
void DirectoryRemoved(const QString& dir);
private:
void MakeTableView();
void MakeListView();
void MakeEmptyView();
// We only have two views, just use a bool to distinguish.
void SetPreferredView(bool table);
void ConsiderViewChange();
void MakeTableView();
void MakeListView();
void MakeEmptyView();
// We only have two views, just use a bool to distinguish.
void SetPreferredView(bool table);
void ConsiderViewChange();
GameListModel* m_model;
TableDelegate* m_delegate;
QSortFilterProxyModel* m_table_proxy;
QSortFilterProxyModel* m_list_proxy;
GameListModel* m_model;
TableDelegate* m_delegate;
QSortFilterProxyModel* m_table_proxy;
QSortFilterProxyModel* m_list_proxy;
QListView* m_list;
QTableView* m_table;
QLabel* m_empty;
bool m_prefer_table;
QListView* m_list;
QTableView* m_table;
QLabel* m_empty;
bool m_prefer_table;
protected:
void keyReleaseEvent(QKeyEvent* event) override;
void keyReleaseEvent(QKeyEvent* event) override;
};

View File

@ -2,102 +2,117 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "DolphinQt2/Resources.h"
#include "DolphinQt2/GameList/GameListModel.h"
#include "DolphinQt2/Resources.h"
GameListModel::GameListModel(QObject* parent)
: QAbstractTableModel(parent)
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);
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();
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();
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();
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();
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();
if (parent.isValid())
return 0;
return m_games.size();
}
int GameListModel::columnCount(const QModelIndex& parent) const
{
return NUM_COLS;
return NUM_COLS;
}
void GameListModel::UpdateGame(QSharedPointer<GameFile> game)
{
QString path = game->GetPath();
QString path = game->GetPath();
int entry = FindGame(path);
if (entry < 0)
entry = m_games.size();
int entry = FindGame(path);
if (entry < 0)
entry = m_games.size();
beginInsertRows(QModelIndex(), entry, entry);
m_games.insert(entry, game);
endInsertRows();
beginInsertRows(QModelIndex(), entry, entry);
m_games.insert(entry, game);
endInsertRows();
}
void GameListModel::RemoveGame(const QString& path)
{
int entry = FindGame(path);
if (entry < 0)
return;
int entry = FindGame(path);
if (entry < 0)
return;
beginRemoveRows(QModelIndex(), entry, entry);
m_games.removeAt(entry);
endRemoveRows();
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;
for (int i = 0; i < m_games.size(); i++)
{
if (m_games[i]->GetPath() == path)
return i;
}
return -1;
}

View File

@ -12,46 +12,46 @@
class GameListModel final : public QAbstractTableModel
{
Q_OBJECT
Q_OBJECT
public:
explicit GameListModel(QObject* parent = nullptr);
explicit GameListModel(QObject* parent = nullptr);
// Qt's Model/View stuff uses these overrides.
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex& parent) const override;
int columnCount(const QModelIndex& parent) const override;
// Qt's Model/View stuff uses these overrides.
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex& parent) const override;
int columnCount(const QModelIndex& parent) const override;
// Path of the Game at the specified index.
QString GetPath(int index) const { return m_games[index]->GetPath(); }
enum
{
COL_PLATFORM = 0,
COL_ID,
COL_BANNER,
COL_TITLE,
COL_DESCRIPTION,
COL_MAKER,
COL_SIZE,
COL_COUNTRY,
COL_RATING,
NUM_COLS
};
// Path of the Game at the specified index.
QString GetPath(int index) const { return m_games[index]->GetPath(); }
enum
{
COL_PLATFORM = 0,
COL_ID,
COL_BANNER,
COL_TITLE,
COL_DESCRIPTION,
COL_MAKER,
COL_SIZE,
COL_COUNTRY,
COL_RATING,
NUM_COLS
};
public slots:
void UpdateGame(QSharedPointer<GameFile> game);
void RemoveGame(const QString& path);
void UpdateGame(QSharedPointer<GameFile> game);
void RemoveGame(const QString& path);
signals:
void DirectoryAdded(const QString& dir);
void DirectoryRemoved(const QString& dir);
void DirectoryAdded(const QString& dir);
void DirectoryRemoved(const QString& dir);
private:
// Index in m_games, or -1 if it isn't found
int FindGame(const QString& path) const;
// Index in m_games, or -1 if it isn't found
int FindGame(const QString& path) const;
GameTracker m_tracker;
QList<QSharedPointer<GameFile>> m_games;
GameTracker m_tracker;
QList<QSharedPointer<GameFile>> m_games;
};

View File

@ -6,101 +6,94 @@
#include <QDirIterator>
#include <QFile>
#include "DolphinQt2/Settings.h"
#include "DolphinQt2/GameList/GameTracker.h"
#include "DolphinQt2/Settings.h"
static const QStringList game_filters{
QStringLiteral("*.gcm"),
QStringLiteral("*.iso"),
QStringLiteral("*.ciso"),
QStringLiteral("*.gcz"),
QStringLiteral("*.wbfs"),
QStringLiteral("*.wad"),
QStringLiteral("*.elf"),
QStringLiteral("*.dol")
};
static const QStringList game_filters{QStringLiteral("*.gcm"), QStringLiteral("*.iso"),
QStringLiteral("*.ciso"), QStringLiteral("*.gcz"),
QStringLiteral("*.wbfs"), QStringLiteral("*.wad"),
QStringLiteral("*.elf"), QStringLiteral("*.dol")};
GameTracker::GameTracker(QObject* parent)
: QFileSystemWatcher(parent)
GameTracker::GameTracker(QObject* parent) : QFileSystemWatcher(parent)
{
m_loader = new GameLoader;
m_loader->moveToThread(&m_loader_thread);
m_loader = new GameLoader;
m_loader->moveToThread(&m_loader_thread);
qRegisterMetaType<QSharedPointer<GameFile>>();
connect(&m_loader_thread, &QThread::finished, m_loader, &QObject::deleteLater);
connect(this, &QFileSystemWatcher::directoryChanged, this, &GameTracker::UpdateDirectory);
connect(this, &QFileSystemWatcher::fileChanged, this, &GameTracker::UpdateFile);
connect(this, &GameTracker::PathChanged, m_loader, &GameLoader::LoadGame);
connect(m_loader, &GameLoader::GameLoaded, this, &GameTracker::GameLoaded);
qRegisterMetaType<QSharedPointer<GameFile>>();
connect(&m_loader_thread, &QThread::finished, m_loader, &QObject::deleteLater);
connect(this, &QFileSystemWatcher::directoryChanged, this, &GameTracker::UpdateDirectory);
connect(this, &QFileSystemWatcher::fileChanged, this, &GameTracker::UpdateFile);
connect(this, &GameTracker::PathChanged, m_loader, &GameLoader::LoadGame);
connect(m_loader, &GameLoader::GameLoaded, this, &GameTracker::GameLoaded);
m_loader_thread.start();
m_loader_thread.start();
for (QString dir : Settings().GetPaths())
AddDirectory(dir);
for (QString dir : Settings().GetPaths())
AddDirectory(dir);
}
GameTracker::~GameTracker()
{
m_loader_thread.quit();
m_loader_thread.wait();
m_loader_thread.quit();
m_loader_thread.wait();
}
void GameTracker::AddDirectory(const QString& dir)
{
if (!QFileInfo(dir).exists())
return;
addPath(dir);
UpdateDirectory(dir);
if (!QFileInfo(dir).exists())
return;
addPath(dir);
UpdateDirectory(dir);
}
void GameTracker::RemoveDirectory(const QString& dir)
{
removePath(dir);
QDirIterator it(dir, game_filters, QDir::NoFilter, QDirIterator::Subdirectories);
while (it.hasNext())
{
QString path = QFileInfo(it.next()).canonicalFilePath();
if (m_tracked_files.contains(path))
{
m_tracked_files[path]--;
if (m_tracked_files[path] == 0)
{
removePath(path);
m_tracked_files.remove(path);
emit GameRemoved(path);
}
}
}
removePath(dir);
QDirIterator it(dir, game_filters, QDir::NoFilter, QDirIterator::Subdirectories);
while (it.hasNext())
{
QString path = QFileInfo(it.next()).canonicalFilePath();
if (m_tracked_files.contains(path))
{
m_tracked_files[path]--;
if (m_tracked_files[path] == 0)
{
removePath(path);
m_tracked_files.remove(path);
emit GameRemoved(path);
}
}
}
}
void GameTracker::UpdateDirectory(const QString& dir)
{
QDirIterator it(dir, game_filters, QDir::NoFilter, QDirIterator::Subdirectories);
while (it.hasNext())
{
QString path = QFileInfo(it.next()).canonicalFilePath();
if (m_tracked_files.contains(path))
{
m_tracked_files[path]++;
}
else
{
addPath(path);
m_tracked_files[path] = 1;
emit PathChanged(path);
}
}
QDirIterator it(dir, game_filters, QDir::NoFilter, QDirIterator::Subdirectories);
while (it.hasNext())
{
QString path = QFileInfo(it.next()).canonicalFilePath();
if (m_tracked_files.contains(path))
{
m_tracked_files[path]++;
}
else
{
addPath(path);
m_tracked_files[path] = 1;
emit PathChanged(path);
}
}
}
void GameTracker::UpdateFile(const QString& file)
{
if (QFileInfo(file).exists())
{
emit PathChanged(file);
}
else if (removePath(file))
{
m_tracked_files.remove(file);
emit GameRemoved(file);
}
if (QFileInfo(file).exists())
{
emit PathChanged(file);
}
else if (removePath(file))
{
m_tracked_files.remove(file);
emit GameRemoved(file);
}
}

View File

@ -23,46 +23,46 @@ class GameLoader;
// private.
class GameTracker final : public QFileSystemWatcher
{
Q_OBJECT
Q_OBJECT
public:
explicit GameTracker(QObject* parent = nullptr);
~GameTracker();
explicit GameTracker(QObject* parent = nullptr);
~GameTracker();
public slots:
void AddDirectory(const QString& dir);
void RemoveDirectory(const QString& dir);
void AddDirectory(const QString& dir);
void RemoveDirectory(const QString& dir);
signals:
void GameLoaded(QSharedPointer<GameFile> game);
void GameRemoved(const QString& path);
void GameLoaded(QSharedPointer<GameFile> game);
void GameRemoved(const QString& path);
void PathChanged(const QString& path);
void PathChanged(const QString& path);
private:
void UpdateDirectory(const QString& dir);
void UpdateFile(const QString& path);
void UpdateDirectory(const QString& dir);
void UpdateFile(const QString& path);
// game path -> number of directories that track it
QMap<QString, int> m_tracked_files;
QThread m_loader_thread;
GameLoader* m_loader;
// game path -> number of directories that track it
QMap<QString, int> m_tracked_files;
QThread m_loader_thread;
GameLoader* m_loader;
};
class GameLoader final : public QObject
{
Q_OBJECT
Q_OBJECT
public slots:
void LoadGame(const QString& path)
{
GameFile* game = new GameFile(path);
if (game->IsValid())
emit GameLoaded(QSharedPointer<GameFile>(game));
}
void LoadGame(const QString& path)
{
GameFile* game = new GameFile(path);
if (game->IsValid())
emit GameLoaded(QSharedPointer<GameFile>(game));
}
signals:
void GameLoaded(QSharedPointer<GameFile> game);
void GameLoaded(QSharedPointer<GameFile> game);
};
Q_DECLARE_METATYPE(QSharedPointer<GameFile>)

View File

@ -9,30 +9,26 @@
static QSize LARGE_BANNER_SIZE(144, 48);
ListProxyModel::ListProxyModel(QObject* parent)
: QSortFilterProxyModel(parent)
ListProxyModel::ListProxyModel(QObject* parent) : QSortFilterProxyModel(parent)
{
setSortCaseSensitivity(Qt::CaseInsensitive);
sort(GameListModel::COL_TITLE);
setSortCaseSensitivity(Qt::CaseInsensitive);
sort(GameListModel::COL_TITLE);
}
QVariant ListProxyModel::data(const QModelIndex& i, int role) const
{
QModelIndex source_index = mapToSource(i);
if (role == Qt::DisplayRole)
{
return sourceModel()->data(
sourceModel()->index(source_index.row(), GameListModel::COL_TITLE),
Qt::DisplayRole);
}
else if (role == Qt::DecorationRole)
{
return sourceModel()->data(
sourceModel()->index(source_index.row(), GameListModel::COL_BANNER),
Qt::DisplayRole).value<QPixmap>().scaled(
LARGE_BANNER_SIZE,
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
}
return QVariant();
QModelIndex source_index = mapToSource(i);
if (role == Qt::DisplayRole)
{
return sourceModel()->data(sourceModel()->index(source_index.row(), GameListModel::COL_TITLE),
Qt::DisplayRole);
}
else if (role == Qt::DecorationRole)
{
return sourceModel()
->data(sourceModel()->index(source_index.row(), GameListModel::COL_BANNER), Qt::DisplayRole)
.value<QPixmap>()
.scaled(LARGE_BANNER_SIZE, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
return QVariant();
}

View File

@ -8,9 +8,9 @@
// single-column large icon + name to be displayed in a QListView.
class ListProxyModel final : public QSortFilterProxyModel
{
Q_OBJECT
Q_OBJECT
public:
explicit ListProxyModel(QObject* parent = nullptr);
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
explicit ListProxyModel(QObject* parent = nullptr);
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
};

View File

@ -4,91 +4,87 @@
#include <QPainter>
#include "DolphinQt2/Resources.h"
#include "DolphinQt2/GameList/GameListModel.h"
#include "DolphinQt2/GameList/TableDelegate.h"
#include "DolphinQt2/Resources.h"
static QSize NORMAL_BANNER_SIZE(96, 32);
// Convert an integer size to a friendly string representation.
static QString FormatSize(qint64 size)
{
QStringList units{
QStringLiteral("KB"),
QStringLiteral("MB"),
QStringLiteral("GB"),
QStringLiteral("TB")
};
QStringListIterator i(units);
QString unit = QStringLiteral("B");
double num = (double) size;
while (num > 1024.0 && i.hasNext())
{
unit = i.next();
num /= 1024.0;
}
return QStringLiteral("%1 %2").arg(QString::number(num, 'f', 1)).arg(unit);
QStringList units{QStringLiteral("KB"), QStringLiteral("MB"), QStringLiteral("GB"),
QStringLiteral("TB")};
QStringListIterator i(units);
QString unit = QStringLiteral("B");
double num = (double)size;
while (num > 1024.0 && i.hasNext())
{
unit = i.next();
num /= 1024.0;
}
return QStringLiteral("%1 %2").arg(QString::number(num, 'f', 1)).arg(unit);
}
TableDelegate::TableDelegate(QWidget* parent) : QStyledItemDelegate(parent)
{
}
void TableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
void TableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QVariant data = index.data(Qt::DisplayRole);
switch (index.column())
{
case GameListModel::COL_PLATFORM:
DrawPixmap(painter, option.rect, Resources::GetPlatform(data.toInt()));
break;
case GameListModel::COL_COUNTRY:
DrawPixmap(painter, option.rect, Resources::GetCountry(data.toInt()));
break;
case GameListModel::COL_RATING:
DrawPixmap(painter, option.rect, Resources::GetRating(data.toInt()));
break;
case GameListModel::COL_BANNER:
DrawPixmap(painter, option.rect, data.value<QPixmap>().scaled(
NORMAL_BANNER_SIZE,
Qt::KeepAspectRatio,
Qt::SmoothTransformation));
break;
case GameListModel::COL_SIZE:
painter->drawText(option.rect, Qt::AlignCenter, FormatSize(data.toULongLong()));
break;
// Fall through.
case GameListModel::COL_ID:
case GameListModel::COL_TITLE:
case GameListModel::COL_DESCRIPTION:
case GameListModel::COL_MAKER:
painter->drawText(option.rect, Qt::AlignVCenter, data.toString());
break;
default: break;
}
QVariant data = index.data(Qt::DisplayRole);
switch (index.column())
{
case GameListModel::COL_PLATFORM:
DrawPixmap(painter, option.rect, Resources::GetPlatform(data.toInt()));
break;
case GameListModel::COL_COUNTRY:
DrawPixmap(painter, option.rect, Resources::GetCountry(data.toInt()));
break;
case GameListModel::COL_RATING:
DrawPixmap(painter, option.rect, Resources::GetRating(data.toInt()));
break;
case GameListModel::COL_BANNER:
DrawPixmap(painter, option.rect,
data.value<QPixmap>().scaled(NORMAL_BANNER_SIZE, Qt::KeepAspectRatio,
Qt::SmoothTransformation));
break;
case GameListModel::COL_SIZE:
painter->drawText(option.rect, Qt::AlignCenter, FormatSize(data.toULongLong()));
break;
// Fall through.
case GameListModel::COL_ID:
case GameListModel::COL_TITLE:
case GameListModel::COL_DESCRIPTION:
case GameListModel::COL_MAKER:
painter->drawText(option.rect, Qt::AlignVCenter, data.toString());
break;
default:
break;
}
}
QSize TableDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
switch (index.column())
{
case GameListModel::COL_PLATFORM:
return Resources::GetPlatform(0).size();
case GameListModel::COL_COUNTRY:
return Resources::GetCountry(0).size();
case GameListModel::COL_RATING:
return Resources::GetRating(0).size();
case GameListModel::COL_BANNER:
return NORMAL_BANNER_SIZE;
default: return QSize(0, 0);
}
switch (index.column())
{
case GameListModel::COL_PLATFORM:
return Resources::GetPlatform(0).size();
case GameListModel::COL_COUNTRY:
return Resources::GetCountry(0).size();
case GameListModel::COL_RATING:
return Resources::GetRating(0).size();
case GameListModel::COL_BANNER:
return NORMAL_BANNER_SIZE;
default:
return QSize(0, 0);
}
}
void TableDelegate::DrawPixmap(QPainter* painter, const QRect& rect, const QPixmap& pixmap) const
{
// We don't want to stretch the pixmap out, so center it in the rect.
painter->drawPixmap(
rect.left() + (rect.width() - pixmap.width()) / 2,
rect.top() + (rect.height() - pixmap.height()) / 2,
pixmap);
// We don't want to stretch the pixmap out, so center it in the rect.
painter->drawPixmap(rect.left() + (rect.width() - pixmap.width()) / 2,
rect.top() + (rect.height() - pixmap.height()) / 2, pixmap);
}

View File

@ -8,13 +8,13 @@
class TableDelegate final : public QStyledItemDelegate
{
Q_OBJECT
Q_OBJECT
public:
explicit TableDelegate(QWidget* parent = nullptr);
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
explicit TableDelegate(QWidget* parent = nullptr);
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
private:
void DrawPixmap(QPainter* painter, const QRect& rect, const QPixmap& pixmap) const;
void DrawPixmap(QPainter* painter, const QRect& rect, const QPixmap& pixmap) const;
};