]>
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 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();
208 } else if (event
->key() == Qt::Key_Down
) {
209 Q_EMIT
focusViewRequest();
213 bool DolphinSearchBox::eventFilter(QObject
*obj
, QEvent
*event
)
215 switch (event
->type()) {
216 case QEvent::FocusIn
:
217 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
218 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
219 // To avoid this issue, we delay the activation of the search box.
220 // We also don't want to schedule the activation process if we are already active,
221 // otherwise we can enter in a loop of FocusIn/FocusOut events with the searchbox of another tab.
223 QTimer::singleShot(0, this, [this] {
234 return QObject::eventFilter(obj
, event
);
237 void DolphinSearchBox::emitSearchRequest()
239 m_startSearchTimer
->stop();
240 m_startedSearching
= true;
241 m_saveSearchAction
->setEnabled(true);
242 Q_EMIT
searchRequest();
245 void DolphinSearchBox::emitCloseRequest()
247 m_startSearchTimer
->stop();
248 m_startedSearching
= false;
249 m_saveSearchAction
->setEnabled(false);
250 Q_EMIT
closeRequest();
253 void DolphinSearchBox::slotConfigurationChanged()
256 if (m_startedSearching
) {
261 void DolphinSearchBox::slotSearchTextChanged(const QString
&text
)
263 if (text
.isEmpty()) {
264 m_startSearchTimer
->stop();
266 m_startSearchTimer
->start();
268 Q_EMIT
searchTextChanged(text
);
271 void DolphinSearchBox::slotReturnPressed()
274 Q_EMIT
focusViewRequest();
277 void DolphinSearchBox::slotFacetChanged()
279 m_startedSearching
= true;
280 m_startSearchTimer
->stop();
281 Q_EMIT
searchRequest();
284 void DolphinSearchBox::slotSearchSaved()
286 const QUrl searchURL
= urlForSearching();
287 if (searchURL
.isValid()) {
288 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
289 DolphinPlacesModelSingleton::instance().placesModel()->addPlace(label
, searchURL
, QStringLiteral("folder-saved-search-symbolic"));
293 void DolphinSearchBox::initButton(QToolButton
*button
)
295 button
->installEventFilter(this);
296 button
->setAutoExclusive(true);
297 button
->setAutoRaise(true);
298 button
->setCheckable(true);
299 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
302 void DolphinSearchBox::loadSettings()
304 if (SearchSettings::location() == QLatin1String("Everywhere")) {
305 m_everywhereButton
->setChecked(true);
307 m_fromHereButton
->setChecked(true);
310 if (SearchSettings::what() == QLatin1String("Content")) {
311 m_contentButton
->setChecked(true);
313 m_fileNameButton
->setChecked(true);
316 updateFacetsVisible();
319 void DolphinSearchBox::saveSettings()
321 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
322 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
323 SearchSettings::self()->save();
326 void DolphinSearchBox::init()
329 m_searchInput
= new QLineEdit(this);
330 m_searchInput
->setPlaceholderText(i18n("Search…"));
331 m_searchInput
->installEventFilter(this);
332 m_searchInput
->setClearButtonEnabled(true);
333 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
334 connect(m_searchInput
, &QLineEdit::returnPressed
, this, &DolphinSearchBox::slotReturnPressed
);
335 connect(m_searchInput
, &QLineEdit::textChanged
, this, &DolphinSearchBox::slotSearchTextChanged
);
336 setFocusProxy(m_searchInput
);
338 // Add "Save search" button inside search box
339 m_saveSearchAction
= new QAction(this);
340 m_saveSearchAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
341 m_saveSearchAction
->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
342 m_saveSearchAction
->setEnabled(false);
343 m_searchInput
->addAction(m_saveSearchAction
, QLineEdit::TrailingPosition
);
344 connect(m_saveSearchAction
, &QAction::triggered
, this, &DolphinSearchBox::slotSearchSaved
);
346 // Create close button
347 QToolButton
*closeButton
= new QToolButton(this);
348 closeButton
->setAutoRaise(true);
349 closeButton
->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
350 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
351 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
353 // Apply layout for the search input
354 QHBoxLayout
*searchInputLayout
= new QHBoxLayout();
355 searchInputLayout
->setContentsMargins(0, 0, 0, 0);
356 searchInputLayout
->addWidget(m_searchInput
);
357 searchInputLayout
->addWidget(closeButton
);
359 // Create "Filename" and "Content" button
360 m_fileNameButton
= new QToolButton(this);
361 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
362 initButton(m_fileNameButton
);
364 m_contentButton
= new QToolButton();
365 m_contentButton
->setText(i18nc("action:button", "Content"));
366 initButton(m_contentButton
);
368 QButtonGroup
*searchWhatGroup
= new QButtonGroup(this);
369 searchWhatGroup
->addButton(m_fileNameButton
);
370 searchWhatGroup
->addButton(m_contentButton
);
372 m_separator
= new KSeparator(Qt::Vertical
, this);
374 // Create "From Here" and "Your files" buttons
375 m_fromHereButton
= new QToolButton(this);
376 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
377 initButton(m_fromHereButton
);
379 m_everywhereButton
= new QToolButton(this);
380 m_everywhereButton
->setText(i18nc("action:button", "Your files"));
381 m_everywhereButton
->setToolTip(i18nc("action:button", "Search in your home directory"));
382 m_everywhereButton
->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
383 m_everywhereButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
384 initButton(m_everywhereButton
);
386 QButtonGroup
*searchLocationGroup
= new QButtonGroup(this);
387 searchLocationGroup
->addButton(m_fromHereButton
);
388 searchLocationGroup
->addButton(m_everywhereButton
);
390 auto moreSearchToolsButton
= new QToolButton(this);
391 moreSearchToolsButton
->setAutoRaise(true);
392 moreSearchToolsButton
->setPopupMode(QToolButton::InstantPopup
);
393 moreSearchToolsButton
->setIcon(QIcon::fromTheme("arrow-down-double"));
394 moreSearchToolsButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
395 moreSearchToolsButton
->setText(i18n("More Search Tools"));
396 moreSearchToolsButton
->setMenu(new QMenu(this));
397 connect(moreSearchToolsButton
->menu(), &QMenu::aboutToShow
, moreSearchToolsButton
->menu(), [this, moreSearchToolsButton
]() {
398 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
399 moreSearchToolsButton
->menu()->clear();
400 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), {"files-find"}, this->m_searchPath
);
403 // Create "Facets" widget
404 m_facetsWidget
= new DolphinFacetsWidget(this);
405 m_facetsWidget
->installEventFilter(this);
406 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
407 m_facetsWidget
->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
408 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
410 // Put the options into a QScrollArea. This prevents increasing the view width
411 // in case that not enough width for the options is available.
412 QWidget
*optionsContainer
= new QWidget(this);
414 // Apply layout for the options
415 QHBoxLayout
*optionsLayout
= new QHBoxLayout(optionsContainer
);
416 optionsLayout
->setContentsMargins(0, 0, 0, 0);
417 optionsLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
418 optionsLayout
->addWidget(m_fileNameButton
);
419 optionsLayout
->addWidget(m_contentButton
);
420 optionsLayout
->addWidget(m_separator
);
421 optionsLayout
->addWidget(m_fromHereButton
);
422 optionsLayout
->addWidget(m_everywhereButton
);
423 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
424 optionsLayout
->addWidget(moreSearchToolsButton
);
425 optionsLayout
->addStretch(1);
427 m_optionsScrollArea
= new QScrollArea(this);
428 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
429 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
430 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
431 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
432 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
433 m_optionsScrollArea
->setWidget(optionsContainer
);
434 m_optionsScrollArea
->setWidgetResizable(true);
436 m_topLayout
= new QVBoxLayout(this);
437 m_topLayout
->setContentsMargins(0, Dolphin::LAYOUT_SPACING_SMALL
, 0, 0);
438 m_topLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
439 m_topLayout
->addLayout(searchInputLayout
);
440 m_topLayout
->addWidget(m_optionsScrollArea
);
441 m_topLayout
->addWidget(m_facetsWidget
);
445 // The searching should be started automatically after the user did not change
446 // the text within one second
447 m_startSearchTimer
= new QTimer(this);
448 m_startSearchTimer
->setSingleShot(true);
449 m_startSearchTimer
->setInterval(1000);
450 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
453 QString
DolphinSearchBox::queryTitle(const QString
&text
) const
455 return i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.", "Query Results from '%1'", text
);
458 QUrl
DolphinSearchBox::balooUrlForSearching() const
461 const QString text
= m_searchInput
->text();
464 query
.addType(m_facetsWidget
->facetType());
466 QStringList queryStrings
= m_facetsWidget
->searchTerms();
468 if (m_contentButton
->isChecked()) {
469 queryStrings
<< text
;
470 } else if (!text
.isEmpty()) {
471 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
474 if (m_fromHereButton
->isChecked()) {
475 query
.setIncludeFolder(m_searchPath
.toLocalFile());
478 query
.setSearchString(queryStrings
.join(QLatin1Char(' ')));
480 return query
.toSearchUrl(queryTitle(text
));
486 void DolphinSearchBox::updateFromQuery(const DolphinQuery
&query
)
488 // Block all signals to avoid unnecessary "searchRequest" signals
489 // while we adjust the search text and the facet widget.
492 const QString customDir
= query
.includeFolder();
493 if (!customDir
.isEmpty()) {
494 setSearchPath(QUrl::fromLocalFile(customDir
));
496 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
499 // If the input box has focus, do not update to avoid messing with user typing
500 if (!m_searchInput
->hasFocus()) {
501 setText(query
.text());
504 if (query
.hasContentSearch()) {
505 m_contentButton
->setChecked(true);
506 } else if (query
.hasFileName()) {
507 m_fileNameButton
->setChecked(true);
510 m_facetsWidget
->resetSearchTerms();
511 m_facetsWidget
->setFacetType(query
.type());
512 const QStringList searchTerms
= query
.searchTerms();
513 for (const QString
&searchTerm
: searchTerms
) {
514 m_facetsWidget
->setSearchTerm(searchTerm
);
517 m_startSearchTimer
->stop();
521 void DolphinSearchBox::updateFacetsVisible()
523 const bool indexingEnabled
= isIndexingEnabled();
524 m_facetsWidget
->setEnabled(indexingEnabled
);
525 m_facetsWidget
->setVisible(indexingEnabled
);
528 bool DolphinSearchBox::isIndexingEnabled() const
531 const Baloo::IndexerConfig searchInfo
;
532 return searchInfo
.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo
.shouldBeIndexed(searchPath().toLocalFile());
538 #include "moc_dolphinsearchbox.cpp"