]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
Don't clear the search-text when switching between tabs
[dolphin.git] / src / search / dolphinsearchbox.cpp
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 #include "dolphinsearchbox.h"
21
22 #include "dolphin_searchsettings.h"
23 #include "dolphinsearchinformation.h"
24
25 #include <KIcon>
26 #include <KLineEdit>
27 #include <KLocale>
28 #include <kseparator.h>
29
30 #include <QButtonGroup>
31 #include <QDir>
32 #include <QEvent>
33 #include <QFormLayout>
34 #include <QHBoxLayout>
35 #include <QKeyEvent>
36 #include <QLabel>
37 #include <QPushButton>
38 #include <QScrollArea>
39 #include <QTimer>
40 #include <QToolButton>
41 #include <QVBoxLayout>
42
43 #include <config-nepomuk.h>
44 #ifdef HAVE_NEPOMUK
45 #include <Nepomuk/Query/AndTerm>
46 #include <Nepomuk/Query/FileQuery>
47 #include <Nepomuk/Query/LiteralTerm>
48 #include <Nepomuk/Query/Query>
49 #include <Nepomuk/Query/QueryParser>
50 #include <Nepomuk/Query/ResourceTypeTerm>
51 #include <Nepomuk/Query/ComparisonTerm>
52 #include <Nepomuk/ResourceManager>
53 #include <Nepomuk/Vocabulary/NFO>
54 #endif
55
56 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
57 QWidget(parent),
58 m_startedSearching(false),
59 m_topLayout(0),
60 m_searchInput(0),
61 m_fileNameButton(0),
62 m_contentButton(0),
63 m_separator(0),
64 m_fromHereButton(0),
65 m_everywhereButton(0),
66 m_searchPath(),
67 m_startSearchTimer(0)
68 {
69 }
70
71 DolphinSearchBox::~DolphinSearchBox()
72 {
73 saveSettings();
74 }
75
76 QString DolphinSearchBox::text() const
77 {
78 return m_searchInput->text();
79 }
80
81 void DolphinSearchBox::setSearchPath(const KUrl& url)
82 {
83 m_searchPath = url;
84
85 QFontMetrics metrics(m_fromHereButton->font());
86 const int maxWidth = metrics.averageCharWidth() * 15;
87
88 QString location = url.fileName();
89 if (location.isEmpty()) {
90 if (url.isLocalFile()) {
91 location = QLatin1String("/");
92 } else {
93 location = url.protocol() + QLatin1String(" - ") + url.host();
94 }
95 }
96
97 const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth);
98 m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation));
99
100 const bool showSearchFromButtons = url.isLocalFile();
101 m_separator->setVisible(showSearchFromButtons);
102 m_fromHereButton->setVisible(showSearchFromButtons);
103 m_everywhereButton->setVisible(showSearchFromButtons);
104 }
105
106 KUrl DolphinSearchBox::searchPath() const
107 {
108 return m_searchPath;
109 }
110
111 KUrl DolphinSearchBox::urlForSearching() const
112 {
113 KUrl url;
114 const DolphinSearchInformation& searchInfo = DolphinSearchInformation::instance();
115 if (searchInfo.isIndexingEnabled() && searchInfo.isPathIndexed(url)) {
116 url = nepomukUrlForSearching();
117 } else {
118 url.setProtocol("filenamesearch");
119 url.addQueryItem("search", m_searchInput->text());
120 if (m_contentButton->isChecked()) {
121 url.addQueryItem("checkContent", "yes");
122 }
123
124 QString encodedUrl;
125 if (m_everywhereButton->isChecked()) {
126 // It is very unlikely, that the majority of Dolphins target users
127 // mean "the whole harddisk" instead of "my home folder" when
128 // selecting the "Everywhere" button.
129 encodedUrl = QDir::homePath();
130 } else {
131 encodedUrl = m_searchPath.url();
132 }
133 url.addQueryItem("url", encodedUrl);
134 }
135
136 return url;
137 }
138
139 void DolphinSearchBox::selectAll()
140 {
141 m_searchInput->selectAll();
142 }
143
144 void DolphinSearchBox::clearText()
145 {
146 m_searchInput->clear();
147 }
148
149 bool DolphinSearchBox::event(QEvent* event)
150 {
151 if (event->type() == QEvent::Polish) {
152 init();
153 }
154 return QWidget::event(event);
155 }
156
157 void DolphinSearchBox::showEvent(QShowEvent* event)
158 {
159 if (!event->spontaneous()) {
160 m_searchInput->setFocus();
161 m_startedSearching = false;
162 }
163 }
164
165 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
166 {
167 QWidget::keyReleaseEvent(event);
168 if (event->key() == Qt::Key_Escape) {
169 if (m_searchInput->text().isEmpty()) {
170 emit closeRequest();
171 } else {
172 m_searchInput->clear();
173 }
174 }
175 }
176
177 void DolphinSearchBox::emitSearchSignal()
178 {
179 m_startSearchTimer->stop();
180 m_startedSearching = true;
181 emit search(m_searchInput->text());
182 }
183
184 void DolphinSearchBox::slotConfigurationChanged()
185 {
186 if (m_startedSearching) {
187 emitSearchSignal();
188 }
189 }
190
191 void DolphinSearchBox::slotSearchTextChanged(const QString& text)
192 {
193 if (text.isEmpty()) {
194 m_startSearchTimer->stop();
195 } else {
196 m_startSearchTimer->start();
197 }
198 emit searchTextChanged(text);
199 }
200
201 void DolphinSearchBox::slotReturnPressed(const QString& text)
202 {
203 emitSearchSignal();
204 emit returnPressed(text);
205 }
206
207 void DolphinSearchBox::initButton(QPushButton* button)
208 {
209 button->setAutoExclusive(true);
210 button->setFlat(true);
211 button->setCheckable(true);
212 connect(button, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
213 }
214
215 void DolphinSearchBox::loadSettings()
216 {
217 if (SearchSettings::location() == QLatin1String("Everywhere")) {
218 m_everywhereButton->setChecked(true);
219 } else {
220 m_fromHereButton->setChecked(true);
221 }
222
223 if (SearchSettings::what() == QLatin1String("Content")) {
224 m_contentButton->setChecked(true);
225 } else {
226 m_fileNameButton->setChecked(true);
227 }
228 }
229
230 void DolphinSearchBox::saveSettings()
231 {
232 SearchSettings::setLocation(m_fromHereButton->isChecked() ? "FromHere" : "Everywhere");
233 SearchSettings::setWhat(m_fileNameButton->isChecked() ? "FileName" : "Content");
234 SearchSettings::self()->writeConfig();
235 }
236
237 void DolphinSearchBox::init()
238 {
239 // Create close button
240 QToolButton* closeButton = new QToolButton(this);
241 closeButton->setAutoRaise(true);
242 closeButton->setIcon(KIcon("dialog-close"));
243 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
244 connect(closeButton, SIGNAL(clicked()), SIGNAL(closeRequest()));
245
246 // Create search label
247 QLabel* searchLabel = new QLabel(i18nc("@label:textbox", "Find:"), this);
248
249 // Create search box
250 m_searchInput = new KLineEdit(this);
251 m_searchInput->setClearButtonShown(true);
252 m_searchInput->setFont(KGlobalSettings::generalFont());
253 setFocusProxy(m_searchInput);
254 connect(m_searchInput, SIGNAL(returnPressed(QString)),
255 this, SLOT(slotReturnPressed(QString)));
256 connect(m_searchInput, SIGNAL(textChanged(QString)),
257 this, SLOT(slotSearchTextChanged(QString)));
258
259 // Apply layout for the search input
260 QHBoxLayout* searchInputLayout = new QHBoxLayout();
261 searchInputLayout->setMargin(0);
262 searchInputLayout->addWidget(closeButton);
263 searchInputLayout->addWidget(searchLabel);
264 searchInputLayout->addWidget(m_searchInput);
265
266 // Create "Filename" and "Content" button
267 m_fileNameButton = new QPushButton(this);
268 m_fileNameButton->setText(i18nc("action:button", "Filename"));
269 initButton(m_fileNameButton);
270
271 m_contentButton = new QPushButton();
272 m_contentButton->setText(i18nc("action:button", "Content"));
273 initButton(m_contentButton);;
274
275 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
276 searchWhatGroup->addButton(m_fileNameButton);
277 searchWhatGroup->addButton(m_contentButton);
278
279 m_separator = new KSeparator(Qt::Vertical, this);
280
281 // Create "From Here" and "Everywhere"button
282 m_fromHereButton = new QPushButton(this);
283 m_fromHereButton->setText(i18nc("action:button", "From Here"));
284 initButton(m_fromHereButton);
285
286 m_everywhereButton = new QPushButton(this);
287 m_everywhereButton->setText(i18nc("action:button", "Everywhere"));
288 initButton(m_everywhereButton);
289
290 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
291 searchLocationGroup->addButton(m_fromHereButton);
292 searchLocationGroup->addButton(m_everywhereButton);
293
294 // Apply layout for the options
295 QHBoxLayout* optionsLayout = new QHBoxLayout();
296 optionsLayout->setMargin(0);
297 optionsLayout->addWidget(m_fileNameButton);
298 optionsLayout->addWidget(m_contentButton);
299 optionsLayout->addWidget(m_separator);
300 optionsLayout->addWidget(m_fromHereButton);
301 optionsLayout->addWidget(m_everywhereButton);
302 optionsLayout->addStretch(1);
303
304 // Put the options into a QScrollArea. This prevents increasing the view width
305 // in case that not enough width for the options is available.
306 QWidget* optionsContainer = new QWidget(this);
307 optionsContainer->setLayout(optionsLayout);
308 QScrollArea* optionsScrollArea = new QScrollArea(this);
309 optionsScrollArea->setFrameShape(QFrame::NoFrame);
310 optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
311 optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
312 optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
313 optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
314 optionsScrollArea->setWidget(optionsContainer);
315 optionsScrollArea->setWidgetResizable(true);
316
317 m_topLayout = new QVBoxLayout(this);
318 m_topLayout->addLayout(searchInputLayout);
319 m_topLayout->addWidget(optionsScrollArea);
320
321 searchLabel->setBuddy(m_searchInput);
322 loadSettings();
323
324 // The searching should be started automatically after the user did not change
325 // the text within one second
326 m_startSearchTimer = new QTimer(this);
327 m_startSearchTimer->setSingleShot(true);
328 m_startSearchTimer->setInterval(1000);
329 connect(m_startSearchTimer, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
330 }
331
332 KUrl DolphinSearchBox::nepomukUrlForSearching() const
333 {
334 #ifdef HAVE_NEPOMUK
335 Nepomuk::Query::AndTerm andTerm;
336
337 // Add input from search filter
338 const QString text = m_searchInput->text();
339 if (!text.isEmpty()) {
340 if (m_fileNameButton->isChecked()) {
341 QString regex = QRegExp::escape(text);
342 regex.replace("\\*", QLatin1String(".*"));
343 regex.replace("\\?", QLatin1String("."));
344 regex.replace("\\", "\\\\");
345 andTerm.addSubTerm(Nepomuk::Query::ComparisonTerm(
346 Nepomuk::Vocabulary::NFO::fileName(),
347 Nepomuk::Query::LiteralTerm(regex),
348 Nepomuk::Query::ComparisonTerm::Regexp));
349 } else {
350 const Nepomuk::Query::Query customQuery = Nepomuk::Query::QueryParser::parseQuery(text, Nepomuk::Query::QueryParser::DetectFilenamePattern);
351 if (customQuery.isValid()) {
352 andTerm.addSubTerm(customQuery.term());
353 }
354 }
355 }
356
357 Nepomuk::Query::FileQuery fileQuery;
358 fileQuery.setFileMode(Nepomuk::Query::FileQuery::QueryFilesAndFolders);
359 fileQuery.setTerm(andTerm);
360 if (m_fromHereButton->isChecked()) {
361 const bool recursive = true;
362 fileQuery.addIncludeFolder(m_searchPath, recursive);
363 }
364
365 return fileQuery.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
366 "Query Results from '%1'",
367 text));
368 #else
369 return KUrl();
370 #endif
371 }
372
373 #include "dolphinsearchbox.moc"