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