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