GameList: Convert columns to enum class

This commit is contained in:
Dentomologist 2021-03-31 15:20:45 -07:00
parent a2fa9aab5b
commit b8bd877a41
5 changed files with 114 additions and 96 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::COL_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::COL_PLATFORM, Mode::Fixed);
SetResizeMode(Column::COL_BANNER, Mode::Fixed);
SetResizeMode(Column::COL_TITLE, Mode::Interactive);
SetResizeMode(Column::COL_DESCRIPTION, Mode::Interactive);
SetResizeMode(Column::COL_MAKER, Mode::Interactive);
SetResizeMode(Column::COL_ID, Mode::Fixed);
SetResizeMode(Column::COL_COUNTRY, Mode::Fixed);
SetResizeMode(Column::COL_SIZE, Mode::Fixed);
SetResizeMode(Column::COL_FILE_NAME, Mode::Interactive);
SetResizeMode(Column::COL_FILE_PATH, Mode::Interactive);
SetResizeMode(Column::COL_FILE_FORMAT, Mode::Fixed);
SetResizeMode(Column::COL_BLOCK_SIZE, Mode::Fixed);
SetResizeMode(Column::COL_COMPRESSION, Mode::Fixed);
SetResizeMode(Column::COL_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::COL_BANNER), 102);
m_list->setColumnWidth(static_cast<int>(Column::COL_PLATFORM), 38);
m_list->setColumnWidth(static_cast<int>(Column::COL_COUNTRY), 38);
m_list->setColumnWidth(static_cast<int>(Column::COL_SIZE), 85);
m_list->setColumnWidth(static_cast<int>(Column::COL_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::COL_PLATFORM, config.m_showSystemColumn);
SetVisiblity(Column::COL_BANNER, config.m_showBannerColumn);
SetVisiblity(Column::COL_TITLE, config.m_showTitleColumn);
SetVisiblity(Column::COL_DESCRIPTION, config.m_showDescriptionColumn);
SetVisiblity(Column::COL_MAKER, config.m_showMakerColumn);
SetVisiblity(Column::COL_ID, config.m_showIDColumn);
SetVisiblity(Column::COL_COUNTRY, config.m_showRegionColumn);
SetVisiblity(Column::COL_SIZE, config.m_showSizeColumn);
SetVisiblity(Column::COL_FILE_NAME, config.m_showFileNameColumn);
SetVisiblity(Column::COL_FILE_PATH, config.m_showFilePathColumn);
SetVisiblity(Column::COL_FILE_FORMAT, config.m_showFileFormatColumn);
SetVisiblity(Column::COL_BLOCK_SIZE, config.m_showBlockSizeColumn);
SetVisiblity(Column::COL_COMPRESSION, config.m_showCompressionColumn);
SetVisiblity(Column::COL_TAGS, config.m_showTagsColumn);
}
void GameList::MakeEmptyView()
@ -881,23 +889,24 @@ 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::COL_PLATFORM},
{tr("Banner"), Column::COL_BANNER},
{tr("Title"), Column::COL_TITLE},
{tr("Description"), Column::COL_DESCRIPTION},
{tr("Maker"), Column::COL_MAKER},
{tr("File Name"), Column::COL_FILE_NAME},
{tr("File Path"), Column::COL_FILE_PATH},
{tr("Game ID"), Column::COL_ID},
{tr("Region"), Column::COL_COUNTRY},
{tr("File Size"), Column::COL_SIZE},
{tr("File Format"), Column::COL_FILE_FORMAT},
{tr("Block Size"), Column::COL_BLOCK_SIZE},
{tr("Compression"), Column::COL_COMPRESSION},
{tr("Tags"), Column::COL_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 +982,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::NUM_COLS); 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::COL_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::COL_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::COL_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::COL_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::COL_ID:
if (role == Qt::DisplayRole || role == SORT_ROLE)
return QString::fromStdString(game.GetGameID());
break;
case COL_DESCRIPTION:
case Column::COL_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::COL_MAKER:
if (role == Qt::DisplayRole || role == SORT_ROLE)
{
return QString::fromStdString(
game.GetMaker(UICommon::GameFile::Variant::LongAndPossiblyCustom));
}
break;
case COL_FILE_NAME:
case Column::COL_FILE_NAME:
if (role == Qt::DisplayRole || role == SORT_ROLE)
return QString::fromStdString(game.GetFileName());
break;
case COL_FILE_PATH:
case Column::COL_FILE_PATH:
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::COL_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::COL_FILE_FORMAT:
if (role == Qt::DisplayRole || role == SORT_ROLE)
return QString::fromStdString(game.GetFileFormatName());
break;
case COL_BLOCK_SIZE:
case Column::COL_BLOCK_SIZE:
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::COL_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::COL_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::COL_TITLE:
return tr("Title");
case COL_ID:
case Column::COL_ID:
return tr("ID");
case COL_BANNER:
case Column::COL_BANNER:
return tr("Banner");
case COL_DESCRIPTION:
case Column::COL_DESCRIPTION:
return tr("Description");
case COL_MAKER:
case Column::COL_MAKER:
return tr("Maker");
case COL_FILE_NAME:
case Column::COL_FILE_NAME:
return tr("File Name");
case COL_FILE_PATH:
case Column::COL_FILE_PATH:
return tr("File Path");
case COL_SIZE:
case Column::COL_SIZE:
return tr("Size");
case COL_FILE_FORMAT:
case Column::COL_FILE_FORMAT:
return tr("File Format");
case COL_BLOCK_SIZE:
case Column::COL_BLOCK_SIZE:
return tr("Block Size");
case COL_COMPRESSION:
case Column::COL_COMPRESSION:
return tr("Compression");
case COL_TAGS:
case Column::COL_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::NUM_COLS);
}
bool GameListModel::ShouldDisplayGameListItem(int index) const

View File

@ -44,7 +44,7 @@ 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,

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::COL_TITLE));
}
QVariant GridProxyModel::data(const QModelIndex& i, int role) const
@ -28,8 +28,10 @@ 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::COL_TITLE)),
Qt::DisplayRole);
}
else if (role == Qt::DecorationRole)
{
@ -43,7 +45,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::COL_BANNER)),
Qt::DecorationRole)
.value<QPixmap>();

View File

@ -23,9 +23,15 @@ bool ListProxyModel::lessThan(const QModelIndex& left, const QModelIndex& 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();
sourceModel()
->index(right.row(), static_cast<int>(GameListModel::Column::COL_TITLE))
.data()
.toString();
const auto left_title =
sourceModel()->index(left.row(), GameListModel::COL_TITLE).data().toString();
sourceModel()
->index(left.row(), static_cast<int>(GameListModel::Column::COL_TITLE))
.data()
.toString();
if (sortOrder() == Qt::AscendingOrder)
return left_title < right_title;