1 /*
2  * Copyright (C) Pedram Pourang (aka Tsu Jan) 2014-2019 <tsujan2000@gmail.com>
3  *
4  * FeatherPad is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * FeatherPad is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * @license GPL-3.0+ <https://spdx.org/licenses/GPL-3.0+.html>
18  */
19 
20 #ifndef SEARCHBAR_H
21 #define SEARCHBAR_H
22 
23 #include <QPointer>
24 #include <QToolButton>
25 #include <QComboBox>
26 #include <QStandardItemModel>
27 #include "lineedit.h"
28 
29 namespace FeatherPad {
30 
31 class ComboBox : public QComboBox
32 {
33     Q_OBJECT
34 public:
35     enum Move {NoMove=0 , MoveUp , MoveDown , MoveFirst , MoveLast};
36 
37     ComboBox (QWidget *parent = nullptr) :
QComboBox(parent)38         QComboBox (parent), hasPopup_ (false) {}
~ComboBox()39     ~ComboBox() {}
40 
41     bool hasPopup() const;
42     void showPopup() override;
43     void hidePopup() override;
44 
45 signals:
46     void moveInHistory (int move);
47 
48 protected:
49     void keyPressEvent (QKeyEvent *event) override;
50 
51 private:
52     bool hasPopup_;
53 };
54 
55 class SearchBar : public QFrame
56 {
57     Q_OBJECT
58 public:
59     SearchBar (QWidget *parent = nullptr,
60                const QList<QKeySequence>& shortcuts = QList<QKeySequence>(),
61                Qt::WindowFlags f = Qt::WindowFlags());
62 
63     void setSearchModel (QStandardItemModel *model);
64     void focusLineEdit();
65     bool lineEditHasFocus() const;
66     QString searchEntry() const;
67     void clearSearchEntry();
68 
69     bool matchCase() const;
70     bool matchWhole() const;
71     bool matchRegex() const;
72 
73     bool hasPopup() const;
74 
75     void updateShortcuts (bool disable);
76 
77 signals:
78     void searchFlagChanged();
79     void find (bool forward);
80 
81 private:
82     void searchStarted();
83     void findForward();
84     void findBackward();
85 
86     QPointer<LineEdit> lineEdit_;
87     QPointer<ComboBox> combo_;
88     QPointer<QToolButton> toolButton_nxt_;
89     QPointer<QToolButton> toolButton_prv_;
90     QPointer<QToolButton> button_case_;
91     QPointer<QToolButton> button_whole_;
92     QPointer<QToolButton> button_regex_;
93     QList<QKeySequence> shortcuts_;
94     bool searchStarted_;
95     QString searchText_;
96 };
97 
98 }
99 
100 #endif // SEARCHBAR_H
101