Duckstation/src/duckstation-qt/gamelistsettingswidget.cpp

129 lines
4.7 KiB
C++
Raw Normal View History

2019-12-31 06:17:17 +00:00
#include "gamelistsettingswidget.h"
#include "common/assert.h"
#include "common/file_system.h"
#include "common/string_util.h"
#include "frontend-common/game_list.h"
#include "gamelistsearchdirectoriesmodel.h"
2019-12-31 06:17:17 +00:00
#include "qthostinterface.h"
#include "qtutils.h"
#include <QtCore/QAbstractTableModel>
#include <QtCore/QDebug>
2019-12-31 06:17:17 +00:00
#include <QtCore/QSettings>
#include <QtCore/QUrl>
2019-12-31 06:17:17 +00:00
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMenu>
2019-12-31 06:17:17 +00:00
#include <QtWidgets/QMessageBox>
#include <algorithm>
GameListSettingsWidget::GameListSettingsWidget(QtHostInterface* host_interface, QWidget* parent /* = nullptr */)
: QWidget(parent), m_host_interface(host_interface)
{
m_ui.setupUi(this);
m_search_directories_model = new GameListSearchDirectoriesModel(host_interface);
2019-12-31 06:17:17 +00:00
m_ui.searchDirectoryList->setModel(m_search_directories_model);
m_ui.searchDirectoryList->setSelectionMode(QAbstractItemView::SingleSelection);
m_ui.searchDirectoryList->setSelectionBehavior(QAbstractItemView::SelectRows);
m_ui.searchDirectoryList->setAlternatingRowColors(true);
m_ui.searchDirectoryList->setShowGrid(false);
m_ui.searchDirectoryList->horizontalHeader()->setHighlightSections(false);
2019-12-31 06:17:17 +00:00
m_ui.searchDirectoryList->verticalHeader()->hide();
m_ui.searchDirectoryList->setCurrentIndex({});
m_ui.searchDirectoryList->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
2019-12-31 06:17:17 +00:00
connect(m_ui.searchDirectoryList, &QTableView::clicked, this, &GameListSettingsWidget::onDirectoryListItemClicked);
connect(m_ui.searchDirectoryList, &QTableView::customContextMenuRequested, this,
&GameListSettingsWidget::onDirectoryListContextMenuRequested);
connect(m_ui.addSearchDirectoryButton, &QPushButton::clicked, this,
&GameListSettingsWidget::onAddSearchDirectoryButtonClicked);
connect(m_ui.removeSearchDirectoryButton, &QPushButton::clicked, this,
&GameListSettingsWidget::onRemoveSearchDirectoryButtonClicked);
connect(m_ui.rescanAllGames, &QPushButton::clicked, this, &GameListSettingsWidget::onRescanAllGamesClicked);
connect(m_ui.scanForNewGames, &QPushButton::clicked, this, &GameListSettingsWidget::onScanForNewGamesClicked);
2019-12-31 06:17:17 +00:00
}
GameListSettingsWidget::~GameListSettingsWidget() = default;
void GameListSettingsWidget::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
QtUtils::ResizeColumnsForTableView(m_ui.searchDirectoryList, {-1, 100});
}
void GameListSettingsWidget::onDirectoryListItemClicked(const QModelIndex& index)
{
if (!index.isValid())
return;
const int row = index.row();
const int column = index.column();
if (column != 1)
return;
m_search_directories_model->setEntryRecursive(row, !m_search_directories_model->isEntryRecursive(row));
}
void GameListSettingsWidget::onDirectoryListContextMenuRequested(const QPoint& point)
{
QModelIndexList selection = m_ui.searchDirectoryList->selectionModel()->selectedIndexes();
if (selection.size() < 1)
return;
const int row = selection[0].row();
QMenu menu;
menu.addAction(tr("Remove"), [this, row]() { m_search_directories_model->removeEntry(row); });
menu.addSeparator();
menu.addAction(tr("Open Directory..."),
[this, row]() { m_search_directories_model->openEntryInExplorer(this, row); });
menu.exec(m_ui.searchDirectoryList->mapToGlobal(point));
}
void GameListSettingsWidget::addSearchDirectory(QWidget* parent_widget)
2019-12-31 06:17:17 +00:00
{
QString dir =
QDir::toNativeSeparators(QFileDialog::getExistingDirectory(parent_widget, tr("Select Search Directory")));
2019-12-31 06:17:17 +00:00
if (dir.isEmpty())
return;
QMessageBox::StandardButton selection =
QMessageBox::question(this, tr("Scan Recursively?"),
tr("Would you like to scan the directory \"%1\" recursively?\n\nScanning recursively takes "
"more time, but will identify files in subdirectories.")
.arg(dir),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if (selection == QMessageBox::Cancel)
return;
const bool recursive = (selection == QMessageBox::Yes);
m_search_directories_model->addEntry(dir, recursive);
}
void GameListSettingsWidget::onAddSearchDirectoryButtonClicked()
{
addSearchDirectory(this);
2019-12-31 06:17:17 +00:00
}
void GameListSettingsWidget::onRemoveSearchDirectoryButtonClicked()
2019-12-31 06:17:17 +00:00
{
QModelIndexList selection = m_ui.searchDirectoryList->selectionModel()->selectedIndexes();
if (selection.size() < 1)
return;
const int row = selection[0].row();
m_search_directories_model->removeEntry(row);
}
void GameListSettingsWidget::onRescanAllGamesClicked()
2019-12-31 06:17:17 +00:00
{
m_host_interface->refreshGameList(true, false);
}
void GameListSettingsWidget::onScanForNewGamesClicked()
{
m_host_interface->refreshGameList(false, false);
2019-12-31 06:17:17 +00:00
}