]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
1c1accd26961f00b7876a4e2d25933024635ae1d
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 * **************************************************************************/
20 #include "dolphinsearchbox.h"
22 #include "dolphin_searchsettings.h"
23 #include "dolphinfacetswidget.h"
24 #include "panels/places/placesitemmodel.h"
26 #include <KLocalizedString>
27 #include <KNS3/KMoreToolsMenuFactory>
29 #include <config-baloo.h>
31 #include <Baloo/Query>
32 #include <Baloo/IndexerConfig>
35 #include <QButtonGroup>
37 #include <QFontDatabase>
38 #include <QHBoxLayout>
42 #include <QScrollArea>
44 #include <QToolButton>
47 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
49 m_startedSearching(false),
52 m_searchInput(nullptr),
53 m_saveSearchAction(nullptr),
54 m_optionsScrollArea(nullptr),
55 m_fileNameButton(nullptr),
56 m_contentButton(nullptr),
58 m_fromHereButton(nullptr),
59 m_everywhereButton(nullptr),
60 m_facetsToggleButton(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
) {
90 QFontMetrics
metrics(m_fromHereButton
->font());
91 const int maxWidth
= metrics
.height() * 8;
93 const QUrl cleanedUrl
= url
.adjusted(QUrl::RemoveUserInfo
| QUrl::StripTrailingSlash
);
94 QString location
= cleanedUrl
.fileName();
95 if (location
.isEmpty()) {
96 location
= cleanedUrl
.toString(QUrl::PreferLocalFile
);
98 if (m_fromHereButton
->isChecked() && cleanedUrl
.path() == QDir::homePath()) {
99 m_fromHereButton
->setChecked(false);
100 m_everywhereButton
->setChecked(true);
102 m_fromHereButton
->setChecked(true);
103 m_everywhereButton
->setChecked(false);
106 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
107 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
108 m_fromHereButton
->setToolTip(i18nc("action:button", "Limit search to '%1' and its subfolders", cleanedUrl
.toString(QUrl::PreferLocalFile
)));
110 m_facetsWidget
->setEnabled(isIndexingEnabled());
113 QUrl
DolphinSearchBox::searchPath() const
118 QUrl
DolphinSearchBox::urlForSearching() const
122 if (isIndexingEnabled()) {
123 url
= balooUrlForSearching();
125 url
.setScheme(QStringLiteral("filenamesearch"));
128 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
129 if (m_contentButton
->isChecked()) {
130 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
134 if (m_everywhereButton
->isChecked()) {
135 encodedUrl
= QDir::homePath();
137 encodedUrl
= m_searchPath
.url();
139 query
.addQueryItem(QStringLiteral("url"), encodedUrl
);
147 void DolphinSearchBox::fromSearchUrl(const QUrl
& url
)
149 if (url
.scheme() == QLatin1String("baloosearch")) {
150 fromBalooSearchUrl(url
);
151 } else if (url
.scheme() == QLatin1String("filenamesearch")) {
152 const QUrlQuery
query(url
);
153 setText(query
.queryItemValue(QStringLiteral("search")));
154 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
155 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
162 void DolphinSearchBox::selectAll()
164 m_searchInput
->selectAll();
167 void DolphinSearchBox::setActive(bool active
)
169 if (active
!= m_active
) {
178 bool DolphinSearchBox::isActive() const
183 bool DolphinSearchBox::event(QEvent
* event
)
185 if (event
->type() == QEvent::Polish
) {
188 return QWidget::event(event
);
191 void DolphinSearchBox::showEvent(QShowEvent
* event
)
193 if (!event
->spontaneous()) {
194 m_searchInput
->setFocus();
195 m_startedSearching
= false;
199 void DolphinSearchBox::hideEvent(QHideEvent
* event
)
202 m_startedSearching
= false;
203 m_startSearchTimer
->stop();
206 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
208 QWidget::keyReleaseEvent(event
);
209 if (event
->key() == Qt::Key_Escape
) {
210 if (m_searchInput
->text().isEmpty()) {
213 m_searchInput
->clear();
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 emit
searchRequest();
250 void DolphinSearchBox::emitCloseRequest()
252 m_startSearchTimer
->stop();
253 m_startedSearching
= false;
254 m_saveSearchAction
->setEnabled(false);
258 void DolphinSearchBox::slotConfigurationChanged()
261 if (m_startedSearching
) {
266 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
269 if (text
.isEmpty()) {
270 m_startSearchTimer
->stop();
272 m_startSearchTimer
->start();
274 emit
searchTextChanged(text
);
277 void DolphinSearchBox::slotReturnPressed()
280 emit
returnPressed();
283 void DolphinSearchBox::slotFacetsButtonToggled()
285 const bool facetsIsVisible
= !m_facetsWidget
->isVisible();
286 m_facetsWidget
->setVisible(facetsIsVisible
);
287 updateFacetsToggleButton();
290 void DolphinSearchBox::slotFacetChanged()
292 m_startedSearching
= true;
293 m_startSearchTimer
->stop();
294 emit
searchRequest();
297 void DolphinSearchBox::slotSearchSaved()
299 const QUrl searchURL
= urlForSearching();
300 if (searchURL
.isValid()) {
301 PlacesItemModel model
;
302 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
303 model
.createPlacesItem(label
,
305 QStringLiteral("folder-saved-search-symbolic"));
309 void DolphinSearchBox::initButton(QToolButton
* button
)
311 button
->installEventFilter(this);
312 button
->setAutoExclusive(true);
313 button
->setAutoRaise(true);
314 button
->setCheckable(true);
315 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
318 void DolphinSearchBox::loadSettings()
320 if (SearchSettings::location() == QLatin1String("Everywhere")) {
321 m_everywhereButton
->setChecked(true);
323 m_fromHereButton
->setChecked(true);
326 if (SearchSettings::what() == QLatin1String("Content")) {
327 m_contentButton
->setChecked(true);
329 m_fileNameButton
->setChecked(true);
332 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
335 void DolphinSearchBox::saveSettings()
337 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
338 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
339 SearchSettings::setShowFacetsWidget(m_facetsToggleButton
->isChecked());
340 SearchSettings::self()->save();
343 void DolphinSearchBox::init()
345 // Create close button
346 QToolButton
* closeButton
= new QToolButton(this);
347 closeButton
->setAutoRaise(true);
348 closeButton
->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
349 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
350 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
353 m_searchInput
= new QLineEdit(this);
354 m_searchInput
->setPlaceholderText(i18n("Search..."));
355 m_searchInput
->installEventFilter(this);
356 m_searchInput
->setClearButtonEnabled(true);
357 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
358 connect(m_searchInput
, &QLineEdit::returnPressed
,
359 this, &DolphinSearchBox::slotReturnPressed
);
360 connect(m_searchInput
, &QLineEdit::textChanged
,
361 this, &DolphinSearchBox::slotSearchTextChanged
);
362 setFocusProxy(m_searchInput
);
364 // Add "Save search" button inside search box
365 m_saveSearchAction
= new QAction(this);
366 m_saveSearchAction
->setIcon (QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
367 m_saveSearchAction
->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
368 m_saveSearchAction
->setEnabled(false);
369 m_searchInput
->addAction(m_saveSearchAction
, QLineEdit::TrailingPosition
);
370 connect(m_saveSearchAction
, &QAction::triggered
, this, &DolphinSearchBox::slotSearchSaved
);
372 // Apply layout for the search input
373 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
374 searchInputLayout
->setContentsMargins(0, 0, 0, 0);
375 searchInputLayout
->addWidget(closeButton
);
376 searchInputLayout
->addWidget(m_searchInput
);
378 // Create "Filename" and "Content" button
379 m_fileNameButton
= new QToolButton(this);
380 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
381 initButton(m_fileNameButton
);
383 m_contentButton
= new QToolButton();
384 m_contentButton
->setText(i18nc("action:button", "Content"));
385 initButton(m_contentButton
);
387 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
388 searchWhatGroup
->addButton(m_fileNameButton
);
389 searchWhatGroup
->addButton(m_contentButton
);
391 m_separator
= new KSeparator(Qt::Vertical
, this);
393 // Create "From Here" and "Your files" buttons
394 m_fromHereButton
= new QToolButton(this);
395 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
396 initButton(m_fromHereButton
);
398 m_everywhereButton
= new QToolButton(this);
399 m_everywhereButton
->setText(i18nc("action:button", "Your files"));
400 m_everywhereButton
->setToolTip(i18nc("action:button", "Search in your home directory"));
401 m_everywhereButton
->setIcon(QIcon::fromTheme(QStringLiteral("user-home")));
402 m_everywhereButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
403 initButton(m_everywhereButton
);
405 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
406 searchLocationGroup
->addButton(m_fromHereButton
);
407 searchLocationGroup
->addButton(m_everywhereButton
);
409 auto moreSearchToolsButton
= new QToolButton(this);
410 moreSearchToolsButton
->setAutoRaise(true);
411 moreSearchToolsButton
->setPopupMode(QToolButton::InstantPopup
);
412 moreSearchToolsButton
->setIcon(QIcon::fromTheme("arrow-down-double"));
413 moreSearchToolsButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
414 moreSearchToolsButton
->setText(i18n("More Search Tools"));
415 moreSearchToolsButton
->setMenu(new QMenu(this));
416 connect(moreSearchToolsButton
->menu(), &QMenu::aboutToShow
, moreSearchToolsButton
->menu(), [this, moreSearchToolsButton
]()
418 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
419 moreSearchToolsButton
->menu()->clear();
420 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), { "files-find" }, this->m_searchPath
);
423 // Create "Facets" widgets
424 m_facetsToggleButton
= new QToolButton(this);
425 m_facetsToggleButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
426 initButton(m_facetsToggleButton
);
427 connect(m_facetsToggleButton
, &QToolButton::clicked
, this, &DolphinSearchBox::slotFacetsButtonToggled
);
429 m_facetsWidget
= new DolphinFacetsWidget(this);
430 m_facetsWidget
->installEventFilter(this);
431 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
432 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
434 // Apply layout for the options
435 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
436 optionsLayout
->setContentsMargins(0, 0, 0, 0);
437 optionsLayout
->addWidget(m_fileNameButton
);
438 optionsLayout
->addWidget(m_contentButton
);
439 optionsLayout
->addWidget(m_separator
);
440 optionsLayout
->addWidget(m_fromHereButton
);
441 optionsLayout
->addWidget(m_everywhereButton
);
442 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
443 optionsLayout
->addWidget(m_facetsToggleButton
);
444 optionsLayout
->addWidget(moreSearchToolsButton
);
445 optionsLayout
->addStretch(1);
447 // Put the options into a QScrollArea. This prevents increasing the view width
448 // in case that not enough width for the options is available.
449 QWidget
* optionsContainer
= new QWidget(this);
450 optionsContainer
->setLayout(optionsLayout
);
452 m_optionsScrollArea
= new QScrollArea(this);
453 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
454 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
455 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
456 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
457 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
458 m_optionsScrollArea
->setWidget(optionsContainer
);
459 m_optionsScrollArea
->setWidgetResizable(true);
461 m_topLayout
= new QVBoxLayout(this);
462 m_topLayout
->setContentsMargins(0, 0, 0, 0);
463 m_topLayout
->addLayout(searchInputLayout
);
464 m_topLayout
->addWidget(m_optionsScrollArea
);
465 m_topLayout
->addWidget(m_facetsWidget
);
469 // The searching should be started automatically after the user did not change
470 // the text within one second
471 m_startSearchTimer
= new QTimer(this);
472 m_startSearchTimer
->setSingleShot(true);
473 m_startSearchTimer
->setInterval(1000);
474 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
476 updateFacetsToggleButton();
479 QUrl
DolphinSearchBox::balooUrlForSearching() const
482 const QString text
= m_searchInput
->text();
485 query
.addType(m_facetsWidget
->facetType());
487 QStringList queryStrings
;
488 QString ratingQuery
= m_facetsWidget
->ratingTerm();
489 if (!ratingQuery
.isEmpty()) {
490 queryStrings
<< ratingQuery
;
493 if (m_contentButton
->isChecked()) {
494 queryStrings
<< text
;
495 } else if (!text
.isEmpty()) {
496 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
499 if (m_fromHereButton
->isChecked()) {
500 query
.setIncludeFolder(m_searchPath
.toLocalFile());
503 query
.setSearchString(queryStrings
.join(QLatin1Char(' ')));
505 return query
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
506 "Query Results from '%1'", text
));
512 void DolphinSearchBox::fromBalooSearchUrl(const QUrl
& url
)
515 const Baloo::Query query
= Baloo::Query::fromSearchUrl(url
);
517 // Block all signals to avoid unnecessary "searchRequest" signals
518 // while we adjust the search text and the facet widget.
521 const QString customDir
= query
.includeFolder();
522 if (!customDir
.isEmpty()) {
523 setSearchPath(QUrl::fromLocalFile(customDir
));
525 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
528 setText(query
.searchString());
530 QStringList types
= query
.types();
531 if (!types
.isEmpty()) {
532 m_facetsWidget
->setFacetType(types
.first());
535 const QStringList subTerms
= query
.searchString().split(' ', QString::SkipEmptyParts
);
536 foreach (const QString
& subTerm
, subTerms
) {
537 if (subTerm
.startsWith(QLatin1String("filename:"))) {
538 const QString value
= subTerm
.mid(9);
540 } else if (m_facetsWidget
->isRatingTerm(subTerm
)) {
541 m_facetsWidget
->setRatingTerm(subTerm
);
545 m_startSearchTimer
->stop();
552 void DolphinSearchBox::updateFacetsToggleButton()
554 const bool facetsIsVisible
= SearchSettings::showFacetsWidget();
555 m_facetsToggleButton
->setChecked(facetsIsVisible
? true : false);
556 m_facetsToggleButton
->setIcon(QIcon::fromTheme(facetsIsVisible
? QStringLiteral("arrow-up-double") : QStringLiteral("arrow-down-double")));
557 m_facetsToggleButton
->setText(facetsIsVisible
? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));
560 bool DolphinSearchBox::isIndexingEnabled() const
563 const Baloo::IndexerConfig searchInfo
;
564 return searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());