]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
DolphinView: set the parent of layout in the ctor
[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 "dolphinquery.h"
13 #include "panels/places/placesitemmodel.h"
14
15 #include <KLocalizedString>
16 #include <KNS3/KMoreToolsMenuFactory>
17 #include <KSeparator>
18 #include <config-baloo.h>
19 #ifdef 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 <QLabel>
31 #include <QLineEdit>
32 #include <QScrollArea>
33 #include <QShowEvent>
34 #include <QTimer>
35 #include <QToolButton>
36 #include <QUrlQuery>
37
38 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
39 QWidget(parent),
40 m_startedSearching(false),
41 m_active(true),
42 m_topLayout(nullptr),
43 m_searchInput(nullptr),
44 m_saveSearchAction(nullptr),
45 m_optionsScrollArea(nullptr),
46 m_fileNameButton(nullptr),
47 m_contentButton(nullptr),
48 m_separator(nullptr),
49 m_fromHereButton(nullptr),
50 m_everywhereButton(nullptr),
51 m_facetsWidget(nullptr),
52 m_searchPath(),
53 m_startSearchTimer(nullptr)
54 {
55 }
56
57 DolphinSearchBox::~DolphinSearchBox()
58 {
59 saveSettings();
60 }
61
62 void DolphinSearchBox::setText(const QString& text)
63 {
64 m_searchInput->setText(text);
65 }
66
67 QString DolphinSearchBox::text() const
68 {
69 return m_searchInput->text();
70 }
71
72 void DolphinSearchBox::setSearchPath(const QUrl& url)
73 {
74 if (url == m_searchPath) {
75 return;
76 }
77
78 const QUrl cleanedUrl = url.adjusted(QUrl::RemoveUserInfo | QUrl::StripTrailingSlash);
79
80 if (cleanedUrl.path() == QDir::homePath()) {
81 m_fromHereButton->setChecked(false);
82 m_everywhereButton->setChecked(true);
83 if (!m_searchPath.isEmpty()) {
84 return;
85 }
86 } else {
87 m_everywhereButton->setChecked(false);
88 m_fromHereButton->setChecked(true);
89 }
90
91 m_searchPath = url;
92
93 QFontMetrics metrics(m_fromHereButton->font());
94 const int maxWidth = metrics.height() * 8;
95
96 QString location = cleanedUrl.fileName();
97 if (location.isEmpty()) {
98 location = cleanedUrl.toString(QUrl::PreferLocalFile);
99 }
100 const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth);
101 m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation));
102 m_fromHereButton->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl.toString(QUrl::PreferLocalFile)));
103 }
104
105 QUrl DolphinSearchBox::searchPath() const
106 {
107 return m_everywhereButton->isChecked() ? QUrl::fromLocalFile(QDir::homePath()) : m_searchPath;
108 }
109
110 QUrl DolphinSearchBox::urlForSearching() const
111 {
112 QUrl url;
113
114 if (isIndexingEnabled()) {
115 url = balooUrlForSearching();
116 } else {
117 url.setScheme(QStringLiteral("filenamesearch"));
118
119 QUrlQuery query;
120 query.addQueryItem(QStringLiteral("search"), m_searchInput->text());
121 if (m_contentButton->isChecked()) {
122 query.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
123 }
124
125 query.addQueryItem(QStringLiteral("url"), searchPath().url());
126 query.addQueryItem(QStringLiteral("title"), queryTitle(m_searchInput->text()));
127
128 url.setQuery(query);
129 }
130
131 return url;
132 }
133
134 void DolphinSearchBox::fromSearchUrl(const QUrl& url)
135 {
136 if (DolphinQuery::supportsScheme(url.scheme())) {
137 const DolphinQuery query = DolphinQuery::fromSearchUrl(url);
138 updateFromQuery(query);
139 } else if (url.scheme() == QLatin1String("filenamesearch")) {
140 const QUrlQuery query(url);
141 setText(query.queryItemValue(QStringLiteral("search")));
142 if (m_searchPath.scheme() != url.scheme()) {
143 m_searchPath = QUrl();
144 }
145 setSearchPath(QUrl::fromUserInput(query.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile));
146 m_contentButton->setChecked(query.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
147 } else {
148 setText(QString());
149 m_searchPath = QUrl();
150 setSearchPath(url);
151 }
152
153 updateFacetsVisible();
154 }
155
156 void DolphinSearchBox::selectAll()
157 {
158 m_searchInput->selectAll();
159 }
160
161 void DolphinSearchBox::setActive(bool active)
162 {
163 if (active != m_active) {
164 m_active = active;
165
166 if (active) {
167 Q_EMIT activated();
168 }
169 }
170 }
171
172 bool DolphinSearchBox::isActive() const
173 {
174 return m_active;
175 }
176
177 bool DolphinSearchBox::event(QEvent* event)
178 {
179 if (event->type() == QEvent::Polish) {
180 init();
181 }
182 return QWidget::event(event);
183 }
184
185 void DolphinSearchBox::showEvent(QShowEvent* event)
186 {
187 if (!event->spontaneous()) {
188 m_searchInput->setFocus();
189 m_startedSearching = false;
190 }
191 }
192
193 void DolphinSearchBox::hideEvent(QHideEvent* event)
194 {
195 Q_UNUSED(event)
196 m_startedSearching = false;
197 m_startSearchTimer->stop();
198 }
199
200 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
201 {
202 QWidget::keyReleaseEvent(event);
203 if (event->key() == Qt::Key_Escape) {
204 if (m_searchInput->text().isEmpty()) {
205 Q_EMIT closeRequest();
206 } else {
207 m_searchInput->clear();
208 }
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
266 if (text.isEmpty()) {
267 m_startSearchTimer->stop();
268 } else {
269 m_startSearchTimer->start();
270 }
271 Q_EMIT searchTextChanged(text);
272 }
273
274 void DolphinSearchBox::slotReturnPressed()
275 {
276 emitSearchRequest();
277 Q_EMIT focusViewRequest();
278 }
279
280 void DolphinSearchBox::slotFacetChanged()
281 {
282 m_startedSearching = true;
283 m_startSearchTimer->stop();
284 Q_EMIT searchRequest();
285 }
286
287 void DolphinSearchBox::slotSearchSaved()
288 {
289 const QUrl searchURL = urlForSearching();
290 if (searchURL.isValid()) {
291 PlacesItemModel model;
292 const QString label = i18n("Search for %1 in %2", text(), searchPath().fileName());
293 model.createPlacesItem(label,
294 searchURL,
295 QStringLiteral("folder-saved-search-symbolic"));
296 }
297 }
298
299 void DolphinSearchBox::initButton(QToolButton* button)
300 {
301 button->installEventFilter(this);
302 button->setAutoExclusive(true);
303 button->setAutoRaise(true);
304 button->setCheckable(true);
305 connect(button, &QToolButton::clicked, this, &DolphinSearchBox::slotConfigurationChanged);
306 }
307
308 void DolphinSearchBox::loadSettings()
309 {
310 if (SearchSettings::location() == QLatin1String("Everywhere")) {
311 m_everywhereButton->setChecked(true);
312 } else {
313 m_fromHereButton->setChecked(true);
314 }
315
316 if (SearchSettings::what() == QLatin1String("Content")) {
317 m_contentButton->setChecked(true);
318 } else {
319 m_fileNameButton->setChecked(true);
320 }
321
322 updateFacetsVisible();
323 }
324
325 void DolphinSearchBox::saveSettings()
326 {
327 SearchSettings::setLocation(m_fromHereButton->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
328 SearchSettings::setWhat(m_fileNameButton->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
329 SearchSettings::self()->save();
330 }
331
332 void DolphinSearchBox::init()
333 {
334 // Create search box
335 m_searchInput = new QLineEdit(this);
336 m_searchInput->setPlaceholderText(i18n("Search..."));
337 m_searchInput->installEventFilter(this);
338 m_searchInput->setClearButtonEnabled(true);
339 m_searchInput->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
340 connect(m_searchInput, &QLineEdit::returnPressed,
341 this, &DolphinSearchBox::slotReturnPressed);
342 connect(m_searchInput, &QLineEdit::textChanged,
343 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 {
407 m_menuFactory.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
408 moreSearchToolsButton->menu()->clear();
409 m_menuFactory->fillMenuFromGroupingNames(moreSearchToolsButton->menu(), { "files-find" }, this->m_searchPath);
410 } );
411
412 // Create "Facets" widget
413 m_facetsWidget = new DolphinFacetsWidget(this);
414 m_facetsWidget->installEventFilter(this);
415 m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
416 m_facetsWidget->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
417 connect(m_facetsWidget, &DolphinFacetsWidget::facetChanged, this, &DolphinSearchBox::slotFacetChanged);
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
423 // Apply layout for the options
424 QHBoxLayout* optionsLayout = new QHBoxLayout(optionsContainer);
425 optionsLayout->setContentsMargins(0, 0, 0, 0);
426 optionsLayout->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
427 optionsLayout->addWidget(m_fileNameButton);
428 optionsLayout->addWidget(m_contentButton);
429 optionsLayout->addWidget(m_separator);
430 optionsLayout->addWidget(m_fromHereButton);
431 optionsLayout->addWidget(m_everywhereButton);
432 optionsLayout->addWidget(new KSeparator(Qt::Vertical, this));
433 optionsLayout->addWidget(moreSearchToolsButton);
434 optionsLayout->addStretch(1);
435
436 m_optionsScrollArea = new QScrollArea(this);
437 m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
438 m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
439 m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
440 m_optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
441 m_optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
442 m_optionsScrollArea->setWidget(optionsContainer);
443 m_optionsScrollArea->setWidgetResizable(true);
444
445 m_topLayout = new QVBoxLayout(this);
446 m_topLayout->setContentsMargins(0, 0, 0, 0);
447 m_topLayout->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
448 m_topLayout->addLayout(searchInputLayout);
449 m_topLayout->addWidget(m_optionsScrollArea);
450 m_topLayout->addWidget(m_facetsWidget);
451
452 loadSettings();
453
454 // The searching should be started automatically after the user did not change
455 // the text within one second
456 m_startSearchTimer = new QTimer(this);
457 m_startSearchTimer->setSingleShot(true);
458 m_startSearchTimer->setInterval(1000);
459 connect(m_startSearchTimer, &QTimer::timeout, this, &DolphinSearchBox::emitSearchRequest);
460 }
461
462 QString DolphinSearchBox::queryTitle(const QString& text) const
463 {
464 return i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
465 "Query Results from '%1'", text);
466 }
467
468 QUrl DolphinSearchBox::balooUrlForSearching() const
469 {
470 #ifdef 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 // If the input box has focus, do not update to avoid messing with user typing
510 if (!m_searchInput->hasFocus()) {
511 setText(query.text());
512 }
513
514 if (query.hasContentSearch()) {
515 m_contentButton->setChecked(true);
516 } else if (query.hasFileName()) {
517 m_fileNameButton->setChecked(true);
518 }
519
520 m_facetsWidget->resetSearchTerms();
521 m_facetsWidget->setFacetType(query.type());
522 const QStringList searchTerms = query.searchTerms();
523 for (const QString& searchTerm : searchTerms) {
524 m_facetsWidget->setSearchTerm(searchTerm);
525 }
526
527 m_startSearchTimer->stop();
528 blockSignals(false);
529 }
530
531 void DolphinSearchBox::updateFacetsVisible()
532 {
533 const bool indexingEnabled = isIndexingEnabled();
534 m_facetsWidget->setEnabled(indexingEnabled);
535 m_facetsWidget->setVisible(indexingEnabled);
536 }
537
538 bool DolphinSearchBox::isIndexingEnabled() const
539 {
540 #ifdef HAVE_BALOO
541 const Baloo::IndexerConfig searchInfo;
542 return searchInfo.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo.shouldBeIndexed(searchPath().toLocalFile());
543 #else
544 return false;
545 #endif
546 }