2018-03-21 10:13:53 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-03-21 10:13:53 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/SearchBar.h"
|
2018-03-21 10:13:53 +00:00
|
|
|
|
2019-03-17 21:08:59 +00:00
|
|
|
#include <QEvent>
|
2018-03-21 10:13:53 +00:00
|
|
|
#include <QHBoxLayout>
|
2019-03-17 21:08:59 +00:00
|
|
|
#include <QKeyEvent>
|
2018-03-21 10:13:53 +00:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
|
|
SearchBar::SearchBar(QWidget* parent) : QWidget(parent)
|
|
|
|
{
|
|
|
|
CreateWidgets();
|
|
|
|
ConnectWidgets();
|
|
|
|
|
|
|
|
setFixedHeight(32);
|
|
|
|
|
|
|
|
setHidden(true);
|
2019-03-17 21:08:59 +00:00
|
|
|
|
|
|
|
installEventFilter(this);
|
2018-03-21 10:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchBar::CreateWidgets()
|
|
|
|
{
|
|
|
|
m_search_edit = new QLineEdit;
|
|
|
|
m_close_button = new QPushButton(tr("Close"));
|
|
|
|
|
2018-03-24 17:02:17 +00:00
|
|
|
m_search_edit->setPlaceholderText(tr("Search games..."));
|
2018-03-21 10:13:53 +00:00
|
|
|
|
|
|
|
auto* layout = new QHBoxLayout;
|
|
|
|
|
|
|
|
layout->addWidget(m_search_edit);
|
|
|
|
layout->addWidget(m_close_button);
|
2018-03-24 17:08:34 +00:00
|
|
|
layout->setSizeConstraint(QLayout::SetMinAndMaxSize);
|
2018-03-21 10:13:53 +00:00
|
|
|
|
|
|
|
setLayout(layout);
|
|
|
|
}
|
|
|
|
|
2019-03-17 21:08:59 +00:00
|
|
|
void SearchBar::Show()
|
|
|
|
{
|
|
|
|
m_search_edit->setFocus();
|
|
|
|
m_search_edit->selectAll();
|
|
|
|
|
|
|
|
// Re-apply the filter string.
|
|
|
|
emit Search(m_search_edit->text());
|
|
|
|
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchBar::Hide()
|
2018-03-21 10:13:53 +00:00
|
|
|
{
|
2019-03-17 21:08:59 +00:00
|
|
|
// Clear the filter string.
|
|
|
|
emit Search(QString());
|
2018-03-21 10:13:53 +00:00
|
|
|
|
2019-03-17 21:08:59 +00:00
|
|
|
m_search_edit->clearFocus();
|
2018-03-21 10:13:53 +00:00
|
|
|
|
2019-03-17 21:08:59 +00:00
|
|
|
hide();
|
2018-03-21 10:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchBar::ConnectWidgets()
|
|
|
|
{
|
|
|
|
connect(m_search_edit, &QLineEdit::textChanged, this, &SearchBar::Search);
|
2019-07-23 22:18:58 +00:00
|
|
|
connect(m_close_button, &QPushButton::clicked, this, &SearchBar::Hide);
|
2019-03-17 21:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchBar::eventFilter(QObject* object, QEvent* event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::KeyPress)
|
|
|
|
{
|
|
|
|
if (static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape)
|
|
|
|
Hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-03-21 10:13:53 +00:00
|
|
|
}
|