| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/SearchBar.h" |
| 5 | |
| 6 | #include <QEvent> |
| 7 | #include <QHBoxLayout> |
| 8 | #include <QKeyEvent> |
| 9 | #include <QLineEdit> |
| 10 | #include <QPushButton> |
| 11 | |
| 12 | SearchBar::SearchBar(QWidget* parent) : QWidget(parent) |
| 13 | { |
| 14 | CreateWidgets(); |
| 15 | ConnectWidgets(); |
| 16 | |
| 17 | setFixedHeight(32); |
| 18 | |
| 19 | setHidden(true); |
| 20 | |
| 21 | installEventFilter(this); |
| 22 | } |
| 23 | |
| 24 | void SearchBar::CreateWidgets() |
| 25 | { |
| 26 | m_search_edit = new QLineEdit; |
| 27 | m_close_button = new QPushButton(tr("Close")); |
| 28 | |
| 29 | m_search_edit->setPlaceholderText(tr("Search games...")); |
| 30 | |
| 31 | auto* layout = new QHBoxLayout; |
| 32 | |
| 33 | layout->addWidget(m_search_edit); |
| 34 | layout->addWidget(m_close_button); |
| 35 | layout->setSizeConstraint(QLayout::SetMinAndMaxSize); |
| 36 | |
| 37 | setLayout(layout); |
| 38 | } |
| 39 | |
| 40 | void SearchBar::Show() |
| 41 | { |
| 42 | m_search_edit->setFocus(); |
| 43 | m_search_edit->selectAll(); |
| 44 | |
| 45 | // Re-apply the filter string. |
| 46 | emit Search(m_search_edit->text()); |
| 47 | |
| 48 | show(); |
| 49 | } |
| 50 | |
| 51 | void SearchBar::Hide() |
| 52 | { |
| 53 | // Clear the filter string. |
| 54 | emit Search(QString()); |
| 55 | |
| 56 | m_search_edit->clearFocus(); |
| 57 | |
| 58 | hide(); |
| 59 | } |
| 60 | |
| 61 | void SearchBar::ConnectWidgets() |
| 62 | { |
| 63 | connect(m_search_edit, &QLineEdit::textChanged, this, &SearchBar::Search); |
| 64 | connect(m_close_button, &QPushButton::clicked, this, &SearchBar::Hide); |
| 65 | } |
| 66 | |
| 67 | bool SearchBar::eventFilter(QObject* object, QEvent* event) |
| 68 | { |
| 69 | if (event->type() == QEvent::KeyPress) |
| 70 | { |
| 71 | if (static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape) |
| 72 | Hide(); |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |