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