]>
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
8 #include "dolphinsearchbox.h"
10 #include "dolphin_searchsettings.h"
11 #include "dolphinfacetswidget.h"
12 #include "dolphinplacesmodelsingleton.h"
13 #include "dolphinquery.h"
15 #include <KLocalizedString>
16 #include <KMoreToolsMenuFactory>
18 #include <config-dolphin.h>
20 #include <Baloo/Query>
21 #include <Baloo/IndexerConfig>
24 #include <QButtonGroup>
26 #include <QFontDatabase>
27 #include <QHBoxLayout>
32 #include <QScrollArea>
35 #include <QToolButton>
38 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
40 m_startedSearching(false),
43 m_searchInput(nullptr),
44 m_saveSearchAction(nullptr),
45 m_optionsScrollArea(nullptr),
46 m_fileNameButton(nullptr),
47 m_contentButton(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 m_searchInput
->setText(text
);
67 QString
DolphinSearchBox::text() const
69 return m_searchInput
->text();
72 void DolphinSearchBox::setSearchPath(const QUrl
& url
)
74 if (url
== m_searchPath
) {
78 const QUrl cleanedUrl
= url
.adjusted(QUrl::RemoveUserInfo
| QUrl::StripTrailingSlash
);
80 if (cleanedUrl
.path() == QDir::homePath()) {
81 m_fromHereButton
->setChecked(false);
82 m_everywhereButton
->setChecked(true);
83 if (!m_searchPath
.isEmpty()) {
87 m_everywhereButton
->setChecked(false);
88 m_fromHereButton
->setChecked(true);
93 QFontMetrics
metrics(m_fromHereButton
->font());
94 const int maxWidth
= metrics
.height() * 8;
96 QString location
= cleanedUrl
.fileName();
97 if (location
.isEmpty()) {
98 location
= cleanedUrl
.toString(QUrl::PreferLocalFile
);
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
)));
105 QUrl
DolphinSearchBox::searchPath() const
107 return m_everywhereButton
->isChecked() ? QUrl::fromLocalFile(QDir::homePath()) : m_searchPath
;
110 QUrl
DolphinSearchBox::urlForSearching() const
114 if (isIndexingEnabled()) {
115 url
= balooUrlForSearching();
117 url
.setScheme(QStringLiteral("filenamesearch"));
120 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
121 if (m_contentButton
->isChecked()) {
122 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
125 query
.addQueryItem(QStringLiteral("url"), searchPath().url());
126 query
.addQueryItem(QStringLiteral("title"), queryTitle(m_searchInput
->text()));
134 void DolphinSearchBox::fromSearchUrl(const QUrl
& url
)
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();
145 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
146 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
149 m_searchPath
= QUrl();
153 updateFacetsVisible();
156 void DolphinSearchBox::selectAll()
158 m_searchInput
->selectAll();
161 void DolphinSearchBox::setActive(bool active
)
163 if (active
!= m_active
) {
172 bool DolphinSearchBox::isActive() const
177 bool DolphinSearchBox::event(QEvent
* event
)
179 if (event
->type() == QEvent::Polish
) {
182 return QWidget::event(event
);
185 void DolphinSearchBox::showEvent(QShowEvent
* event
)
187 if (!event
->spontaneous()) {
188 m_searchInput
->setFocus();
189 m_startedSearching
= false;
193 void DolphinSearchBox::hideEvent(QHideEvent
* event
)
196 m_startedSearching
= false;
197 m_startSearchTimer
->stop();
200 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
202 QWidget::keyReleaseEvent(event
);
203 if (event
->key() == Qt::Key_Escape
) {
204 if (m_searchInput
->text().isEmpty()) {
205 Q_EMIT
closeRequest();
207 m_searchInput
->clear();
210 else if (event
->key() == Qt::Key_Down
) {
211 Q_EMIT
focusViewRequest();
215 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
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.
225 QTimer::singleShot(0, this, [this] {
236 return QObject::eventFilter(obj
, event
);
239 void DolphinSearchBox::emitSearchRequest()
241 m_startSearchTimer
->stop();
242 m_startedSearching
= true;
243 m_saveSearchAction
->setEnabled(true);
244 Q_EMIT
searchRequest();
247 void DolphinSearchBox::emitCloseRequest()
249 m_startSearchTimer
->stop();
250 m_startedSearching
= false;
251 m_saveSearchAction
->setEnabled(false);
252 Q_EMIT
closeRequest();
255 void DolphinSearchBox::slotConfigurationChanged()
258 if (m_startedSearching
) {
263 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
266 if (text
.isEmpty()) {
267 m_startSearchTimer
->stop();
269 m_startSearchTimer
->start();
271 Q_EMIT
searchTextChanged(text
);
274 void DolphinSearchBox::slotReturnPressed()
277 Q_EMIT
focusViewRequest();
280 void DolphinSearchBox::slotFacetChanged()
282 m_startedSearching
= true;
283 m_startSearchTimer
->stop();
284 Q_EMIT
searchRequest();
287 void DolphinSearchBox::slotSearchSaved()
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"));
296 void DolphinSearchBox::initButton(QToolButton
* button
)
298 button
->installEventFilter(this);
299 button
->setAutoExclusive(true);
300 button
->setAutoRaise(true);
301 button
->setCheckable(true);
302 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
305 void DolphinSearchBox::loadSettings()
307 if (SearchSettings::location() == QLatin1String("Everywhere")) {
308 m_everywhereButton
->setChecked(true);
310 m_fromHereButton
->setChecked(true);
313 if (SearchSettings::what() == QLatin1String("Content")) {
314 m_contentButton
->setChecked(true);
316 m_fileNameButton
->setChecked(true);
319 updateFacetsVisible();
322 void DolphinSearchBox::saveSettings()
324 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
325 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
326 SearchSettings::self()->save();
329 void DolphinSearchBox::init()
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
);
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
);
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
);
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
);
364 // Create "Filename" and "Content" button
365 m_fileNameButton
= new QToolButton(this);
366 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
367 initButton(m_fileNameButton
);
369 m_contentButton
= new QToolButton();
370 m_contentButton
->setText(i18nc("action:button", "Content"));
371 initButton(m_contentButton
);
373 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
374 searchWhatGroup
->addButton(m_fileNameButton
);
375 searchWhatGroup
->addButton(m_contentButton
);
377 m_separator
= new KSeparator(Qt::Vertical
, this);
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
);
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
);
391 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
392 searchLocationGroup
->addButton(m_fromHereButton
);
393 searchLocationGroup
->addButton(m_everywhereButton
);
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
]()
404 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
405 moreSearchToolsButton
->menu()->clear();
406 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), { "files-find" }, this->m_searchPath
);
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
);
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);
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);
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);
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
);
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
);
459 QString
DolphinSearchBox::queryTitle(const QString
& text
) const
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
);
465 QUrl
DolphinSearchBox::balooUrlForSearching() const
468 const QString text
= m_searchInput
->text();
471 query
.addType(m_facetsWidget
->facetType());
473 QStringList queryStrings
= m_facetsWidget
->searchTerms();
475 if (m_contentButton
->isChecked()) {
476 queryStrings
<< text
;
477 } else if (!text
.isEmpty()) {
478 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
481 if (m_fromHereButton
->isChecked()) {
482 query
.setIncludeFolder(m_searchPath
.toLocalFile());
485 query
.setSearchString(queryStrings
.join(QLatin1Char(' ')));
487 return query
.toSearchUrl(queryTitle(text
));
493 void DolphinSearchBox::updateFromQuery(const DolphinQuery
& query
)
495 // Block all signals to avoid unnecessary "searchRequest" signals
496 // while we adjust the search text and the facet widget.
499 const QString customDir
= query
.includeFolder();
500 if (!customDir
.isEmpty()) {
501 setSearchPath(QUrl::fromLocalFile(customDir
));
503 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
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());
511 if (query
.hasContentSearch()) {
512 m_contentButton
->setChecked(true);
513 } else if (query
.hasFileName()) {
514 m_fileNameButton
->setChecked(true);
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
);
524 m_startSearchTimer
->stop();
528 void DolphinSearchBox::updateFacetsVisible()
530 const bool indexingEnabled
= isIndexingEnabled();
531 m_facetsWidget
->setEnabled(indexingEnabled
);
532 m_facetsWidget
->setVisible(indexingEnabled
);
535 bool DolphinSearchBox::isIndexingEnabled() const
538 const Baloo::IndexerConfig searchInfo
;
539 return searchInfo
.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo
.shouldBeIndexed(searchPath().toLocalFile());