]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
2 * SPDX-FileCopyrightText: 2010 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinsearchbox.h"
10 #include "dolphin_searchsettings.h"
11 #include "dolphinfacetswidget.h"
12 #include "dolphinplacesmodelsingleton.h"
13 #include "dolphinquery.h"
15 #include "config-dolphin.h"
16 #include <KIO/ApplicationLauncherJob>
17 #include <KLocalizedString>
21 #include <Baloo/IndexerConfig>
22 #include <Baloo/Query>
25 #include <QButtonGroup>
27 #include <QFontDatabase>
28 #include <QHBoxLayout>
32 #include <QScrollArea>
35 #include <QToolButton>
38 DolphinSearchBox::DolphinSearchBox(QWidget
*parent
)
40 , m_startedSearching(false)
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)
53 , m_startSearchTimer(nullptr)
57 DolphinSearchBox::~DolphinSearchBox()
62 void DolphinSearchBox::setText(const QString
&text
)
64 if (m_searchInput
->text() != text
) {
65 m_searchInput
->setText(text
);
69 QString
DolphinSearchBox::text() const
71 return m_searchInput
->text();
74 void DolphinSearchBox::setSearchPath(const QUrl
&url
)
76 if (url
== m_searchPath
) {
80 const QUrl cleanedUrl
= url
.adjusted(QUrl::RemoveUserInfo
| QUrl::StripTrailingSlash
);
82 if (cleanedUrl
.path() == QDir::homePath()) {
83 m_fromHereButton
->setChecked(false);
84 m_everywhereButton
->setChecked(true);
85 if (!m_searchPath
.isEmpty()) {
89 m_everywhereButton
->setChecked(false);
90 m_fromHereButton
->setChecked(true);
95 QFontMetrics
metrics(m_fromHereButton
->font());
96 const int maxWidth
= metrics
.height() * 8;
98 QString location
= cleanedUrl
.fileName();
99 if (location
.isEmpty()) {
100 location
= cleanedUrl
.toString(QUrl::PreferLocalFile
);
102 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
103 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
104 m_fromHereButton
->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl
.toString(QUrl::PreferLocalFile
)));
107 QUrl
DolphinSearchBox::searchPath() const
109 return m_everywhereButton
->isChecked() ? QUrl::fromLocalFile(QDir::homePath()) : m_searchPath
;
112 QUrl
DolphinSearchBox::urlForSearching() const
116 if (isIndexingEnabled()) {
117 url
= balooUrlForSearching();
119 url
.setScheme(QStringLiteral("filenamesearch"));
122 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
123 if (m_contentButton
->isChecked()) {
124 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
127 query
.addQueryItem(QStringLiteral("url"), searchPath().url());
128 query
.addQueryItem(QStringLiteral("title"), queryTitle(m_searchInput
->text()));
136 void DolphinSearchBox::fromSearchUrl(const QUrl
&url
)
138 if (DolphinQuery::supportsScheme(url
.scheme())) {
139 const DolphinQuery query
= DolphinQuery::fromSearchUrl(url
);
140 updateFromQuery(query
);
141 } else if (url
.scheme() == QLatin1String("filenamesearch")) {
142 const QUrlQuery
query(url
);
143 setText(query
.queryItemValue(QStringLiteral("search")));
144 if (m_searchPath
.scheme() != url
.scheme()) {
145 m_searchPath
= QUrl();
147 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
148 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
151 m_searchPath
= QUrl();
155 updateFacetsVisible();
158 void DolphinSearchBox::selectAll()
160 m_searchInput
->selectAll();
163 void DolphinSearchBox::setActive(bool active
)
165 if (active
!= m_active
) {
174 bool DolphinSearchBox::isActive() const
179 bool DolphinSearchBox::event(QEvent
*event
)
181 if (event
->type() == QEvent::Polish
) {
184 return QWidget::event(event
);
187 void DolphinSearchBox::showEvent(QShowEvent
*event
)
189 if (!event
->spontaneous()) {
190 m_searchInput
->setFocus();
191 m_startedSearching
= false;
195 void DolphinSearchBox::hideEvent(QHideEvent
*event
)
198 m_startedSearching
= false;
199 m_startSearchTimer
->stop();
202 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
*event
)
204 QWidget::keyReleaseEvent(event
);
205 if (event
->key() == Qt::Key_Escape
) {
206 if (m_searchInput
->text().isEmpty()) {
209 m_searchInput
->clear();
211 } else if (event
->key() == Qt::Key_Down
) {
212 Q_EMIT
focusViewRequest();
216 bool DolphinSearchBox::eventFilter(QObject
*obj
, QEvent
*event
)
218 switch (event
->type()) {
219 case QEvent::FocusIn
:
220 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
221 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
222 // To avoid this issue, we delay the activation of the search box.
223 // We also don't want to schedule the activation process if we are already active,
224 // otherwise we can enter in a loop of FocusIn/FocusOut events with the searchbox of another tab.
226 QTimer::singleShot(0, this, [this] {
237 return QObject::eventFilter(obj
, event
);
240 void DolphinSearchBox::emitSearchRequest()
242 m_startSearchTimer
->stop();
243 m_startedSearching
= true;
244 m_saveSearchAction
->setEnabled(true);
245 Q_EMIT
searchRequest();
248 void DolphinSearchBox::emitCloseRequest()
250 m_startSearchTimer
->stop();
251 m_startedSearching
= false;
252 m_saveSearchAction
->setEnabled(false);
253 Q_EMIT
closeRequest();
256 void DolphinSearchBox::slotConfigurationChanged()
259 if (m_startedSearching
) {
264 void DolphinSearchBox::slotSearchTextChanged(const QString
&text
)
266 if (text
.isEmpty()) {
267 // Restore URL when search box is cleared by closing and reopening the box.
269 Q_EMIT
openRequest();
271 m_startSearchTimer
->start();
273 Q_EMIT
searchTextChanged(text
);
276 void DolphinSearchBox::slotReturnPressed()
278 if (m_searchInput
->text().isEmpty()) {
283 Q_EMIT
focusViewRequest();
286 void DolphinSearchBox::slotFacetChanged()
288 m_startedSearching
= true;
289 m_startSearchTimer
->stop();
290 Q_EMIT
searchRequest();
293 void DolphinSearchBox::slotSearchSaved()
295 const QUrl searchURL
= urlForSearching();
296 if (searchURL
.isValid()) {
297 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
298 DolphinPlacesModelSingleton::instance().placesModel()->addPlace(label
, searchURL
, QStringLiteral("folder-saved-search-symbolic"));
302 void DolphinSearchBox::initButton(QToolButton
*button
)
304 button
->installEventFilter(this);
305 button
->setAutoExclusive(true);
306 button
->setAutoRaise(true);
307 button
->setCheckable(true);
308 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
311 void DolphinSearchBox::loadSettings()
313 if (SearchSettings::location() == QLatin1String("Everywhere")) {
314 m_everywhereButton
->setChecked(true);
316 m_fromHereButton
->setChecked(true);
319 if (SearchSettings::what() == QLatin1String("Content")) {
320 m_contentButton
->setChecked(true);
322 m_fileNameButton
->setChecked(true);
325 updateFacetsVisible();
328 void DolphinSearchBox::saveSettings()
330 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
331 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
332 SearchSettings::self()->save();
335 void DolphinSearchBox::init()
338 m_searchInput
= new QLineEdit(this);
339 m_searchInput
->setPlaceholderText(i18n("Search…"));
340 m_searchInput
->installEventFilter(this);
341 m_searchInput
->setClearButtonEnabled(true);
342 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
343 connect(m_searchInput
, &QLineEdit::returnPressed
, this, &DolphinSearchBox::slotReturnPressed
);
344 connect(m_searchInput
, &QLineEdit::textChanged
, this, &DolphinSearchBox::slotSearchTextChanged
);
345 setFocusProxy(m_searchInput
);
347 // Add "Save search" button inside search box
348 m_saveSearchAction
= new QAction(this);
349 m_saveSearchAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
350 m_saveSearchAction
->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
351 m_saveSearchAction
->setEnabled(false);
352 m_searchInput
->addAction(m_saveSearchAction
, QLineEdit::TrailingPosition
);
353 connect(m_saveSearchAction
, &QAction::triggered
, this, &DolphinSearchBox::slotSearchSaved
);
355 // Create close button
356 QToolButton
*closeButton
= new QToolButton(this);
357 closeButton
->setAutoRaise(true);
358 closeButton
->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
359 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
360 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
362 // Apply layout for the search input
363 QHBoxLayout
*searchInputLayout
= new QHBoxLayout();
364 searchInputLayout
->setContentsMargins(0, 0, 0, 0);
365 searchInputLayout
->addWidget(m_searchInput
);
366 searchInputLayout
->addWidget(closeButton
);
368 // Create "Filename" and "Content" button
369 m_fileNameButton
= new QToolButton(this);
370 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
371 initButton(m_fileNameButton
);
373 m_contentButton
= new QToolButton();
374 m_contentButton
->setText(i18nc("action:button", "Content"));
375 initButton(m_contentButton
);
377 QButtonGroup
*searchWhatGroup
= new QButtonGroup(this);
378 searchWhatGroup
->addButton(m_fileNameButton
);
379 searchWhatGroup
->addButton(m_contentButton
);
381 m_separator
= new KSeparator(Qt::Vertical
, this);
383 // Create "From Here" and "Your files" buttons
384 m_fromHereButton
= new QToolButton(this);
385 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
386 initButton(m_fromHereButton
);
388 m_everywhereButton
= new QToolButton(this);
389 m_everywhereButton
->setText(i18nc("action:button", "Your files"));
390 m_everywhereButton
->setToolTip(i18nc("action:button", "Search in your home directory"));
391 m_everywhereButton
->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
392 m_everywhereButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
393 initButton(m_everywhereButton
);
395 QButtonGroup
*searchLocationGroup
= new QButtonGroup(this);
396 searchLocationGroup
->addButton(m_fromHereButton
);
397 searchLocationGroup
->addButton(m_everywhereButton
);
399 KService::Ptr kfind
= KService::serviceByDesktopName(QStringLiteral("org.kde.kfind"));
401 QToolButton
*kfindToolsButton
= nullptr;
403 kfindToolsButton
= new QToolButton(this);
404 kfindToolsButton
->setAutoRaise(true);
405 kfindToolsButton
->setPopupMode(QToolButton::InstantPopup
);
406 kfindToolsButton
->setIcon(QIcon::fromTheme("arrow-down-double"));
407 kfindToolsButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
408 kfindToolsButton
->setText(i18n("Open %1", kfind
->name()));
409 kfindToolsButton
->setIcon(QIcon::fromTheme(kfind
->icon()));
411 connect(kfindToolsButton
, &QToolButton::clicked
, this, [this, kfind
] {
412 auto *job
= new KIO::ApplicationLauncherJob(kfind
);
413 job
->setUrls({m_searchPath
});
418 // Create "Facets" widget
419 m_facetsWidget
= new DolphinFacetsWidget(this);
420 m_facetsWidget
->installEventFilter(this);
421 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
422 m_facetsWidget
->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
423 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
425 // Put the options into a QScrollArea. This prevents increasing the view width
426 // in case that not enough width for the options is available.
427 QWidget
*optionsContainer
= new QWidget(this);
429 // Apply layout for the options
430 QHBoxLayout
*optionsLayout
= new QHBoxLayout(optionsContainer
);
431 optionsLayout
->setContentsMargins(0, 0, 0, 0);
432 optionsLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
433 optionsLayout
->addWidget(m_fileNameButton
);
434 optionsLayout
->addWidget(m_contentButton
);
435 optionsLayout
->addWidget(m_separator
);
436 optionsLayout
->addWidget(m_fromHereButton
);
437 optionsLayout
->addWidget(m_everywhereButton
);
438 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
439 if (kfindToolsButton
) {
440 optionsLayout
->addWidget(kfindToolsButton
);
442 optionsLayout
->addStretch(1);
444 m_optionsScrollArea
= new QScrollArea(this);
445 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
446 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
447 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
448 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
449 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
450 m_optionsScrollArea
->setWidget(optionsContainer
);
451 m_optionsScrollArea
->setWidgetResizable(true);
453 m_topLayout
= new QVBoxLayout(this);
454 m_topLayout
->setContentsMargins(0, Dolphin::LAYOUT_SPACING_SMALL
, 0, 0);
455 m_topLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
456 m_topLayout
->addLayout(searchInputLayout
);
457 m_topLayout
->addWidget(m_optionsScrollArea
);
458 m_topLayout
->addWidget(m_facetsWidget
);
462 // The searching should be started automatically after the user did not change
463 // the text for a while
464 m_startSearchTimer
= new QTimer(this);
465 m_startSearchTimer
->setSingleShot(true);
466 m_startSearchTimer
->setInterval(500);
467 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
470 QString
DolphinSearchBox::queryTitle(const QString
&text
) const
472 return i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.", "Query Results from '%1'", text
);
475 QUrl
DolphinSearchBox::balooUrlForSearching() const
478 const QString text
= m_searchInput
->text();
481 query
.addType(m_facetsWidget
->facetType());
483 QStringList queryStrings
= m_facetsWidget
->searchTerms();
485 if (m_contentButton
->isChecked()) {
486 queryStrings
<< text
;
487 } else if (!text
.isEmpty()) {
488 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
491 if (m_fromHereButton
->isChecked()) {
492 query
.setIncludeFolder(m_searchPath
.toLocalFile());
495 query
.setSearchString(queryStrings
.join(QLatin1Char(' ')));
497 return query
.toSearchUrl(queryTitle(text
));
503 void DolphinSearchBox::updateFromQuery(const DolphinQuery
&query
)
505 // Block all signals to avoid unnecessary "searchRequest" signals
506 // while we adjust the search text and the facet widget.
509 const QString customDir
= query
.includeFolder();
510 if (!customDir
.isEmpty()) {
511 setSearchPath(QUrl::fromLocalFile(customDir
));
513 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
516 setText(query
.text());
518 if (query
.hasContentSearch()) {
519 m_contentButton
->setChecked(true);
520 } else if (query
.hasFileName()) {
521 m_fileNameButton
->setChecked(true);
524 m_facetsWidget
->resetSearchTerms();
525 m_facetsWidget
->setFacetType(query
.type());
526 const QStringList searchTerms
= query
.searchTerms();
527 for (const QString
&searchTerm
: searchTerms
) {
528 m_facetsWidget
->setSearchTerm(searchTerm
);
531 m_startSearchTimer
->stop();
535 void DolphinSearchBox::updateFacetsVisible()
537 const bool indexingEnabled
= isIndexingEnabled();
538 m_facetsWidget
->setEnabled(indexingEnabled
);
539 m_facetsWidget
->setVisible(indexingEnabled
);
542 bool DolphinSearchBox::isIndexingEnabled() const
545 const Baloo::IndexerConfig searchInfo
;
546 return searchInfo
.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo
.shouldBeIndexed(searchPath().toLocalFile());
552 #include "moc_dolphinsearchbox.cpp"