]>
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 <KIO/ApplicationLauncherJob>
17 #include <KLocalizedString>
21 #include <Baloo/IndexerConfig>
22 #include <Baloo/Query>
25 #include <QButtonGroup>
27 #include <QFontDatabase>
28 #include <QHBoxLayout>
32 #include <QScrollArea>
35 #include <QToolButton>
38 DolphinSearchBox::DolphinSearchBox(QWidget
*parent
)
40 , m_startedSearching(false)
42 , m_topLayout(nullptr)
43 , m_searchInput(nullptr)
44 , m_saveSearchAction(nullptr)
45 , m_optionsScrollArea(nullptr)
46 , m_fileNameButton(nullptr)
47 , m_contentButton(nullptr)
48 , m_separator(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 if (m_searchInput
->text() != text
) {
65 m_searchInput
->setText(text
);
69 QString
DolphinSearchBox::text() const
71 return m_searchInput
->text();
74 void DolphinSearchBox::setSearchPath(const QUrl
&url
)
76 if (url
== m_searchPath
) {
80 const QUrl cleanedUrl
= url
.adjusted(QUrl::RemoveUserInfo
| QUrl::StripTrailingSlash
);
82 if (cleanedUrl
.path() == QDir::homePath()) {
83 m_fromHereButton
->setChecked(false);
84 m_everywhereButton
->setChecked(true);
85 if (!m_searchPath
.isEmpty()) {
89 m_everywhereButton
->setChecked(false);
90 m_fromHereButton
->setChecked(true);
95 QFontMetrics
metrics(m_fromHereButton
->font());
96 const int maxWidth
= metrics
.height() * 8;
98 QString location
= cleanedUrl
.fileName();
99 if (location
.isEmpty()) {
100 location
= cleanedUrl
.toString(QUrl::PreferLocalFile
);
102 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
103 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
104 m_fromHereButton
->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl
.toString(QUrl::PreferLocalFile
)));
107 QUrl
DolphinSearchBox::searchPath() const
109 return m_everywhereButton
->isChecked() ? QUrl::fromLocalFile(QDir::homePath()) : m_searchPath
;
112 QUrl
DolphinSearchBox::urlForSearching() const
116 if (isIndexingEnabled()) {
117 url
= balooUrlForSearching();
119 url
.setScheme(QStringLiteral("filenamesearch"));
122 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
123 if (m_contentButton
->isChecked()) {
124 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
127 query
.addQueryItem(QStringLiteral("url"), searchPath().url());
128 query
.addQueryItem(QStringLiteral("title"), queryTitle(m_searchInput
->text()));
136 void DolphinSearchBox::fromSearchUrl(const QUrl
&url
)
138 if (DolphinQuery::supportsScheme(url
.scheme())) {
139 const DolphinQuery query
= DolphinQuery::fromSearchUrl(url
);
140 updateFromQuery(query
);
141 } else if (url
.scheme() == QLatin1String("filenamesearch")) {
142 const QUrlQuery
query(url
);
143 setText(query
.queryItemValue(QStringLiteral("search")));
144 if (m_searchPath
.scheme() != url
.scheme()) {
145 m_searchPath
= QUrl();
147 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
148 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
151 m_searchPath
= QUrl();
155 updateFacetsVisible();
158 void DolphinSearchBox::selectAll()
160 m_searchInput
->selectAll();
163 void DolphinSearchBox::setActive(bool active
)
165 if (active
!= m_active
) {
174 bool DolphinSearchBox::isActive() const
179 bool DolphinSearchBox::event(QEvent
*event
)
181 if (event
->type() == QEvent::Polish
) {
184 return QWidget::event(event
);
187 void DolphinSearchBox::showEvent(QShowEvent
*event
)
189 if (!event
->spontaneous()) {
190 m_searchInput
->setFocus();
191 m_startedSearching
= false;
195 void DolphinSearchBox::hideEvent(QHideEvent
*event
)
198 m_startedSearching
= false;
199 if (m_startSearchTimer
) {
200 m_startSearchTimer
->stop();
204 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
*event
)
206 QWidget::keyReleaseEvent(event
);
207 if (event
->key() == Qt::Key_Escape
) {
208 if (m_searchInput
->text().isEmpty()) {
211 m_searchInput
->clear();
213 } else if (event
->key() == Qt::Key_Down
) {
214 Q_EMIT
focusViewRequest();
218 bool DolphinSearchBox::eventFilter(QObject
*obj
, QEvent
*event
)
220 switch (event
->type()) {
221 case QEvent::FocusIn
:
222 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
223 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
224 // To avoid this issue, we delay the activation of the search box.
225 // We also don't want to schedule the activation process if we are already active,
226 // otherwise we can enter in a loop of FocusIn/FocusOut events with the searchbox of another tab.
228 QTimer::singleShot(0, this, [this] {
239 return QObject::eventFilter(obj
, event
);
242 void DolphinSearchBox::emitSearchRequest()
244 m_startSearchTimer
->stop();
245 m_startedSearching
= true;
246 m_saveSearchAction
->setEnabled(true);
247 Q_EMIT
searchRequest();
250 void DolphinSearchBox::emitCloseRequest()
252 m_startSearchTimer
->stop();
253 m_startedSearching
= false;
254 m_saveSearchAction
->setEnabled(false);
255 Q_EMIT
closeRequest();
258 void DolphinSearchBox::slotConfigurationChanged()
261 if (m_startedSearching
) {
266 void DolphinSearchBox::slotSearchTextChanged(const QString
&text
)
268 if (text
.isEmpty()) {
269 // Restore URL when search box is cleared by closing and reopening the box.
271 Q_EMIT
openRequest();
273 m_startSearchTimer
->start();
275 Q_EMIT
searchTextChanged(text
);
278 void DolphinSearchBox::slotReturnPressed()
280 if (m_searchInput
->text().isEmpty()) {
285 Q_EMIT
focusViewRequest();
288 void DolphinSearchBox::slotFacetChanged()
290 m_startedSearching
= true;
291 m_startSearchTimer
->stop();
292 Q_EMIT
searchRequest();
295 void DolphinSearchBox::slotSearchSaved()
297 const QUrl searchURL
= urlForSearching();
298 if (searchURL
.isValid()) {
299 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
300 DolphinPlacesModelSingleton::instance().placesModel()->addPlace(label
, searchURL
, QStringLiteral("folder-saved-search-symbolic"));
304 void DolphinSearchBox::initButton(QToolButton
*button
)
306 button
->installEventFilter(this);
307 button
->setAutoExclusive(true);
308 button
->setAutoRaise(true);
309 button
->setCheckable(true);
310 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
313 void DolphinSearchBox::loadSettings()
315 if (SearchSettings::location() == QLatin1String("Everywhere")) {
316 m_everywhereButton
->setChecked(true);
318 m_fromHereButton
->setChecked(true);
321 if (SearchSettings::what() == QLatin1String("Content")) {
322 m_contentButton
->setChecked(true);
324 m_fileNameButton
->setChecked(true);
327 updateFacetsVisible();
330 void DolphinSearchBox::saveSettings()
332 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
333 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
334 SearchSettings::self()->save();
337 void DolphinSearchBox::init()
340 m_searchInput
= new QLineEdit(this);
341 m_searchInput
->setPlaceholderText(i18n("Search…"));
342 m_searchInput
->installEventFilter(this);
343 m_searchInput
->setClearButtonEnabled(true);
344 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
345 connect(m_searchInput
, &QLineEdit::returnPressed
, this, &DolphinSearchBox::slotReturnPressed
);
346 connect(m_searchInput
, &QLineEdit::textChanged
, this, &DolphinSearchBox::slotSearchTextChanged
);
347 setFocusProxy(m_searchInput
);
349 // Add "Save search" button inside search box
350 m_saveSearchAction
= new QAction(this);
351 m_saveSearchAction
->setIcon(QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
352 m_saveSearchAction
->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
353 m_saveSearchAction
->setEnabled(false);
354 m_searchInput
->addAction(m_saveSearchAction
, QLineEdit::TrailingPosition
);
355 connect(m_saveSearchAction
, &QAction::triggered
, this, &DolphinSearchBox::slotSearchSaved
);
357 // Create close button
358 QToolButton
*closeButton
= new QToolButton(this);
359 closeButton
->setAutoRaise(true);
360 closeButton
->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
361 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
362 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
364 // Apply layout for the search input
365 QHBoxLayout
*searchInputLayout
= new QHBoxLayout();
366 searchInputLayout
->setContentsMargins(0, 0, 0, 0);
367 searchInputLayout
->addWidget(m_searchInput
);
368 searchInputLayout
->addWidget(closeButton
);
370 // Create "Filename" and "Content" button
371 m_fileNameButton
= new QToolButton(this);
372 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
373 initButton(m_fileNameButton
);
375 m_contentButton
= new QToolButton();
376 m_contentButton
->setText(i18nc("action:button", "Content"));
377 initButton(m_contentButton
);
379 QButtonGroup
*searchWhatGroup
= new QButtonGroup(this);
380 searchWhatGroup
->addButton(m_fileNameButton
);
381 searchWhatGroup
->addButton(m_contentButton
);
383 m_separator
= new KSeparator(Qt::Vertical
, this);
385 // Create "From Here" and "Your files" buttons
386 m_fromHereButton
= new QToolButton(this);
387 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
388 initButton(m_fromHereButton
);
390 m_everywhereButton
= new QToolButton(this);
391 m_everywhereButton
->setText(i18nc("action:button", "Your files"));
392 m_everywhereButton
->setToolTip(i18nc("action:button", "Search in your home directory"));
393 m_everywhereButton
->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
394 m_everywhereButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
395 initButton(m_everywhereButton
);
397 QButtonGroup
*searchLocationGroup
= new QButtonGroup(this);
398 searchLocationGroup
->addButton(m_fromHereButton
);
399 searchLocationGroup
->addButton(m_everywhereButton
);
401 KService::Ptr kfind
= KService::serviceByDesktopName(QStringLiteral("org.kde.kfind"));
403 QToolButton
*kfindToolsButton
= nullptr;
405 kfindToolsButton
= new QToolButton(this);
406 kfindToolsButton
->setAutoRaise(true);
407 kfindToolsButton
->setPopupMode(QToolButton::InstantPopup
);
408 kfindToolsButton
->setIcon(QIcon::fromTheme("arrow-down-double"));
409 kfindToolsButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
410 kfindToolsButton
->setText(i18n("Open %1", kfind
->name()));
411 kfindToolsButton
->setIcon(QIcon::fromTheme(kfind
->icon()));
413 connect(kfindToolsButton
, &QToolButton::clicked
, this, [this, kfind
] {
414 auto *job
= new KIO::ApplicationLauncherJob(kfind
);
415 job
->setUrls({m_searchPath
});
420 // Create "Facets" widget
421 m_facetsWidget
= new DolphinFacetsWidget(this);
422 m_facetsWidget
->installEventFilter(this);
423 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
424 m_facetsWidget
->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
425 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
427 // Put the options into a QScrollArea. This prevents increasing the view width
428 // in case that not enough width for the options is available.
429 QWidget
*optionsContainer
= new QWidget(this);
431 // Apply layout for the options
432 QHBoxLayout
*optionsLayout
= new QHBoxLayout(optionsContainer
);
433 optionsLayout
->setContentsMargins(0, 0, 0, 0);
434 optionsLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
435 optionsLayout
->addWidget(m_fileNameButton
);
436 optionsLayout
->addWidget(m_contentButton
);
437 optionsLayout
->addWidget(m_separator
);
438 optionsLayout
->addWidget(m_fromHereButton
);
439 optionsLayout
->addWidget(m_everywhereButton
);
440 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
441 if (kfindToolsButton
) {
442 optionsLayout
->addWidget(kfindToolsButton
);
444 optionsLayout
->addStretch(1);
446 m_optionsScrollArea
= new QScrollArea(this);
447 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
448 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
449 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
450 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
451 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
452 m_optionsScrollArea
->setWidget(optionsContainer
);
453 m_optionsScrollArea
->setWidgetResizable(true);
455 m_topLayout
= new QVBoxLayout(this);
456 m_topLayout
->setContentsMargins(0, Dolphin::LAYOUT_SPACING_SMALL
, 0, 0);
457 m_topLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
458 m_topLayout
->addLayout(searchInputLayout
);
459 m_topLayout
->addWidget(m_optionsScrollArea
);
460 m_topLayout
->addWidget(m_facetsWidget
);
464 // The searching should be started automatically after the user did not change
465 // the text for a while
466 m_startSearchTimer
= new QTimer(this);
467 m_startSearchTimer
->setSingleShot(true);
468 m_startSearchTimer
->setInterval(500);
469 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
472 QString
DolphinSearchBox::queryTitle(const QString
&text
) const
474 return i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.", "Query Results from '%1'", text
);
477 QUrl
DolphinSearchBox::balooUrlForSearching() const
480 const QString text
= m_searchInput
->text();
483 query
.addType(m_facetsWidget
->facetType());
485 QStringList queryStrings
= m_facetsWidget
->searchTerms();
487 if (m_contentButton
->isChecked()) {
488 queryStrings
<< text
;
489 } else if (!text
.isEmpty()) {
490 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
493 if (m_fromHereButton
->isChecked()) {
494 query
.setIncludeFolder(m_searchPath
.toLocalFile());
497 query
.setSearchString(queryStrings
.join(QLatin1Char(' ')));
499 return query
.toSearchUrl(queryTitle(text
));
505 void DolphinSearchBox::updateFromQuery(const DolphinQuery
&query
)
507 // Block all signals to avoid unnecessary "searchRequest" signals
508 // while we adjust the search text and the facet widget.
511 const QString customDir
= query
.includeFolder();
512 if (!customDir
.isEmpty()) {
513 setSearchPath(QUrl::fromLocalFile(customDir
));
515 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
518 setText(query
.text());
520 if (query
.hasContentSearch()) {
521 m_contentButton
->setChecked(true);
522 } else if (query
.hasFileName()) {
523 m_fileNameButton
->setChecked(true);
526 m_facetsWidget
->resetSearchTerms();
527 m_facetsWidget
->setFacetType(query
.type());
528 const QStringList searchTerms
= query
.searchTerms();
529 for (const QString
&searchTerm
: searchTerms
) {
530 m_facetsWidget
->setSearchTerm(searchTerm
);
533 m_startSearchTimer
->stop();
537 void DolphinSearchBox::updateFacetsVisible()
539 const bool indexingEnabled
= isIndexingEnabled();
540 m_facetsWidget
->setEnabled(indexingEnabled
);
541 m_facetsWidget
->setVisible(indexingEnabled
);
544 bool DolphinSearchBox::isIndexingEnabled() const
547 const Baloo::IndexerConfig searchInfo
;
548 return searchInfo
.fileIndexingEnabled() && !searchPath().isEmpty() && searchInfo
.shouldBeIndexed(searchPath().toLocalFile());
554 #include "moc_dolphinsearchbox.cpp"