File indexing completed on 2024-04-28 17:03:10

0001 /*
0002  * SPDX-FileCopyrightText: 2010 Peter Penz <peter.penz19@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef DOLPHINSEARCHBOX_H
0008 #define DOLPHINSEARCHBOX_H
0009 
0010 #include <QUrl>
0011 #include <QWidget>
0012 
0013 class DolphinFacetsWidget;
0014 class DolphinQuery;
0015 class QLineEdit;
0016 class KSeparator;
0017 class QToolButton;
0018 class QScrollArea;
0019 class QLabel;
0020 class QVBoxLayout;
0021 
0022 /**
0023  * @brief Input box for searching files with or without Baloo.
0024  *
0025  * The widget allows to specify:
0026  * - Where to search: Everywhere or below the current directory
0027  * - What to search: Filenames or content
0028  *
0029  * If Baloo is available and the current folder is indexed, further
0030  * options are offered.
0031  */
0032 class DolphinSearchBox : public QWidget
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     explicit DolphinSearchBox(QWidget *parent = nullptr);
0038     ~DolphinSearchBox() override;
0039 
0040     /**
0041      * Sets the text that should be used as input for
0042      * searching.
0043      */
0044     void setText(const QString &text);
0045 
0046     /**
0047      * Returns the text that should be used as input
0048      * for searching.
0049      */
0050     QString text() const;
0051 
0052     /**
0053      * Sets the current path that is used as root for searching files.
0054      * If @url is the Home dir, "From Here" is selected instead.
0055      */
0056     void setSearchPath(const QUrl &url);
0057     QUrl searchPath() const;
0058 
0059     /** @return URL that will start the searching of files. */
0060     QUrl urlForSearching() const;
0061 
0062     /**
0063      * Extracts information from the given search \a url to
0064      * initialize the search box properly.
0065      */
0066     void fromSearchUrl(const QUrl &url);
0067 
0068     /**
0069      * Selects the whole text of the search box.
0070      */
0071     void selectAll();
0072 
0073     /**
0074      * Set the search box to the active mode, if \a active
0075      * is true. The active mode is default. The inactive mode only differs
0076      * visually from the active mode, no change of the behavior is given.
0077      *
0078      * Using the search box in the inactive mode is useful when having split views,
0079      * where the inactive view is indicated by an search box visually.
0080      */
0081     void setActive(bool active);
0082 
0083     /**
0084      * @return True, if the search box is in the active mode.
0085      * @see    DolphinSearchBox::setActive()
0086      */
0087     bool isActive() const;
0088 
0089 protected:
0090     bool event(QEvent *event) override;
0091     void showEvent(QShowEvent *event) override;
0092     void hideEvent(QHideEvent *event) override;
0093     void keyReleaseEvent(QKeyEvent *event) override;
0094     bool eventFilter(QObject *obj, QEvent *event) override;
0095 
0096 Q_SIGNALS:
0097     /**
0098      * Is emitted when a searching should be triggered.
0099      */
0100     void searchRequest();
0101 
0102     /**
0103      * Is emitted when the user has changed a character of
0104      * the text that should be used as input for searching.
0105      */
0106     void searchTextChanged(const QString &text);
0107 
0108     /**
0109      * Emitted as soon as the search box should get closed.
0110      */
0111     void closeRequest();
0112 
0113     /**
0114      * Is emitted when the search box should be opened.
0115      */
0116     void openRequest();
0117 
0118     /**
0119      * Is emitted, if the searchbox has been activated by
0120      * an user interaction
0121      * @see DolphinSearchBox::setActive()
0122      */
0123     void activated();
0124     void focusViewRequest();
0125 
0126 private Q_SLOTS:
0127     void emitSearchRequest();
0128     void emitCloseRequest();
0129     void slotConfigurationChanged();
0130     void slotSearchTextChanged(const QString &text);
0131     void slotReturnPressed();
0132     void slotFacetChanged();
0133     void slotSearchSaved();
0134 
0135 private:
0136     void initButton(QToolButton *button);
0137     void loadSettings();
0138     void saveSettings();
0139     void init();
0140 
0141     /**
0142      * @return URL that represents the Baloo query for starting the search.
0143      */
0144     QUrl balooUrlForSearching() const;
0145 
0146     /**
0147      * Sets the searchbox UI with the parameters established by the \a query
0148      */
0149     void updateFromQuery(const DolphinQuery &query);
0150 
0151     void updateFacetsVisible();
0152 
0153     bool isIndexingEnabled() const;
0154 
0155 private:
0156     QString queryTitle(const QString &text) const;
0157 
0158     bool m_startedSearching;
0159     bool m_active;
0160 
0161     QVBoxLayout *m_topLayout;
0162 
0163     QLineEdit *m_searchInput;
0164     QAction *m_saveSearchAction;
0165     QScrollArea *m_optionsScrollArea;
0166     QToolButton *m_fileNameButton;
0167     QToolButton *m_contentButton;
0168     KSeparator *m_separator;
0169     QToolButton *m_fromHereButton;
0170     QToolButton *m_everywhereButton;
0171     DolphinFacetsWidget *m_facetsWidget;
0172 
0173     QUrl m_searchPath;
0174 
0175     QTimer *m_startSearchTimer;
0176 };
0177 
0178 #endif