Improve keyboard navigation in game list (#1132)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions

* Improve keyboard navigation

* don't start game in elf viewer mode or gridview mode with empty item selected

* fix eventFilter return
This commit is contained in:
tGecko 2024-09-28 20:04:47 +02:00 committed by GitHub
parent 7b5d66e5c1
commit dc96338c2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 67 additions and 38 deletions

View file

@ -22,7 +22,7 @@ GameGridFrame::GameGridFrame(std::shared_ptr<GameInfoClass> game_info_get, QWidg
this->setContextMenuPolicy(Qt::CustomContextMenu); this->setContextMenuPolicy(Qt::CustomContextMenu);
PopulateGameGrid(m_game_info->m_games, false); PopulateGameGrid(m_game_info->m_games, false);
connect(this, &QTableWidget::cellClicked, this, &GameGridFrame::SetGridBackgroundImage); connect(this, &QTableWidget::currentCellChanged, this, &GameGridFrame::onCurrentCellChanged);
connect(this->verticalScrollBar(), &QScrollBar::valueChanged, this, connect(this->verticalScrollBar(), &QScrollBar::valueChanged, this,
&GameGridFrame::RefreshGridBackgroundImage); &GameGridFrame::RefreshGridBackgroundImage);
@ -31,22 +31,33 @@ GameGridFrame::GameGridFrame(std::shared_ptr<GameInfoClass> game_info_get, QWidg
connect(this, &QTableWidget::customContextMenuRequested, this, [=, this](const QPoint& pos) { connect(this, &QTableWidget::customContextMenuRequested, this, [=, this](const QPoint& pos) {
m_gui_context_menus.RequestGameMenu(pos, m_game_info->m_games, this, false); m_gui_context_menus.RequestGameMenu(pos, m_game_info->m_games, this, false);
}); });
connect(this, &QTableWidget::cellClicked, this, [&]() {
cellClicked = true;
crtRow = this->currentRow();
crtColumn = this->currentColumn();
columnCnt = this->columnCount();
});
} }
void GameGridFrame::PlayBackgroundMusic(QTableWidgetItem* item) { void GameGridFrame::onCurrentCellChanged(int currentRow, int currentColumn, int previousRow,
if (!item) { int previousColumn) {
cellClicked = true;
crtRow = currentRow;
crtColumn = currentColumn;
columnCnt = this->columnCount();
auto itemID = (crtRow * columnCnt) + currentColumn;
if (itemID > m_game_info->m_games.count() - 1) {
validCellSelected = false;
BackgroundMusicPlayer::getInstance().stopMusic(); BackgroundMusicPlayer::getInstance().stopMusic();
return; return;
} }
QString snd0path; validCellSelected = true;
Common::FS::PathToQString(snd0path, m_game_info->m_games[item->row()].snd0_path); SetGridBackgroundImage(crtRow, crtColumn);
BackgroundMusicPlayer::getInstance().playMusic(snd0path); auto snd0Path = QString::fromStdString(m_game_info->m_games[itemID].snd0_path.string());
PlayBackgroundMusic(snd0Path);
}
void GameGridFrame::PlayBackgroundMusic(QString path) {
if (path.isEmpty()) {
BackgroundMusicPlayer::getInstance().stopMusic();
return;
}
BackgroundMusicPlayer::getInstance().playMusic(path);
} }
void GameGridFrame::PopulateGameGrid(QVector<GameInfo> m_games_search, bool fromSearch) { void GameGridFrame::PopulateGameGrid(QVector<GameInfo> m_games_search, bool fromSearch) {
@ -157,3 +168,7 @@ void GameGridFrame::RefreshGridBackgroundImage() {
this->setPalette(palette); this->setPalette(palette);
} }
} }
bool GameGridFrame::IsValidCellSelected() {
return validCellSelected;
}

View file

@ -20,7 +20,9 @@ Q_SIGNALS:
public Q_SLOTS: public Q_SLOTS:
void SetGridBackgroundImage(int row, int column); void SetGridBackgroundImage(int row, int column);
void RefreshGridBackgroundImage(); void RefreshGridBackgroundImage();
void PlayBackgroundMusic(QTableWidgetItem* item); void PlayBackgroundMusic(QString path);
void onCurrentCellChanged(int currentRow, int currentColumn, int previousRow,
int previousColumn);
private: private:
QImage backgroundImage; QImage backgroundImage;
@ -28,10 +30,12 @@ private:
GuiContextMenus m_gui_context_menus; GuiContextMenus m_gui_context_menus;
std::shared_ptr<GameInfoClass> m_game_info; std::shared_ptr<GameInfoClass> m_game_info;
std::shared_ptr<QVector<GameInfo>> m_games_shared; std::shared_ptr<QVector<GameInfo>> m_games_shared;
bool validCellSelected = false;
public: public:
explicit GameGridFrame(std::shared_ptr<GameInfoClass> game_info_get, QWidget* parent = nullptr); explicit GameGridFrame(std::shared_ptr<GameInfoClass> game_info_get, QWidget* parent = nullptr);
void PopulateGameGrid(QVector<GameInfo> m_games, bool fromSearch); void PopulateGameGrid(QVector<GameInfo> m_games, bool fromSearch);
bool IsValidCellSelected();
bool cellClicked = false; bool cellClicked = false;
int icon_size; int icon_size;

View file

@ -41,7 +41,7 @@ GameListFrame::GameListFrame(std::shared_ptr<GameInfoClass> game_info_get, QWidg
this->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Fixed); this->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Fixed);
PopulateGameList(); PopulateGameList();
connect(this, &QTableWidget::itemClicked, this, &GameListFrame::SetListBackgroundImage); connect(this, &QTableWidget::currentCellChanged, this, &GameListFrame::onCurrentCellChanged);
connect(this->verticalScrollBar(), &QScrollBar::valueChanged, this, connect(this->verticalScrollBar(), &QScrollBar::valueChanged, this,
&GameListFrame::RefreshListBackgroundImage); &GameListFrame::RefreshListBackgroundImage);
connect(this->horizontalScrollBar(), &QScrollBar::valueChanged, this, connect(this->horizontalScrollBar(), &QScrollBar::valueChanged, this,
@ -69,6 +69,16 @@ GameListFrame::GameListFrame(std::shared_ptr<GameInfoClass> game_info_get, QWidg
}); });
} }
void GameListFrame::onCurrentCellChanged(int currentRow, int currentColumn, int previousRow,
int previousColumn) {
QTableWidgetItem* item = this->item(currentRow, currentColumn);
if (!item) {
return;
}
SetListBackgroundImage(item);
PlayBackgroundMusic(item);
}
void GameListFrame::PlayBackgroundMusic(QTableWidgetItem* item) { void GameListFrame::PlayBackgroundMusic(QTableWidgetItem* item) {
if (!item) { if (!item) {
BackgroundMusicPlayer::getInstance().stopMusic(); BackgroundMusicPlayer::getInstance().stopMusic();

View file

@ -23,6 +23,8 @@ public Q_SLOTS:
void SortNameAscending(int columnIndex); void SortNameAscending(int columnIndex);
void SortNameDescending(int columnIndex); void SortNameDescending(int columnIndex);
void PlayBackgroundMusic(QTableWidgetItem* item); void PlayBackgroundMusic(QTableWidgetItem* item);
void onCurrentCellChanged(int currentRow, int currentColumn, int previousRow,
int previousColumn);
private: private:
void SetTableItem(int row, int column, QString itemStr); void SetTableItem(int row, int column, QString itemStr);
@ -63,4 +65,4 @@ public:
} }
return false; return false;
} }
}; };

View file

@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include <QDockWidget> #include <QDockWidget>
#include <QKeyEvent>
#include <QProgressDialog> #include <QProgressDialog>
#include <common/scm_rev.h> #include <common/scm_rev.h>
@ -21,6 +22,7 @@
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this); ui->setupUi(this);
installEventFilter(this);
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
} }
@ -306,6 +308,7 @@ void MainWindow::CreateConnects() {
}); });
// List // List
connect(ui->setlistModeListAct, &QAction::triggered, m_dock_widget.data(), [this]() { connect(ui->setlistModeListAct, &QAction::triggered, m_dock_widget.data(), [this]() {
BackgroundMusicPlayer::getInstance().stopMusic();
m_dock_widget->setWidget(m_game_list_frame.data()); m_dock_widget->setWidget(m_game_list_frame.data());
m_game_grid_frame->hide(); m_game_grid_frame->hide();
m_elf_viewer->hide(); m_elf_viewer->hide();
@ -322,6 +325,7 @@ void MainWindow::CreateConnects() {
}); });
// Grid // Grid
connect(ui->setlistModeGridAct, &QAction::triggered, m_dock_widget.data(), [this]() { connect(ui->setlistModeGridAct, &QAction::triggered, m_dock_widget.data(), [this]() {
BackgroundMusicPlayer::getInstance().stopMusic();
m_dock_widget->setWidget(m_game_grid_frame.data()); m_dock_widget->setWidget(m_game_grid_frame.data());
m_game_grid_frame->show(); m_game_grid_frame->show();
m_game_list_frame->hide(); m_game_list_frame->hide();
@ -338,6 +342,7 @@ void MainWindow::CreateConnects() {
}); });
// Elf // Elf
connect(ui->setlistElfAct, &QAction::triggered, m_dock_widget.data(), [this]() { connect(ui->setlistElfAct, &QAction::triggered, m_dock_widget.data(), [this]() {
BackgroundMusicPlayer::getInstance().stopMusic();
m_dock_widget->setWidget(m_elf_viewer.data()); m_dock_widget->setWidget(m_elf_viewer.data());
m_game_grid_frame->hide(); m_game_grid_frame->hide();
m_game_list_frame->hide(); m_game_list_frame->hide();
@ -512,29 +517,6 @@ void MainWindow::CreateConnects() {
isIconBlack = false; isIconBlack = false;
} }
}); });
connect(m_game_grid_frame.get(), &QTableWidget::cellClicked, this,
&MainWindow::PlayBackgroundMusic);
connect(m_game_list_frame.get(), &QTableWidget::cellClicked, this,
&MainWindow::PlayBackgroundMusic);
}
void MainWindow::PlayBackgroundMusic() {
if (isGameRunning || !Config::getPlayBGM()) {
BackgroundMusicPlayer::getInstance().stopMusic();
return;
}
int itemID = isTableList ? m_game_list_frame->currentItem()->row()
: m_game_grid_frame->crtRow * m_game_grid_frame->columnCnt +
m_game_grid_frame->crtColumn;
if (itemID > m_game_info->m_games.size() - 1) {
// Can happen in grid mode
BackgroundMusicPlayer::getInstance().stopMusic();
return;
}
QString snd0path;
Common::FS::PathToQString(snd0path, m_game_info->m_games[itemID].snd0_path);
BackgroundMusicPlayer::getInstance().playMusic(snd0path);
} }
void MainWindow::StartGame() { void MainWindow::StartGame() {
@ -1047,3 +1029,17 @@ void MainWindow::OnLanguageChanged(const std::string& locale) {
LoadTranslation(); LoadTranslation();
} }
bool MainWindow::eventFilter(QObject* obj, QEvent* event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
auto tblMode = Config::getTableMode();
if (tblMode != 2 && (tblMode != 1 || m_game_grid_frame->IsValidCellSelected())) {
StartGame();
return true;
}
}
}
return QMainWindow::eventFilter(obj, event);
}

View file

@ -94,6 +94,8 @@ private:
QTranslator* translator; QTranslator* translator;
protected: protected:
bool eventFilter(QObject* obj, QEvent* event) override;
void dragEnterEvent(QDragEnterEvent* event1) override { void dragEnterEvent(QDragEnterEvent* event1) override {
if (event1->mimeData()->hasUrls()) { if (event1->mimeData()->hasUrls()) {
event1->acceptProposedAction(); event1->acceptProposedAction();