]> 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 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 } else if (event->key() == Qt::Key_Down) {
209 Q_EMIT focusViewRequest();
210 }
211 }
212
213 bool DolphinSearchBox::eventFilter(QObject *obj, QEvent *event)
214 {
215 switch (event->type()) {
216 case QEvent::FocusIn:
217 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
218 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
219 // To avoid this issue, we delay the activation of the search box.
220 // We also don't want to schedule the activation process if we are already active,
221 // otherwise we can enter in a loop of FocusIn/FocusOut events with the searchbox of another tab.
222 if (!isActive()) {
223 QTimer::singleShot(0, this, [this] {
224 setActive(true);
225 setFocus();
226 });
227 }
228 break;
229
230 default:
231 break;
232 }
233
234 return QObject::eventFilter(obj, event);
235 }
236
237 void DolphinSearchBox::emitSearchRequest()
238 {
239 m_startSearchTimer->stop();
240 m_startedSearching = true;
241 m_saveSearchAction->setEnabled(true);
242 Q_EMIT searchRequest();
243 }
244
245 void DolphinSearchBox::emitCloseRequest()
246 {
247 m_startSearchTimer->stop();
248 m_startedSearching = false;
249 m_saveSearchAction->setEnabled(false);
250 Q_EMIT closeRequest();
251 }
252
253 void DolphinSearchBox::slotConfigurationChanged()
254 {
255 saveSettings();
256 if (m_startedSearching) {
257 emitSearchRequest();
258 }
259 }
260
261 void DolphinSearchBox::slotSearchTextChanged(const QString &text)
262 {
263 if (text.isEmpty()) {
264 m_startSearchTimer->stop();
265 } else {
266 m_startSearchTimer->start();
267 }
268 Q_EMIT searchTextChanged(text);
269 }
270
271 void DolphinSearchBox::slotReturnPressed()
272 {
273 emitSearchRequest();
274 Q_EMIT focusViewRequest();
275 }
276
277 void DolphinSearchBox::slotFacetChanged()
278 {
279 m_startedSearching = true;
280 m_startSearchTimer->stop();
281 Q_EMIT searchRequest();
282 }
283
284 void DolphinSearchBox::slotSearchSaved()
285 {
286 const QUrl searchURL = urlForSearching();
287 if (searchURL.isValid()) {
288 const QString label = i18n("Search for %1 in %2", text(), searchPath().fileName());
289 DolphinPlacesModelSingleton::instance().placesModel()->addPlace(label, searchURL, QStringLiteral("folder-saved-search-symbolic"));
290 }
291 }
292
293 void DolphinSearchBox::initButton(QToolButton *button)
294 {
295 button->installEventFilter(this);
296 button->setAutoExclusive(true);
297 button->setAutoRaise(true);
298 button->setCheckable(true);
299 connect(button, &QToolButton::clicked, this, &DolphinSearchBox::slotConfigurationChanged);
300 }
301
302 void DolphinSearchBox::loadSettings()
303 {
304 if (SearchSettings::location() == QLatin1String("Everywhere")) {
305 m_everywhereButton->setChecked(true);
306 } else {
307 m_fromHereButton->setChecked(true);
308 }
309
310 if (SearchSettings::what() == QLatin1String("Content")) {
311 m_contentButton->setChecked(true);
312 } else {
313 m_fileNameButton->setChecked(true);
314 }
315
316 updateFacetsVisible();
317 }
318
319 void DolphinSearchBox::saveSettings()
320 {
321 SearchSettings::setLocation(m_fromHereButton->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
322 SearchSettings::setWhat(m_fileNameButton->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
323 SearchSettings::self()->save();
324 }
325
326 void DolphinSearchBox::init()
327 {
328 // Create search box
329 m_searchInput = new QLineEdit(this);
330 m_searchInput->setPlaceholderText(i18n("Search…"));
331 m_searchInput->installEventFilter(this);
332 m_searchInput->setClearButtonEnabled(true);
333 m_searchInput->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
334 connect(m_searchInput, &QLineEdit::returnPressed, this, &DolphinSearchBox::slotReturnPressed);
335 connect(m_searchInput, &QLineEdit::textChanged, this, &DolphinSearchBox::slotSearchTextChanged);
336 setFocusProxy(m_searchInput);
337
338 // Add "Save search" button inside search box
339 m_saveSearchAction = new QAction(this);
340 m_saveSearchAction->setIcon(QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
341 m_saveSearchAction->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
342 m_saveSearchAction->setEnabled(false);
343 m_searchInput->addAction(m_saveSearchAction, QLineEdit::TrailingPosition);
344 connect(m_saveSearchAction, &QAction::triggered, this, &DolphinSearchBox::slotSearchSaved);
345
346 // Create close button
347 QToolButton *closeButton = new QToolButton(this);
348 closeButton->setAutoRaise(true);
349 closeButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
350 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
351 connect(closeButton, &QToolButton::clicked, this, &DolphinSearchBox::emitCloseRequest);
352
353 // Apply layout for the search input
354 QHBoxLayout *searchInputLayout = new QHBoxLayout();
355 searchInputLayout->setContentsMargins(0, 0, 0, 0);
356 searchInputLayout->addWidget(m_searchInput);
357 searchInputLayout->addWidget(closeButton);
358
359 // Create "Filename" and "Content" button
360 m_fileNameButton = new QToolButton(this);
361 m_fileNameButton->setText(i18nc("action:button", "Filename"));
362 initButton(m_fileNameButton);
363
364 m_contentButton = new QToolButton();
365 m_contentButton->setText(i18nc("action:button", "Content"));
366 initButton(m_contentButton);
367
368 QButtonGroup *searchWhatGroup = new QButtonGroup(this);
369 searchWhatGroup->addButton(m_fileNameButton);
370 searchWhatGroup->addButton(m_contentButton);
371
372 m_separator = new KSeparator(Qt::Vertical, this);
373
374 // Create "From Here" and "Your files" buttons
375 m_fromHereButton = new QToolButton(this);
376 m_fromHereButton->setText(i18nc("action:button", "From Here"));
377 initButton(m_fromHereButton);
378
379 m_everywhereButton = new QToolButton(this);
380 m_everywhereButton->setText(i18nc("action:button", "Your files"));
381 m_everywhereButton->setToolTip(i18nc("action:button", "Search in your home directory"));
382 m_everywhereButton->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
383 m_everywhereButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
384 initButton(m_everywhereButton);
385
386 QButtonGroup *searchLocationGroup = new QButtonGroup(this);
387 searchLocationGroup->addButton(m_fromHereButton);
388 searchLocationGroup->addButton(m_everywhereButton);
389
390 auto moreSearchToolsButton = new QToolButton(this);
391 moreSearchToolsButton->setAutoRaise(true);
392 moreSearchToolsButton->setPopupMode(QToolButton::InstantPopup);
393 moreSearchToolsButton->setIcon(QIcon::fromTheme("arrow-down-double"));
394 moreSearchToolsButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
395 moreSearchToolsButton->setText(i18n("More Search Tools"));
396 moreSearchToolsButton->setMenu(new QMenu(this));
397 connect(moreSearchToolsButton->menu(), &QMenu::aboutToShow, moreSearchToolsButton->menu(), [this, moreSearchToolsButton]() {
398 m_menuFactory.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
399 moreSearchToolsButton->menu()->clear();
400 m_menuFactory->fillMenuFromGroupingNames(moreSearchToolsButton->menu(), {"files-find"}, this->m_searchPath);
401 });
402
403 // Create "Facets" widget
404 m_facetsWidget = new DolphinFacetsWidget(this);
405 m_facetsWidget->installEventFilter(this);
406 m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
407 m_facetsWidget->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
408 connect(m_facetsWidget, &DolphinFacetsWidget::facetChanged, this, &DolphinSearchBox::slotFacetChanged);
409
410 // Put the options into a QScrollArea. This prevents increasing the view width
411 // in case that not enough width for the options is available.
412 QWidget *optionsContainer = new QWidget(this);
413
414 // Apply layout for the options
415 QHBoxLayout *optionsLayout = new QHBoxLayout(optionsContainer);
416 optionsLayout->setContentsMargins(0, 0, 0, 0);
417 optionsLayout->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
418 optionsLayout->addWidget(m_fileNameButton);
419 optionsLayout->addWidget(m_contentButton);
420 optionsLayout->addWidget(m_separator);
421 optionsLayout->addWidget(m_fromHereButton);
422 optionsLayout->addWidget(m_everywhereButton);
423 optionsLayout->addWidget(new KSeparator(Qt::Vertical, this));
424 optionsLayout->addWidget(moreSearchToolsButton);
425 optionsLayout->addStretch(1);
426
427 m_optionsScrollArea = new QScrollArea(this);
428 m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
429 m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
430 m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
431 m_optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
432 m_optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
433 m_optionsScrollArea->setWidget(optionsContainer);
434 m_optionsScrollArea->setWidgetResizable(true);
435
436 m_topLayout = new QVBoxLayout(this);
437 m_topLayout->setContentsMargins(0, Dolphin::LAYOUT_SPACING_SMALL, 0, 0);
438 m_topLayout->setSpacing(Dolphin::LAYOUT_SPACING_SMALL);
439 m_topLayout->addLayout(searchInputLayout);
440 m_topLayout->addWidget(m_optionsScrollArea);
441 m_topLayout->addWidget(m_facetsWidget);
442
443 loadSettings();
444
445 // The searching should be started automatically after the user did not change
446 // the text within one second
447 m_startSearchTimer = new QTimer(this);
448 m_startSearchTimer->setSingleShot(true);
449 m_startSearchTimer->setInterval(1000);
450 connect(m_startSearchTimer, &QTimer::timeout, this, &DolphinSearchBox::emitSearchRequest);
451 }
452
453 QString DolphinSearchBox::queryTitle(const QString &text) const
454 {
455 return i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.", "Query Results from '%1'", text);
456 }
457
458 QUrl DolphinSearchBox::balooUrlForSearching() const
459 {
460 #if HAVE_BALOO
461 const QString text = m_searchInput->text();
462
463 Baloo::Query query;
464 query.addType(m_facetsWidget->facetType());
465
466 QStringList queryStrings = m_facetsWidget->searchTerms();
467
468 if (m_contentButton->isChecked()) {
469 queryStrings << text;
470 } else if (!text.isEmpty()) {
471 queryStrings << QStringLiteral("filename:\"%1\"").arg(text);
472 }
473
474 if (m_fromHereButton->isChecked()) {
475 query.setIncludeFolder(m_searchPath.toLocalFile());
476 }
477
478 query.setSearchString(queryStrings.join(QLatin1Char(' ')));
479
480 return query.toSearchUrl(queryTitle(text));
481 #else
482 return QUrl();
483 #endif
484 }
485
486 void DolphinSearchBox::updateFromQuery(const DolphinQuery &query)
487 {
488 // Block all signals to avoid unnecessary "searchRequest" signals
489 // while we adjust the search text and the facet widget.
490 blockSignals(true);
491
492 const QString customDir = query.includeFolder();
493 if (!customDir.isEmpty()) {
494 setSearchPath(QUrl::fromLocalFile(customDir));
495 } else {
496 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
497 }
498
499 // If the input box has focus, do not update to avoid messing with user typing
500 if (!m_searchInput->hasFocus()) {
501 setText(query.text());
502 }
503
504 if (query.hasContentSearch()) {
505 m_contentButton->setChecked(true);
506 } else if (query.hasFileName()) {
507 m_fileNameButton->setChecked(true);
508 }
509
510 m_facetsWidget->resetSearchTerms();
511 m_facetsWidget->setFacetType(query.type());
512 const QStringList searchTerms = query.searchTerms();
513 for (const QString &searchTerm : searchTerms) {
514 m_facetsWidget->setSearchTerm(searchTerm);
515 }
516
517 m_startSearchTimer->stop();
518 blockSignals(false);
519 }
520
521 void DolphinSearchBox::updateFacetsVisible()
522 {
523 const bool indexingEnabled = isIndexingEnabled();
524 m_facetsWidget->setEnabled(indexingEnabled);
525 m_facetsWidget->setVisible(indexingEnabled);
526 }
527
528 bool DolphinSearchBox::isIndexingEnabled() const
529 {
530 #if HAVE_BALOO
531 const Baloo::IndexerConfig searchInfo;
532 return searchInfo.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo.shouldBeIndexed(searchPath().toLocalFile());
533 #else
534 return false;
535 #endif
536 }
537
538 #include "moc_dolphinsearchbox.cpp"