]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
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_searchLabel(nullptr),
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_facetsToggleButton(nullptr),
62 m_facetsWidget(nullptr),
64 m_startSearchTimer(nullptr)
68 DolphinSearchBox::~DolphinSearchBox()
73 void DolphinSearchBox::setText(const QString
& text
)
75 m_searchInput
->setText(text
);
78 QString
DolphinSearchBox::text() const
80 return m_searchInput
->text();
83 void DolphinSearchBox::setSearchPath(const QUrl
& url
)
87 QFontMetrics
metrics(m_fromHereButton
->font());
88 const int maxWidth
= metrics
.height() * 8;
90 QString location
= url
.fileName();
91 if (location
.isEmpty()) {
92 if (url
.isLocalFile()) {
93 location
= QStringLiteral("/");
95 location
= url
.scheme() + QLatin1String(" - ") + url
.host();
99 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
100 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
102 const bool showSearchFromButtons
= url
.isLocalFile();
103 m_separator
->setVisible(showSearchFromButtons
);
104 m_fromHereButton
->setVisible(showSearchFromButtons
);
105 m_everywhereButton
->setVisible(showSearchFromButtons
);
107 bool hasFacetsSupport
= false;
109 const Baloo::IndexerConfig searchInfo
;
110 hasFacetsSupport
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
112 m_facetsWidget
->setEnabled(hasFacetsSupport
);
115 QUrl
DolphinSearchBox::searchPath() const
120 QUrl
DolphinSearchBox::urlForSearching() const
123 bool useBalooSearch
= false;
125 const Baloo::IndexerConfig searchInfo
;
126 useBalooSearch
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
128 if (useBalooSearch
) {
129 url
= balooUrlForSearching();
131 url
.setScheme(QStringLiteral("filenamesearch"));
134 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
135 if (m_contentButton
->isChecked()) {
136 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
140 if (m_everywhereButton
->isChecked()) {
141 // It is very unlikely, that the majority of Dolphins target users
142 // mean "the whole harddisk" instead of "my home folder" when
143 // selecting the "Everywhere" button.
144 encodedUrl
= QDir::homePath();
146 encodedUrl
= m_searchPath
.url();
148 query
.addQueryItem(QStringLiteral("url"), encodedUrl
);
156 void DolphinSearchBox::fromSearchUrl(const QUrl
& url
)
158 if (url
.scheme() == QLatin1String("baloosearch")) {
159 fromBalooSearchUrl(url
);
160 } else if (url
.scheme() == QLatin1String("filenamesearch")) {
161 const QUrlQuery
query(url
);
162 setText(query
.queryItemValue(QStringLiteral("search")));
163 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
164 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
171 void DolphinSearchBox::selectAll()
173 m_searchInput
->selectAll();
176 void DolphinSearchBox::setActive(bool active
)
178 if (active
!= m_active
) {
187 bool DolphinSearchBox::isActive() const
192 bool DolphinSearchBox::event(QEvent
* event
)
194 if (event
->type() == QEvent::Polish
) {
197 return QWidget::event(event
);
200 void DolphinSearchBox::showEvent(QShowEvent
* event
)
202 if (!event
->spontaneous()) {
203 m_searchInput
->setFocus();
204 m_startedSearching
= false;
208 void DolphinSearchBox::hideEvent(QHideEvent
* event
)
211 m_startedSearching
= false;
212 m_startSearchTimer
->stop();
215 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
217 QWidget::keyReleaseEvent(event
);
218 if (event
->key() == Qt::Key_Escape
) {
219 if (m_searchInput
->text().isEmpty()) {
222 m_searchInput
->clear();
227 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
229 switch (event
->type()) {
230 case QEvent::FocusIn
:
231 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
232 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
233 // To avoid this issue, we delay the activation of the search box.
234 // We also don't want to schedule the activation process if we are already active,
235 // otherwise we can enter in a loop of FocusIn/FocusOut events with the searchbox of another tab.
237 QTimer::singleShot(0, this, [this] {
248 return QObject::eventFilter(obj
, event
);
251 void DolphinSearchBox::emitSearchRequest()
253 m_startSearchTimer
->stop();
254 m_startedSearching
= true;
255 m_saveSearchAction
->setEnabled(true);
256 emit
searchRequest();
259 void DolphinSearchBox::emitCloseRequest()
261 m_startSearchTimer
->stop();
262 m_startedSearching
= false;
263 m_saveSearchAction
->setEnabled(false);
267 void DolphinSearchBox::slotConfigurationChanged()
270 if (m_startedSearching
) {
275 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
278 if (text
.isEmpty()) {
279 m_startSearchTimer
->stop();
281 m_startSearchTimer
->start();
283 emit
searchTextChanged(text
);
286 void DolphinSearchBox::slotReturnPressed()
289 emit
returnPressed();
292 void DolphinSearchBox::slotFacetsButtonToggled()
294 const bool facetsIsVisible
= !m_facetsWidget
->isVisible();
295 m_facetsWidget
->setVisible(facetsIsVisible
);
296 updateFacetsToggleButton();
299 void DolphinSearchBox::slotFacetChanged()
301 m_startedSearching
= true;
302 m_startSearchTimer
->stop();
303 emit
searchRequest();
306 void DolphinSearchBox::slotSearchSaved()
308 const QUrl searchURL
= urlForSearching();
309 if (searchURL
.isValid()) {
310 PlacesItemModel model
;
311 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
312 model
.createPlacesItem(label
,
314 QStringLiteral("folder-saved-search-symbolic"));
318 void DolphinSearchBox::initButton(QToolButton
* button
)
320 button
->installEventFilter(this);
321 button
->setAutoExclusive(true);
322 button
->setAutoRaise(true);
323 button
->setCheckable(true);
324 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
327 void DolphinSearchBox::loadSettings()
329 if (SearchSettings::location() == QLatin1String("Everywhere")) {
330 m_everywhereButton
->setChecked(true);
332 m_fromHereButton
->setChecked(true);
335 if (SearchSettings::what() == QLatin1String("Content")) {
336 m_contentButton
->setChecked(true);
338 m_fileNameButton
->setChecked(true);
341 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
344 void DolphinSearchBox::saveSettings()
346 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
347 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
348 SearchSettings::setShowFacetsWidget(m_facetsToggleButton
->isChecked());
349 SearchSettings::self()->save();
352 void DolphinSearchBox::init()
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 // Create search label
362 m_searchLabel
= new QLabel(this);
365 m_searchInput
= new QLineEdit(this);
366 m_searchInput
->installEventFilter(this);
367 m_searchInput
->setClearButtonEnabled(true);
368 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
369 connect(m_searchInput
, &QLineEdit::returnPressed
,
370 this, &DolphinSearchBox::slotReturnPressed
);
371 connect(m_searchInput
, &QLineEdit::textChanged
,
372 this, &DolphinSearchBox::slotSearchTextChanged
);
373 setFocusProxy(m_searchInput
);
375 // Add "Save search" button inside search box
376 m_saveSearchAction
= new QAction(this);
377 m_saveSearchAction
->setIcon (QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
378 m_saveSearchAction
->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
379 m_saveSearchAction
->setEnabled(false);
380 m_searchInput
->addAction(m_saveSearchAction
, QLineEdit::TrailingPosition
);
381 connect(m_saveSearchAction
, &QAction::triggered
, this, &DolphinSearchBox::slotSearchSaved
);
383 // Apply layout for the search input
384 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
385 searchInputLayout
->setContentsMargins(0, 0, 0, 0);
386 searchInputLayout
->addWidget(closeButton
);
387 searchInputLayout
->addWidget(m_searchLabel
);
388 searchInputLayout
->addWidget(m_searchInput
);
390 // Create "Filename" and "Content" button
391 m_fileNameButton
= new QToolButton(this);
392 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
393 initButton(m_fileNameButton
);
395 m_contentButton
= new QToolButton();
396 m_contentButton
->setText(i18nc("action:button", "Content"));
397 initButton(m_contentButton
);
399 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
400 searchWhatGroup
->addButton(m_fileNameButton
);
401 searchWhatGroup
->addButton(m_contentButton
);
403 m_separator
= new KSeparator(Qt::Vertical
, this);
405 // Create "From Here" and "Everywhere"button
406 m_fromHereButton
= new QToolButton(this);
407 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
408 initButton(m_fromHereButton
);
410 m_everywhereButton
= new QToolButton(this);
411 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
412 initButton(m_everywhereButton
);
414 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
415 searchLocationGroup
->addButton(m_fromHereButton
);
416 searchLocationGroup
->addButton(m_everywhereButton
);
418 auto moreSearchToolsButton
= new QToolButton(this);
419 moreSearchToolsButton
->setAutoRaise(true);
420 moreSearchToolsButton
->setPopupMode(QToolButton::InstantPopup
);
421 moreSearchToolsButton
->setIcon(QIcon::fromTheme("arrow-down-double"));
422 moreSearchToolsButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
423 moreSearchToolsButton
->setText(i18n("More Search Tools"));
424 moreSearchToolsButton
->setMenu(new QMenu(this));
425 connect(moreSearchToolsButton
->menu(), &QMenu::aboutToShow
, moreSearchToolsButton
->menu(), [this, moreSearchToolsButton
]()
427 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
428 moreSearchToolsButton
->menu()->clear();
429 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), { "files-find" }, this->m_searchPath
);
432 // Create "Facets" widgets
433 m_facetsToggleButton
= new QToolButton(this);
434 m_facetsToggleButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
435 initButton(m_facetsToggleButton
);
436 connect(m_facetsToggleButton
, &QToolButton::clicked
, this, &DolphinSearchBox::slotFacetsButtonToggled
);
438 m_facetsWidget
= new DolphinFacetsWidget(this);
439 m_facetsWidget
->installEventFilter(this);
440 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
441 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
443 // Apply layout for the options
444 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
445 optionsLayout
->setContentsMargins(0, 0, 0, 0);
446 optionsLayout
->addWidget(m_fileNameButton
);
447 optionsLayout
->addWidget(m_contentButton
);
448 optionsLayout
->addWidget(m_separator
);
449 optionsLayout
->addWidget(m_fromHereButton
);
450 optionsLayout
->addWidget(m_everywhereButton
);
451 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
452 optionsLayout
->addWidget(m_facetsToggleButton
);
453 optionsLayout
->addWidget(moreSearchToolsButton
);
454 optionsLayout
->addStretch(1);
456 // Put the options into a QScrollArea. This prevents increasing the view width
457 // in case that not enough width for the options is available.
458 QWidget
* optionsContainer
= new QWidget(this);
459 optionsContainer
->setLayout(optionsLayout
);
461 m_optionsScrollArea
= new QScrollArea(this);
462 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
463 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
464 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
465 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
466 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
467 m_optionsScrollArea
->setWidget(optionsContainer
);
468 m_optionsScrollArea
->setWidgetResizable(true);
470 m_topLayout
= new QVBoxLayout(this);
471 m_topLayout
->setContentsMargins(0, 0, 0, 0);
472 m_topLayout
->addLayout(searchInputLayout
);
473 m_topLayout
->addWidget(m_optionsScrollArea
);
474 m_topLayout
->addWidget(m_facetsWidget
);
478 // The searching should be started automatically after the user did not change
479 // the text within one second
480 m_startSearchTimer
= new QTimer(this);
481 m_startSearchTimer
->setSingleShot(true);
482 m_startSearchTimer
->setInterval(1000);
483 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
485 updateFacetsToggleButton();
488 QUrl
DolphinSearchBox::balooUrlForSearching() const
491 const QString text
= m_searchInput
->text();
494 query
.addType(m_facetsWidget
->facetType());
496 QStringList queryStrings
;
497 QString ratingQuery
= m_facetsWidget
->ratingTerm();
498 if (!ratingQuery
.isEmpty()) {
499 queryStrings
<< ratingQuery
;
502 if (m_contentButton
->isChecked()) {
503 queryStrings
<< text
;
504 } else if (!text
.isEmpty()) {
505 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
508 if (m_fromHereButton
->isChecked()) {
509 query
.setIncludeFolder(m_searchPath
.toLocalFile());
512 query
.setSearchString(queryStrings
.join(QStringLiteral(" ")));
514 return query
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
515 "Query Results from '%1'", text
));
521 void DolphinSearchBox::fromBalooSearchUrl(const QUrl
& url
)
524 const Baloo::Query query
= Baloo::Query::fromSearchUrl(url
);
526 // Block all signals to avoid unnecessary "searchRequest" signals
527 // while we adjust the search text and the facet widget.
530 const QString customDir
= query
.includeFolder();
531 if (!customDir
.isEmpty()) {
532 setSearchPath(QUrl::fromLocalFile(customDir
));
534 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
537 setText(query
.searchString());
539 QStringList types
= query
.types();
540 if (!types
.isEmpty()) {
541 m_facetsWidget
->setFacetType(types
.first());
544 const QStringList subTerms
= query
.searchString().split(' ', QString::SkipEmptyParts
);
545 foreach (const QString
& subTerm
, subTerms
) {
546 if (subTerm
.startsWith(QLatin1String("filename:"))) {
547 const QString value
= subTerm
.mid(9);
549 } else if (m_facetsWidget
->isRatingTerm(subTerm
)) {
550 m_facetsWidget
->setRatingTerm(subTerm
);
554 m_startSearchTimer
->stop();
561 void DolphinSearchBox::updateFacetsToggleButton()
563 const bool facetsIsVisible
= SearchSettings::showFacetsWidget();
564 m_facetsToggleButton
->setChecked(facetsIsVisible
? true : false);
565 m_facetsToggleButton
->setIcon(QIcon::fromTheme(facetsIsVisible
? QStringLiteral("arrow-up-double") : QStringLiteral("arrow-down-double")));
566 m_facetsToggleButton
->setText(facetsIsVisible
? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));