]>
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 "dolphinquery.h"
13 #include "panels/places/placesitemmodel.h"
15 #include <KLocalizedString>
16 #include <KNS3/KMoreToolsMenuFactory>
18 #include <config-baloo.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 PlacesItemModel model
;
292 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
293 model
.createPlacesItem(label
,
295 QStringLiteral("folder-saved-search-symbolic"));
299 void DolphinSearchBox::initButton(QToolButton
* button
)
301 button
->installEventFilter(this);
302 button
->setAutoExclusive(true);
303 button
->setAutoRaise(true);
304 button
->setCheckable(true);
305 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
308 void DolphinSearchBox::loadSettings()
310 if (SearchSettings::location() == QLatin1String("Everywhere")) {
311 m_everywhereButton
->setChecked(true);
313 m_fromHereButton
->setChecked(true);
316 if (SearchSettings::what() == QLatin1String("Content")) {
317 m_contentButton
->setChecked(true);
319 m_fileNameButton
->setChecked(true);
322 updateFacetsVisible();
325 void DolphinSearchBox::saveSettings()
327 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
328 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
329 SearchSettings::self()->save();
332 void DolphinSearchBox::init()
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
);
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
);
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
);
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
);
367 // Create "Filename" and "Content" button
368 m_fileNameButton
= new QToolButton(this);
369 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
370 initButton(m_fileNameButton
);
372 m_contentButton
= new QToolButton();
373 m_contentButton
->setText(i18nc("action:button", "Content"));
374 initButton(m_contentButton
);
376 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
377 searchWhatGroup
->addButton(m_fileNameButton
);
378 searchWhatGroup
->addButton(m_contentButton
);
380 m_separator
= new KSeparator(Qt::Vertical
, this);
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
);
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
);
394 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
395 searchLocationGroup
->addButton(m_fromHereButton
);
396 searchLocationGroup
->addButton(m_everywhereButton
);
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
]()
407 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
408 moreSearchToolsButton
->menu()->clear();
409 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), { "files-find" }, this->m_searchPath
);
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
);
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);
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);
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);
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
);
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
);
462 QString
DolphinSearchBox::queryTitle(const QString
& text
) const
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
);
468 QUrl
DolphinSearchBox::balooUrlForSearching() const
471 const QString text
= m_searchInput
->text();
474 query
.addType(m_facetsWidget
->facetType());
476 QStringList queryStrings
= m_facetsWidget
->searchTerms();
478 if (m_contentButton
->isChecked()) {
479 queryStrings
<< text
;
480 } else if (!text
.isEmpty()) {
481 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
484 if (m_fromHereButton
->isChecked()) {
485 query
.setIncludeFolder(m_searchPath
.toLocalFile());
488 query
.setSearchString(queryStrings
.join(QLatin1Char(' ')));
490 return query
.toSearchUrl(queryTitle(text
));
496 void DolphinSearchBox::updateFromQuery(const DolphinQuery
& query
)
498 // Block all signals to avoid unnecessary "searchRequest" signals
499 // while we adjust the search text and the facet widget.
502 const QString customDir
= query
.includeFolder();
503 if (!customDir
.isEmpty()) {
504 setSearchPath(QUrl::fromLocalFile(customDir
));
506 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
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());
514 if (query
.hasContentSearch()) {
515 m_contentButton
->setChecked(true);
516 } else if (query
.hasFileName()) {
517 m_fileNameButton
->setChecked(true);
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
);
527 m_startSearchTimer
->stop();
531 void DolphinSearchBox::updateFacetsVisible()
533 const bool indexingEnabled
= isIndexingEnabled();
534 m_facetsWidget
->setEnabled(indexingEnabled
);
535 m_facetsWidget
->setVisible(indexingEnabled
);
538 bool DolphinSearchBox::isIndexingEnabled() const
541 const Baloo::IndexerConfig searchInfo
;
542 return searchInfo
.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo
.shouldBeIndexed(searchPath().toLocalFile());