]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
Merge branch '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 Q_EMIT openRequest();
269 } else {
270 m_startSearchTimer->start();
271 }
272 Q_EMIT searchTextChanged(text);
273 }
274
275 void DolphinSearchBox::slotReturnPressed()
276 {
277 if (m_searchInput->text().isEmpty()) {
278 return;
279 }
280
281 emitSearchRequest();
282 Q_EMIT focusViewRequest();
283 }
284
285 void DolphinSearchBox::slotFacetChanged()
286 {
287 m_startedSearching = true;
288 m_startSearchTimer->stop();
289 Q_EMIT searchRequest();
290 }
291
292 void DolphinSearchBox::slotSearchSaved()
293 {
294 const QUrl searchURL = urlForSearching();
295 if (searchURL.isValid()) {
296 const QString label = i18n("Search for %1 in %2", text(), searchPath().fileName());
297 DolphinPlacesModelSingleton::instance().placesModel()->addPlace(label, searchURL, QStringLiteral("folder-saved-search-symbolic"));
298 }
299 }
300
301 void DolphinSearchBox::initButton(QToolButton *button)
302 {
303 button->installEventFilter(this);
304 button->setAutoExclusive(true);
305 button->setAutoRaise(true);
306 button->setCheckable(true);
307 connect(button, &QToolButton::clicked, this, &DolphinSearchBox::slotConfigurationChanged);
308 }
309
310 void DolphinSearchBox::loadSettings()
311 {
312 if (SearchSettings::location() == QLatin1String("Everywhere")) {
313 m_everywhereButton->setChecked(true);
314 } else {
315 m_fromHereButton->setChecked(true);
316 }
317
318 if (SearchSettings::what() == QLatin1String("Content")) {
319 m_contentButton->setChecked(true);
320 } else {
321 m_fileNameButton->setChecked(true);
322 }
323
324 updateFacetsVisible();
325 }
326
327 void DolphinSearchBox::saveSettings()
328 {
329 SearchSettings::setLocation(m_fromHereButton->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
330 SearchSettings::setWhat(m_fileNameButton->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
331 SearchSettings::self()->save();
332 }
333
334 void DolphinSearchBox::init()
335 {
336 // Create search box
337 m_searchInput = new QLineEdit(this);
338 m_searchInput->setPlaceholderText(i18n("Search…"));
339 m_searchInput->installEventFilter(this);
340 m_searchInput->setClearButtonEnabled(true);
341 m_searchInput->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
342 connect(m_searchInput, &QLineEdit::returnPressed, this, &DolphinSearchBox::slotReturnPressed);
343 connect(m_searchInput, &QLineEdit::textChanged, this, &DolphinSearchBox::slotSearchTextChanged);
344 setFocusProxy(m_searchInput);
345
346 // Add "Save search" button inside search box
347 m_saveSearchAction = new QAction(this);
348 m_saveSearchAction->setIcon(QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
349 m_saveSearchAction->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
350 m_saveSearchAction->setEnabled(false);
351 m_searchInput->addAction(m_saveSearchAction, QLineEdit::TrailingPosition);
352 connect(m_saveSearchAction, &QAction::triggered, this, &DolphinSearchBox::slotSearchSaved);
353
354 // Create close button
355 QToolButton *closeButton = new QToolButton(this);
356 closeButton->setAutoRaise(true);
357 closeButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
358 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
359 connect(closeButton, &QToolButton::clicked, this, &DolphinSearchBox::emitCloseRequest);
360
361 // Apply layout for the search input
362 QHBoxLayout *searchInputLayout = new QHBoxLayout();
363 searchInputLayout->setContentsMargins(0, 0, 0, 0);
364 searchInputLayout->addWidget(m_searchInput);
365 searchInputLayout->addWidget(closeButton);
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
380 m_separator = new KSeparator(Qt::Vertical, this);
381
382 // Create "From Here" and "Your files" buttons
383 m_fromHereButton = new QToolButton(this);
384 m_fromHereButton->setText(i18nc("action:button", "From Here"));
385 initButton(m_fromHereButton);
386
387 m_everywhereButton = new QToolButton(this);
388 m_everywhereButton->setText(i18nc("action:button", "Your files"));
389 m_everywhereButton->setToolTip(i18nc("action:button", "Search in your home directory"));
390 m_everywhereButton->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
391 m_everywhereButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
392 initButton(m_everywhereButton);
393
394 QButtonGroup *searchLocationGroup = new QButtonGroup(this);
395 searchLocationGroup->addButton(m_fromHereButton);
396 searchLocationGroup->addButton(m_everywhereButton);
397
398 auto moreSearchToolsButton = new QToolButton(this);
399 moreSearchToolsButton->setAutoRaise(true);
400 moreSearchToolsButton->setPopupMode(QToolButton::InstantPopup);
401 moreSearchToolsButton->setIcon(QIcon::fromTheme("arrow-down-double"));
402 moreSearchToolsButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
403 moreSearchToolsButton->setText(i18n("More Search Tools"));
404 moreSearchToolsButton->setMenu(new QMenu(this));
405 connect(moreSearchToolsButton->menu(), &QMenu::aboutToShow, moreSearchToolsButton->menu(), [this, moreSearchToolsButton]() {
406 m_menuFactory.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
407 moreSearchToolsButton->menu()->clear();
408 m_menuFactory->fillMenuFromGroupingNames(moreSearchToolsButton->menu(), {"files-find"}, this->m_searchPath);
409 });
410
411 // Create "Facets" widget
412 m_facetsWidget = new DolphinFacetsWidget(this);
413 m_facetsWidget->installEventFilter(this);
414 m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
415 m_facetsWidget->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
416 connect(m_facetsWidget, &DolphinFacetsWidget::facetChanged, this, &DolphinSearchBox::slotFacetChanged);
417
418 // Put the options into a QScrollArea. This prevents increasing the view width
419 // in case that not enough width for the options is available.
420 QWidget *optionsContainer = new QWidget(this);
421
422 // Apply layout for the options
423 QHBoxLayout *optionsLayout = new QHBoxLayout(optionsContainer);
424 optionsLayout->setContentsMargins(0, 0, 0, 0);
425 optionsLayout->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
426 optionsLayout->addWidget(m_fileNameButton);
427 optionsLayout->addWidget(m_contentButton);
428 optionsLayout->addWidget(m_separator);
429 optionsLayout->addWidget(m_fromHereButton);
430 optionsLayout->addWidget(m_everywhereButton);
431 optionsLayout->addWidget(new KSeparator(Qt::Vertical, this));
432 optionsLayout->addWidget(moreSearchToolsButton);
433 optionsLayout->addStretch(1);
434
435 m_optionsScrollArea = new QScrollArea(this);
436 m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
437 m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
438 m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
439 m_optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
440 m_optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
441 m_optionsScrollArea->setWidget(optionsContainer);
442 m_optionsScrollArea->setWidgetResizable(true);
443
444 m_topLayout = new QVBoxLayout(this);
445 m_topLayout->setContentsMargins(0, Dolphin::LAYOUT_SPACING_SMALL, 0, 0);
446 m_topLayout->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
447 m_topLayout->addLayout(searchInputLayout);
448 m_topLayout->addWidget(m_optionsScrollArea);
449 m_topLayout->addWidget(m_facetsWidget);
450
451 loadSettings();
452
453 // The searching should be started automatically after the user did not change
454 // the text for a while
455 m_startSearchTimer = new QTimer(this);
456 m_startSearchTimer->setSingleShot(true);
457 m_startSearchTimer->setInterval(500);
458 connect(m_startSearchTimer, &QTimer::timeout, this, &DolphinSearchBox::emitSearchRequest);
459 }
460
461 QString DolphinSearchBox::queryTitle(const QString &text) const
462 {
463 return i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.", "Query Results from '%1'", text);
464 }
465
466 QUrl DolphinSearchBox::balooUrlForSearching() const
467 {
468 #if HAVE_BALOO
469 const QString text = m_searchInput->text();
470
471 Baloo::Query query;
472 query.addType(m_facetsWidget->facetType());
473
474 QStringList queryStrings = m_facetsWidget->searchTerms();
475
476 if (m_contentButton->isChecked()) {
477 queryStrings << text;
478 } else if (!text.isEmpty()) {
479 queryStrings << QStringLiteral("filename:\"%1\"").arg(text);
480 }
481
482 if (m_fromHereButton->isChecked()) {
483 query.setIncludeFolder(m_searchPath.toLocalFile());
484 }
485
486 query.setSearchString(queryStrings.join(QLatin1Char(' ')));
487
488 return query.toSearchUrl(queryTitle(text));
489 #else
490 return QUrl();
491 #endif
492 }
493
494 void DolphinSearchBox::updateFromQuery(const DolphinQuery &query)
495 {
496 // Block all signals to avoid unnecessary "searchRequest" signals
497 // while we adjust the search text and the facet widget.
498 blockSignals(true);
499
500 const QString customDir = query.includeFolder();
501 if (!customDir.isEmpty()) {
502 setSearchPath(QUrl::fromLocalFile(customDir));
503 } else {
504 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
505 }
506
507 setText(query.text());
508
509 if (query.hasContentSearch()) {
510 m_contentButton->setChecked(true);
511 } else if (query.hasFileName()) {
512 m_fileNameButton->setChecked(true);
513 }
514
515 m_facetsWidget->resetSearchTerms();
516 m_facetsWidget->setFacetType(query.type());
517 const QStringList searchTerms = query.searchTerms();
518 for (const QString &searchTerm : searchTerms) {
519 m_facetsWidget->setSearchTerm(searchTerm);
520 }
521
522 m_startSearchTimer->stop();
523 blockSignals(false);
524 }
525
526 void DolphinSearchBox::updateFacetsVisible()
527 {
528 const bool indexingEnabled = isIndexingEnabled();
529 m_facetsWidget->setEnabled(indexingEnabled);
530 m_facetsWidget->setVisible(indexingEnabled);
531 }
532
533 bool DolphinSearchBox::isIndexingEnabled() const
534 {
535 #if HAVE_BALOO
536 const Baloo::IndexerConfig searchInfo;
537 return searchInfo.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo.shouldBeIndexed(searchPath().toLocalFile());
538 #else
539 return false;
540 #endif
541 }
542
543 #include "moc_dolphinsearchbox.cpp"