]>
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"
27 #include <KLocalizedString>
29 #include <KNS3/KMoreToolsMenuFactory>
31 #include <QButtonGroup>
34 #include <QHBoxLayout>
37 #include <QScrollArea>
39 #include <QToolButton>
40 #include <QVBoxLayout>
43 #include <config-baloo.h>
45 #include <Baloo/Query>
46 #include <Baloo/IndexerConfig>
48 #include <QFontDatabase>
50 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
52 m_startedSearching(false),
57 m_optionsScrollArea(0),
62 m_everywhereButton(0),
63 m_facetsToggleButton(0),
70 DolphinSearchBox::~DolphinSearchBox()
75 void DolphinSearchBox::setText(const QString
& text
)
77 m_searchInput
->setText(text
);
80 QString
DolphinSearchBox::text() const
82 return m_searchInput
->text();
85 void DolphinSearchBox::setSearchPath(const QUrl
& url
)
89 QFontMetrics
metrics(m_fromHereButton
->font());
90 const int maxWidth
= metrics
.height() * 8;
92 QString location
= url
.fileName();
93 if (location
.isEmpty()) {
94 if (url
.isLocalFile()) {
95 location
= QStringLiteral("/");
97 location
= url
.scheme() + QLatin1String(" - ") + url
.host();
101 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
102 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
104 const bool showSearchFromButtons
= url
.isLocalFile();
105 m_separator
->setVisible(showSearchFromButtons
);
106 m_fromHereButton
->setVisible(showSearchFromButtons
);
107 m_everywhereButton
->setVisible(showSearchFromButtons
);
109 bool hasFacetsSupport
= false;
111 const Baloo::IndexerConfig searchInfo
;
112 hasFacetsSupport
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
114 m_facetsWidget
->setEnabled(hasFacetsSupport
);
117 QUrl
DolphinSearchBox::searchPath() const
122 QUrl
DolphinSearchBox::urlForSearching() const
125 bool useBalooSearch
= false;
127 const Baloo::IndexerConfig searchInfo
;
128 useBalooSearch
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
130 if (useBalooSearch
) {
131 url
= balooUrlForSearching();
133 url
.setScheme(QStringLiteral("filenamesearch"));
136 query
.addQueryItem(QStringLiteral("search"), m_searchInput
->text());
137 if (m_contentButton
->isChecked()) {
138 query
.addQueryItem(QStringLiteral("checkContent"), QStringLiteral("yes"));
142 if (m_everywhereButton
->isChecked()) {
143 // It is very unlikely, that the majority of Dolphins target users
144 // mean "the whole harddisk" instead of "my home folder" when
145 // selecting the "Everywhere" button.
146 encodedUrl
= QDir::homePath();
148 encodedUrl
= m_searchPath
.url();
150 query
.addQueryItem(QStringLiteral("url"), encodedUrl
);
158 void DolphinSearchBox::fromSearchUrl(const QUrl
& url
)
160 if (url
.scheme() == QLatin1String("baloosearch")) {
161 fromBalooSearchUrl(url
);
162 } else if (url
.scheme() == QLatin1String("filenamesearch")) {
163 const QUrlQuery
query(url
);
164 setText(query
.queryItemValue(QStringLiteral("search")));
165 setSearchPath(QUrl::fromUserInput(query
.queryItemValue(QStringLiteral("url")), QString(), QUrl::AssumeLocalFile
));
166 m_contentButton
->setChecked(query
.queryItemValue(QStringLiteral("checkContent")) == QLatin1String("yes"));
173 void DolphinSearchBox::selectAll()
175 m_searchInput
->selectAll();
178 void DolphinSearchBox::setActive(bool active
)
180 if (active
!= m_active
) {
189 bool DolphinSearchBox::isActive() const
194 bool DolphinSearchBox::event(QEvent
* event
)
196 if (event
->type() == QEvent::Polish
) {
199 return QWidget::event(event
);
202 void DolphinSearchBox::showEvent(QShowEvent
* event
)
204 if (!event
->spontaneous()) {
205 m_searchInput
->setFocus();
206 m_startedSearching
= false;
210 void DolphinSearchBox::hideEvent(QHideEvent
* event
)
213 m_startedSearching
= false;
214 m_startSearchTimer
->stop();
217 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
219 QWidget::keyReleaseEvent(event
);
220 if (event
->key() == Qt::Key_Escape
) {
221 if (m_searchInput
->text().isEmpty()) {
224 m_searchInput
->clear();
229 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
231 switch (event
->type()) {
232 case QEvent::FocusIn
:
233 // #379135: we get the FocusIn event when we close a tab but we don't want to emit
234 // the activated() signal before the removeTab() call in DolphinTabWidget::closeTab() returns.
235 // To avoid this issue, we delay the activation of the search box.
236 QTimer::singleShot(0, this, [this] {
246 return QObject::eventFilter(obj
, event
);
249 void DolphinSearchBox::emitSearchRequest()
251 m_startSearchTimer
->stop();
252 m_startedSearching
= true;
253 emit
searchRequest();
256 void DolphinSearchBox::emitCloseRequest()
258 m_startSearchTimer
->stop();
259 m_startedSearching
= false;
263 void DolphinSearchBox::slotConfigurationChanged()
266 if (m_startedSearching
) {
271 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
274 if (text
.isEmpty()) {
275 m_startSearchTimer
->stop();
277 m_startSearchTimer
->start();
279 emit
searchTextChanged(text
);
282 void DolphinSearchBox::slotReturnPressed()
285 emit
returnPressed();
288 void DolphinSearchBox::slotFacetsButtonToggled()
290 const bool facetsIsVisible
= !m_facetsWidget
->isVisible();
291 m_facetsWidget
->setVisible(facetsIsVisible
);
292 updateFacetsToggleButton();
295 void DolphinSearchBox::slotFacetChanged()
297 m_startedSearching
= true;
298 m_startSearchTimer
->stop();
299 emit
searchRequest();
302 void DolphinSearchBox::initButton(QToolButton
* button
)
304 button
->installEventFilter(this);
305 button
->setAutoExclusive(true);
306 button
->setAutoRaise(true);
307 button
->setCheckable(true);
308 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
311 void DolphinSearchBox::loadSettings()
313 if (SearchSettings::location() == QLatin1String("Everywhere")) {
314 m_everywhereButton
->setChecked(true);
316 m_fromHereButton
->setChecked(true);
319 if (SearchSettings::what() == QLatin1String("Content")) {
320 m_contentButton
->setChecked(true);
322 m_fileNameButton
->setChecked(true);
325 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
328 void DolphinSearchBox::saveSettings()
330 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? QStringLiteral("FromHere") : QStringLiteral("Everywhere"));
331 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? QStringLiteral("FileName") : QStringLiteral("Content"));
332 SearchSettings::setShowFacetsWidget(m_facetsToggleButton
->isChecked());
333 SearchSettings::self()->save();
336 void DolphinSearchBox::init()
338 // Create close button
339 QToolButton
* closeButton
= new QToolButton(this);
340 closeButton
->setAutoRaise(true);
341 closeButton
->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
342 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
343 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
345 // Create search label
346 m_searchLabel
= new QLabel(this);
349 m_searchInput
= new QLineEdit(this);
350 m_searchInput
->installEventFilter(this);
351 m_searchInput
->setClearButtonEnabled(true);
352 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
353 connect(m_searchInput
, &QLineEdit::returnPressed
,
354 this, &DolphinSearchBox::slotReturnPressed
);
355 connect(m_searchInput
, &QLineEdit::textChanged
,
356 this, &DolphinSearchBox::slotSearchTextChanged
);
357 setFocusProxy(m_searchInput
);
359 // Apply layout for the search input
360 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
361 searchInputLayout
->setMargin(0);
362 searchInputLayout
->addWidget(closeButton
);
363 searchInputLayout
->addWidget(m_searchLabel
);
364 searchInputLayout
->addWidget(m_searchInput
);
366 // Create "Filename" and "Content" button
367 m_fileNameButton
= new QToolButton(this);
368 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
369 initButton(m_fileNameButton
);
371 m_contentButton
= new QToolButton();
372 m_contentButton
->setText(i18nc("action:button", "Content"));
373 initButton(m_contentButton
);
375 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
376 searchWhatGroup
->addButton(m_fileNameButton
);
377 searchWhatGroup
->addButton(m_contentButton
);
379 m_separator
= new KSeparator(Qt::Vertical
, this);
381 // Create "From Here" and "Everywhere"button
382 m_fromHereButton
= new QToolButton(this);
383 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
384 initButton(m_fromHereButton
);
386 m_everywhereButton
= new QToolButton(this);
387 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
388 initButton(m_everywhereButton
);
390 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
391 searchLocationGroup
->addButton(m_fromHereButton
);
392 searchLocationGroup
->addButton(m_everywhereButton
);
394 auto moreSearchToolsButton
= new QToolButton(this);
395 moreSearchToolsButton
->setAutoRaise(true);
396 moreSearchToolsButton
->setPopupMode(QToolButton::InstantPopup
);
397 moreSearchToolsButton
->setIcon(QIcon::fromTheme("arrow-down-double"));
398 moreSearchToolsButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
399 moreSearchToolsButton
->setText(i18n("More Search Tools"));
400 moreSearchToolsButton
->setMenu(new QMenu(this));
401 connect(moreSearchToolsButton
->menu(), &QMenu::aboutToShow
, moreSearchToolsButton
->menu(), [this, moreSearchToolsButton
]()
403 m_menuFactory
.reset(new KMoreToolsMenuFactory("dolphin/search-tools"));
404 moreSearchToolsButton
->menu()->clear();
405 m_menuFactory
->fillMenuFromGroupingNames(moreSearchToolsButton
->menu(), { "files-find" }, this->m_searchPath
);
408 // Create "Facets" widgets
409 m_facetsToggleButton
= new QToolButton(this);
410 m_facetsToggleButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
411 initButton(m_facetsToggleButton
);
412 connect(m_facetsToggleButton
, &QToolButton::clicked
, this, &DolphinSearchBox::slotFacetsButtonToggled
);
414 m_facetsWidget
= new DolphinFacetsWidget(this);
415 m_facetsWidget
->installEventFilter(this);
416 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
417 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
419 // Apply layout for the options
420 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
421 optionsLayout
->setMargin(0);
422 optionsLayout
->addWidget(m_fileNameButton
);
423 optionsLayout
->addWidget(m_contentButton
);
424 optionsLayout
->addWidget(m_separator
);
425 optionsLayout
->addWidget(m_fromHereButton
);
426 optionsLayout
->addWidget(m_everywhereButton
);
427 optionsLayout
->addWidget(new KSeparator(Qt::Vertical
, this));
428 optionsLayout
->addWidget(m_facetsToggleButton
);
429 optionsLayout
->addWidget(moreSearchToolsButton
);
430 optionsLayout
->addStretch(1);
432 // Put the options into a QScrollArea. This prevents increasing the view width
433 // in case that not enough width for the options is available.
434 QWidget
* optionsContainer
= new QWidget(this);
435 optionsContainer
->setLayout(optionsLayout
);
437 m_optionsScrollArea
= new QScrollArea(this);
438 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
439 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
440 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
441 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
442 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
443 m_optionsScrollArea
->setWidget(optionsContainer
);
444 m_optionsScrollArea
->setWidgetResizable(true);
446 m_topLayout
= new QVBoxLayout(this);
447 m_topLayout
->setMargin(0);
448 m_topLayout
->addLayout(searchInputLayout
);
449 m_topLayout
->addWidget(m_optionsScrollArea
);
450 m_topLayout
->addWidget(m_facetsWidget
);
454 // The searching should be started automatically after the user did not change
455 // the text within one second
456 m_startSearchTimer
= new QTimer(this);
457 m_startSearchTimer
->setSingleShot(true);
458 m_startSearchTimer
->setInterval(1000);
459 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
461 updateFacetsToggleButton();
464 QUrl
DolphinSearchBox::balooUrlForSearching() const
467 const QString text
= m_searchInput
->text();
470 query
.addType(m_facetsWidget
->facetType());
472 QStringList queryStrings
;
473 QString ratingQuery
= m_facetsWidget
->ratingTerm();
474 if (!ratingQuery
.isEmpty()) {
475 queryStrings
<< ratingQuery
;
478 if (m_contentButton
->isChecked()) {
479 queryStrings
<< text
;
480 } else if (!text
.isEmpty()) {
481 queryStrings
<< QStringLiteral("filename:\"%1\"").arg(text
);
484 if (m_fromHereButton
->isChecked()) {
485 query
.setIncludeFolder(m_searchPath
.toLocalFile());
488 query
.setSearchString(queryStrings
.join(QStringLiteral(" ")));
490 return query
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
491 "Query Results from '%1'", text
));
497 void DolphinSearchBox::fromBalooSearchUrl(const QUrl
& url
)
500 const Baloo::Query query
= Baloo::Query::fromSearchUrl(url
);
502 // Block all signals to avoid unnecessary "searchRequest" signals
503 // while we adjust the search text and the facet widget.
506 const QString customDir
= query
.includeFolder();
507 if (!customDir
.isEmpty()) {
508 setSearchPath(QUrl::fromLocalFile(customDir
));
510 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
513 setText(query
.searchString());
515 QStringList types
= query
.types();
516 if (!types
.isEmpty()) {
517 m_facetsWidget
->setFacetType(types
.first());
520 const QStringList subTerms
= query
.searchString().split(' ', QString::SkipEmptyParts
);
521 foreach (const QString
& subTerm
, subTerms
) {
522 if (subTerm
.startsWith(QLatin1String("filename:"))) {
523 const QString value
= subTerm
.mid(9);
525 } else if (m_facetsWidget
->isRatingTerm(subTerm
)) {
526 m_facetsWidget
->setRatingTerm(subTerm
);
530 m_startSearchTimer
->stop();
537 void DolphinSearchBox::updateFacetsToggleButton()
539 const bool facetsIsVisible
= SearchSettings::showFacetsWidget();
540 m_facetsToggleButton
->setChecked(facetsIsVisible
? true : false);
541 m_facetsToggleButton
->setIcon(QIcon::fromTheme(facetsIsVisible
? QStringLiteral("arrow-up-double") : QStringLiteral("arrow-down-double")));
542 m_facetsToggleButton
->setText(facetsIsVisible
? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));