]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
26c78c11151cc2a66fada8aece903361cf4bac34
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>
31 #include <QScrollArea>
34 #include <QToolButton>
37 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
39 m_startedSearching(false),
42 m_searchInput(nullptr),
43 m_saveSearchAction(nullptr),
44 m_optionsScrollArea(nullptr),
45 m_fileNameButton(nullptr),
46 m_contentButton(nullptr),
48 m_fromHereButton(nullptr),
49 m_everywhereButton(nullptr),
50 m_facetsWidget(nullptr),
52 m_startSearchTimer(nullptr)
56 DolphinSearchBox::~DolphinSearchBox()
61 void DolphinSearchBox::setText(const QString
& text
)
63 m_searchInput
->setText(text
);
66 QString
DolphinSearchBox::text() const
68 return m_searchInput
->text();
71 void DolphinSearchBox::setSearchPath(const QUrl
& url
)
73 if (url
== m_searchPath
) {
77 const QUrl cleanedUrl
= url
.adjusted(QUrl::RemoveUserInfo
| QUrl::StripTrailingSlash
);
79 if (cleanedUrl
.path() == QDir::homePath()) {
80 m_fromHereButton
->setChecked(false);
81 m_everywhereButton
->setChecked(true);
82 if (!m_searchPath
.isEmpty()) {
86 m_everywhereButton
->setChecked(false);
87 m_fromHereButton
->setChecked(true);
92 QFontMetrics
metrics(m_fromHereButton
->font());
93 const int maxWidth
= metrics
.height() * 8;
95 QString location
= cleanedUrl
.fileName();
96 if (location
.isEmpty()) {
97 location
= cleanedUrl
.toString(QUrl::PreferLocalFile
);
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
)));
104 QUrl
DolphinSearchBox::searchPath() const
106 return m_everywhereButton
->isChecked() ? QUrl::fromLocalFile(QDir::homePath()) : m_searchPath
;
109 QUrl
DolphinSearchBox::urlForSearching() const
113 if (isIndexingEnabled()) {
114 url
= balooUrlForSearching();
116 url
.setScheme(QStringLiteral("filenamesearch"));
119 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
120 if (m_contentButton
->isChecked()) {
121 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
124 query
.addQueryItem(QStringLiteral("url"), searchPath().url());
125 query
.addQueryItem(QStringLiteral("title"), queryTitle(m_searchInput
->text()));
133 void DolphinSearchBox::fromSearchUrl(const QUrl
& url
)
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();
144 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
145 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
148 m_searchPath
= QUrl();
152 updateFacetsVisible();
155 void DolphinSearchBox::selectAll()
157 m_searchInput
->selectAll();
160 void DolphinSearchBox::setActive(bool active
)
162 if (active
!= m_active
) {
171 bool DolphinSearchBox::isActive() const
176 bool DolphinSearchBox::event(QEvent
* event
)
178 if (event
->type() == QEvent::Polish
) {
181 return QWidget::event(event
);
184 void DolphinSearchBox::showEvent(QShowEvent
* event
)
186 if (!event
->spontaneous()) {
187 m_searchInput
->setFocus();
188 m_startedSearching
= false;
192 void DolphinSearchBox::hideEvent(QHideEvent
* event
)
195 m_startedSearching
= false;
196 m_startSearchTimer
->stop();
199 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
201 QWidget::keyReleaseEvent(event
);
202 if (event
->key() == Qt::Key_Escape
) {
203 if (m_searchInput
->text().isEmpty()) {
204 Q_EMIT
closeRequest();
206 m_searchInput
->clear();
209 else if (event
->key() == Qt::Key_Down
) {
210 Q_EMIT
focusViewRequest();
214 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
216 switch (event
->type()) {
217 case QEvent::FocusIn
:
218 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
219 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
220 // To avoid this issue, we delay the activation of the search box.
221 // We also don't want to schedule the activation process if we are already active,
222 // otherwise we can enter in a loop of FocusIn/FocusOut events with the searchbox of another tab.
224 QTimer::singleShot(0, this, [this] {
235 return QObject::eventFilter(obj
, event
);
238 void DolphinSearchBox::emitSearchRequest()
240 m_startSearchTimer
->stop();
241 m_startedSearching
= true;
242 m_saveSearchAction
->setEnabled(true);
243 Q_EMIT
searchRequest();
246 void DolphinSearchBox::emitCloseRequest()
248 m_startSearchTimer
->stop();
249 m_startedSearching
= false;
250 m_saveSearchAction
->setEnabled(false);
251 Q_EMIT
closeRequest();
254 void DolphinSearchBox::slotConfigurationChanged()
257 if (m_startedSearching
) {
262 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
265 if (text
.isEmpty()) {
266 m_startSearchTimer
->stop();
268 m_startSearchTimer
->start();
270 Q_EMIT
searchTextChanged(text
);
273 void DolphinSearchBox::slotReturnPressed()
276 Q_EMIT
focusViewRequest();
279 void DolphinSearchBox::slotFacetChanged()
281 m_startedSearching
= true;
282 m_startSearchTimer
->stop();
283 Q_EMIT
searchRequest();
286 void DolphinSearchBox::slotSearchSaved()
288 const QUrl searchURL
= urlForSearching();
289 if (searchURL
.isValid()) {
290 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
291 DolphinPlacesModelSingleton::instance().placesModel()->addPlace(label
, searchURL
, QStringLiteral("folder-saved-search-symbolic"));
295 void DolphinSearchBox::initButton(QToolButton
* button
)
297 button
->installEventFilter(this);
298 button
->setAutoExclusive(true);
299 button
->setAutoRaise(true);
300 button
->setCheckable(true);
301 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
304 void DolphinSearchBox::loadSettings()
306 if (SearchSettings::location() == QLatin1String("Everywhere")) {
307 m_everywhereButton
->setChecked(true);
309 m_fromHereButton
->setChecked(true);
312 if (SearchSettings::what() == QLatin1String("Content")) {
313 m_contentButton
->setChecked(true);
315 m_fileNameButton
->setChecked(true);
318 updateFacetsVisible();
321 void DolphinSearchBox::saveSettings()
323 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
324 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
325 SearchSettings::self()->save();
328 void DolphinSearchBox::init()
331 m_searchInput
= new QLineEdit(this);
332 m_searchInput
->setPlaceholderText(i18n("Search..."));
333 m_searchInput
->installEventFilter(this);
334 m_searchInput
->setClearButtonEnabled(true);
335 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
336 connect(m_searchInput
, &QLineEdit::returnPressed
,
337 this, &DolphinSearchBox::slotReturnPressed
);
338 connect(m_searchInput
, &QLineEdit::textChanged
,
339 this, &DolphinSearchBox::slotSearchTextChanged
);
340 setFocusProxy(m_searchInput
);
342 // Add "Save search" button inside search box
343 m_saveSearchAction
= new QAction(this);
344 m_saveSearchAction
->setIcon (QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
345 m_saveSearchAction
->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
346 m_saveSearchAction
->setEnabled(false);
347 m_searchInput
->addAction(m_saveSearchAction
, QLineEdit::TrailingPosition
);
348 connect(m_saveSearchAction
, &QAction::triggered
, this, &DolphinSearchBox::slotSearchSaved
);
350 // Create close button
351 QToolButton
* closeButton
= new QToolButton(this);
352 closeButton
->setAutoRaise(true);
353 closeButton
->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
354 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
355 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
357 // Apply layout for the search input
358 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
359 searchInputLayout
->setContentsMargins(0, 0, 0, 0);
360 searchInputLayout
->addWidget(m_searchInput
);
361 searchInputLayout
->addWidget(closeButton
);
363 // Create "Filename" and "Content" button
364 m_fileNameButton
= new QToolButton(this);
365 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
366 initButton(m_fileNameButton
);
368 m_contentButton
= new QToolButton();
369 m_contentButton
->setText(i18nc("action:button", "Content"));
370 initButton(m_contentButton
);
372 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
373 searchWhatGroup
->addButton(m_fileNameButton
);
374 searchWhatGroup
->addButton(m_contentButton
);
376 m_separator
= new KSeparator(Qt::Vertical
, this);
378 // Create "From Here" and "Your files" buttons
379 m_fromHereButton
= new QToolButton(this);
380 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
381 initButton(m_fromHereButton
);
383 m_everywhereButton
= new QToolButton(this);
384 m_everywhereButton
->setText(i18nc("action:button", "Your files"));
385 m_everywhereButton
->setToolTip(i18nc("action:button", "Search in your home directory"));
386 m_everywhereButton
->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
387 m_everywhereButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
388 initButton(m_everywhereButton
);
390 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
391 searchLocationGroup
->addButton(m_fromHereButton
);
392 searchLocationGroup
->addButton(m_everywhereButton
);
394 auto moreSearchToolsButton
= new QToolButton(this);
395 moreSearchToolsButton
->setAutoRaise(true);
396 moreSearchToolsButton
->setPopupMode(QToolButton::InstantPopup
);
397 moreSearchToolsButton
->setIcon(QIcon::fromTheme("arrow-down-double"));
398 moreSearchToolsButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
399 moreSearchToolsButton
->setText(i18n("More Search Tools"));
400 moreSearchToolsButton
->setMenu(new QMenu(this));
401 connect(moreSearchToolsButton
->menu(), &QMenu::aboutToShow
, moreSearchToolsButton
->menu(), [this, moreSearchToolsButton
]()
403 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
404 moreSearchToolsButton
->menu()->clear();
405 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), { "files-find" }, this->m_searchPath
);
408 // Create "Facets" widget
409 m_facetsWidget
= new DolphinFacetsWidget(this);
410 m_facetsWidget
->installEventFilter(this);
411 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
412 m_facetsWidget
->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
413 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
415 // Put the options into a QScrollArea. This prevents increasing the view width
416 // in case that not enough width for the options is available.
417 QWidget
* optionsContainer
= new QWidget(this);
419 // Apply layout for the options
420 QHBoxLayout
* optionsLayout
= new QHBoxLayout(optionsContainer
);
421 optionsLayout
->setContentsMargins(0, 0, 0, 0);
422 optionsLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
423 optionsLayout
->addWidget(m_fileNameButton
);
424 optionsLayout
->addWidget(m_contentButton
);
425 optionsLayout
->addWidget(m_separator
);
426 optionsLayout
->addWidget(m_fromHereButton
);
427 optionsLayout
->addWidget(m_everywhereButton
);
428 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
429 optionsLayout
->addWidget(moreSearchToolsButton
);
430 optionsLayout
->addStretch(1);
432 m_optionsScrollArea
= new QScrollArea(this);
433 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
434 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
435 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
436 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
437 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
438 m_optionsScrollArea
->setWidget(optionsContainer
);
439 m_optionsScrollArea
->setWidgetResizable(true);
441 m_topLayout
= new QVBoxLayout(this);
442 m_topLayout
->setContentsMargins(0, Dolphin::LAYOUT_SPACING_SMALL
, 0, 0);
443 m_topLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
444 m_topLayout
->addLayout(searchInputLayout
);
445 m_topLayout
->addWidget(m_optionsScrollArea
);
446 m_topLayout
->addWidget(m_facetsWidget
);
450 // The searching should be started automatically after the user did not change
451 // the text within one second
452 m_startSearchTimer
= new QTimer(this);
453 m_startSearchTimer
->setSingleShot(true);
454 m_startSearchTimer
->setInterval(1000);
455 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
458 QString
DolphinSearchBox::queryTitle(const QString
& text
) const
460 return i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
461 "Query Results from '%1'", text
);
464 QUrl
DolphinSearchBox::balooUrlForSearching() const
467 const QString text
= m_searchInput
->text();
470 query
.addType(m_facetsWidget
->facetType());
472 QStringList queryStrings
= m_facetsWidget
->searchTerms();
474 if (m_contentButton
->isChecked()) {
475 queryStrings
<< text
;
476 } else if (!text
.isEmpty()) {
477 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
480 if (m_fromHereButton
->isChecked()) {
481 query
.setIncludeFolder(m_searchPath
.toLocalFile());
484 query
.setSearchString(queryStrings
.join(QLatin1Char(' ')));
486 return query
.toSearchUrl(queryTitle(text
));
492 void DolphinSearchBox::updateFromQuery(const DolphinQuery
& query
)
494 // Block all signals to avoid unnecessary "searchRequest" signals
495 // while we adjust the search text and the facet widget.
498 const QString customDir
= query
.includeFolder();
499 if (!customDir
.isEmpty()) {
500 setSearchPath(QUrl::fromLocalFile(customDir
));
502 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
505 // If the input box has focus, do not update to avoid messing with user typing
506 if (!m_searchInput
->hasFocus()) {
507 setText(query
.text());
510 if (query
.hasContentSearch()) {
511 m_contentButton
->setChecked(true);
512 } else if (query
.hasFileName()) {
513 m_fileNameButton
->setChecked(true);
516 m_facetsWidget
->resetSearchTerms();
517 m_facetsWidget
->setFacetType(query
.type());
518 const QStringList searchTerms
= query
.searchTerms();
519 for (const QString
& searchTerm
: searchTerms
) {
520 m_facetsWidget
->setSearchTerm(searchTerm
);
523 m_startSearchTimer
->stop();
527 void DolphinSearchBox::updateFacetsVisible()
529 const bool indexingEnabled
= isIndexingEnabled();
530 m_facetsWidget
->setEnabled(indexingEnabled
);
531 m_facetsWidget
->setVisible(indexingEnabled
);
534 bool DolphinSearchBox::isIndexingEnabled() const
537 const Baloo::IndexerConfig searchInfo
;
538 return searchInfo
.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo
.shouldBeIndexed(searchPath().toLocalFile());