Duckstation/src/duckstation-qt/gamelistwidget.cpp

421 lines
14 KiB
C++
Raw Normal View History

2019-12-31 06:17:17 +00:00
#include "gamelistwidget.h"
#include "common/string_util.h"
2019-12-31 06:17:17 +00:00
#include "core/settings.h"
#include "frontend-common/game_list.h"
#include "gamelistmodel.h"
2019-12-31 06:17:17 +00:00
#include "qthostinterface.h"
#include "qtutils.h"
2020-01-08 05:01:04 +00:00
#include <QtCore/QSortFilterProxyModel>
#include <QtGui/QPixmap>
2020-09-23 14:02:13 +00:00
#include <QtGui/QWheelEvent>
2019-12-31 06:17:17 +00:00
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMenu>
2019-12-31 06:17:17 +00:00
2020-01-08 05:01:04 +00:00
class GameListSortModel final : public QSortFilterProxyModel
{
public:
GameListSortModel(GameListModel* parent) : QSortFilterProxyModel(parent), m_model(parent) {}
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override
{
// TODO: Search
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override
{
const bool ascending = sortOrder() == Qt::AscendingOrder;
2020-07-30 17:40:51 +00:00
return m_model->lessThan(source_left, source_right, source_left.column(), ascending);
2020-01-08 05:01:04 +00:00
}
private:
GameListModel* m_model;
};
2019-12-31 06:17:17 +00:00
GameListWidget::GameListWidget(QWidget* parent /* = nullptr */) : QStackedWidget(parent) {}
GameListWidget::~GameListWidget() = default;
void GameListWidget::initialize(QtHostInterface* host_interface)
{
m_host_interface = host_interface;
m_game_list = host_interface->getGameList();
connect(m_host_interface, &QtHostInterface::gameListRefreshed, this, &GameListWidget::onGameListRefreshed);
2020-09-23 14:02:13 +00:00
m_model = new GameListModel(m_game_list, this);
m_model->setCoverScale(host_interface->GetFloatSettingValue("UI", "GameListCoverArtScale", 0.45f));
m_model->setShowCoverTitles(host_interface->GetBoolSettingValue("UI", "GameListShowCoverTitles", true));
m_sort_model = new GameListSortModel(m_model);
m_sort_model->setSourceModel(m_model);
2019-12-31 06:17:17 +00:00
m_table_view = new QTableView(this);
2020-09-23 14:02:13 +00:00
m_table_view->setModel(m_sort_model);
2020-01-08 05:01:04 +00:00
m_table_view->setSortingEnabled(true);
2019-12-31 06:17:17 +00:00
m_table_view->setSelectionMode(QAbstractItemView::SingleSelection);
m_table_view->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table_view->setContextMenuPolicy(Qt::CustomContextMenu);
2019-12-31 06:17:17 +00:00
m_table_view->setAlternatingRowColors(true);
m_table_view->setShowGrid(false);
m_table_view->setCurrentIndex({});
m_table_view->horizontalHeader()->setHighlightSections(false);
m_table_view->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
2019-12-31 06:17:17 +00:00
m_table_view->verticalHeader()->hide();
m_table_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
2019-12-31 06:17:17 +00:00
loadTableViewColumnVisibilitySettings();
loadTableViewColumnSortSettings();
2020-03-02 01:08:20 +00:00
connect(m_table_view->selectionModel(), &QItemSelectionModel::currentChanged, this,
&GameListWidget::onSelectionModelCurrentChanged);
connect(m_table_view, &QTableView::activated, this, &GameListWidget::onTableViewItemActivated);
connect(m_table_view, &QTableView::customContextMenuRequested, this,
&GameListWidget::onTableViewContextMenuRequested);
connect(m_table_view->horizontalHeader(), &QHeaderView::customContextMenuRequested, this,
&GameListWidget::onTableViewHeaderContextMenuRequested);
connect(m_table_view->horizontalHeader(), &QHeaderView::sortIndicatorChanged, this,
&GameListWidget::onTableViewHeaderSortIndicatorChanged);
2019-12-31 06:17:17 +00:00
insertWidget(0, m_table_view);
2020-09-23 14:02:13 +00:00
m_list_view = new GameListGridListView(this);
m_list_view->setModel(m_sort_model);
m_list_view->setModelColumn(GameListModel::Column_Cover);
m_list_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
m_list_view->setViewMode(QListView::IconMode);
m_list_view->setResizeMode(QListView::Adjust);
m_list_view->setUniformItemSizes(true);
m_list_view->setContextMenuPolicy(Qt::CustomContextMenu);
m_list_view->setFrameStyle(QFrame::NoFrame);
m_list_view->setSpacing(m_model->getCoverArtSpacing());
updateListFont();
connect(m_list_view->selectionModel(), &QItemSelectionModel::currentChanged, this,
&GameListWidget::onSelectionModelCurrentChanged);
2020-09-23 14:08:29 +00:00
connect(m_list_view, &GameListGridListView::zoomIn, this, &GameListWidget::gridZoomIn);
connect(m_list_view, &GameListGridListView::zoomOut, this, &GameListWidget::gridZoomOut);
connect(m_list_view, &QListView::activated, this, &GameListWidget::onListViewItemActivated);
2020-09-23 14:02:13 +00:00
connect(m_list_view, &QListView::customContextMenuRequested, this, &GameListWidget::onListViewContextMenuRequested);
insertWidget(1, m_list_view);
if (m_host_interface->GetBoolSettingValue("UI", "GameListGridView", false))
setCurrentIndex(1);
else
setCurrentIndex(0);
resizeTableViewColumnsToFit();
2019-12-31 06:17:17 +00:00
}
2020-09-23 14:02:13 +00:00
bool GameListWidget::isShowingGameList() const
{
return currentIndex() == 0;
}
bool GameListWidget::isShowingGameGrid() const
{
return currentIndex() == 1;
}
bool GameListWidget::getShowGridCoverTitles() const
{
return m_model->getShowCoverTitles();
}
2019-12-31 06:17:17 +00:00
void GameListWidget::onGameListRefreshed()
{
2020-09-23 14:02:13 +00:00
m_model->refresh();
2019-12-31 06:17:17 +00:00
}
void GameListWidget::onSelectionModelCurrentChanged(const QModelIndex& current, const QModelIndex& previous)
2019-12-31 06:17:17 +00:00
{
2020-09-23 14:02:13 +00:00
const QModelIndex source_index = m_sort_model->mapToSource(current);
if (!source_index.isValid() || source_index.row() >= static_cast<int>(m_game_list->GetEntryCount()))
{
emit entrySelected(nullptr);
2019-12-31 06:17:17 +00:00
return;
}
2019-12-31 06:17:17 +00:00
const GameListEntry& entry = m_game_list->GetEntries().at(source_index.row());
emit entrySelected(&entry);
2019-12-31 06:17:17 +00:00
}
void GameListWidget::onTableViewItemActivated(const QModelIndex& index)
{
2020-09-23 14:02:13 +00:00
const QModelIndex source_index = m_sort_model->mapToSource(index);
if (!source_index.isValid() || source_index.row() >= static_cast<int>(m_game_list->GetEntryCount()))
return;
const GameListEntry& entry = m_game_list->GetEntries().at(source_index.row());
emit entryDoubleClicked(&entry);
}
void GameListWidget::onTableViewContextMenuRequested(const QPoint& point)
{
const GameListEntry* entry = getSelectedEntry();
if (!entry)
return;
emit entryContextMenuRequested(m_table_view->mapToGlobal(point), entry);
}
void GameListWidget::onListViewItemActivated(const QModelIndex& index)
2020-09-23 14:02:13 +00:00
{
const QModelIndex source_index = m_sort_model->mapToSource(index);
if (!source_index.isValid() || source_index.row() >= static_cast<int>(m_game_list->GetEntryCount()))
return;
const GameListEntry& entry = m_game_list->GetEntries().at(source_index.row());
emit entryDoubleClicked(&entry);
}
void GameListWidget::onListViewContextMenuRequested(const QPoint& point)
{
const GameListEntry* entry = getSelectedEntry();
if (!entry)
return;
emit entryContextMenuRequested(m_list_view->mapToGlobal(point), entry);
}
void GameListWidget::onTableViewHeaderContextMenuRequested(const QPoint& point)
{
QMenu menu;
for (int column = 0; column < GameListModel::Column_Count; column++)
{
2020-09-23 14:02:13 +00:00
if (column == GameListModel::Column_Cover)
continue;
QAction* action = menu.addAction(m_model->getColumnDisplayName(column));
action->setCheckable(true);
action->setChecked(!m_table_view->isColumnHidden(column));
connect(action, &QAction::toggled, [this, column](bool enabled) {
m_table_view->setColumnHidden(column, !enabled);
saveTableViewColumnVisibilitySettings(column);
resizeTableViewColumnsToFit();
});
}
menu.exec(m_table_view->mapToGlobal(point));
}
void GameListWidget::onTableViewHeaderSortIndicatorChanged(int, Qt::SortOrder)
{
saveTableViewColumnSortSettings();
}
2020-09-23 14:02:13 +00:00
void GameListWidget::listZoom(float delta)
{
static constexpr float MIN_SCALE = 0.1f;
static constexpr float MAX_SCALE = 2.0f;
const float new_scale = std::clamp(m_model->getCoverScale() + delta, MIN_SCALE, MAX_SCALE);
m_host_interface->SetFloatSettingValue("UI", "GameListCoverArtScale", new_scale);
m_model->setCoverScale(new_scale);
updateListFont();
m_model->refresh();
}
2020-09-23 14:08:29 +00:00
void GameListWidget::gridZoomIn()
2020-09-23 14:02:13 +00:00
{
listZoom(0.05f);
}
2020-09-23 14:08:29 +00:00
void GameListWidget::gridZoomOut()
2020-09-23 14:02:13 +00:00
{
listZoom(-0.05f);
}
2020-09-23 14:08:29 +00:00
void GameListWidget::refreshGridCovers()
{
m_model->refreshCovers();
}
2020-09-23 14:02:13 +00:00
void GameListWidget::showGameList()
{
if (currentIndex() == 0)
return;
m_host_interface->SetBoolSettingValue("UI", "GameListGridView", false);
setCurrentIndex(0);
resizeTableViewColumnsToFit();
}
void GameListWidget::showGameGrid()
{
if (currentIndex() == 1)
return;
m_host_interface->SetBoolSettingValue("UI", "GameListGridView", true);
setCurrentIndex(1);
}
void GameListWidget::setShowCoverTitles(bool enabled)
{
if (m_model->getShowCoverTitles() == enabled)
return;
m_host_interface->SetBoolSettingValue("UI", "GameListShowCoverTitles", enabled);
m_model->setShowCoverTitles(enabled);
if (isShowingGameGrid())
m_model->refresh();
}
void GameListWidget::updateListFont()
{
QFont font;
font.setPointSizeF(16.0f * m_model->getCoverScale());
m_list_view->setFont(font);
}
2019-12-31 06:17:17 +00:00
void GameListWidget::resizeEvent(QResizeEvent* event)
{
QStackedWidget::resizeEvent(event);
resizeTableViewColumnsToFit();
}
2019-12-31 06:17:17 +00:00
void GameListWidget::resizeTableViewColumnsToFit()
{
QtUtils::ResizeColumnsForTableView(m_table_view, {
32, // type
80, // code
-1, // title
-1, // file title
200, // developer
200, // publisher
200, // genre
50, // year
100, // players
80, // size
50, // region
100 // compatibility
});
2019-12-31 06:17:17 +00:00
}
static TinyString getColumnVisibilitySettingsKeyName(int column)
{
return TinyString::FromFormat("Show%s", GameListModel::getColumnName(static_cast<GameListModel::Column>(column)));
}
void GameListWidget::loadTableViewColumnVisibilitySettings()
{
static constexpr std::array<bool, GameListModel::Column_Count> DEFAULT_VISIBILITY = {{
true, // type
true, // code
true, // title
false, // file title
true, // developer
false, // publisher
false, // genre
true, // year
false, // players
true, // size
true, // region
true // compatibility
}};
for (int column = 0; column < GameListModel::Column_Count; column++)
{
const bool visible = m_host_interface->GetBoolSettingValue(
"GameListTableView", getColumnVisibilitySettingsKeyName(column), DEFAULT_VISIBILITY[column]);
m_table_view->setColumnHidden(column, !visible);
}
}
void GameListWidget::saveTableViewColumnVisibilitySettings()
{
for (int column = 0; column < GameListModel::Column_Count; column++)
{
const bool visible = !m_table_view->isColumnHidden(column);
m_host_interface->SetBoolSettingValue("GameListTableView", getColumnVisibilitySettingsKeyName(column), visible);
}
}
void GameListWidget::saveTableViewColumnVisibilitySettings(int column)
{
const bool visible = !m_table_view->isColumnHidden(column);
m_host_interface->SetBoolSettingValue("GameListTableView", getColumnVisibilitySettingsKeyName(column), visible);
}
void GameListWidget::loadTableViewColumnSortSettings()
{
const GameListModel::Column DEFAULT_SORT_COLUMN = GameListModel::Column_Type;
const bool DEFAULT_SORT_DESCENDING = false;
const GameListModel::Column sort_column =
GameListModel::getColumnIdForName(m_host_interface->GetStringSettingValue("GameListTableView", "SortColumn"))
.value_or(DEFAULT_SORT_COLUMN);
const bool sort_descending =
m_host_interface->GetBoolSettingValue("GameListTableView", "SortDescending", DEFAULT_SORT_DESCENDING);
2020-09-23 14:02:13 +00:00
m_sort_model->sort(sort_column, sort_descending ? Qt::DescendingOrder : Qt::AscendingOrder);
}
void GameListWidget::saveTableViewColumnSortSettings()
{
const int sort_column = m_table_view->horizontalHeader()->sortIndicatorSection();
const bool sort_descending = (m_table_view->horizontalHeader()->sortIndicatorOrder() == Qt::DescendingOrder);
if (sort_column >= 0 && sort_column < GameListModel::Column_Count)
{
m_host_interface->SetStringSettingValue(
"GameListTableView", "SortColumn", GameListModel::getColumnName(static_cast<GameListModel::Column>(sort_column)));
}
m_host_interface->SetBoolSettingValue("GameListTableView", "SortDescending", sort_descending);
}
const GameListEntry* GameListWidget::getSelectedEntry() const
{
2020-09-23 14:02:13 +00:00
if (currentIndex() == 0)
{
const QItemSelectionModel* selection_model = m_table_view->selectionModel();
if (!selection_model->hasSelection())
return nullptr;
2020-09-23 14:02:13 +00:00
const QModelIndexList selected_rows = selection_model->selectedRows();
if (selected_rows.empty())
return nullptr;
2020-09-23 14:02:13 +00:00
const QModelIndex source_index = m_sort_model->mapToSource(selected_rows[0]);
if (!source_index.isValid() || source_index.row() >= static_cast<int>(m_game_list->GetEntryCount()))
return nullptr;
return &m_game_list->GetEntries().at(source_index.row());
}
else
{
const QItemSelectionModel* selection_model = m_list_view->selectionModel();
if (!selection_model->hasSelection())
return nullptr;
const QModelIndex source_index = m_sort_model->mapToSource(selection_model->currentIndex());
if (!source_index.isValid() || source_index.row() >= static_cast<int>(m_game_list->GetEntryCount()))
return nullptr;
return &m_game_list->GetEntries().at(source_index.row());
}
}
GameListGridListView::GameListGridListView(QWidget* parent /*= nullptr*/) : QListView(parent) {}
void GameListGridListView::wheelEvent(QWheelEvent* e)
{
if (e->modifiers() & Qt::ControlModifier)
{
int dy = e->angleDelta().y();
if (dy != 0)
{
if (dy < 0)
zoomOut();
else
zoomIn();
return;
}
}
2020-09-23 14:02:13 +00:00
QListView::wheelEvent(e);
}