File indexing completed on 2024-04-28 05:50:54

0001 /*
0002     SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef INCREMENTALSEARCHBAR_H
0008 #define INCREMENTALSEARCHBAR_H
0009 
0010 // Qt
0011 #include <QBitArray>
0012 #include <QWidget>
0013 
0014 #include "konsoleprivate_export.h"
0015 
0016 class QAction;
0017 class QTimer;
0018 class QLineEdit;
0019 class QToolButton;
0020 
0021 namespace Konsole
0022 {
0023 /**
0024  * A widget which allows users to search incrementally through a document for a
0025  * a text string or regular expression.
0026  *
0027  * The widget consists of a text box into which the user can enter their search text and
0028  * buttons to trigger a search for the next and previous matches for the search text.
0029  *
0030  * When the search text is changed, the searchChanged() signal is emitted.  A search through
0031  * the document for the new text should begin immediately and the active view of the document
0032  * should jump to display any matches if found.  setFoundMatch() should be called whenever the
0033  * search text changes to indicate whether a match for the text was found in the document.
0034  *
0035  * findNextClicked() and findPreviousClicked() signals are emitted when the user presses buttons
0036  * to find next and previous matches respectively.
0037  *
0038  * The first indicates whether searches are case sensitive.
0039  * The matchCaseToggled() signal is emitted when this is changed.
0040  * The second indicates whether the search text should be treated as a plain string or
0041  * as a regular expression.
0042  * The matchRegExpToggled() signal is emitted when this is changed.
0043  */
0044 class KONSOLEPRIVATE_EXPORT IncrementalSearchBar : public QWidget
0045 {
0046     Q_OBJECT
0047 
0048 public:
0049     /**
0050      * This enum defines the options that can be checked.
0051      */
0052     enum SearchOptions {
0053         /** Highlight all matches */
0054         HighlightMatches = 0,
0055         /** Searches are case-sensitive or not */
0056         MatchCase = 1,
0057         /** Searches use regular expressions */
0058         RegExp = 2,
0059         /** Search from the bottom and up **/
0060         ReverseSearch = 3,
0061     };
0062 
0063     /**
0064      * Constructs a new incremental search bar with the given parent widget
0065      */
0066     explicit IncrementalSearchBar(QWidget *parent = nullptr);
0067 
0068     /* Returns search options that are checked */
0069     const QBitArray optionsChecked();
0070 
0071     /**
0072      * Sets an indicator for the user as to whether or not a match for the
0073      * current search text was found in the document.
0074      *
0075      * The indicator will not be shown if the search text is empty ( because
0076      * the user has not yet entered a query ).
0077      *
0078      * @param match True if a match was found or false otherwise.  If true,
0079      * and the search text is non-empty, an indicator that no matches were
0080      * found will be shown.
0081      */
0082     void setFoundMatch(bool match);
0083 
0084     /** Returns the current search text */
0085     QString searchText();
0086 
0087     void setSearchText(const QString &text);
0088 
0089     void focusLineEdit();
0090 
0091     void setOptions();
0092 
0093     // reimplemented
0094     void setVisible(bool visible) override;
0095 Q_SIGNALS:
0096     /** Emitted when the text entered in the search box is altered */
0097     void searchChanged(const QString &text);
0098     /** Emitted when the user clicks the button to find the next match */
0099     void findNextClicked();
0100     /** Emitted when the user clicks the button to find the previous match */
0101     void findPreviousClicked();
0102     /** The search from beginning/end button */
0103     void searchFromClicked();
0104     /**
0105      * Emitted when the user toggles the checkbox to indicate whether
0106      * matches for the search text should be highlighted
0107      */
0108     void highlightMatchesToggled(bool);
0109     /**
0110      * Emitted when the user toggles the checkbox to indicate whether
0111      * the search direction should be reversed.
0112      */
0113     void reverseSearchToggled(bool);
0114     /**
0115      * Emitted when the user toggles the checkbox to indicate whether
0116      * matching for the search text should be case sensitive
0117      */
0118     void matchCaseToggled(bool);
0119     /**
0120      * Emitted when the user toggles the checkbox to indicate whether
0121      * the search text should be treated as a plain string or a regular expression
0122      */
0123     void matchRegExpToggled(bool);
0124     /** Emitted when the close button is clicked */
0125     void closeClicked();
0126     /** Emitted when the return button is pressed in the search box */
0127     void searchReturnPressed(const QString &text);
0128     /** Emitted when shift+return buttons are pressed in the search box */
0129     void searchShiftPlusReturnPressed();
0130     /** A movement key not handled is forwarded to the terminal display */
0131     void unhandledMovementKeyPressed(QKeyEvent *event);
0132 
0133 protected:
0134     bool eventFilter(QObject *watched, QEvent *event) override;
0135     void keyPressEvent(QKeyEvent *event) override;
0136 public Q_SLOTS:
0137     void clearLineEdit();
0138 
0139 private Q_SLOTS:
0140     void notifySearchChanged();
0141     void updateButtonsAccordingToReverseSearchSetting();
0142 
0143 private:
0144     Q_DISABLE_COPY(IncrementalSearchBar)
0145 
0146     QLineEdit *_searchEdit;
0147     QAction *_caseSensitive;
0148     QAction *_regExpression;
0149     QAction *_highlightMatches;
0150     QAction *_reverseSearch;
0151     QToolButton *_findNextButton;
0152     QToolButton *_findPreviousButton;
0153     QToolButton *_searchFromButton;
0154     QTimer *_searchTimer;
0155 };
0156 }
0157 #endif // INCREMENTALSEARCHBAR_H