UI: Massive revamp, new features and improvements

This commit is contained in:
Connor McLaughlin
2022-07-11 23:03:29 +10:00
parent 3fb61865e5
commit b42b5501f6
425 changed files with 39701 additions and 29487 deletions

View File

@@ -1,24 +1,67 @@
#include "gamelistwidget.h"
#include "common/assert.h"
#include "common/string_util.h"
#include "core/host_settings.h"
#include "core/settings.h"
#include "frontend-common/game_list.h"
#include "gamelistmodel.h"
#include "qthostinterface.h"
#include "gamelistrefreshthread.h"
#include "qthost.h"
#include "qtutils.h"
#include <QtCore/QSortFilterProxyModel>
#include <QtGui/QGuiApplication>
#include <QtGui/QPixmap>
#include <QtGui/QWheelEvent>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMenu>
static constexpr float MIN_SCALE = 0.1f;
static constexpr float MAX_SCALE = 2.0f;
static const char* SUPPORTED_FORMATS_STRING =
QT_TRANSLATE_NOOP(GameListWidget, ".cue (Cue Sheets)\n"
".iso/.img (Single Track Image)\n"
".ecm (Error Code Modeling Image)\n"
".mds (Media Descriptor Sidecar)\n"
".chd (Compressed Hunks of Data)\n"
".pbp (PlayStation Portable, Only Decrypted)");
class GameListSortModel final : public QSortFilterProxyModel
{
public:
GameListSortModel(GameListModel* parent) : QSortFilterProxyModel(parent), m_model(parent) {}
explicit GameListSortModel(GameListModel* parent) : QSortFilterProxyModel(parent), m_model(parent) {}
void setFilterType(GameList::EntryType type)
{
m_filter_type = type;
invalidateRowsFilter();
}
void setFilterRegion(DiscRegion region)
{
m_filter_region = region;
invalidateRowsFilter();
}
void setFilterName(const QString& name)
{
m_filter_name = name;
invalidateRowsFilter();
}
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override
{
// TODO: Search
if (m_filter_type != GameList::EntryType::Count || m_filter_region != DiscRegion::Count || !m_filter_name.isEmpty())
{
const auto lock = GameList::GetLock();
const GameList::Entry* entry = GameList::GetEntryByIndex(source_row);
if (m_filter_type != GameList::EntryType::Count && entry->type != m_filter_type)
return false;
if (m_filter_region != DiscRegion::Count && entry->region != m_filter_region)
return false;
if (!m_filter_name.isEmpty() &&
!QString::fromStdString(entry->title).contains(m_filter_name, Qt::CaseInsensitive))
return false;
}
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
@@ -29,26 +72,55 @@ public:
private:
GameListModel* m_model;
GameList::EntryType m_filter_type = GameList::EntryType::Count;
DiscRegion m_filter_region = DiscRegion::Count;
QString m_filter_name;
};
GameListWidget::GameListWidget(QWidget* parent /* = nullptr */) : QStackedWidget(parent) {}
GameListWidget::GameListWidget(QWidget* parent /* = nullptr */) : QWidget(parent) {}
GameListWidget::~GameListWidget() = default;
void GameListWidget::initialize(QtHostInterface* host_interface)
void GameListWidget::initialize()
{
m_host_interface = host_interface;
m_game_list = host_interface->getGameList();
connect(m_host_interface, &QtHostInterface::gameListRefreshed, this, &GameListWidget::onGameListRefreshed);
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_model = new GameListModel(this);
m_model->setCoverScale(Host::GetBaseFloatSettingValue("UI", "GameListCoverArtScale", 0.45f));
m_model->setShowCoverTitles(Host::GetBaseBoolSettingValue("UI", "GameListShowCoverTitles", true));
m_sort_model = new GameListSortModel(m_model);
m_sort_model->setSourceModel(m_model);
m_table_view = new QTableView(this);
m_ui.setupUi(this);
for (u32 type = 0; type < static_cast<u32>(GameList::EntryType::Count); type++)
{
m_ui.filterType->addItem(
QtUtils::GetIconForEntryType(static_cast<GameList::EntryType>(type)),
qApp->translate("GameList", GameList::GetEntryTypeDisplayName(static_cast<GameList::EntryType>(type))));
}
for (u32 region = 0; region < static_cast<u32>(DiscRegion::Count); region++)
{
m_ui.filterRegion->addItem(QtUtils::GetIconForRegion(static_cast<DiscRegion>(region)),
QString::fromUtf8(Settings::GetDiscRegionName(static_cast<DiscRegion>(region))));
}
connect(m_ui.viewGameList, &QPushButton::clicked, this, &GameListWidget::showGameList);
connect(m_ui.viewGameGrid, &QPushButton::clicked, this, &GameListWidget::showGameGrid);
connect(m_ui.gridScale, &QSlider::valueChanged, this, &GameListWidget::gridIntScale);
connect(m_ui.viewGridTitles, &QPushButton::toggled, this, &GameListWidget::setShowCoverTitles);
connect(m_ui.filterType, &QComboBox::currentIndexChanged, this, [this](int index) {
m_sort_model->setFilterType((index == 0) ? GameList::EntryType::Count :
static_cast<GameList::EntryType>(index - 1));
});
connect(m_ui.filterRegion, &QComboBox::currentIndexChanged, this, [this](int index) {
m_sort_model->setFilterRegion((index == 0) ? DiscRegion::Count : static_cast<DiscRegion>(index - 1));
});
connect(m_ui.searchText, &QLineEdit::textChanged, this,
[this](const QString& text) { m_sort_model->setFilterName(text); });
// Works around a strange bug where after hiding the game list, the cursor for the whole window changes to a beam..
// m_ui.searchText->setCursor(QCursor(Qt::ArrowCursor));
m_table_view = new QTableView(m_ui.stack);
m_table_view->setModel(m_sort_model);
m_table_view->setSortingEnabled(true);
m_table_view->setSelectionMode(QAbstractItemView::SingleSelection);
@@ -61,6 +133,7 @@ void GameListWidget::initialize(QtHostInterface* host_interface)
m_table_view->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
m_table_view->verticalHeader()->hide();
m_table_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_table_view->setVerticalScrollMode(QAbstractItemView::ScrollMode::ScrollPerPixel);
loadTableViewColumnVisibilitySettings();
loadTableViewColumnSortSettings();
@@ -75,18 +148,21 @@ void GameListWidget::initialize(QtHostInterface* host_interface)
connect(m_table_view->horizontalHeader(), &QHeaderView::sortIndicatorChanged, this,
&GameListWidget::onTableViewHeaderSortIndicatorChanged);
insertWidget(0, m_table_view);
m_ui.stack->insertWidget(0, m_table_view);
m_list_view = new GameListGridListView(this);
m_list_view = new GameListGridListView(m_ui.stack);
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->setItemAlignment(Qt::AlignHCenter);
m_list_view->setContextMenuPolicy(Qt::CustomContextMenu);
m_list_view->setFrameStyle(QFrame::NoFrame);
m_list_view->setSpacing(m_model->getCoverArtSpacing());
m_list_view->setVerticalScrollMode(QAbstractItemView::ScrollMode::ScrollPerPixel);
updateListFont();
connect(m_list_view->selectionModel(), &QItemSelectionModel::currentChanged, this,
@@ -96,24 +172,32 @@ void GameListWidget::initialize(QtHostInterface* host_interface)
connect(m_list_view, &QListView::activated, this, &GameListWidget::onListViewItemActivated);
connect(m_list_view, &QListView::customContextMenuRequested, this, &GameListWidget::onListViewContextMenuRequested);
insertWidget(1, m_list_view);
m_ui.stack->insertWidget(1, m_list_view);
if (m_host_interface->GetBoolSettingValue("UI", "GameListGridView", false))
setCurrentIndex(1);
m_empty_widget = new QWidget(m_ui.stack);
m_empty_ui.setupUi(m_empty_widget);
m_empty_ui.supportedFormats->setText(qApp->translate("GameListWidget", SUPPORTED_FORMATS_STRING));
connect(m_empty_ui.addGameDirectory, &QPushButton::clicked, this, [this]() { emit addGameDirectoryRequested(); });
connect(m_empty_ui.scanForNewGames, &QPushButton::clicked, this, [this]() { refresh(false); });
m_ui.stack->insertWidget(2, m_empty_widget);
if (Host::GetBaseBoolSettingValue("UI", "GameListGridView", false))
m_ui.stack->setCurrentIndex(1);
else
setCurrentIndex(0);
m_ui.stack->setCurrentIndex(0);
updateToolbar();
resizeTableViewColumnsToFit();
}
bool GameListWidget::isShowingGameList() const
{
return currentIndex() == 0;
return m_ui.stack->currentIndex() == 0;
}
bool GameListWidget::isShowingGameGrid() const
{
return currentIndex() == 1;
return m_ui.stack->currentIndex() == 1;
}
bool GameListWidget::getShowGridCoverTitles() const
@@ -121,60 +205,89 @@ bool GameListWidget::getShowGridCoverTitles() const
return m_model->getShowCoverTitles();
}
void GameListWidget::onGameListRefreshed()
void GameListWidget::refresh(bool invalidate_cache)
{
cancelRefresh();
m_refresh_thread = new GameListRefreshThread(invalidate_cache);
connect(m_refresh_thread, &GameListRefreshThread::refreshProgress, this, &GameListWidget::onRefreshProgress,
Qt::QueuedConnection);
connect(m_refresh_thread, &GameListRefreshThread::refreshComplete, this, &GameListWidget::onRefreshComplete,
Qt::QueuedConnection);
m_refresh_thread->start();
}
void GameListWidget::cancelRefresh()
{
if (!m_refresh_thread)
return;
m_refresh_thread->cancel();
m_refresh_thread->wait();
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
AssertMsg(!m_refresh_thread, "Game list thread should be unreferenced by now");
}
void GameListWidget::onRefreshProgress(const QString& status, int current, int total)
{
// switch away from the placeholder while we scan, in case we find anything
if (m_ui.stack->currentIndex() == 2)
m_ui.stack->setCurrentIndex(Host::GetBaseBoolSettingValue("UI", "GameListGridView", false) ? 1 : 0);
m_model->refresh();
emit refreshProgress(status, current, total);
}
void GameListWidget::onRefreshComplete()
{
m_model->refresh();
emit refreshComplete();
AssertMsg(m_refresh_thread, "Has a refresh thread");
m_refresh_thread->wait();
delete m_refresh_thread;
m_refresh_thread = nullptr;
// if we still had no games, switch to the helper widget
if (m_model->rowCount() == 0)
m_ui.stack->setCurrentIndex(2);
}
void GameListWidget::onSelectionModelCurrentChanged(const QModelIndex& current, const QModelIndex& previous)
{
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);
if (!source_index.isValid() || source_index.row() >= static_cast<int>(GameList::GetEntryCount()))
return;
}
const GameListEntry& entry = m_game_list->GetEntries().at(source_index.row());
emit entrySelected(&entry);
emit selectionChanged();
}
void GameListWidget::onTableViewItemActivated(const QModelIndex& index)
{
const QModelIndex source_index = m_sort_model->mapToSource(index);
if (!source_index.isValid() || source_index.row() >= static_cast<int>(m_game_list->GetEntryCount()))
if (!source_index.isValid() || source_index.row() >= static_cast<int>(GameList::GetEntryCount()))
return;
const GameListEntry& entry = m_game_list->GetEntries().at(source_index.row());
emit entryDoubleClicked(&entry);
emit entryActivated();
}
void GameListWidget::onTableViewContextMenuRequested(const QPoint& point)
{
const GameListEntry* entry = getSelectedEntry();
if (!entry)
return;
emit entryContextMenuRequested(m_table_view->mapToGlobal(point), entry);
emit entryContextMenuRequested(m_table_view->mapToGlobal(point));
}
void GameListWidget::onListViewItemActivated(const QModelIndex& index)
{
const QModelIndex source_index = m_sort_model->mapToSource(index);
if (!source_index.isValid() || source_index.row() >= static_cast<int>(m_game_list->GetEntryCount()))
if (!source_index.isValid() || source_index.row() >= static_cast<int>(GameList::GetEntryCount()))
return;
const GameListEntry& entry = m_game_list->GetEntries().at(source_index.row());
emit entryDoubleClicked(&entry);
emit entryActivated();
}
void GameListWidget::onListViewContextMenuRequested(const QPoint& point)
{
const GameListEntry* entry = getSelectedEntry();
if (!entry)
return;
emit entryContextMenuRequested(m_list_view->mapToGlobal(point), entry);
emit entryContextMenuRequested(m_list_view->mapToGlobal(point));
}
void GameListWidget::onTableViewHeaderContextMenuRequested(const QPoint& point)
@@ -206,13 +319,11 @@ void GameListWidget::onTableViewHeaderSortIndicatorChanged(int, Qt::SortOrder)
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);
Host::SetBaseFloatSettingValue("UI", "GameListCoverArtScale", new_scale);
m_model->setCoverScale(new_scale);
updateListFont();
updateToolbar();
m_model->refresh();
}
@@ -227,6 +338,18 @@ void GameListWidget::gridZoomOut()
listZoom(-0.05f);
}
void GameListWidget::gridIntScale(int int_scale)
{
const float new_scale = std::clamp(static_cast<float>(int_scale) / 100.0f, MIN_SCALE, MAX_SCALE);
Host::SetBaseFloatSettingValue("UI", "GameListCoverArtScale", new_scale);
m_model->setCoverScale(new_scale);
updateListFont();
updateToolbar();
m_model->refresh();
}
void GameListWidget::refreshGridCovers()
{
m_model->refreshCovers();
@@ -234,32 +357,47 @@ void GameListWidget::refreshGridCovers()
void GameListWidget::showGameList()
{
if (currentIndex() == 0)
if (m_ui.stack->currentIndex() == 0 || m_model->rowCount() == 0)
{
updateToolbar();
return;
}
m_host_interface->SetBoolSettingValue("UI", "GameListGridView", false);
setCurrentIndex(0);
Host::SetBaseBoolSettingValue("UI", "GameListGridView", false);
m_ui.stack->setCurrentIndex(0);
resizeTableViewColumnsToFit();
updateToolbar();
emit layoutChange();
}
void GameListWidget::showGameGrid()
{
if (currentIndex() == 1)
if (m_ui.stack->currentIndex() == 1 || m_model->rowCount() == 0)
{
updateToolbar();
return;
}
m_host_interface->SetBoolSettingValue("UI", "GameListGridView", true);
setCurrentIndex(1);
Host::SetBaseBoolSettingValue("UI", "GameListGridView", true);
m_ui.stack->setCurrentIndex(1);
updateToolbar();
emit layoutChange();
}
void GameListWidget::setShowCoverTitles(bool enabled)
{
if (m_model->getShowCoverTitles() == enabled)
{
updateToolbar();
return;
}
m_host_interface->SetBoolSettingValue("UI", "GameListShowCoverTitles", enabled);
Host::SetBaseBoolSettingValue("UI", "GameListShowCoverTitles", enabled);
m_model->setShowCoverTitles(enabled);
if (isShowingGameGrid())
m_model->refresh();
updateToolbar();
emit layoutChange();
}
void GameListWidget::updateListFont()
@@ -269,9 +407,33 @@ void GameListWidget::updateListFont()
m_list_view->setFont(font);
}
void GameListWidget::updateToolbar()
{
const bool grid_view = isShowingGameGrid();
{
QSignalBlocker sb(m_ui.viewGameGrid);
m_ui.viewGameGrid->setChecked(grid_view);
}
{
QSignalBlocker sb(m_ui.viewGameList);
m_ui.viewGameList->setChecked(!grid_view);
}
{
QSignalBlocker sb(m_ui.viewGridTitles);
m_ui.viewGridTitles->setChecked(m_model->getShowCoverTitles());
}
{
QSignalBlocker sb(m_ui.gridScale);
m_ui.gridScale->setValue(static_cast<int>(m_model->getCoverScale() * 100.0f));
}
m_ui.viewGridTitles->setEnabled(grid_view);
m_ui.gridScale->setEnabled(grid_view);
}
void GameListWidget::resizeEvent(QResizeEvent* event)
{
QStackedWidget::resizeEvent(event);
QWidget::resizeEvent(event);
resizeTableViewColumnsToFit();
}
@@ -287,7 +449,7 @@ void GameListWidget::resizeTableViewColumnsToFit()
200, // genre
50, // year
100, // players
80, // size
80, // size
50, // region
100 // compatibility
});
@@ -317,8 +479,8 @@ void GameListWidget::loadTableViewColumnVisibilitySettings()
for (int column = 0; column < GameListModel::Column_Count; column++)
{
const bool visible = m_host_interface->GetBoolSettingValue(
"GameListTableView", getColumnVisibilitySettingsKeyName(column), DEFAULT_VISIBILITY[column]);
const bool visible = Host::GetBaseBoolSettingValue("GameListTableView", getColumnVisibilitySettingsKeyName(column),
DEFAULT_VISIBILITY[column]);
m_table_view->setColumnHidden(column, !visible);
}
}
@@ -328,14 +490,14 @@ 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);
Host::SetBaseBoolSettingValue("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);
Host::SetBaseBoolSettingValue("GameListTableView", getColumnVisibilitySettingsKeyName(column), visible);
}
void GameListWidget::loadTableViewColumnSortSettings()
@@ -344,10 +506,10 @@ void GameListWidget::loadTableViewColumnSortSettings()
const bool DEFAULT_SORT_DESCENDING = false;
const GameListModel::Column sort_column =
GameListModel::getColumnIdForName(m_host_interface->GetStringSettingValue("GameListTableView", "SortColumn"))
GameListModel::getColumnIdForName(Host::GetBaseStringSettingValue("GameListTableView", "SortColumn"))
.value_or(DEFAULT_SORT_COLUMN);
const bool sort_descending =
m_host_interface->GetBoolSettingValue("GameListTableView", "SortDescending", DEFAULT_SORT_DESCENDING);
Host::GetBaseBoolSettingValue("GameListTableView", "SortDescending", DEFAULT_SORT_DESCENDING);
m_sort_model->sort(sort_column, sort_descending ? Qt::DescendingOrder : Qt::AscendingOrder);
}
@@ -358,16 +520,16 @@ void GameListWidget::saveTableViewColumnSortSettings()
if (sort_column >= 0 && sort_column < GameListModel::Column_Count)
{
m_host_interface->SetStringSettingValue(
"GameListTableView", "SortColumn", GameListModel::getColumnName(static_cast<GameListModel::Column>(sort_column)));
Host::SetBaseStringSettingValue("GameListTableView", "SortColumn",
GameListModel::getColumnName(static_cast<GameListModel::Column>(sort_column)));
}
m_host_interface->SetBoolSettingValue("GameListTableView", "SortDescending", sort_descending);
Host::SetBaseBoolSettingValue("GameListTableView", "SortDescending", sort_descending);
}
const GameListEntry* GameListWidget::getSelectedEntry() const
const GameList::Entry* GameListWidget::getSelectedEntry() const
{
if (currentIndex() == 0)
if (m_ui.stack->currentIndex() == 0)
{
const QItemSelectionModel* selection_model = m_table_view->selectionModel();
if (!selection_model->hasSelection())
@@ -378,10 +540,10 @@ const GameListEntry* GameListWidget::getSelectedEntry() const
return nullptr;
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()))
if (!source_index.isValid())
return nullptr;
return &m_game_list->GetEntries().at(source_index.row());
return GameList::GetEntryByIndex(source_index.row());
}
else
{
@@ -390,10 +552,10 @@ const GameListEntry* GameListWidget::getSelectedEntry() const
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()))
if (!source_index.isValid())
return nullptr;
return &m_game_list->GetEntries().at(source_index.row());
return GameList::GetEntryByIndex(source_index.row());
}
}