]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
26c78c11151cc2a66fada8aece903361cf4bac34
[dolphin.git] / src / search / dolphinsearchbox.cpp
1 /*
2 * SPDX-FileCopyrightText: 2010 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "global.h"
8 #include "dolphinsearchbox.h"
9
10 #include "dolphin_searchsettings.h"
11 #include "dolphinfacetswidget.h"
12 #include "dolphinplacesmodelsingleton.h"
13 #include "dolphinquery.h"
14
15 #include <KLocalizedString>
16 #include <KMoreToolsMenuFactory>
17 #include <KSeparator>
18 #include "config-dolphin.h"
19 #if HAVE_BALOO
20 #include <Baloo/Query>
21 #include <Baloo/IndexerConfig>
22 #endif
23
24 #include <QButtonGroup>
25 #include <QDir>
26 #include <QFontDatabase>
27 #include <QHBoxLayout>
28 #include <QIcon>
29 #include <QKeyEvent>
30 #include <QLineEdit>
31 #include <QScrollArea>
32 #include <QShowEvent>
33 #include <QTimer>
34 #include <QToolButton>
35 #include <QUrlQuery>
36
37 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
38 QWidget(parent),
39 m_startedSearching(false),
40 m_active(true),
41 m_topLayout(nullptr),
42 m_searchInput(nullptr),
43 m_saveSearchAction(nullptr),
44 m_optionsScrollArea(nullptr),
45 m_fileNameButton(nullptr),
46 m_contentButton(nullptr),
47 m_separator(nullptr),
48 m_fromHereButton(nullptr),
49 m_everywhereButton(nullptr),
50 m_facetsWidget(nullptr),
51 m_searchPath(),
52 m_startSearchTimer(nullptr)
53 {
54 }
55
56 DolphinSearchBox::~DolphinSearchBox()
57 {
58 saveSettings();
59 }
60
61 void DolphinSearchBox::setText(const QString& text)
62 {
63 m_searchInput->setText(text);
64 }
65
66 QString DolphinSearchBox::text() const
67 {
68 return m_searchInput->text();
69 }
70
71 void DolphinSearchBox::setSearchPath(const QUrl& url)
72 {
73 if (url == m_searchPath) {
74 return;
75 }
76
77 const QUrl cleanedUrl = url.adjusted(QUrl::RemoveUserInfo | QUrl::StripTrailingSlash);
78
79 if (cleanedUrl.path() == QDir::homePath()) {
80 m_fromHereButton->setChecked(false);
81 m_everywhereButton->setChecked(true);
82 if (!m_searchPath.isEmpty()) {
83 return;
84 }
85 } else {
86 m_everywhereButton->setChecked(false);
87 m_fromHereButton->setChecked(true);
88 }
89
90 m_searchPath = url;
91
92 QFontMetrics metrics(m_fromHereButton->font());
93 const int maxWidth = metrics.height() * 8;
94
95 QString location = cleanedUrl.fileName();
96 if (location.isEmpty()) {
97 location = cleanedUrl.toString(QUrl::PreferLocalFile);
98 }
99 const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth);
100 m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation));
101 m_fromHereButton->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl.toString(QUrl::PreferLocalFile)));
102 }
103
104 QUrl DolphinSearchBox::searchPath() const
105 {
106 return m_everywhereButton->isChecked() ? QUrl::fromLocalFile(QDir::homePath()) : m_searchPath;
107 }
108
109 QUrl DolphinSearchBox::urlForSearching() const
110 {
111 QUrl url;
112
113 if (isIndexingEnabled()) {
114 url = balooUrlForSearching();
115 } else {
116 url.setScheme(QStringLiteral("filenamesearch"));
117
118 QUrlQuery query;
119 query.addQueryItem(QStringLiteral("search"), m_searchInput->text());
120 if (m_contentButton->isChecked()) {
121 query.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
122 }
123
124 query.addQueryItem(QStringLiteral("url"), searchPath().url());
125 query.addQueryItem(QStringLiteral("title"), queryTitle(m_searchInput->text()));
126
127 url.setQuery(query);
128 }
129
130 return url;
131 }
132
133 void DolphinSearchBox::fromSearchUrl(const QUrl& url)
134 {
135 if (DolphinQuery::supportsScheme(url.scheme())) {
136 const DolphinQuery query = DolphinQuery::fromSearchUrl(url);
137 updateFromQuery(query);
138 } else if (url.scheme() == QLatin1String("filenamesearch")) {
139 const QUrlQuery query(url);
140 setText(query.queryItemValue(QStringLiteral("search")));
141 if (m_searchPath.scheme() != url.scheme()) {
142 m_searchPath = QUrl();
143 }
144 setSearchPath(QUrl::fromUserInput(query.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile));
145 m_contentButton->setChecked(query.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
146 } else {
147 setText(QString());
148 m_searchPath = QUrl();
149 setSearchPath(url);
150 }
151
152 updateFacetsVisible();
153 }
154
155 void DolphinSearchBox::selectAll()
156 {
157 m_searchInput->selectAll();
158 }
159
160 void DolphinSearchBox::setActive(bool active)
161 {
162 if (active != m_active) {
163 m_active = active;
164
165 if (active) {
166 Q_EMIT activated();
167 }
168 }
169 }
170
171 bool DolphinSearchBox::isActive() const
172 {
173 return m_active;
174 }
175
176 bool DolphinSearchBox::event(QEvent* event)
177 {
178 if (event->type() == QEvent::Polish) {
179 init();
180 }
181 return QWidget::event(event);
182 }
183
184 void DolphinSearchBox::showEvent(QShowEvent* event)
185 {
186 if (!event->spontaneous()) {
187 m_searchInput->setFocus();
188 m_startedSearching = false;
189 }
190 }
191
192 void DolphinSearchBox::hideEvent(QHideEvent* event)
193 {
194 Q_UNUSED(event)
195 m_startedSearching = false;
196 m_startSearchTimer->stop();
197 }
198
199 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
200 {
201 QWidget::keyReleaseEvent(event);
202 if (event->key() == Qt::Key_Escape) {
203 if (m_searchInput->text().isEmpty()) {
204 Q_EMIT closeRequest();
205 } else {
206 m_searchInput->clear();
207 }
208 }
209 else if (event->key() == Qt::Key_Down) {
210 Q_EMIT focusViewRequest();
211 }
212 }
213
214 bool DolphinSearchBox::eventFilter(QObject* obj, QEvent* event)
215 {
216 switch (event->type()) {
217 case QEvent::FocusIn:
218 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
219 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
220 // To avoid this issue, we delay the activation of the search box.
221 // We also don't want to schedule the activation process if we are already active,
222 // otherwise we can enter in a loop of FocusIn/FocusOut events with the searchbox of another tab.
223 if (!isActive()) {
224 QTimer::singleShot(0, this, [this] {
225 setActive(true);
226 setFocus();
227 });
228 }
229 break;
230
231 default:
232 break;
233 }
234
235 return QObject::eventFilter(obj, event);
236 }
237
238 void DolphinSearchBox::emitSearchRequest()
239 {
240 m_startSearchTimer->stop();
241 m_startedSearching = true;
242 m_saveSearchAction->setEnabled(true);
243 Q_EMIT searchRequest();
244 }
245
246 void DolphinSearchBox::emitCloseRequest()
247 {
248 m_startSearchTimer->stop();
249 m_startedSearching = false;
250 m_saveSearchAction->setEnabled(false);
251 Q_EMIT closeRequest();
252 }
253
254 void DolphinSearchBox::slotConfigurationChanged()
255 {
256 saveSettings();
257 if (m_startedSearching) {
258 emitSearchRequest();
259 }
260 }
261
262 void DolphinSearchBox::slotSearchTextChanged(const QString& text)
263 {
264
265 if (text.isEmpty()) {
266 m_startSearchTimer->stop();
267 } else {
268 m_startSearchTimer->start();
269 }
270 Q_EMIT searchTextChanged(text);
271 }
272
273 void DolphinSearchBox::slotReturnPressed()
274 {
275 emitSearchRequest();
276 Q_EMIT focusViewRequest();
277 }
278
279 void DolphinSearchBox::slotFacetChanged()
280 {
281 m_startedSearching = true;
282 m_startSearchTimer->stop();
283 Q_EMIT searchRequest();
284 }
285
286 void DolphinSearchBox::slotSearchSaved()
287 {
288 const QUrl searchURL = urlForSearching();
289 if (searchURL.isValid()) {
290 const QString label = i18n("Search for %1 in %2", text(), searchPath().fileName());
291 DolphinPlacesModelSingleton::instance().placesModel()->addPlace(label, searchURL, QStringLiteral("folder-saved-search-symbolic"));
292 }
293 }
294
295 void DolphinSearchBox::initButton(QToolButton* button)
296 {
297 button->installEventFilter(this);
298 button->setAutoExclusive(true);
299 button->setAutoRaise(true);
300 button->setCheckable(true);
301 connect(button, &QToolButton::clicked, this, &DolphinSearchBox::slotConfigurationChanged);
302 }
303
304 void DolphinSearchBox::loadSettings()
305 {
306 if (SearchSettings::location() == QLatin1String("Everywhere")) {
307 m_everywhereButton->setChecked(true);
308 } else {
309 m_fromHereButton->setChecked(true);
310 }
311
312 if (SearchSettings::what() == QLatin1String("Content")) {
313 m_contentButton->setChecked(true);
314 } else {
315 m_fileNameButton->setChecked(true);
316 }
317
318 updateFacetsVisible();
319 }
320
321 void DolphinSearchBox::saveSettings()
322 {
323 SearchSettings::setLocation(m_fromHereButton->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
324 SearchSettings::setWhat(m_fileNameButton->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
325 SearchSettings::self()->save();
326 }
327
328 void DolphinSearchBox::init()
329 {
330 // Create search box
331 m_searchInput = new QLineEdit(this);
332 m_searchInput->setPlaceholderText(i18n("Search..."));
333 m_searchInput->installEventFilter(this);
334 m_searchInput->setClearButtonEnabled(true);
335 m_searchInput->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
336 connect(m_searchInput, &QLineEdit::returnPressed,
337 this, &DolphinSearchBox::slotReturnPressed);
338 connect(m_searchInput, &QLineEdit::textChanged,
339 this, &DolphinSearchBox::slotSearchTextChanged);
340 setFocusProxy(m_searchInput);
341
342 // Add "Save search" button inside search box
343 m_saveSearchAction = new QAction(this);
344 m_saveSearchAction->setIcon (QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
345 m_saveSearchAction->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
346 m_saveSearchAction->setEnabled(false);
347 m_searchInput->addAction(m_saveSearchAction, QLineEdit::TrailingPosition);
348 connect(m_saveSearchAction, &QAction::triggered, this, &DolphinSearchBox::slotSearchSaved);
349
350 // Create close button
351 QToolButton* closeButton = new QToolButton(this);
352 closeButton->setAutoRaise(true);
353 closeButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
354 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
355 connect(closeButton, &QToolButton::clicked, this, &DolphinSearchBox::emitCloseRequest);
356
357 // Apply layout for the search input
358 QHBoxLayout* searchInputLayout = new QHBoxLayout();
359 searchInputLayout->setContentsMargins(0, 0, 0, 0);
360 searchInputLayout->addWidget(m_searchInput);
361 searchInputLayout->addWidget(closeButton);
362
363 // Create "Filename" and "Content" button
364 m_fileNameButton = new QToolButton(this);
365 m_fileNameButton->setText(i18nc("action:button", "Filename"));
366 initButton(m_fileNameButton);
367
368 m_contentButton = new QToolButton();
369 m_contentButton->setText(i18nc("action:button", "Content"));
370 initButton(m_contentButton);
371
372 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
373 searchWhatGroup->addButton(m_fileNameButton);
374 searchWhatGroup->addButton(m_contentButton);
375
376 m_separator = new KSeparator(Qt::Vertical, this);
377
378 // Create "From Here" and "Your files" buttons
379 m_fromHereButton = new QToolButton(this);
380 m_fromHereButton->setText(i18nc("action:button", "From Here"));
381 initButton(m_fromHereButton);
382
383 m_everywhereButton = new QToolButton(this);
384 m_everywhereButton->setText(i18nc("action:button", "Your files"));
385 m_everywhereButton->setToolTip(i18nc("action:button", "Search in your home directory"));
386 m_everywhereButton->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
387 m_everywhereButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
388 initButton(m_everywhereButton);
389
390 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
391 searchLocationGroup->addButton(m_fromHereButton);
392 searchLocationGroup->addButton(m_everywhereButton);
393
394 auto moreSearchToolsButton = new QToolButton(this);
395 moreSearchToolsButton->setAutoRaise(true);
396 moreSearchToolsButton->setPopupMode(QToolButton::InstantPopup);
397 moreSearchToolsButton->setIcon(QIcon::fromTheme("arrow-down-double"));
398 moreSearchToolsButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
399 moreSearchToolsButton->setText(i18n("More Search Tools"));
400 moreSearchToolsButton->setMenu(new QMenu(this));
401 connect(moreSearchToolsButton->menu(), &QMenu::aboutToShow, moreSearchToolsButton->menu(), [this, moreSearchToolsButton]()
402 {
403 m_menuFactory.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
404 moreSearchToolsButton->menu()->clear();
405 m_menuFactory->fillMenuFromGroupingNames(moreSearchToolsButton->menu(), { "files-find" }, this->m_searchPath);
406 } );
407
408 // Create "Facets" widget
409 m_facetsWidget = new DolphinFacetsWidget(this);
410 m_facetsWidget->installEventFilter(this);
411 m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
412 m_facetsWidget->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
413 connect(m_facetsWidget, &DolphinFacetsWidget::facetChanged, this, &DolphinSearchBox::slotFacetChanged);
414
415 // Put the options into a QScrollArea. This prevents increasing the view width
416 // in case that not enough width for the options is available.
417 QWidget* optionsContainer = new QWidget(this);
418
419 // Apply layout for the options
420 QHBoxLayout* optionsLayout = new QHBoxLayout(optionsContainer);
421 optionsLayout->setContentsMargins(0, 0, 0, 0);
422 optionsLayout->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
423 optionsLayout->addWidget(m_fileNameButton);
424 optionsLayout->addWidget(m_contentButton);
425 optionsLayout->addWidget(m_separator);
426 optionsLayout->addWidget(m_fromHereButton);
427 optionsLayout->addWidget(m_everywhereButton);
428 optionsLayout->addWidget(new KSeparator(Qt::Vertical, this));
429 optionsLayout->addWidget(moreSearchToolsButton);
430 optionsLayout->addStretch(1);
431
432 m_optionsScrollArea = new QScrollArea(this);
433 m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
434 m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
435 m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
436 m_optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
437 m_optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
438 m_optionsScrollArea->setWidget(optionsContainer);
439 m_optionsScrollArea->setWidgetResizable(true);
440
441 m_topLayout = new QVBoxLayout(this);
442 m_topLayout->setContentsMargins(0, Dolphin::LAYOUT_SPACING_SMALL, 0, 0);
443 m_topLayout->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
444 m_topLayout->addLayout(searchInputLayout);
445 m_topLayout->addWidget(m_optionsScrollArea);
446 m_topLayout->addWidget(m_facetsWidget);
447
448 loadSettings();
449
450 // The searching should be started automatically after the user did not change
451 // the text within one second
452 m_startSearchTimer = new QTimer(this);
453 m_startSearchTimer->setSingleShot(true);
454 m_startSearchTimer->setInterval(1000);
455 connect(m_startSearchTimer, &QTimer::timeout, this, &DolphinSearchBox::emitSearchRequest);
456 }
457
458 QString DolphinSearchBox::queryTitle(const QString& text) const
459 {
460 return i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
461 "Query Results from '%1'", text);
462 }
463
464 QUrl DolphinSearchBox::balooUrlForSearching() const
465 {
466 #if HAVE_BALOO
467 const QString text = m_searchInput->text();
468
469 Baloo::Query query;
470 query.addType(m_facetsWidget->facetType());
471
472 QStringList queryStrings = m_facetsWidget->searchTerms();
473
474 if (m_contentButton->isChecked()) {
475 queryStrings << text;
476 } else if (!text.isEmpty()) {
477 queryStrings << QStringLiteral("filename:\"%1\"").arg(text);
478 }
479
480 if (m_fromHereButton->isChecked()) {
481 query.setIncludeFolder(m_searchPath.toLocalFile());
482 }
483
484 query.setSearchString(queryStrings.join(QLatin1Char(' ')));
485
486 return query.toSearchUrl(queryTitle(text));
487 #else
488 return QUrl();
489 #endif
490 }
491
492 void DolphinSearchBox::updateFromQuery(const DolphinQuery& query)
493 {
494 // Block all signals to avoid unnecessary "searchRequest" signals
495 // while we adjust the search text and the facet widget.
496 blockSignals(true);
497
498 const QString customDir = query.includeFolder();
499 if (!customDir.isEmpty()) {
500 setSearchPath(QUrl::fromLocalFile(customDir));
501 } else {
502 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
503 }
504
505 // If the input box has focus, do not update to avoid messing with user typing
506 if (!m_searchInput->hasFocus()) {
507 setText(query.text());
508 }
509
510 if (query.hasContentSearch()) {
511 m_contentButton->setChecked(true);
512 } else if (query.hasFileName()) {
513 m_fileNameButton->setChecked(true);
514 }
515
516 m_facetsWidget->resetSearchTerms();
517 m_facetsWidget->setFacetType(query.type());
518 const QStringList searchTerms = query.searchTerms();
519 for (const QString& searchTerm : searchTerms) {
520 m_facetsWidget->setSearchTerm(searchTerm);
521 }
522
523 m_startSearchTimer->stop();
524 blockSignals(false);
525 }
526
527 void DolphinSearchBox::updateFacetsVisible()
528 {
529 const bool indexingEnabled = isIndexingEnabled();
530 m_facetsWidget->setEnabled(indexingEnabled);
531 m_facetsWidget->setVisible(indexingEnabled);
532 }
533
534 bool DolphinSearchBox::isIndexingEnabled() const
535 {
536 #if HAVE_BALOO
537 const Baloo::IndexerConfig searchInfo;
538 return searchInfo.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo.shouldBeIndexed(searchPath().toLocalFile());
539 #else
540 return false;
541 #endif
542 }