]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
KLineEdit to QLineEdit
[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 "dolphinfacetswidget.h"
24
25 #include <QIcon>
26 #include <QLineEdit>
27 #include <KLocalizedString>
28 #include <KSeparator>
29
30 #include <QButtonGroup>
31 #include <QDir>
32 #include <QEvent>
33 #include <QHBoxLayout>
34 #include <QKeyEvent>
35 #include <QLabel>
36 #include <QScrollArea>
37 #include <QTimer>
38 #include <QToolButton>
39 #include <QVBoxLayout>
40
41 #ifdef HAVE_BALOO
42 #include <Baloo/NaturalFileQueryParser>
43 #include <Baloo/QueryBuilder>
44 #include <Baloo/Query>
45 #include <Baloo/Term>
46 #include <Baloo/IndexerConfig>
47 #endif
48 #include <QFontDatabase>
49
50 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
51 QWidget(parent),
52 m_startedSearching(false),
53 m_active(true),
54 m_topLayout(0),
55 m_searchLabel(0),
56 m_searchInput(0),
57 m_optionsScrollArea(0),
58 m_fileNameButton(0),
59 m_contentButton(0),
60 m_separator(0),
61 m_fromHereButton(0),
62 m_everywhereButton(0),
63 m_facetsToggleButton(0),
64 m_facetsWidget(0),
65 m_searchPath(),
66 m_startSearchTimer(0)
67 {
68 }
69
70 DolphinSearchBox::~DolphinSearchBox()
71 {
72 saveSettings();
73 }
74
75 void DolphinSearchBox::setText(const QString& text)
76 {
77 m_searchInput->setText(text);
78 }
79
80 QString DolphinSearchBox::text() const
81 {
82 return m_searchInput->text();
83 }
84
85 void DolphinSearchBox::setSearchPath(const QUrl& url)
86 {
87 m_searchPath = url;
88
89 QFontMetrics metrics(m_fromHereButton->font());
90 const int maxWidth = metrics.height() * 8;
91
92 QString location = url.fileName();
93 if (location.isEmpty()) {
94 if (url.isLocalFile()) {
95 location = QLatin1String("/");
96 } else {
97 location = url.scheme() + QLatin1String(" - ") + url.host();
98 }
99 }
100
101 const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth);
102 m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation));
103
104 const bool showSearchFromButtons = url.isLocalFile();
105 m_separator->setVisible(showSearchFromButtons);
106 m_fromHereButton->setVisible(showSearchFromButtons);
107 m_everywhereButton->setVisible(showSearchFromButtons);
108
109 bool hasFacetsSupport = false;
110 #ifdef HAVE_BALOO
111 const Baloo::IndexerConfig searchInfo;
112 hasFacetsSupport = searchInfo.fileIndexingEnabled() && searchInfo.shouldBeIndexed(m_searchPath.toLocalFile());
113 #endif
114 m_facetsWidget->setEnabled(hasFacetsSupport);
115 }
116
117 QUrl DolphinSearchBox::searchPath() const
118 {
119 return m_searchPath;
120 }
121
122 QUrl DolphinSearchBox::urlForSearching() const
123 {
124 QUrl url;
125 bool useBalooSearch = false;
126 #ifdef HAVE_BALOO
127 const Baloo::IndexerConfig searchInfo;
128 useBalooSearch = searchInfo.fileIndexingEnabled() && searchInfo.shouldBeIndexed(m_searchPath.toLocalFile());
129 #endif
130 if (useBalooSearch) {
131 url = balooUrlForSearching();
132 } else {
133 url.setScheme("filenamesearch");
134 url.addQueryItem("search", m_searchInput->text());
135 if (m_contentButton->isChecked()) {
136 url.addQueryItem("checkContent", "yes");
137 }
138
139 QString encodedUrl;
140 if (m_everywhereButton->isChecked()) {
141 // It is very unlikely, that the majority of Dolphins target users
142 // mean "the whole harddisk" instead of "my home folder" when
143 // selecting the "Everywhere" button.
144 encodedUrl = QDir::homePath();
145 } else {
146 encodedUrl = m_searchPath.url();
147 }
148 url.addQueryItem("url", encodedUrl);
149 }
150
151 return url;
152 }
153
154 void DolphinSearchBox::fromSearchUrl(const QUrl& url)
155 {
156 if (url.scheme() == "baloosearch") {
157 fromBalooSearchUrl(url);
158 } else if (url.scheme() == "filenamesearch") {
159 setText(url.queryItemValue("search"));
160 setSearchPath(url.queryItemValue("url"));
161 m_contentButton->setChecked(url.queryItemValue("checkContent") == "yes");
162 } else {
163 setText(QString());
164 setSearchPath(url);
165 }
166 }
167
168 void DolphinSearchBox::selectAll()
169 {
170 m_searchInput->selectAll();
171 }
172
173 void DolphinSearchBox::setActive(bool active)
174 {
175 if (active != m_active) {
176 m_active = active;
177
178 if (active) {
179 emit activated();
180 }
181 }
182 }
183
184 bool DolphinSearchBox::isActive() const
185 {
186 return m_active;
187 }
188
189 bool DolphinSearchBox::event(QEvent* event)
190 {
191 if (event->type() == QEvent::Polish) {
192 init();
193 }
194 return QWidget::event(event);
195 }
196
197 void DolphinSearchBox::showEvent(QShowEvent* event)
198 {
199 if (!event->spontaneous()) {
200 m_searchInput->setFocus();
201 m_startedSearching = false;
202 }
203 }
204
205 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
206 {
207 QWidget::keyReleaseEvent(event);
208 if (event->key() == Qt::Key_Escape) {
209 if (m_searchInput->text().isEmpty()) {
210 emit closeRequest();
211 } else {
212 m_searchInput->clear();
213 }
214 }
215 }
216
217 bool DolphinSearchBox::eventFilter(QObject* obj, QEvent* event)
218 {
219 switch (event->type()) {
220 case QEvent::FocusIn:
221 setActive(true);
222 setFocus();
223 break;
224
225 default:
226 break;
227 }
228
229 return QObject::eventFilter(obj, event);
230 }
231
232 void DolphinSearchBox::emitSearchRequest()
233 {
234 m_startSearchTimer->stop();
235 m_startedSearching = true;
236 emit searchRequest();
237 }
238
239 void DolphinSearchBox::emitCloseRequest()
240 {
241 m_startSearchTimer->stop();
242 m_startedSearching = false;
243 emit closeRequest();
244 }
245
246 void DolphinSearchBox::slotConfigurationChanged()
247 {
248 saveSettings();
249 if (m_startedSearching) {
250 emitSearchRequest();
251 }
252 }
253
254 void DolphinSearchBox::slotSearchTextChanged()
255 {
256 const QString text = m_searchInput->text();
257
258 if (text.isEmpty()) {
259 m_startSearchTimer->stop();
260 } else {
261 m_startSearchTimer->start();
262 }
263 emit searchTextChanged(text);
264 }
265
266 void DolphinSearchBox::slotReturnPressed()
267 {
268 emitSearchRequest();
269 emit returnPressed(m_searchInput->text());
270 }
271
272 void DolphinSearchBox::updateSearchInputParsing()
273 {
274 #ifdef HAVE_BALOO
275 m_searchInput->setParsingEnabled(m_contentButton->isChecked());
276 #endif
277 }
278
279 void DolphinSearchBox::slotFacetsButtonToggled()
280 {
281 const bool facetsIsVisible = !m_facetsWidget->isVisible();
282 m_facetsWidget->setVisible(facetsIsVisible);
283 updateFacetsToggleButton();
284 }
285
286 void DolphinSearchBox::slotFacetChanged()
287 {
288 m_startedSearching = true;
289 m_startSearchTimer->stop();
290 emit searchRequest();
291 }
292
293 void DolphinSearchBox::initButton(QToolButton* button)
294 {
295 button->installEventFilter(this);
296 button->setAutoExclusive(true);
297 button->setAutoRaise(true);
298 button->setCheckable(true);
299 connect(button, &QToolButton::clicked, this, &DolphinSearchBox::slotConfigurationChanged);
300 }
301
302 void DolphinSearchBox::loadSettings()
303 {
304 if (SearchSettings::location() == QLatin1String("Everywhere")) {
305 m_everywhereButton->setChecked(true);
306 } else {
307 m_fromHereButton->setChecked(true);
308 }
309
310 if (SearchSettings::what() == QLatin1String("Content")) {
311 m_contentButton->setChecked(true);
312 } else {
313 m_fileNameButton->setChecked(true);
314 }
315
316 m_facetsWidget->setVisible(SearchSettings::showFacetsWidget());
317 updateSearchInputParsing();
318 }
319
320 void DolphinSearchBox::saveSettings()
321 {
322 SearchSettings::setLocation(m_fromHereButton->isChecked() ? "FromHere" : "Everywhere");
323 SearchSettings::setWhat(m_fileNameButton->isChecked() ? "FileName" : "Content");
324 SearchSettings::setShowFacetsWidget(m_facetsToggleButton->isChecked());
325 SearchSettings::self()->save();
326 }
327
328 void DolphinSearchBox::init()
329 {
330 // Create close button
331 QToolButton* closeButton = new QToolButton(this);
332 closeButton->setAutoRaise(true);
333 closeButton->setIcon(QIcon::fromTheme("dialog-close"));
334 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
335 connect(closeButton, &QToolButton::clicked, this, &DolphinSearchBox::emitCloseRequest);
336
337 // Create search label
338 m_searchLabel = new QLabel(this);
339
340 // Create search box
341 #ifdef HAVE_BALOO
342 m_queryParser.reset(new Baloo::NaturalFileQueryParser);
343 m_searchInput = new Baloo::QueryBuilder(m_queryParser.data(), this);
344 connect(m_searchInput, &Baloo::QueryBuilder::editingFinished,
345 this, &DolphinSearchBox::slotReturnPressed);
346 connect(m_searchInput, &Baloo::QueryBuilder::textChanged,
347 this, &DolphinSearchBox::slotSearchTextChanged);
348 #else
349 m_searchInput = new QLineEdit(this);
350 m_searchInput->installEventFilter(this);
351 m_searchInput->setClearButtonEnabled(true);
352 m_searchInput->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
353 connect(m_searchInput, &QLineEdit::returnPressed,
354 this, &DolphinSearchBox::slotReturnPressed);
355 connect(m_searchInput, &QLineEdit::textChanged,
356 this, &DolphinSearchBox::slotSearchTextChanged);
357 #endif
358 setFocusProxy(m_searchInput);
359
360 // Apply layout for the search input
361 QHBoxLayout* searchInputLayout = new QHBoxLayout();
362 searchInputLayout->setMargin(0);
363 searchInputLayout->addWidget(closeButton);
364 searchInputLayout->addWidget(m_searchLabel);
365 searchInputLayout->addWidget(m_searchInput);
366
367 // Create "Filename" and "Content" button
368 m_fileNameButton = new QToolButton(this);
369 m_fileNameButton->setText(i18nc("action:button", "Filename"));
370 initButton(m_fileNameButton);
371
372 m_contentButton = new QToolButton();
373 m_contentButton->setText(i18nc("action:button", "Content"));
374 initButton(m_contentButton);
375
376 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
377 searchWhatGroup->addButton(m_fileNameButton);
378 searchWhatGroup->addButton(m_contentButton);
379 connect(searchWhatGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
380 this, &DolphinSearchBox::updateSearchInputParsing);
381
382 m_separator = new KSeparator(Qt::Vertical, this);
383
384 // Create "From Here" and "Everywhere"button
385 m_fromHereButton = new QToolButton(this);
386 m_fromHereButton->setText(i18nc("action:button", "From Here"));
387 initButton(m_fromHereButton);
388
389 m_everywhereButton = new QToolButton(this);
390 m_everywhereButton->setText(i18nc("action:button", "Everywhere"));
391 initButton(m_everywhereButton);
392
393 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
394 searchLocationGroup->addButton(m_fromHereButton);
395 searchLocationGroup->addButton(m_everywhereButton);
396
397 // Create "Facets" widgets
398 m_facetsToggleButton = new QToolButton(this);
399 m_facetsToggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
400 initButton(m_facetsToggleButton);
401 connect(m_facetsToggleButton, &QToolButton::clicked, this, &DolphinSearchBox::slotFacetsButtonToggled);
402
403 m_facetsWidget = new DolphinFacetsWidget(this);
404 m_facetsWidget->installEventFilter(this);
405 m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
406 connect(m_facetsWidget, &DolphinFacetsWidget::facetChanged, this, &DolphinSearchBox::slotFacetChanged);
407
408 // Apply layout for the options
409 QHBoxLayout* optionsLayout = new QHBoxLayout();
410 optionsLayout->setMargin(0);
411 optionsLayout->addWidget(m_fileNameButton);
412 optionsLayout->addWidget(m_contentButton);
413 optionsLayout->addWidget(m_separator);
414 optionsLayout->addWidget(m_fromHereButton);
415 optionsLayout->addWidget(m_everywhereButton);
416 optionsLayout->addStretch(1);
417 optionsLayout->addWidget(m_facetsToggleButton);
418
419 // Put the options into a QScrollArea. This prevents increasing the view width
420 // in case that not enough width for the options is available.
421 QWidget* optionsContainer = new QWidget(this);
422 optionsContainer->setLayout(optionsLayout);
423
424 m_optionsScrollArea = new QScrollArea(this);
425 m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
426 m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
427 m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
428 m_optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
429 m_optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
430 m_optionsScrollArea->setWidget(optionsContainer);
431 m_optionsScrollArea->setWidgetResizable(true);
432
433 m_topLayout = new QVBoxLayout(this);
434 m_topLayout->setMargin(0);
435 m_topLayout->addLayout(searchInputLayout);
436 m_topLayout->addWidget(m_optionsScrollArea);
437 m_topLayout->addWidget(m_facetsWidget);
438
439 loadSettings();
440
441 // The searching should be started automatically after the user did not change
442 // the text within one second
443 m_startSearchTimer = new QTimer(this);
444 m_startSearchTimer->setSingleShot(true);
445 m_startSearchTimer->setInterval(1000);
446 connect(m_startSearchTimer, &QTimer::timeout, this, &DolphinSearchBox::emitSearchRequest);
447
448 updateFacetsToggleButton();
449 }
450
451 QUrl DolphinSearchBox::balooUrlForSearching() const
452 {
453 #ifdef HAVE_BALOO
454 const QString text = m_searchInput->text();
455
456 Baloo::Query query;
457
458 if (m_contentButton->isChecked()) {
459 query = m_queryParser->parse(text, Baloo::NaturalQueryParser::DetectFilenamePattern);
460 } else {
461 query.setTerm(Baloo::Term(QLatin1String("filename"), text));
462 }
463
464 // Configure the query so that it returns files and takes the rating into account
465 query.addType("File");
466 query.addType(m_facetsWidget->facetType());
467
468 Baloo::Term term(Baloo::Term::And);
469 Baloo::Term ratingTerm = m_facetsWidget->ratingTerm();
470
471 if (ratingTerm.isValid()) {
472 term.addSubTerm(query.term());
473 term.addSubTerm(ratingTerm);
474
475 query.setTerm(term);
476 }
477
478 if (m_fromHereButton->isChecked()) {
479 query.setIncludeFolder(m_searchPath.toLocalFile());
480 }
481
482 return query.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
483 "Query Results from '%1'", text));
484 #else
485 return QUrl();
486 #endif
487 }
488
489 void DolphinSearchBox::fromBalooSearchUrl(const QUrl& url)
490 {
491 #ifdef HAVE_BALOO
492 const Baloo::Query query = Baloo::Query::fromSearchUrl(url);
493 const Baloo::Term term = query.term();
494
495 // Block all signals to avoid unnecessary "searchRequest" signals
496 // while we adjust the search text and the facet widget.
497 blockSignals(true);
498
499 const QString customDir = query.includeFolder();
500 if (!customDir.isEmpty()) {
501 setSearchPath(customDir);
502 } else {
503 setSearchPath(QDir::homePath());
504 }
505
506 if (!query.searchString().isEmpty()) {
507 setText(query.searchString());
508 }
509
510 QStringList types = query.types();
511 types.removeOne("File"); // We are only interested in facet widget types
512 if (!types.isEmpty()) {
513 m_facetsWidget->setFacetType(types.first());
514 }
515
516 foreach (const Baloo::Term& subTerm, term.subTerms()) {
517 const QString property = subTerm.property();
518
519 if (property == QLatin1String("filename")) {
520 setText(subTerm.value().toString());
521 } else if (m_facetsWidget->isRatingTerm(subTerm)) {
522 m_facetsWidget->setRatingTerm(subTerm);
523 }
524 }
525
526 m_startSearchTimer->stop();
527 blockSignals(false);
528 #endif
529 }
530
531 void DolphinSearchBox::updateFacetsToggleButton()
532 {
533 const bool facetsIsVisible = SearchSettings::showFacetsWidget();
534 m_facetsToggleButton->setChecked(facetsIsVisible ? true : false);
535 m_facetsToggleButton->setIcon(QIcon::fromTheme(facetsIsVisible ? "arrow-up-double" : "arrow-down-double"));
536 m_facetsToggleButton->setText(facetsIsVisible ? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));
537 }
538