File indexing completed on 2024-04-28 15:40:12

0001 // SPDX-FileCopyrightText: 2003-2020 The KPhotoAlbum Development Team
0002 // SPDX-FileCopyrightText: 2021-2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 #include "SearchBar.h"
0007 
0008 #include <KLocalizedString>
0009 #include <QEvent>
0010 #include <QKeyEvent>
0011 #include <QLineEdit>
0012 #include <kactioncollection.h>
0013 #include <kmainwindow.h>
0014 #include <qapplication.h>
0015 #include <qlabel.h>
0016 
0017 MainWindow::SearchBar::SearchBar(KMainWindow *parent)
0018     : KToolBar(parent)
0019 {
0020     setWindowTitle(i18nc("Name/title of the search bar toolbar widget", "Search Bar"));
0021     setWindowIcon(QIcon::fromTheme(QLatin1String("search")));
0022 
0023     m_edit = new QLineEdit(this);
0024     m_edit->setClearButtonEnabled(true);
0025     m_edit->setPlaceholderText(i18nc("@label:textbox", "Search ..."));
0026 
0027     addWidget(m_edit);
0028     connect(m_edit, &QLineEdit::textChanged, this, &SearchBar::textChanged);
0029     connect(m_edit, &QLineEdit::returnPressed, this, &SearchBar::returnPressed);
0030 
0031     m_edit->installEventFilter(this);
0032     setFocusProxy(m_edit);
0033 }
0034 
0035 bool MainWindow::SearchBar::eventFilter(QObject *, QEvent *e)
0036 {
0037     if (e->type() == QEvent::KeyPress) {
0038         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
0039         if (ke->key() == Qt::Key_Up || ke->key() == Qt::Key_Down || ke->key() == Qt::Key_Left || ke->key() == Qt::Key_Right || ke->key() == Qt::Key_PageDown || ke->key() == Qt::Key_PageUp || ke->key() == Qt::Key_Home || ke->key() == Qt::Key_End) {
0040             Q_EMIT movementKeyPressed(ke);
0041             return true;
0042         } else if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return) {
0043             // If I don't interpret return and enter here, but simply rely
0044             // on QLineEdit itself to Q_EMIT the signal, then it will
0045             // propagate to the main window, and from there be delivered to
0046             // the central widget.
0047             Q_EMIT returnPressed();
0048             return true;
0049         } else if (ke->key() == Qt::Key_Escape)
0050             clear();
0051     }
0052     if (e->type() == QEvent::FocusIn) {
0053         // this ensures that BrowserWidget::slotLimitToMatch is called when the search bar is activated
0054         Q_EMIT textChanged(m_edit->text());
0055     }
0056     return false;
0057 }
0058 
0059 void MainWindow::SearchBar::clear()
0060 {
0061     m_edit->clear();
0062     Q_EMIT cleared();
0063 }
0064 
0065 void MainWindow::SearchBar::setLineEditEnabled(bool enabled)
0066 {
0067     m_edit->setEnabled(enabled);
0068 }
0069 
0070 // vi:expandtab:tabstop=4 shiftwidth=4:
0071 
0072 #include "moc_SearchBar.cpp"