]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
751b23942fc9397dafeaf4e4b9211a5cde7ca0b4
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 * **************************************************************************/
21 #include "dolphinsearchbox.h"
23 #include "dolphin_searchsettings.h"
24 #include "dolphinfacetswidget.h"
25 #include "panels/places/placesitemmodel.h"
27 #include <KLocalizedString>
28 #include <KNS3/KMoreToolsMenuFactory>
30 #include <config-baloo.h>
32 #include <Baloo/Query>
33 #include <Baloo/IndexerConfig>
36 #include <QButtonGroup>
38 #include <QFontDatabase>
39 #include <QHBoxLayout>
43 #include <QScrollArea>
45 #include <QToolButton>
48 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
50 m_startedSearching(false),
53 m_searchInput(nullptr),
54 m_saveSearchAction(nullptr),
55 m_optionsScrollArea(nullptr),
56 m_fileNameButton(nullptr),
57 m_contentButton(nullptr),
59 m_fromHereButton(nullptr),
60 m_everywhereButton(nullptr),
61 m_facetsWidget(nullptr),
63 m_startSearchTimer(nullptr)
67 DolphinSearchBox::~DolphinSearchBox()
72 void DolphinSearchBox::setText(const QString
& text
)
74 m_searchInput
->setText(text
);
77 QString
DolphinSearchBox::text() const
79 return m_searchInput
->text();
82 void DolphinSearchBox::setSearchPath(const QUrl
& url
)
84 if (url
== m_searchPath
) {
88 const QUrl cleanedUrl
= url
.adjusted(QUrl::RemoveUserInfo
| QUrl::StripTrailingSlash
);
90 if (cleanedUrl
.path() == QDir::homePath()) {
91 m_fromHereButton
->setChecked(false);
92 m_everywhereButton
->setChecked(true);
93 if (!m_searchPath
.isEmpty()) {
97 m_everywhereButton
->setChecked(false);
98 m_fromHereButton
->setChecked(true);
103 QFontMetrics
metrics(m_fromHereButton
->font());
104 const int maxWidth
= metrics
.height() * 8;
106 QString location
= cleanedUrl
.fileName();
107 if (location
.isEmpty()) {
108 location
= cleanedUrl
.toString(QUrl::PreferLocalFile
);
110 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
111 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
112 m_fromHereButton
->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl
.toString(QUrl::PreferLocalFile
)));
115 QUrl
DolphinSearchBox::searchPath() const
117 return m_everywhereButton
->isChecked() ? QUrl::fromLocalFile(QDir::homePath()) : m_searchPath
;
120 QUrl
DolphinSearchBox::urlForSearching() const
124 if (isIndexingEnabled()) {
125 url
= balooUrlForSearching();
127 url
.setScheme(QStringLiteral("filenamesearch"));
130 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
131 if (m_contentButton
->isChecked()) {
132 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
135 query
.addQueryItem(QStringLiteral("url"), searchPath().url());
143 void DolphinSearchBox::fromSearchUrl(const QUrl
& url
)
145 if (url
.scheme() == QLatin1String("baloosearch")) {
146 fromBalooSearchUrl(url
);
147 } else if (url
.scheme() == QLatin1String("filenamesearch")) {
148 const QUrlQuery
query(url
);
149 setText(query
.queryItemValue(QStringLiteral("search")));
150 if (m_searchPath
.scheme() != url
.scheme()) {
151 m_searchPath
= QUrl();
153 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
154 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
157 m_searchPath
= QUrl();
161 updateFacetsVisible();
164 void DolphinSearchBox::selectAll()
166 m_searchInput
->selectAll();
169 void DolphinSearchBox::setActive(bool active
)
171 if (active
!= m_active
) {
180 bool DolphinSearchBox::isActive() const
185 bool DolphinSearchBox::event(QEvent
* event
)
187 if (event
->type() == QEvent::Polish
) {
190 return QWidget::event(event
);
193 void DolphinSearchBox::showEvent(QShowEvent
* event
)
195 if (!event
->spontaneous()) {
196 m_searchInput
->setFocus();
197 m_startedSearching
= false;
201 void DolphinSearchBox::hideEvent(QHideEvent
* event
)
204 m_startedSearching
= false;
205 m_startSearchTimer
->stop();
208 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
210 QWidget::keyReleaseEvent(event
);
211 if (event
->key() == Qt::Key_Escape
) {
212 if (m_searchInput
->text().isEmpty()) {
215 m_searchInput
->clear();
220 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
222 switch (event
->type()) {
223 case QEvent::FocusIn
:
224 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
225 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
226 // To avoid this issue, we delay the activation of the search box.
227 // We also don't want to schedule the activation process if we are already active,
228 // otherwise we can enter in a loop of FocusIn/FocusOut events with the searchbox of another tab.
230 QTimer::singleShot(0, this, [this] {
241 return QObject::eventFilter(obj
, event
);
244 void DolphinSearchBox::emitSearchRequest()
246 m_startSearchTimer
->stop();
247 m_startedSearching
= true;
248 m_saveSearchAction
->setEnabled(true);
249 emit
searchRequest();
252 void DolphinSearchBox::emitCloseRequest()
254 m_startSearchTimer
->stop();
255 m_startedSearching
= false;
256 m_saveSearchAction
->setEnabled(false);
260 void DolphinSearchBox::slotConfigurationChanged()
263 if (m_startedSearching
) {
268 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
271 if (text
.isEmpty()) {
272 m_startSearchTimer
->stop();
274 m_startSearchTimer
->start();
276 emit
searchTextChanged(text
);
279 void DolphinSearchBox::slotReturnPressed()
282 emit
returnPressed();
285 void DolphinSearchBox::slotFacetChanged()
287 m_startedSearching
= true;
288 m_startSearchTimer
->stop();
289 emit
searchRequest();
292 void DolphinSearchBox::slotSearchSaved()
294 const QUrl searchURL
= urlForSearching();
295 if (searchURL
.isValid()) {
296 PlacesItemModel model
;
297 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
298 model
.createPlacesItem(label
,
300 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()
339 // Create close button
340 QToolButton
* closeButton
= new QToolButton(this);
341 closeButton
->setAutoRaise(true);
342 closeButton
->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
343 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
344 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
347 m_searchInput
= new QLineEdit(this);
348 m_searchInput
->setPlaceholderText(i18n("Search..."));
349 m_searchInput
->installEventFilter(this);
350 m_searchInput
->setClearButtonEnabled(true);
351 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
352 connect(m_searchInput
, &QLineEdit::returnPressed
,
353 this, &DolphinSearchBox::slotReturnPressed
);
354 connect(m_searchInput
, &QLineEdit::textChanged
,
355 this, &DolphinSearchBox::slotSearchTextChanged
);
356 setFocusProxy(m_searchInput
);
358 // Add "Save search" button inside search box
359 m_saveSearchAction
= new QAction(this);
360 m_saveSearchAction
->setIcon (QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
361 m_saveSearchAction
->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
362 m_saveSearchAction
->setEnabled(false);
363 m_searchInput
->addAction(m_saveSearchAction
, QLineEdit::TrailingPosition
);
364 connect(m_saveSearchAction
, &QAction::triggered
, this, &DolphinSearchBox::slotSearchSaved
);
366 // Apply layout for the search input
367 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
368 searchInputLayout
->setContentsMargins(0, 0, 0, 0);
369 searchInputLayout
->addWidget(closeButton
);
370 searchInputLayout
->addWidget(m_searchInput
);
372 // Create "Filename" and "Content" button
373 m_fileNameButton
= new QToolButton(this);
374 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
375 initButton(m_fileNameButton
);
377 m_contentButton
= new QToolButton();
378 m_contentButton
->setText(i18nc("action:button", "Content"));
379 initButton(m_contentButton
);
381 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
382 searchWhatGroup
->addButton(m_fileNameButton
);
383 searchWhatGroup
->addButton(m_contentButton
);
385 m_separator
= new KSeparator(Qt::Vertical
, this);
387 // Create "From Here" and "Your files" buttons
388 m_fromHereButton
= new QToolButton(this);
389 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
390 initButton(m_fromHereButton
);
392 m_everywhereButton
= new QToolButton(this);
393 m_everywhereButton
->setText(i18nc("action:button", "Your files"));
394 m_everywhereButton
->setToolTip(i18nc("action:button", "Search in your home directory"));
395 m_everywhereButton
->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
396 m_everywhereButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
397 initButton(m_everywhereButton
);
399 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
400 searchLocationGroup
->addButton(m_fromHereButton
);
401 searchLocationGroup
->addButton(m_everywhereButton
);
403 auto moreSearchToolsButton
= new QToolButton(this);
404 moreSearchToolsButton
->setAutoRaise(true);
405 moreSearchToolsButton
->setPopupMode(QToolButton::InstantPopup
);
406 moreSearchToolsButton
->setIcon(QIcon::fromTheme("arrow-down-double"));
407 moreSearchToolsButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
408 moreSearchToolsButton
->setText(i18n("More Search Tools"));
409 moreSearchToolsButton
->setMenu(new QMenu(this));
410 connect(moreSearchToolsButton
->menu(), &QMenu::aboutToShow
, moreSearchToolsButton
->menu(), [this, moreSearchToolsButton
]()
412 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
413 moreSearchToolsButton
->menu()->clear();
414 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), { "files-find" }, this->m_searchPath
);
417 // Create "Facets" widget
418 m_facetsWidget
= new DolphinFacetsWidget(this);
419 m_facetsWidget
->installEventFilter(this);
420 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
421 m_facetsWidget
->layout()->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
422 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
424 // Apply layout for the options
425 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
426 optionsLayout
->setContentsMargins(0, 0, 0, 0);
427 optionsLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
428 optionsLayout
->addWidget(m_fileNameButton
);
429 optionsLayout
->addWidget(m_contentButton
);
430 optionsLayout
->addWidget(m_separator
);
431 optionsLayout
->addWidget(m_fromHereButton
);
432 optionsLayout
->addWidget(m_everywhereButton
);
433 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
434 optionsLayout
->addWidget(moreSearchToolsButton
);
435 optionsLayout
->addStretch(1);
437 // Put the options into a QScrollArea. This prevents increasing the view width
438 // in case that not enough width for the options is available.
439 QWidget
* optionsContainer
= new QWidget(this);
440 optionsContainer
->setLayout(optionsLayout
);
442 m_optionsScrollArea
= new QScrollArea(this);
443 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
444 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
445 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
446 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
447 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
448 m_optionsScrollArea
->setWidget(optionsContainer
);
449 m_optionsScrollArea
->setWidgetResizable(true);
451 m_topLayout
= new QVBoxLayout(this);
452 m_topLayout
->setContentsMargins(0, 0, 0, 0);
453 m_topLayout
->setSpacing(Dolphin::LAYOUT_SPACING_SMALL
);
454 m_topLayout
->addLayout(searchInputLayout
);
455 m_topLayout
->addWidget(m_optionsScrollArea
);
456 m_topLayout
->addWidget(m_facetsWidget
);
460 // The searching should be started automatically after the user did not change
461 // the text within one second
462 m_startSearchTimer
= new QTimer(this);
463 m_startSearchTimer
->setSingleShot(true);
464 m_startSearchTimer
->setInterval(1000);
465 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
468 QUrl
DolphinSearchBox::balooUrlForSearching() const
471 const QString text
= m_searchInput
->text();
474 query
.addType(m_facetsWidget
->facetType());
476 QStringList queryStrings
;
477 QString ratingQuery
= m_facetsWidget
->ratingTerm();
478 if (!ratingQuery
.isEmpty()) {
479 queryStrings
<< ratingQuery
;
482 if (m_contentButton
->isChecked()) {
483 queryStrings
<< text
;
484 } else if (!text
.isEmpty()) {
485 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
488 if (m_fromHereButton
->isChecked()) {
489 query
.setIncludeFolder(m_searchPath
.toLocalFile());
492 query
.setSearchString(queryStrings
.join(QLatin1Char(' ')));
494 return query
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
495 "Query Results from '%1'", text
));
501 void DolphinSearchBox::fromBalooSearchUrl(const QUrl
& url
)
504 const Baloo::Query query
= Baloo::Query::fromSearchUrl(url
);
506 // Block all signals to avoid unnecessary "searchRequest" signals
507 // while we adjust the search text and the facet widget.
510 const QString customDir
= query
.includeFolder();
511 if (!customDir
.isEmpty()) {
512 setSearchPath(QUrl::fromLocalFile(customDir
));
514 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
517 m_facetsWidget
->resetOptions();
519 setText(query
.searchString());
521 QStringList types
= query
.types();
522 if (!types
.isEmpty()) {
523 m_facetsWidget
->setFacetType(types
.first());
526 const QStringList subTerms
= query
.searchString().split(' ', QString::SkipEmptyParts
);
527 foreach (const QString
& subTerm
, subTerms
) {
528 if (subTerm
.startsWith(QLatin1String("filename:"))) {
529 const QString value
= subTerm
.mid(9);
531 } else if (m_facetsWidget
->isRatingTerm(subTerm
)) {
532 m_facetsWidget
->setRatingTerm(subTerm
);
536 m_startSearchTimer
->stop();
543 void DolphinSearchBox::updateFacetsVisible()
545 const bool indexingEnabled
= isIndexingEnabled();
546 m_facetsWidget
->setEnabled(indexingEnabled
);
547 m_facetsWidget
->setVisible(indexingEnabled
);
550 bool DolphinSearchBox::isIndexingEnabled() const
553 const Baloo::IndexerConfig searchInfo
;
554 return searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(searchPath().toLocalFile());