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

0001 // SPDX-FileCopyrightText: 2003-2010 Jesper K. Pedersen <blackie@kde.org>
0002 // SPDX-FileCopyrightText: 2021-2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 #ifndef SEARCHBAR_H
0007 #define SEARCHBAR_H
0008 
0009 #include <QEvent>
0010 #include <ktoolbar.h>
0011 
0012 class QLineEdit;
0013 class KMainWindow;
0014 
0015 namespace MainWindow
0016 {
0017 
0018 /**
0019  * @brief The SearchBar class is a thin wrapper around a search box (that is a QLineEdit).
0020  * It makes the search box usable as a toolbar.
0021  *
0022  * It also installs an event filter for the search box that clears the search text when the escape key is pressed,
0023  * and exposes movement keys via signal.
0024  *
0025  * ## Signals:
0026  *
0027  * The textChanged() and returnPressed() signals are what you would expect from a QLineEdit.
0028  * Additionally, there is cleared() signal that is emitted when the SearchBar is reset,
0029  * and keyPressed() that is emitted for a a few special keys.
0030  */
0031 class SearchBar : public KToolBar
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     explicit SearchBar(KMainWindow *parent);
0037 
0038 protected:
0039     bool eventFilter(QObject *watched, QEvent *e) override;
0040 
0041 public Q_SLOTS:
0042     /**
0043      * @brief Clears the content of the search box.
0044      */
0045     void clear();
0046     /**
0047      * @brief setLineEditEnabled calls setEnabled() on the search box.
0048      * @param enabled
0049      */
0050     void setLineEditEnabled(bool enabled);
0051 
0052 Q_SIGNALS:
0053     /**
0054      * @see QLineEdit::textChanged
0055      */
0056     void textChanged(const QString &);
0057     /**
0058      * @see QLineEdit::returnPressed
0059      */
0060     void returnPressed();
0061     /**
0062      * @brief cleared is emitted whenever the search box contents are cleared.
0063      * This can happen either via key press (Escape key) or programatically.
0064      */
0065     void cleared();
0066     /**
0067      * @brief movementKeyPressed is emitted when a movement key is pressed.
0068      * QKeyEvents that are signalled this way are:
0069      *  - arrow keys
0070      *  - Page up and Page down keys
0071      *  - Home and End keys
0072      */
0073     void movementKeyPressed(QKeyEvent *);
0074 
0075 private:
0076     QLineEdit *m_edit;
0077 };
0078 }
0079 
0080 #endif /* SEARCHBAR_H */
0081 
0082 // vi:expandtab:tabstop=4 shiftwidth=4: