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"
29 #include <KGlobalSettings>
31 #include <QButtonGroup>
34 #include <QFormLayout>
35 #include <QHBoxLayout>
38 #include <QScrollArea>
40 #include <QToolButton>
41 #include <QVBoxLayout>
44 #include <Baloo/NaturalFileQueryParser>
45 #include <Baloo/QueryBuilder>
46 #include <Baloo/Query>
48 #include <Baloo/IndexerConfig>
51 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
53 m_startedSearching(false),
58 m_optionsScrollArea(0),
63 m_everywhereButton(0),
64 m_facetsToggleButton(0),
71 DolphinSearchBox::~DolphinSearchBox()
76 void DolphinSearchBox::setText(const QString
& text
)
78 m_searchInput
->setText(text
);
81 QString
DolphinSearchBox::text() const
83 return m_searchInput
->text();
86 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
90 QFontMetrics
metrics(m_fromHereButton
->font());
91 const int maxWidth
= metrics
.height() * 8;
93 QString location
= url
.fileName();
94 if (location
.isEmpty()) {
95 if (url
.isLocalFile()) {
96 location
= QLatin1String("/");
98 location
= url
.protocol() + QLatin1String(" - ") + url
.host();
102 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
103 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
105 const bool showSearchFromButtons
= url
.isLocalFile();
106 m_separator
->setVisible(showSearchFromButtons
);
107 m_fromHereButton
->setVisible(showSearchFromButtons
);
108 m_everywhereButton
->setVisible(showSearchFromButtons
);
110 bool hasFacetsSupport
= false;
112 const Baloo::IndexerConfig searchInfo
;
113 hasFacetsSupport
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
115 m_facetsWidget
->setEnabled(hasFacetsSupport
);
118 KUrl
DolphinSearchBox::searchPath() const
123 KUrl
DolphinSearchBox::urlForSearching() const
126 bool useBalooSearch
= false;
128 const Baloo::IndexerConfig searchInfo
;
129 useBalooSearch
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
131 if (useBalooSearch
) {
132 url
= balooUrlForSearching();
134 url
.setProtocol("filenamesearch");
135 url
.addQueryItem("search", m_searchInput
->text());
136 if (m_contentButton
->isChecked()) {
137 url
.addQueryItem("checkContent", "yes");
141 if (m_everywhereButton
->isChecked()) {
142 // It is very unlikely, that the majority of Dolphins target users
143 // mean "the whole harddisk" instead of "my home folder" when
144 // selecting the "Everywhere" button.
145 encodedUrl
= QDir::homePath();
147 encodedUrl
= m_searchPath
.url();
149 url
.addQueryItem("url", encodedUrl
);
155 void DolphinSearchBox::fromSearchUrl(const KUrl
& url
)
157 if (url
.protocol() == "baloosearch") {
158 fromBalooSearchUrl(url
);
159 } else if (url
.protocol() == "filenamesearch") {
160 const QMap
<QString
, QString
>& queryItems
= url
.queryItems();
161 setText(queryItems
.value("search"));
162 setSearchPath(queryItems
.value("url"));
163 m_contentButton
->setChecked(queryItems
.value("checkContent") == "yes");
170 void DolphinSearchBox::selectAll()
172 m_searchInput
->selectAll();
175 void DolphinSearchBox::setActive(bool active
)
177 if (active
!= m_active
) {
186 bool DolphinSearchBox::isActive() const
191 bool DolphinSearchBox::event(QEvent
* event
)
193 if (event
->type() == QEvent::Polish
) {
196 return QWidget::event(event
);
199 void DolphinSearchBox::showEvent(QShowEvent
* event
)
201 if (!event
->spontaneous()) {
202 m_searchInput
->setFocus();
203 m_startedSearching
= false;
207 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
209 QWidget::keyReleaseEvent(event
);
210 if (event
->key() == Qt::Key_Escape
) {
211 if (m_searchInput
->text().isEmpty()) {
214 m_searchInput
->clear();
219 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
221 switch (event
->type()) {
222 case QEvent::FocusIn
:
231 return QObject::eventFilter(obj
, event
);
234 void DolphinSearchBox::emitSearchRequest()
236 m_startSearchTimer
->stop();
237 m_startedSearching
= true;
238 emit
searchRequest();
241 void DolphinSearchBox::emitCloseRequest()
243 m_startSearchTimer
->stop();
244 m_startedSearching
= false;
248 void DolphinSearchBox::slotConfigurationChanged()
251 if (m_startedSearching
) {
256 void DolphinSearchBox::slotSearchTextChanged()
258 const QString text
= m_searchInput
->text();
260 if (text
.isEmpty()) {
261 m_startSearchTimer
->stop();
263 m_startSearchTimer
->start();
265 emit
searchTextChanged(text
);
268 void DolphinSearchBox::slotReturnPressed()
271 emit
returnPressed(m_searchInput
->text());
274 void DolphinSearchBox::updateSearchInputParsing()
277 m_searchInput
->setParsingEnabled(m_contentButton
->isChecked());
281 void DolphinSearchBox::slotFacetsButtonToggled()
283 const bool facetsIsVisible
= !m_facetsWidget
->isVisible();
284 m_facetsWidget
->setVisible(facetsIsVisible
);
285 updateFacetsToggleButton();
288 void DolphinSearchBox::slotFacetChanged()
290 m_startedSearching
= true;
291 m_startSearchTimer
->stop();
292 emit
searchRequest();
295 void DolphinSearchBox::initButton(QToolButton
* button
)
297 button
->installEventFilter(this);
298 button
->setAutoExclusive(true);
299 button
->setAutoRaise(true);
300 button
->setCheckable(true);
301 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
304 void DolphinSearchBox::loadSettings()
306 if (SearchSettings::location() == QLatin1String("Everywhere")) {
307 m_everywhereButton
->setChecked(true);
309 m_fromHereButton
->setChecked(true);
312 if (SearchSettings::what() == QLatin1String("Content")) {
313 m_contentButton
->setChecked(true);
315 m_fileNameButton
->setChecked(true);
318 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
319 updateSearchInputParsing();
322 void DolphinSearchBox::saveSettings()
324 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
325 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
326 SearchSettings::setShowFacetsWidget(m_facetsToggleButton
->isChecked());
327 SearchSettings::self()->writeConfig();
330 void DolphinSearchBox::init()
332 // Create close button
333 QToolButton
* closeButton
= new QToolButton(this);
334 closeButton
->setAutoRaise(true);
335 closeButton
->setIcon(QIcon::fromTheme("dialog-close"));
336 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
337 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
339 // Create search label
340 m_searchLabel
= new QLabel(this);
344 m_queryParser
.reset(new Baloo::NaturalFileQueryParser
);
345 m_searchInput
= new Baloo::QueryBuilder(m_queryParser
.data(), this);
346 connect(m_searchInput
, &Baloo::QueryBuilder::editingFinished
,
347 this, &DolphinSearchBox::slotReturnPressed
);
348 connect(m_searchInput
, &Baloo::QueryBuilder::textChanged
,
349 this, &DolphinSearchBox::slotSearchTextChanged
);
351 m_searchInput
= new KLineEdit(this);
352 m_searchInput
->installEventFilter(this);
353 m_searchInput
->setClearButtonShown(true);
354 m_searchInput
->setFont(KGlobalSettings::generalFont());
355 connect(m_searchInput
, &KLineEdit::returnPressed
,
356 this, &DolphinSearchBox::slotReturnPressed
);
357 connect(m_searchInput
, &KLineEdit::textChanged
,
358 this, &DolphinSearchBox::slotSearchTextChanged
);
360 setFocusProxy(m_searchInput
);
362 // Apply layout for the search input
363 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
364 searchInputLayout
->setMargin(0);
365 searchInputLayout
->addWidget(closeButton
);
366 searchInputLayout
->addWidget(m_searchLabel
);
367 searchInputLayout
->addWidget(m_searchInput
);
369 // Create "Filename" and "Content" button
370 m_fileNameButton
= new QToolButton(this);
371 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
372 initButton(m_fileNameButton
);
374 m_contentButton
= new QToolButton();
375 m_contentButton
->setText(i18nc("action:button", "Content"));
376 initButton(m_contentButton
);
378 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
379 searchWhatGroup
->addButton(m_fileNameButton
);
380 searchWhatGroup
->addButton(m_contentButton
);
381 connect(searchWhatGroup
, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked
),
382 this, &DolphinSearchBox::updateSearchInputParsing
);
384 m_separator
= new KSeparator(Qt::Vertical
, this);
386 // Create "From Here" and "Everywhere"button
387 m_fromHereButton
= new QToolButton(this);
388 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
389 initButton(m_fromHereButton
);
391 m_everywhereButton
= new QToolButton(this);
392 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
393 initButton(m_everywhereButton
);
395 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
396 searchLocationGroup
->addButton(m_fromHereButton
);
397 searchLocationGroup
->addButton(m_everywhereButton
);
399 // Create "Facets" widgets
400 m_facetsToggleButton
= new QToolButton(this);
401 m_facetsToggleButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
402 initButton(m_facetsToggleButton
);
403 connect(m_facetsToggleButton
, &QToolButton::clicked
, this, &DolphinSearchBox::slotFacetsButtonToggled
);
405 m_facetsWidget
= new DolphinFacetsWidget(this);
406 m_facetsWidget
->installEventFilter(this);
407 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
408 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
410 // Apply layout for the options
411 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
412 optionsLayout
->setMargin(0);
413 optionsLayout
->addWidget(m_fileNameButton
);
414 optionsLayout
->addWidget(m_contentButton
);
415 optionsLayout
->addWidget(m_separator
);
416 optionsLayout
->addWidget(m_fromHereButton
);
417 optionsLayout
->addWidget(m_everywhereButton
);
418 optionsLayout
->addStretch(1);
419 optionsLayout
->addWidget(m_facetsToggleButton
);
421 // Put the options into a QScrollArea. This prevents increasing the view width
422 // in case that not enough width for the options is available.
423 QWidget
* optionsContainer
= new QWidget(this);
424 optionsContainer
->setLayout(optionsLayout
);
426 m_optionsScrollArea
= new QScrollArea(this);
427 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
428 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
429 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
430 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
431 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
432 m_optionsScrollArea
->setWidget(optionsContainer
);
433 m_optionsScrollArea
->setWidgetResizable(true);
435 m_topLayout
= new QVBoxLayout(this);
436 m_topLayout
->setMargin(0);
437 m_topLayout
->addLayout(searchInputLayout
);
438 m_topLayout
->addWidget(m_optionsScrollArea
);
439 m_topLayout
->addWidget(m_facetsWidget
);
443 // The searching should be started automatically after the user did not change
444 // the text within one second
445 m_startSearchTimer
= new QTimer(this);
446 m_startSearchTimer
->setSingleShot(true);
447 m_startSearchTimer
->setInterval(1000);
448 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
450 updateFacetsToggleButton();
453 KUrl
DolphinSearchBox::balooUrlForSearching() const
456 const QString text
= m_searchInput
->text();
460 if (m_contentButton
->isChecked()) {
461 query
= m_queryParser
->parse(text
, Baloo::NaturalQueryParser::DetectFilenamePattern
);
463 query
.setTerm(Baloo::Term(QLatin1String("filename"), text
));
466 // Configure the query so that it returns files and takes the rating into account
467 query
.addType("File");
468 query
.addType(m_facetsWidget
->facetType());
470 Baloo::Term
term(Baloo::Term::And
);
471 Baloo::Term ratingTerm
= m_facetsWidget
->ratingTerm();
473 if (ratingTerm
.isValid()) {
474 term
.addSubTerm(query
.term());
475 term
.addSubTerm(ratingTerm
);
480 if (m_fromHereButton
->isChecked()) {
481 query
.addCustomOption("includeFolder", m_searchPath
.toLocalFile());
484 return query
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
485 "Query Results from '%1'", text
));
491 void DolphinSearchBox::fromBalooSearchUrl(const KUrl
& url
)
494 const Baloo::Query query
= Baloo::Query::fromSearchUrl(url
);
495 const Baloo::Term term
= query
.term();
497 // Block all signals to avoid unnecessary "searchRequest" signals
498 // while we adjust the search text and the facet widget.
501 const QVariantMap customOptions
= query
.customOptions();
502 if (customOptions
.contains("includeFolder")) {
503 setSearchPath(customOptions
.value("includeFolder").toString());
505 setSearchPath(QDir::homePath());
508 if (!query
.searchString().isEmpty()) {
509 setText(query
.searchString());
512 QStringList types
= query
.types();
513 types
.removeOne("File"); // We are only interested in facet widget types
514 if (!types
.isEmpty()) {
515 m_facetsWidget
->setFacetType(types
.first());
518 foreach (const Baloo::Term
& subTerm
, term
.subTerms()) {
519 const QString property
= subTerm
.property();
521 if (property
== QLatin1String("filename")) {
522 setText(subTerm
.value().toString());
523 } else if (m_facetsWidget
->isRatingTerm(subTerm
)) {
524 m_facetsWidget
->setRatingTerm(subTerm
);
528 m_startSearchTimer
->stop();
533 void DolphinSearchBox::updateFacetsToggleButton()
535 const bool facetsIsVisible
= SearchSettings::showFacetsWidget();
536 m_facetsToggleButton
->setChecked(facetsIsVisible
? true : false);
537 m_facetsToggleButton
->setIcon(QIcon::fromTheme(facetsIsVisible
? "arrow-up-double" : "arrow-down-double"));
538 m_facetsToggleButton
->setText(facetsIsVisible
? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));