Merge pull request #9620 from Dentomologist/convert_gamelist_col_to_enum_class

Convert GameList col enum to enum class
This commit is contained in:
Léo Lam 2021-04-06 12:08:15 +02:00 committed by GitHub
commit a1ee6ace71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 129 additions and 113 deletions

View File

@ -145,34 +145,42 @@ void GameList::MakeListView()
connect(hor_header, &QHeaderView::sectionResized, this, &GameList::OnSectionResized);
if (!Settings::GetQSettings().contains(QStringLiteral("tableheader/state")))
m_list->sortByColumn(GameListModel::COL_TITLE, Qt::AscendingOrder);
m_list->sortByColumn(static_cast<int>(GameListModel::Column::Title), Qt::AscendingOrder);
hor_header->setSectionResizeMode(GameListModel::COL_PLATFORM, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_BANNER, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_TITLE, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_DESCRIPTION, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_MAKER, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_ID, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_COUNTRY, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_SIZE, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_FILE_NAME, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_FILE_PATH, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_FILE_FORMAT, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_BLOCK_SIZE, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_COMPRESSION, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_TAGS, QHeaderView::Interactive);
const auto SetResizeMode = [&hor_header](const GameListModel::Column column,
const QHeaderView::ResizeMode mode) {
hor_header->setSectionResizeMode(static_cast<int>(column), mode);
};
{
using Column = GameListModel::Column;
using Mode = QHeaderView::ResizeMode;
SetResizeMode(Column::Platform, Mode::Fixed);
SetResizeMode(Column::Banner, Mode::Fixed);
SetResizeMode(Column::Title, Mode::Interactive);
SetResizeMode(Column::Description, Mode::Interactive);
SetResizeMode(Column::Maker, Mode::Interactive);
SetResizeMode(Column::ID, Mode::Fixed);
SetResizeMode(Column::Country, Mode::Fixed);
SetResizeMode(Column::Size, Mode::Fixed);
SetResizeMode(Column::FileName, Mode::Interactive);
SetResizeMode(Column::FilePath, Mode::Interactive);
SetResizeMode(Column::FileFormat, Mode::Fixed);
SetResizeMode(Column::BlockSize, Mode::Fixed);
SetResizeMode(Column::Compression, Mode::Fixed);
SetResizeMode(Column::Tags, Mode::Interactive);
// Cells have 3 pixels of padding, so the width of these needs to be image width + 6. Banners
// are 96 pixels wide, platform and country icons are 32 pixels wide.
m_list->setColumnWidth(static_cast<int>(Column::Banner), 102);
m_list->setColumnWidth(static_cast<int>(Column::Platform), 38);
m_list->setColumnWidth(static_cast<int>(Column::Country), 38);
m_list->setColumnWidth(static_cast<int>(Column::Size), 85);
m_list->setColumnWidth(static_cast<int>(Column::ID), 70);
}
// There's some odd platform-specific behavior with default minimum section size
hor_header->setMinimumSectionSize(38);
// Cells have 3 pixels of padding, so the width of these needs to be image width + 6. Banners are
// 96 pixels wide, platform and country icons are 32 pixels wide.
m_list->setColumnWidth(GameListModel::COL_BANNER, 102);
m_list->setColumnWidth(GameListModel::COL_PLATFORM, 38);
m_list->setColumnWidth(GameListModel::COL_COUNTRY, 38);
m_list->setColumnWidth(GameListModel::COL_SIZE, 85);
m_list->setColumnWidth(GameListModel::COL_ID, 70);
UpdateColumnVisibility();
m_list->verticalHeader()->hide();
@ -192,26 +200,26 @@ GameList::~GameList()
void GameList::UpdateColumnVisibility()
{
m_list->setColumnHidden(GameListModel::COL_PLATFORM, !SConfig::GetInstance().m_showSystemColumn);
m_list->setColumnHidden(GameListModel::COL_BANNER, !SConfig::GetInstance().m_showBannerColumn);
m_list->setColumnHidden(GameListModel::COL_TITLE, !SConfig::GetInstance().m_showTitleColumn);
m_list->setColumnHidden(GameListModel::COL_DESCRIPTION,
!SConfig::GetInstance().m_showDescriptionColumn);
m_list->setColumnHidden(GameListModel::COL_MAKER, !SConfig::GetInstance().m_showMakerColumn);
m_list->setColumnHidden(GameListModel::COL_ID, !SConfig::GetInstance().m_showIDColumn);
m_list->setColumnHidden(GameListModel::COL_COUNTRY, !SConfig::GetInstance().m_showRegionColumn);
m_list->setColumnHidden(GameListModel::COL_SIZE, !SConfig::GetInstance().m_showSizeColumn);
m_list->setColumnHidden(GameListModel::COL_FILE_NAME,
!SConfig::GetInstance().m_showFileNameColumn);
m_list->setColumnHidden(GameListModel::COL_FILE_PATH,
!SConfig::GetInstance().m_showFilePathColumn);
m_list->setColumnHidden(GameListModel::COL_FILE_FORMAT,
!SConfig::GetInstance().m_showFileFormatColumn);
m_list->setColumnHidden(GameListModel::COL_BLOCK_SIZE,
!SConfig::GetInstance().m_showBlockSizeColumn);
m_list->setColumnHidden(GameListModel::COL_COMPRESSION,
!SConfig::GetInstance().m_showCompressionColumn);
m_list->setColumnHidden(GameListModel::COL_TAGS, !SConfig::GetInstance().m_showTagsColumn);
const auto& config = SConfig::GetInstance();
const auto SetVisiblity = [this](const GameListModel::Column column, const bool is_visible) {
m_list->setColumnHidden(static_cast<int>(column), !is_visible);
};
using Column = GameListModel::Column;
SetVisiblity(Column::Platform, config.m_showSystemColumn);
SetVisiblity(Column::Banner, config.m_showBannerColumn);
SetVisiblity(Column::Title, config.m_showTitleColumn);
SetVisiblity(Column::Description, config.m_showDescriptionColumn);
SetVisiblity(Column::Maker, config.m_showMakerColumn);
SetVisiblity(Column::ID, config.m_showIDColumn);
SetVisiblity(Column::Country, config.m_showRegionColumn);
SetVisiblity(Column::Size, config.m_showSizeColumn);
SetVisiblity(Column::FileName, config.m_showFileNameColumn);
SetVisiblity(Column::FilePath, config.m_showFilePathColumn);
SetVisiblity(Column::FileFormat, config.m_showFileFormatColumn);
SetVisiblity(Column::BlockSize, config.m_showBlockSizeColumn);
SetVisiblity(Column::Compression, config.m_showCompressionColumn);
SetVisiblity(Column::Tags, config.m_showTagsColumn);
}
void GameList::MakeEmptyView()
@ -881,23 +889,25 @@ void GameList::keyPressEvent(QKeyEvent* event)
void GameList::OnColumnVisibilityToggled(const QString& row, bool visible)
{
static const QMap<QString, int> rowname_to_col_index = {
{tr("Platform"), GameListModel::COL_PLATFORM},
{tr("Banner"), GameListModel::COL_BANNER},
{tr("Title"), GameListModel::COL_TITLE},
{tr("Description"), GameListModel::COL_DESCRIPTION},
{tr("Maker"), GameListModel::COL_MAKER},
{tr("File Name"), GameListModel::COL_FILE_NAME},
{tr("File Path"), GameListModel::COL_FILE_PATH},
{tr("Game ID"), GameListModel::COL_ID},
{tr("Region"), GameListModel::COL_COUNTRY},
{tr("File Size"), GameListModel::COL_SIZE},
{tr("File Format"), GameListModel::COL_FILE_FORMAT},
{tr("Block Size"), GameListModel::COL_BLOCK_SIZE},
{tr("Compression"), GameListModel::COL_COMPRESSION},
{tr("Tags"), GameListModel::COL_TAGS}};
using Column = GameListModel::Column;
static const QMap<QString, Column> rowname_to_column = {
{tr("Platform"), Column::Platform},
{tr("Banner"), Column::Banner},
{tr("Title"), Column::Title},
{tr("Description"), Column::Description},
{tr("Maker"), Column::Maker},
{tr("File Name"), Column::FileName},
{tr("File Path"), Column::FilePath},
{tr("Game ID"), Column::ID},
{tr("Region"), Column::Country},
{tr("File Size"), Column::Size},
{tr("File Format"), Column::FileFormat},
{tr("Block Size"), Column::BlockSize},
{tr("Compression"), Column::Compression},
{tr("Tags"), Column::Tags},
};
m_list->setColumnHidden(rowname_to_col_index[row], !visible);
m_list->setColumnHidden(static_cast<int>(rowname_to_column[row]), !visible);
}
void GameList::OnGameListVisibilityChanged()
@ -973,7 +983,7 @@ void GameList::OnHeaderViewChanged()
std::vector<int> candidate_columns;
// Iterate through all columns
for (int i = 0; i < GameListModel::NUM_COLS; i++)
for (int i = 0; i < static_cast<int>(GameListModel::Column::Count); i++)
{
if (m_list->isColumnHidden(i))
continue;

View File

@ -59,21 +59,21 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
const UICommon::GameFile& game = *m_games[index.row()];
switch (index.column())
switch (static_cast<Column>(index.column()))
{
case COL_PLATFORM:
case Column::Platform:
if (role == Qt::DecorationRole)
return Resources::GetPlatform(game.GetPlatform());
if (role == SORT_ROLE)
return static_cast<int>(game.GetPlatform());
break;
case COL_COUNTRY:
case Column::Country:
if (role == Qt::DecorationRole)
return Resources::GetCountry(game.GetCountry());
if (role == SORT_ROLE)
return static_cast<int>(game.GetCountry());
break;
case COL_BANNER:
case Column::Banner:
if (role == Qt::DecorationRole)
{
// GameCube banners are 96x32, but Wii banners are 192x64.
@ -88,7 +88,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
return banner;
}
break;
case COL_TITLE:
case Column::Title:
if (role == Qt::DisplayRole || role == SORT_ROLE)
{
QString name = QString::fromStdString(game.GetName(m_title_database));
@ -124,11 +124,11 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
return name;
}
break;
case COL_ID:
case Column::ID:
if (role == Qt::DisplayRole || role == SORT_ROLE)
return QString::fromStdString(game.GetGameID());
break;
case COL_DESCRIPTION:
case Column::Description:
if (role == Qt::DisplayRole || role == SORT_ROLE)
{
return QString::fromStdString(
@ -136,18 +136,18 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
.replace(QLatin1Char('\n'), QLatin1Char(' '));
}
break;
case COL_MAKER:
case Column::Maker:
if (role == Qt::DisplayRole || role == SORT_ROLE)
{
return QString::fromStdString(
game.GetMaker(UICommon::GameFile::Variant::LongAndPossiblyCustom));
}
break;
case COL_FILE_NAME:
case Column::FileName:
if (role == Qt::DisplayRole || role == SORT_ROLE)
return QString::fromStdString(game.GetFileName());
break;
case COL_FILE_PATH:
case Column::FilePath:
if (role == Qt::DisplayRole || role == SORT_ROLE)
{
QString file_path = QDir::toNativeSeparators(
@ -157,7 +157,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
return file_path;
}
break;
case COL_SIZE:
case Column::Size:
if (role == Qt::DisplayRole)
{
std::string str = UICommon::FormatSize(game.GetFileSize());
@ -171,24 +171,24 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
if (role == SORT_ROLE)
return static_cast<quint64>(game.GetFileSize());
break;
case COL_FILE_FORMAT:
case Column::FileFormat:
if (role == Qt::DisplayRole || role == SORT_ROLE)
return QString::fromStdString(game.GetFileFormatName());
break;
case COL_BLOCK_SIZE:
case Column::BlockSize:
if (role == Qt::DisplayRole)
return QString::fromStdString(UICommon::FormatSize(game.GetBlockSize()));
if (role == SORT_ROLE)
return static_cast<quint64>(game.GetBlockSize());
break;
case COL_COMPRESSION:
case Column::Compression:
if (role == Qt::DisplayRole || role == SORT_ROLE)
{
const QString compression = QString::fromStdString(game.GetCompressionMethod());
return compression.isEmpty() ? tr("No Compression") : compression;
}
break;
case COL_TAGS:
case Column::Tags:
if (role == Qt::DisplayRole || role == SORT_ROLE)
{
auto tags = GetGameTags(game.GetFilePath());
@ -206,31 +206,31 @@ QVariant GameListModel::headerData(int section, Qt::Orientation orientation, int
if (orientation == Qt::Vertical || role != Qt::DisplayRole)
return QVariant();
switch (section)
switch (static_cast<Column>(section))
{
case COL_TITLE:
case Column::Title:
return tr("Title");
case COL_ID:
case Column::ID:
return tr("ID");
case COL_BANNER:
case Column::Banner:
return tr("Banner");
case COL_DESCRIPTION:
case Column::Description:
return tr("Description");
case COL_MAKER:
case Column::Maker:
return tr("Maker");
case COL_FILE_NAME:
case Column::FileName:
return tr("File Name");
case COL_FILE_PATH:
case Column::FilePath:
return tr("File Path");
case COL_SIZE:
case Column::Size:
return tr("Size");
case COL_FILE_FORMAT:
case Column::FileFormat:
return tr("File Format");
case COL_BLOCK_SIZE:
case Column::BlockSize:
return tr("Block Size");
case COL_COMPRESSION:
case Column::Compression:
return tr("Compression");
case COL_TAGS:
case Column::Tags:
return tr("Tags");
}
return QVariant();
@ -247,7 +247,7 @@ int GameListModel::columnCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return NUM_COLS;
return static_cast<int>(Column::Count);
}
bool GameListModel::ShouldDisplayGameListItem(int index) const

View File

@ -44,23 +44,23 @@ public:
// Using a custom sort role as it sometimes differs slightly from the default Qt::DisplayRole.
static constexpr int SORT_ROLE = Qt::UserRole;
enum
enum class Column
{
COL_PLATFORM = 0,
COL_BANNER,
COL_TITLE,
COL_DESCRIPTION,
COL_MAKER,
COL_ID,
COL_COUNTRY,
COL_SIZE,
COL_FILE_NAME,
COL_FILE_PATH,
COL_FILE_FORMAT,
COL_BLOCK_SIZE,
COL_COMPRESSION,
COL_TAGS,
NUM_COLS
Platform = 0,
Banner,
Title,
Description,
Maker,
ID,
Country,
Size,
FileName,
FilePath,
FileFormat,
BlockSize,
Compression,
Tags,
Count,
};
void AddGame(const std::shared_ptr<const UICommon::GameFile>& game);

View File

@ -20,7 +20,7 @@ const QSize LARGE_BANNER_SIZE(144, 48);
GridProxyModel::GridProxyModel(QObject* parent) : QSortFilterProxyModel(parent)
{
setSortCaseSensitivity(Qt::CaseInsensitive);
sort(GameListModel::COL_TITLE);
sort(static_cast<int>(GameListModel::Column::Title));
}
QVariant GridProxyModel::data(const QModelIndex& i, int role) const
@ -28,8 +28,9 @@ QVariant GridProxyModel::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);
return sourceModel()->data(
sourceModel()->index(source_index.row(), static_cast<int>(GameListModel::Column::Title)),
Qt::DisplayRole);
}
else if (role == Qt::DecorationRole)
{
@ -43,7 +44,8 @@ QVariant GridProxyModel::data(const QModelIndex& i, int role) const
if (buffer.empty() || !Config::Get(Config::MAIN_USE_GAME_COVERS))
{
QPixmap banner = model
->data(model->index(source_index.row(), GameListModel::COL_BANNER),
->data(model->index(source_index.row(),
static_cast<int>(GameListModel::Column::Banner)),
Qt::DecorationRole)
.value<QPixmap>();

View File

@ -22,10 +22,14 @@ bool ListProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right)
return QSortFilterProxyModel::lessThan(left, right);
// If two items are otherwise equal, compare them by their title
const auto right_title =
sourceModel()->index(right.row(), GameListModel::COL_TITLE).data().toString();
const auto left_title =
sourceModel()->index(left.row(), GameListModel::COL_TITLE).data().toString();
const auto right_title = sourceModel()
->index(right.row(), static_cast<int>(GameListModel::Column::Title))
.data()
.toString();
const auto left_title = sourceModel()
->index(left.row(), static_cast<int>(GameListModel::Column::Title))
.data()
.toString();
if (sortOrder() == Qt::AscendingOrder)
return left_title < right_title;