From 3292abb41d36749eabfa1dca1381d3d1deee4f3f Mon Sep 17 00:00:00 2001 From: spycrab Date: Wed, 21 Mar 2018 11:13:53 +0100 Subject: [PATCH] Qt: Implement search --- Source/Core/DolphinQt2/CMakeLists.txt | 1 + Source/Core/DolphinQt2/DolphinQt2.vcxproj | 3 ++ Source/Core/DolphinQt2/GameList/GameList.cpp | 8 +++ Source/Core/DolphinQt2/GameList/GameList.h | 3 +- .../DolphinQt2/GameList/GameListModel.cpp | 9 ++++ .../Core/DolphinQt2/GameList/GameListModel.h | 3 ++ Source/Core/DolphinQt2/MainWindow.cpp | 16 +++++- Source/Core/DolphinQt2/MainWindow.h | 2 + Source/Core/DolphinQt2/MenuBar.cpp | 4 ++ Source/Core/DolphinQt2/MenuBar.h | 1 + Source/Core/DolphinQt2/SearchBar.cpp | 54 +++++++++++++++++++ Source/Core/DolphinQt2/SearchBar.h | 31 +++++++++++ 12 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 Source/Core/DolphinQt2/SearchBar.cpp create mode 100644 Source/Core/DolphinQt2/SearchBar.h diff --git a/Source/Core/DolphinQt2/CMakeLists.txt b/Source/Core/DolphinQt2/CMakeLists.txt index 13c8acb237..f4b7ca78e9 100644 --- a/Source/Core/DolphinQt2/CMakeLists.txt +++ b/Source/Core/DolphinQt2/CMakeLists.txt @@ -26,6 +26,7 @@ set(SRCS MenuBar.cpp RenderWidget.cpp Resources.cpp + SearchBar.cpp Settings.cpp ToolBar.cpp Translation.cpp diff --git a/Source/Core/DolphinQt2/DolphinQt2.vcxproj b/Source/Core/DolphinQt2/DolphinQt2.vcxproj index 59694ab6ce..711d452eec 100644 --- a/Source/Core/DolphinQt2/DolphinQt2.vcxproj +++ b/Source/Core/DolphinQt2/DolphinQt2.vcxproj @@ -122,6 +122,7 @@ + @@ -188,6 +189,7 @@ + @@ -280,6 +282,7 @@ + diff --git a/Source/Core/DolphinQt2/GameList/GameList.cpp b/Source/Core/DolphinQt2/GameList/GameList.cpp index 996d86fe4c..f486a67510 100644 --- a/Source/Core/DolphinQt2/GameList/GameList.cpp +++ b/Source/Core/DolphinQt2/GameList/GameList.cpp @@ -521,3 +521,11 @@ void GameList::OnHeaderViewChanged() QSettings().setValue(QStringLiteral("tableheader/state"), m_list->horizontalHeader()->saveState()); } + +void GameList::SetSearchTerm(const QString& term) +{ + m_model->SetSearchTerm(term); + + m_list_proxy->invalidate(); + m_grid_proxy->invalidate(); +} diff --git a/Source/Core/DolphinQt2/GameList/GameList.h b/Source/Core/DolphinQt2/GameList/GameList.h index d58827dac9..6185d4cd2e 100644 --- a/Source/Core/DolphinQt2/GameList/GameList.h +++ b/Source/Core/DolphinQt2/GameList/GameList.h @@ -27,9 +27,10 @@ public: void SetListView() { SetPreferredView(true); } void SetGridView() { SetPreferredView(false); } void SetViewColumn(int col, bool view) { m_list->setColumnHidden(col, !view); } + void SetSearchTerm(const QString& term); + void OnColumnVisibilityToggled(const QString& row, bool visible); void OnGameListVisibilityChanged(); - signals: void GameSelected(); void NetPlayHost(const QString& game_id); diff --git a/Source/Core/DolphinQt2/GameList/GameListModel.cpp b/Source/Core/DolphinQt2/GameList/GameListModel.cpp index 7dd5cf4d0b..cf0b230052 100644 --- a/Source/Core/DolphinQt2/GameList/GameListModel.cpp +++ b/Source/Core/DolphinQt2/GameList/GameListModel.cpp @@ -150,6 +150,10 @@ bool GameListModel::ShouldDisplayGameListItem(int index) const { const UICommon::GameFile& game = *m_games[index]; + if (!m_term.isEmpty() && + !QString::fromStdString(game.GetName()).contains(m_term, Qt::CaseInsensitive)) + return false; + const bool show_platform = [&game] { switch (game.GetPlatform()) { @@ -244,3 +248,8 @@ int GameListModel::FindGame(const std::string& path) const } return -1; } + +void GameListModel::SetSearchTerm(const QString& term) +{ + m_term = term; +} diff --git a/Source/Core/DolphinQt2/GameList/GameListModel.h b/Source/Core/DolphinQt2/GameList/GameListModel.h index 8a8585da1a..619999a236 100644 --- a/Source/Core/DolphinQt2/GameList/GameListModel.h +++ b/Source/Core/DolphinQt2/GameList/GameListModel.h @@ -36,6 +36,8 @@ public: return QString::fromStdString(m_games[index]->GetUniqueIdentifier()); } bool ShouldDisplayGameListItem(int index) const; + void SetSearchTerm(const QString& term); + enum { COL_PLATFORM = 0, @@ -60,4 +62,5 @@ private: GameTracker m_tracker; QList> m_games; + QString m_term; }; diff --git a/Source/Core/DolphinQt2/MainWindow.cpp b/Source/Core/DolphinQt2/MainWindow.cpp index 3e17cfce56..6b06bd0a12 100644 --- a/Source/Core/DolphinQt2/MainWindow.cpp +++ b/Source/Core/DolphinQt2/MainWindow.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -65,6 +66,7 @@ #include "DolphinQt2/QtUtils/RunOnObject.h" #include "DolphinQt2/QtUtils/WindowActivationEventFilter.h" #include "DolphinQt2/Resources.h" +#include "DolphinQt2/SearchBar.h" #include "DolphinQt2/Settings.h" #include "DolphinQt2/TAS/GCTASInputWindow.h" #include "DolphinQt2/TAS/WiiTASInputWindow.h" @@ -164,6 +166,7 @@ void MainWindow::CreateComponents() { m_menu_bar = new MenuBar(this); m_tool_bar = new ToolBar(this); + m_search_bar = new SearchBar(this); m_game_list = new GameList(this); m_render_widget = new RenderWidget; m_stack = new QStackedWidget(this); @@ -275,6 +278,8 @@ void MainWindow::ConnectMenuBar() // View connect(m_menu_bar, &MenuBar::ShowList, m_game_list, &GameList::SetListView); connect(m_menu_bar, &MenuBar::ShowGrid, m_game_list, &GameList::SetGridView); + connect(m_menu_bar, &MenuBar::ToggleSearch, m_search_bar, &SearchBar::Toggle); + connect(m_menu_bar, &MenuBar::ColumnVisibilityToggled, m_game_list, &GameList::OnColumnVisibilityToggled); @@ -372,7 +377,16 @@ void MainWindow::ConnectRenderWidget() void MainWindow::ConnectStack() { - m_stack->addWidget(m_game_list); + auto* widget = new QWidget; + auto* layout = new QVBoxLayout; + widget->setLayout(layout); + + layout->addWidget(m_game_list); + layout->addWidget(m_search_bar); + + connect(m_search_bar, &SearchBar::Search, m_game_list, &GameList::SetSearchTerm); + + m_stack->addWidget(widget); setCentralWidget(m_stack); diff --git a/Source/Core/DolphinQt2/MainWindow.h b/Source/Core/DolphinQt2/MainWindow.h index f59bef6947..47fa09f287 100644 --- a/Source/Core/DolphinQt2/MainWindow.h +++ b/Source/Core/DolphinQt2/MainWindow.h @@ -30,6 +30,7 @@ class NetPlayClient; class NetPlayDialog; class NetPlayServer; class NetPlaySetupDialog; +class SearchBar; class SettingsWindow; class ControllersWindow; class DragEnterEvent; @@ -137,6 +138,7 @@ private: QStackedWidget* m_stack; ToolBar* m_tool_bar; MenuBar* m_menu_bar; + SearchBar* m_search_bar; GameList* m_game_list; RenderWidget* m_render_widget; bool m_rendering_to_main; diff --git a/Source/Core/DolphinQt2/MenuBar.cpp b/Source/Core/DolphinQt2/MenuBar.cpp index 4fffb2d4cb..8b29277a41 100644 --- a/Source/Core/DolphinQt2/MenuBar.cpp +++ b/Source/Core/DolphinQt2/MenuBar.cpp @@ -357,6 +357,10 @@ void MenuBar::AddViewMenu() view_menu->addSeparator(); AddShowPlatformsMenu(view_menu); AddShowRegionsMenu(view_menu); + + view_menu->addSeparator(); + AddAction(view_menu, tr("Search"), this, &MenuBar::ToggleSearch, + QKeySequence(QStringLiteral("Ctrl+F"))); } void MenuBar::AddOptionsMenu() diff --git a/Source/Core/DolphinQt2/MenuBar.h b/Source/Core/DolphinQt2/MenuBar.h index 3fd11808b1..2c5b164d05 100644 --- a/Source/Core/DolphinQt2/MenuBar.h +++ b/Source/Core/DolphinQt2/MenuBar.h @@ -82,6 +82,7 @@ signals: // View void ShowList(); void ShowGrid(); + void ToggleSearch(); void ColumnVisibilityToggled(const QString& row, bool visible); void GameListPlatformVisibilityToggled(const QString& row, bool visible); void GameListRegionVisibilityToggled(const QString& row, bool visible); diff --git a/Source/Core/DolphinQt2/SearchBar.cpp b/Source/Core/DolphinQt2/SearchBar.cpp new file mode 100644 index 0000000000..d4ccb8638e --- /dev/null +++ b/Source/Core/DolphinQt2/SearchBar.cpp @@ -0,0 +1,54 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "DolphinQt2/SearchBar.h" + +#include +#include +#include +#include + +SearchBar::SearchBar(QWidget* parent) : QWidget(parent) +{ + CreateWidgets(); + ConnectWidgets(); + + setFixedHeight(32); + + setHidden(true); +} + +void SearchBar::CreateWidgets() +{ + m_search_edit = new QLineEdit; + m_close_button = new QPushButton(tr("Close")); + + m_search_edit->setPlaceholderText(tr("Type your search term here")); + + auto* layout = new QHBoxLayout; + + layout->addWidget(m_search_edit); + layout->addWidget(m_close_button); + layout->setMargin(0); + + setLayout(layout); +} + +void SearchBar::Toggle() +{ + m_search_edit->clear(); + + setHidden(isVisible()); + + if (isVisible()) + m_search_edit->setFocus(); + else + m_search_edit->clearFocus(); +} + +void SearchBar::ConnectWidgets() +{ + connect(m_search_edit, &QLineEdit::textChanged, this, &SearchBar::Search); + connect(m_close_button, &QPushButton::pressed, this, &SearchBar::Toggle); +} diff --git a/Source/Core/DolphinQt2/SearchBar.h b/Source/Core/DolphinQt2/SearchBar.h new file mode 100644 index 0000000000..1a4ea7957d --- /dev/null +++ b/Source/Core/DolphinQt2/SearchBar.h @@ -0,0 +1,31 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include + +class QCheckBox; +class QLineEdit; +class QPushButton; + +class SearchBar : public QWidget +{ + Q_OBJECT +public: + explicit SearchBar(QWidget* parent = nullptr); + + void Toggle(); +signals: + void Search(const QString& serach); + +private: + void CreateWidgets(); + void ConnectWidgets(); + + QLineEdit* m_search_edit; + QCheckBox* m_wii_check; + QCheckBox* m_gc_check; + QPushButton* m_close_button; +};