]>
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"
25 #include <panels/places/placesitemmodel.h>
29 #include <KLocalizedString>
31 #include <KNS3/KMoreToolsMenuFactory>
33 #include <QButtonGroup>
36 #include <QHBoxLayout>
39 #include <QScrollArea>
41 #include <QToolButton>
42 #include <QVBoxLayout>
45 #include <config-baloo.h>
47 #include <Baloo/Query>
48 #include <Baloo/IndexerConfig>
50 #include <QFontDatabase>
52 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
54 m_startedSearching(false),
57 m_searchLabel(nullptr),
58 m_searchInput(nullptr),
59 m_saveSearchAction(nullptr),
60 m_optionsScrollArea(nullptr),
61 m_fileNameButton(nullptr),
62 m_contentButton(nullptr),
64 m_fromHereButton(nullptr),
65 m_everywhereButton(nullptr),
66 m_facetsToggleButton(nullptr),
67 m_facetsWidget(nullptr),
69 m_startSearchTimer(nullptr)
73 DolphinSearchBox::~DolphinSearchBox()
78 void DolphinSearchBox::setText(const QString
& text
)
80 m_searchInput
->setText(text
);
83 QString
DolphinSearchBox::text() const
85 return m_searchInput
->text();
88 void DolphinSearchBox::setSearchPath(const QUrl
& url
)
92 QFontMetrics
metrics(m_fromHereButton
->font());
93 const int maxWidth
= metrics
.height() * 8;
95 QString location
= url
.fileName();
96 if (location
.isEmpty()) {
97 if (url
.isLocalFile()) {
98 location
= QStringLiteral("/");
100 location
= url
.scheme() + QLatin1String(" - ") + url
.host();
104 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
105 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
107 const bool showSearchFromButtons
= url
.isLocalFile();
108 m_separator
->setVisible(showSearchFromButtons
);
109 m_fromHereButton
->setVisible(showSearchFromButtons
);
110 m_everywhereButton
->setVisible(showSearchFromButtons
);
112 bool hasFacetsSupport
= false;
114 const Baloo::IndexerConfig searchInfo
;
115 hasFacetsSupport
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
117 m_facetsWidget
->setEnabled(hasFacetsSupport
);
120 QUrl
DolphinSearchBox::searchPath() const
125 QUrl
DolphinSearchBox::urlForSearching() const
128 bool useBalooSearch
= false;
130 const Baloo::IndexerConfig searchInfo
;
131 useBalooSearch
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
133 if (useBalooSearch
) {
134 url
= balooUrlForSearching();
136 url
.setScheme(QStringLiteral("filenamesearch"));
139 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
140 if (m_contentButton
->isChecked()) {
141 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
145 if (m_everywhereButton
->isChecked()) {
146 // It is very unlikely, that the majority of Dolphins target users
147 // mean "the whole harddisk" instead of "my home folder" when
148 // selecting the "Everywhere" button.
149 encodedUrl
= QDir::homePath();
151 encodedUrl
= m_searchPath
.url();
153 query
.addQueryItem(QStringLiteral("url"), encodedUrl
);
161 void DolphinSearchBox::fromSearchUrl(const QUrl
& url
)
163 if (url
.scheme() == QLatin1String("baloosearch")) {
164 fromBalooSearchUrl(url
);
165 } else if (url
.scheme() == QLatin1String("filenamesearch")) {
166 const QUrlQuery
query(url
);
167 setText(query
.queryItemValue(QStringLiteral("search")));
168 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
169 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
176 void DolphinSearchBox::selectAll()
178 m_searchInput
->selectAll();
181 void DolphinSearchBox::setActive(bool active
)
183 if (active
!= m_active
) {
192 bool DolphinSearchBox::isActive() const
197 bool DolphinSearchBox::event(QEvent
* event
)
199 if (event
->type() == QEvent::Polish
) {
202 return QWidget::event(event
);
205 void DolphinSearchBox::showEvent(QShowEvent
* event
)
207 if (!event
->spontaneous()) {
208 m_searchInput
->setFocus();
209 m_startedSearching
= false;
213 void DolphinSearchBox::hideEvent(QHideEvent
* event
)
216 m_startedSearching
= false;
217 m_startSearchTimer
->stop();
220 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
222 QWidget::keyReleaseEvent(event
);
223 if (event
->key() == Qt::Key_Escape
) {
224 if (m_searchInput
->text().isEmpty()) {
227 m_searchInput
->clear();
232 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
234 switch (event
->type()) {
235 case QEvent::FocusIn
:
236 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
237 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
238 // To avoid this issue, we delay the activation of the search box.
239 QTimer::singleShot(0, this, [this] {
249 return QObject::eventFilter(obj
, event
);
252 void DolphinSearchBox::emitSearchRequest()
254 m_startSearchTimer
->stop();
255 m_startedSearching
= true;
256 m_saveSearchAction
->setEnabled(true);
257 emit
searchRequest();
260 void DolphinSearchBox::emitCloseRequest()
262 m_startSearchTimer
->stop();
263 m_startedSearching
= false;
264 m_saveSearchAction
->setEnabled(false);
268 void DolphinSearchBox::slotConfigurationChanged()
271 if (m_startedSearching
) {
276 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
279 if (text
.isEmpty()) {
280 m_startSearchTimer
->stop();
282 m_startSearchTimer
->start();
284 emit
searchTextChanged(text
);
287 void DolphinSearchBox::slotReturnPressed()
290 emit
returnPressed();
293 void DolphinSearchBox::slotFacetsButtonToggled()
295 const bool facetsIsVisible
= !m_facetsWidget
->isVisible();
296 m_facetsWidget
->setVisible(facetsIsVisible
);
297 updateFacetsToggleButton();
300 void DolphinSearchBox::slotFacetChanged()
302 m_startedSearching
= true;
303 m_startSearchTimer
->stop();
304 emit
searchRequest();
307 void DolphinSearchBox::slotSearchSaved()
309 const QUrl searchURL
= urlForSearching();
310 if (searchURL
.isValid()) {
311 PlacesItemModel model
;
312 const QString label
= i18n("Search for %1 in %2", text(), searchPath().fileName());
313 model
.createPlacesItem(label
,
315 QStringLiteral("folder-saved-search-symbolic"));
319 void DolphinSearchBox::initButton(QToolButton
* button
)
321 button
->installEventFilter(this);
322 button
->setAutoExclusive(true);
323 button
->setAutoRaise(true);
324 button
->setCheckable(true);
325 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
328 void DolphinSearchBox::loadSettings()
330 if (SearchSettings::location() == QLatin1String("Everywhere")) {
331 m_everywhereButton
->setChecked(true);
333 m_fromHereButton
->setChecked(true);
336 if (SearchSettings::what() == QLatin1String("Content")) {
337 m_contentButton
->setChecked(true);
339 m_fileNameButton
->setChecked(true);
342 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
345 void DolphinSearchBox::saveSettings()
347 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
348 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
349 SearchSettings::setShowFacetsWidget(m_facetsToggleButton
->isChecked());
350 SearchSettings::self()->save();
353 void DolphinSearchBox::init()
355 // Create close button
356 QToolButton
* closeButton
= new QToolButton(this);
357 closeButton
->setAutoRaise(true);
358 closeButton
->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
359 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
360 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
362 // Create search label
363 m_searchLabel
= new QLabel(this);
366 m_searchInput
= new QLineEdit(this);
367 m_searchInput
->installEventFilter(this);
368 m_searchInput
->setClearButtonEnabled(true);
369 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
370 connect(m_searchInput
, &QLineEdit::returnPressed
,
371 this, &DolphinSearchBox::slotReturnPressed
);
372 connect(m_searchInput
, &QLineEdit::textChanged
,
373 this, &DolphinSearchBox::slotSearchTextChanged
);
374 setFocusProxy(m_searchInput
);
376 // Add "Save search" button inside search box
377 m_saveSearchAction
= new QAction(this);
378 m_saveSearchAction
->setIcon (QIcon::fromTheme(QStringLiteral("document-save-symbolic")));
379 m_saveSearchAction
->setText(i18nc("action:button", "Save this search to quickly access it again in the future"));
380 m_saveSearchAction
->setEnabled(false);
381 m_searchInput
->addAction(m_saveSearchAction
, QLineEdit::TrailingPosition
);
382 connect(m_saveSearchAction
, &QAction::triggered
, this, &DolphinSearchBox::slotSearchSaved
);
384 // Apply layout for the search input
385 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
386 searchInputLayout
->setMargin(0);
387 searchInputLayout
->addWidget(closeButton
);
388 searchInputLayout
->addWidget(m_searchLabel
);
389 searchInputLayout
->addWidget(m_searchInput
);
391 // Create "Filename" and "Content" button
392 m_fileNameButton
= new QToolButton(this);
393 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
394 initButton(m_fileNameButton
);
396 m_contentButton
= new QToolButton();
397 m_contentButton
->setText(i18nc("action:button", "Content"));
398 initButton(m_contentButton
);
400 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
401 searchWhatGroup
->addButton(m_fileNameButton
);
402 searchWhatGroup
->addButton(m_contentButton
);
404 m_separator
= new KSeparator(Qt::Vertical
, this);
406 // Create "From Here" and "Everywhere"button
407 m_fromHereButton
= new QToolButton(this);
408 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
409 initButton(m_fromHereButton
);
411 m_everywhereButton
= new QToolButton(this);
412 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
413 initButton(m_everywhereButton
);
415 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
416 searchLocationGroup
->addButton(m_fromHereButton
);
417 searchLocationGroup
->addButton(m_everywhereButton
);
419 auto moreSearchToolsButton
= new QToolButton(this);
420 moreSearchToolsButton
->setAutoRaise(true);
421 moreSearchToolsButton
->setPopupMode(QToolButton::InstantPopup
);
422 moreSearchToolsButton
->setIcon(QIcon::fromTheme("arrow-down-double"));
423 moreSearchToolsButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
424 moreSearchToolsButton
->setText(i18n("More Search Tools"));
425 moreSearchToolsButton
->setMenu(new QMenu(this));
426 connect(moreSearchToolsButton
->menu(), &QMenu::aboutToShow
, moreSearchToolsButton
->menu(), [this, moreSearchToolsButton
]()
428 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
429 moreSearchToolsButton
->menu()->clear();
430 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), { "files-find" }, this->m_searchPath
);
433 // Create "Facets" widgets
434 m_facetsToggleButton
= new QToolButton(this);
435 m_facetsToggleButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
436 initButton(m_facetsToggleButton
);
437 connect(m_facetsToggleButton
, &QToolButton::clicked
, this, &DolphinSearchBox::slotFacetsButtonToggled
);
439 m_facetsWidget
= new DolphinFacetsWidget(this);
440 m_facetsWidget
->installEventFilter(this);
441 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
442 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
444 // Apply layout for the options
445 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
446 optionsLayout
->setMargin(0);
447 optionsLayout
->addWidget(m_fileNameButton
);
448 optionsLayout
->addWidget(m_contentButton
);
449 optionsLayout
->addWidget(m_separator
);
450 optionsLayout
->addWidget(m_fromHereButton
);
451 optionsLayout
->addWidget(m_everywhereButton
);
452 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
453 optionsLayout
->addWidget(m_facetsToggleButton
);
454 optionsLayout
->addWidget(moreSearchToolsButton
);
455 optionsLayout
->addStretch(1);
457 // Put the options into a QScrollArea. This prevents increasing the view width
458 // in case that not enough width for the options is available.
459 QWidget
* optionsContainer
= new QWidget(this);
460 optionsContainer
->setLayout(optionsLayout
);
462 m_optionsScrollArea
= new QScrollArea(this);
463 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
464 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
465 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
466 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
467 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
468 m_optionsScrollArea
->setWidget(optionsContainer
);
469 m_optionsScrollArea
->setWidgetResizable(true);
471 m_topLayout
= new QVBoxLayout(this);
472 m_topLayout
->setMargin(0);
473 m_topLayout
->addLayout(searchInputLayout
);
474 m_topLayout
->addWidget(m_optionsScrollArea
);
475 m_topLayout
->addWidget(m_facetsWidget
);
479 // The searching should be started automatically after the user did not change
480 // the text within one second
481 m_startSearchTimer
= new QTimer(this);
482 m_startSearchTimer
->setSingleShot(true);
483 m_startSearchTimer
->setInterval(1000);
484 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
486 updateFacetsToggleButton();
489 QUrl
DolphinSearchBox::balooUrlForSearching() const
492 const QString text
= m_searchInput
->text();
495 query
.addType(m_facetsWidget
->facetType());
497 QStringList queryStrings
;
498 QString ratingQuery
= m_facetsWidget
->ratingTerm();
499 if (!ratingQuery
.isEmpty()) {
500 queryStrings
<< ratingQuery
;
503 if (m_contentButton
->isChecked()) {
504 queryStrings
<< text
;
505 } else if (!text
.isEmpty()) {
506 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
509 if (m_fromHereButton
->isChecked()) {
510 query
.setIncludeFolder(m_searchPath
.toLocalFile());
513 query
.setSearchString(queryStrings
.join(QStringLiteral(" ")));
515 return query
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
516 "Query Results from '%1'", text
));
522 void DolphinSearchBox::fromBalooSearchUrl(const QUrl
& url
)
525 const Baloo::Query query
= Baloo::Query::fromSearchUrl(url
);
527 // Block all signals to avoid unnecessary "searchRequest" signals
528 // while we adjust the search text and the facet widget.
531 const QString customDir
= query
.includeFolder();
532 if (!customDir
.isEmpty()) {
533 setSearchPath(QUrl::fromLocalFile(customDir
));
535 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
538 setText(query
.searchString());
540 QStringList types
= query
.types();
541 if (!types
.isEmpty()) {
542 m_facetsWidget
->setFacetType(types
.first());
545 const QStringList subTerms
= query
.searchString().split(' ', QString::SkipEmptyParts
);
546 foreach (const QString
& subTerm
, subTerms
) {
547 if (subTerm
.startsWith(QLatin1String("filename:"))) {
548 const QString value
= subTerm
.mid(9);
550 } else if (m_facetsWidget
->isRatingTerm(subTerm
)) {
551 m_facetsWidget
->setRatingTerm(subTerm
);
555 m_startSearchTimer
->stop();
562 void DolphinSearchBox::updateFacetsToggleButton()
564 const bool facetsIsVisible
= SearchSettings::showFacetsWidget();
565 m_facetsToggleButton
->setChecked(facetsIsVisible
? true : false);
566 m_facetsToggleButton
->setIcon(QIcon::fromTheme(facetsIsVisible
? QStringLiteral("arrow-up-double") : QStringLiteral("arrow-down-double")));
567 m_facetsToggleButton
->setText(facetsIsVisible
? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));