]>
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 <KLocalizedString>
17 #include <KMoreToolsMenuFactory>
20 #include <Baloo/IndexerConfig>
21 #include <Baloo/Query>
24 #include <QButtonGroup>
26 #include <QFontDatabase>
27 #include <QHBoxLayout>
31 #include <QScrollArea>
34 #include <QToolButton>
37 DolphinSearchBox::DolphinSearchBox(QWidget
*parent
)
39 , m_startedSearching(false)
41 , m_topLayout(nullptr)
42 , m_searchInput(nullptr)
43 , m_saveSearchAction(nullptr)
44 , m_optionsScrollArea(nullptr)
45 , m_fileNameButton(nullptr)
46 , m_contentButton(nullptr)
47 , m_separator(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 if (m_searchInput
->text() != text
) {
64 m_searchInput
->setText(text
);
68 QString
DolphinSearchBox::text() const
70 return m_searchInput
->text();
73 void DolphinSearchBox::setSearchPath(const QUrl
&url
)
75 if (url
== m_searchPath
) {
79 const QUrl cleanedUrl
= url
.adjusted(QUrl::RemoveUserInfo
| QUrl::StripTrailingSlash
);
81 if (cleanedUrl
.path() == QDir::homePath()) {
82 m_fromHereButton
->setChecked(false);
83 m_everywhereButton
->setChecked(true);
84 if (!m_searchPath
.isEmpty()) {
88 m_everywhereButton
->setChecked(false);
89 m_fromHereButton
->setChecked(true);
94 QFontMetrics
metrics(m_fromHereButton
->font());
95 const int maxWidth
= metrics
.height() * 8;
97 QString location
= cleanedUrl
.fileName();
98 if (location
.isEmpty()) {
99 location
= cleanedUrl
.toString(QUrl::PreferLocalFile
);
101 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
102 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
103 m_fromHereButton
->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl
.toString(QUrl::PreferLocalFile
)));
106 QUrl
DolphinSearchBox::searchPath() const
108 return m_everywhereButton
->isChecked() ? QUrl::fromLocalFile(QDir::homePath()) : m_searchPath
;
111 QUrl
DolphinSearchBox::urlForSearching() const
115 if (isIndexingEnabled()) {
116 url
= balooUrlForSearching();
118 url
.setScheme(QStringLiteral("filenamesearch"));
121 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
122 if (m_contentButton
->isChecked()) {
123 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
126 query
.addQueryItem(QStringLiteral("url"), searchPath().url());
127 query
.addQueryItem(QStringLiteral("title"), queryTitle(m_searchInput
->text()));
135 void DolphinSearchBox::fromSearchUrl(const QUrl
&url
)
137 if (DolphinQuery::supportsScheme(url
.scheme())) {
138 const DolphinQuery query
= DolphinQuery::fromSearchUrl(url
);
139 updateFromQuery(query
);
140 } else if (url
.scheme() == QLatin1String("filenamesearch")) {
141 const QUrlQuery
query(url
);
142 setText(query
.queryItemValue(QStringLiteral("search")));
143 if (m_searchPath
.scheme() != url
.scheme()) {
144 m_searchPath
= QUrl();
146 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
147 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
150 m_searchPath
= QUrl();
154 updateFacetsVisible();
157 void DolphinSearchBox::selectAll()
159 m_searchInput
->selectAll();
162 void DolphinSearchBox::setActive(bool active
)
164 if (active
!= m_active
) {
173 bool DolphinSearchBox::isActive() const
178 bool DolphinSearchBox::event(QEvent
*event
)
180 if (event
->type() == QEvent::Polish
) {
183 return QWidget::event(event
);
186 void DolphinSearchBox::showEvent(QShowEvent
*event
)
188 if (!event
->spontaneous()) {
189 m_searchInput
->setFocus();
190 m_startedSearching
= false;
194 void DolphinSearchBox::hideEvent(QHideEvent
*event
)
197 m_startedSearching
= false;
198 m_startSearchTimer
->stop();
201 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
*event
)
203 QWidget::keyReleaseEvent(event
);
204 if (event
->key() == Qt::Key_Escape
) {
205 if (m_searchInput
->text().isEmpty()) {
208 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
)
265 if (text
.isEmpty()) {
266 // Restore URL when search box is cleared by closing and reopening the box.
268 Q_EMIT
openRequest();
270 m_startSearchTimer
->start();
272 Q_EMIT
searchTextChanged(text
);
275 void DolphinSearchBox::slotReturnPressed()
277 if (m_searchInput
->text().isEmpty()) {
282 Q_EMIT
focusViewRequest();
285 void DolphinSearchBox::slotFacetChanged()
287 m_startedSearching
= true;
288 m_startSearchTimer
->stop();
289 Q_EMIT
searchRequest();
292 void DolphinSearchBox::slotSearchSaved()
294 const QUrl searchURL
= urlForSearching();
295 if (searchURL
.isValid()) {
296 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
297 DolphinPlacesModelSingleton::instance().placesModel()->addPlace(label
, searchURL
, QStringLiteral("folder-saved-search-symbolic"));
301 void DolphinSearchBox::initButton(QToolButton
*button
)
303 button
->installEventFilter(this);
304 button
->setAutoExclusive(true);
305 button
->setAutoRaise(true);
306 button
->setCheckable(true);
307 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
310 void DolphinSearchBox::loadSettings()
312 if (SearchSettings::location() == QLatin1String("Everywhere")) {
313 m_everywhereButton
->setChecked(true);
315 m_fromHereButton
->setChecked(true);
318 if (SearchSettings::what() == QLatin1String("Content")) {
319 m_contentButton
->setChecked(true);
321 m_fileNameButton
->setChecked(true);
324 updateFacetsVisible();
327 void DolphinSearchBox::saveSettings()
329 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
330 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
331 SearchSettings::self()->save();
334 void DolphinSearchBox::init()
337 m_searchInput
= new QLineEdit(this);
338 m_searchInput
->setPlaceholderText(i18n("Search…"));
339 m_searchInput
->installEventFilter(this);
340 m_searchInput
->setClearButtonEnabled(true);
341 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
342 connect(m_searchInput
, &QLineEdit::returnPressed
, this, &DolphinSearchBox::slotReturnPressed
);
343 connect(m_searchInput
, &QLineEdit::textChanged
, 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
]() {
406 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
407 moreSearchToolsButton
->menu()->clear();
408 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), {"files-find"}, this->m_searchPath
);
411 // Create "Facets" widget
412 m_facetsWidget
= new DolphinFacetsWidget(this);
413 m_facetsWidget
->installEventFilter(this);
414 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
415 m_facetsWidget
->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
416 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
418 // Put the options into a QScrollArea. This prevents increasing the view width
419 // in case that not enough width for the options is available.
420 QWidget
*optionsContainer
= new QWidget(this);
422 // Apply layout for the options
423 QHBoxLayout
*optionsLayout
= new QHBoxLayout(optionsContainer
);
424 optionsLayout
->setContentsMargins(0, 0, 0, 0);
425 optionsLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
426 optionsLayout
->addWidget(m_fileNameButton
);
427 optionsLayout
->addWidget(m_contentButton
);
428 optionsLayout
->addWidget(m_separator
);
429 optionsLayout
->addWidget(m_fromHereButton
);
430 optionsLayout
->addWidget(m_everywhereButton
);
431 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
432 optionsLayout
->addWidget(moreSearchToolsButton
);
433 optionsLayout
->addStretch(1);
435 m_optionsScrollArea
= new QScrollArea(this);
436 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
437 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
438 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
439 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
440 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
441 m_optionsScrollArea
->setWidget(optionsContainer
);
442 m_optionsScrollArea
->setWidgetResizable(true);
444 m_topLayout
= new QVBoxLayout(this);
445 m_topLayout
->setContentsMargins(0, Dolphin::LAYOUT_SPACING_SMALL
, 0, 0);
446 m_topLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
447 m_topLayout
->addLayout(searchInputLayout
);
448 m_topLayout
->addWidget(m_optionsScrollArea
);
449 m_topLayout
->addWidget(m_facetsWidget
);
453 // The searching should be started automatically after the user did not change
454 // the text for a while
455 m_startSearchTimer
= new QTimer(this);
456 m_startSearchTimer
->setSingleShot(true);
457 m_startSearchTimer
->setInterval(500);
458 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
461 QString
DolphinSearchBox::queryTitle(const QString
&text
) const
463 return i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.", "Query Results from '%1'", text
);
466 QUrl
DolphinSearchBox::balooUrlForSearching() const
469 const QString text
= m_searchInput
->text();
472 query
.addType(m_facetsWidget
->facetType());
474 QStringList queryStrings
= m_facetsWidget
->searchTerms();
476 if (m_contentButton
->isChecked()) {
477 queryStrings
<< text
;
478 } else if (!text
.isEmpty()) {
479 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
482 if (m_fromHereButton
->isChecked()) {
483 query
.setIncludeFolder(m_searchPath
.toLocalFile());
486 query
.setSearchString(queryStrings
.join(QLatin1Char(' ')));
488 return query
.toSearchUrl(queryTitle(text
));
494 void DolphinSearchBox::updateFromQuery(const DolphinQuery
&query
)
496 // Block all signals to avoid unnecessary "searchRequest" signals
497 // while we adjust the search text and the facet widget.
500 const QString customDir
= query
.includeFolder();
501 if (!customDir
.isEmpty()) {
502 setSearchPath(QUrl::fromLocalFile(customDir
));
504 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
507 setText(query
.text());
509 if (query
.hasContentSearch()) {
510 m_contentButton
->setChecked(true);
511 } else if (query
.hasFileName()) {
512 m_fileNameButton
->setChecked(true);
515 m_facetsWidget
->resetSearchTerms();
516 m_facetsWidget
->setFacetType(query
.type());
517 const QStringList searchTerms
= query
.searchTerms();
518 for (const QString
&searchTerm
: searchTerms
) {
519 m_facetsWidget
->setSearchTerm(searchTerm
);
522 m_startSearchTimer
->stop();
526 void DolphinSearchBox::updateFacetsVisible()
528 const bool indexingEnabled
= isIndexingEnabled();
529 m_facetsWidget
->setEnabled(indexingEnabled
);
530 m_facetsWidget
->setVisible(indexingEnabled
);
533 bool DolphinSearchBox::isIndexingEnabled() const
536 const Baloo::IndexerConfig searchInfo
;
537 return searchInfo
.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo
.shouldBeIndexed(searchPath().toLocalFile());
543 #include "moc_dolphinsearchbox.cpp"