1/***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20#ifndef DOLPHINSEARCHBOX_H
21#define DOLPHINSEARCHBOX_H
22
23#include <KUrl>
24#include <QList>
25#include <QWidget>
26
27class DolphinFacetsWidget;
28class KLineEdit;
29class KSeparator;
30class QToolButton;
31class QScrollArea;
32class QLabel;
33class QVBoxLayout;
34
35/**
36 * @brief Input box for searching files with or without Baloo.
37 *
38 * The widget allows to specify:
39 * - Where to search: Everywhere or below the current directory
40 * - What to search: Filenames or content
41 *
42 * If Baloo is available and the current folder is indexed, further
43 * options are offered.
44 */
45class DolphinSearchBox : public QWidget {
46 Q_OBJECT
47
48public:
49 explicit DolphinSearchBox(QWidget* parent = 0);
50 virtual ~DolphinSearchBox();
51
52 /**
53 * Sets the text that should be used as input for
54 * searching.
55 */
56 void setText(const QString& text);
57
58 /**
59 * Returns the text that should be used as input
60 * for searching.
61 */
62 QString text() const;
63
64 /**
65 * Sets the current path that is used as root for
66 * searching files, if "From Here" has been selected.
67 */
68 void setSearchPath(const KUrl& url);
69 KUrl searchPath() const;
70
71 /** @return URL that will start the searching of files. */
72 KUrl urlForSearching() const;
73
74 /**
75 * Extracts information from the given search \a url to
76 * initialize the search box properly.
77 */
78 void fromSearchUrl(const KUrl& url);
79
80 /**
81 * Selects the whole text of the search box.
82 */
83 void selectAll();
84
85 /**
86 * Set the search box to the active mode, if \a active
87 * is true. The active mode is default. The inactive mode only differs
88 * visually from the active mode, no change of the behavior is given.
89 *
90 * Using the search box in the inactive mode is useful when having split views,
91 * where the inactive view is indicated by an search box visually.
92 */
93 void setActive(bool active);
94
95 /**
96 * @return True, if the search box is in the active mode.
97 * @see DolphinSearchBox::setActive()
98 */
99 bool isActive() const;
100
101protected:
102 virtual bool event(QEvent* event);
103 virtual void showEvent(QShowEvent* event);
104 virtual void keyReleaseEvent(QKeyEvent* event);
105 virtual bool eventFilter(QObject* obj, QEvent* event);
106
107signals:
108 /**
109 * Is emitted when a searching should be triggered.
110 */
111 void searchRequest();
112
113 /**
114 * Is emitted when the user has changed a character of
115 * the text that should be used as input for searching.
116 */
117 void searchTextChanged(const QString& text);
118
119 void returnPressed(const QString& text);
120
121 /**
122 * Emitted as soon as the search box should get closed.
123 */
124 void closeRequest();
125
126 /**
127 * Is emitted, if the searchbox has been activated by
128 * an user interaction
129 * @see DolphinSearchBox::setActive()
130 */
131 void activated();
132
133private slots:
134 void emitSearchRequest();
135 void emitCloseRequest();
136 void slotConfigurationChanged();
137 void slotSearchTextChanged(const QString& text);
138 void slotReturnPressed(const QString& text);
139 void slotFacetsButtonToggled();
140 void slotFacetChanged();
141
142private:
143 void initButton(QToolButton* button);
144 void loadSettings();
145 void saveSettings();
146 void init();
147
148 /**
149 * @return URL that represents the Baloo query for starting the search.
150 */
151 KUrl balooUrlForSearching() const;
152
153 /**
154 * Extracts information from the given Baloo search \a url to
155 * initialize the search box properly.
156 */
157 void fromBalooSearchUrl(const KUrl& url);
158
159 void updateFacetsToggleButton();
160private:
161 bool m_startedSearching;
162 bool m_active;
163
164 QVBoxLayout* m_topLayout;
165
166 QLabel* m_searchLabel;
167 KLineEdit* m_searchInput;
168 QScrollArea* m_optionsScrollArea;
169 QToolButton* m_fileNameButton;
170 QToolButton* m_contentButton;
171 KSeparator* m_separator;
172 QToolButton* m_fromHereButton;
173 QToolButton* m_everywhereButton;
174 QToolButton* m_facetsToggleButton;
175 DolphinFacetsWidget* m_facetsWidget;
176
177 KUrl m_searchPath;
178
179 QTimer* m_startSearchTimer;
180};
181
182#endif
183