]>
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>
30 #include <QButtonGroup>
33 #include <QHBoxLayout>
36 #include <QScrollArea>
38 #include <QToolButton>
39 #include <QVBoxLayout>
42 #include <config-baloo.h>
44 #include <Baloo/Query>
45 #include <Baloo/IndexerConfig>
47 #include <QFontDatabase>
49 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
51 m_startedSearching(false),
56 m_optionsScrollArea(0),
61 m_everywhereButton(0),
62 m_facetsToggleButton(0),
69 DolphinSearchBox::~DolphinSearchBox()
74 void DolphinSearchBox::setText(const QString
& text
)
76 m_searchInput
->setText(text
);
79 QString
DolphinSearchBox::text() const
81 return m_searchInput
->text();
84 void DolphinSearchBox::setSearchPath(const QUrl
& url
)
88 QFontMetrics
metrics(m_fromHereButton
->font());
89 const int maxWidth
= metrics
.height() * 8;
91 QString location
= url
.fileName();
92 if (location
.isEmpty()) {
93 if (url
.isLocalFile()) {
94 location
= QLatin1String("/");
96 location
= url
.scheme() + QLatin1String(" - ") + url
.host();
100 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
101 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
103 const bool showSearchFromButtons
= url
.isLocalFile();
104 m_separator
->setVisible(showSearchFromButtons
);
105 m_fromHereButton
->setVisible(showSearchFromButtons
);
106 m_everywhereButton
->setVisible(showSearchFromButtons
);
108 bool hasFacetsSupport
= false;
110 const Baloo::IndexerConfig searchInfo
;
111 hasFacetsSupport
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
113 m_facetsWidget
->setEnabled(hasFacetsSupport
);
116 QUrl
DolphinSearchBox::searchPath() const
121 QUrl
DolphinSearchBox::urlForSearching() const
124 bool useBalooSearch
= false;
126 const Baloo::IndexerConfig searchInfo
;
127 useBalooSearch
= searchInfo
.fileIndexingEnabled() && searchInfo
.shouldBeIndexed(m_searchPath
.toLocalFile());
129 if (useBalooSearch
) {
130 url
= balooUrlForSearching();
132 url
.setScheme("filenamesearch");
135 query
.addQueryItem("search", m_searchInput
->text());
136 if (m_contentButton
->isChecked()) {
137 query
.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 query
.addQueryItem("url", encodedUrl
);
157 void DolphinSearchBox::fromSearchUrl(const QUrl
& url
)
159 if (url
.scheme() == "baloosearch") {
160 fromBalooSearchUrl(url
);
161 } else if (url
.scheme() == "filenamesearch") {
162 const QUrlQuery
query(url
);
163 setText(query
.queryItemValue("search"));
164 setSearchPath(QUrl::fromUserInput(query
.queryItemValue("url"), QString(), QUrl::AssumeLocalFile
));
165 m_contentButton
->setChecked(query
.queryItemValue("checkContent") == "yes");
172 void DolphinSearchBox::selectAll()
174 m_searchInput
->selectAll();
177 void DolphinSearchBox::setActive(bool active
)
179 if (active
!= m_active
) {
188 bool DolphinSearchBox::isActive() const
193 bool DolphinSearchBox::event(QEvent
* event
)
195 if (event
->type() == QEvent::Polish
) {
198 return QWidget::event(event
);
201 void DolphinSearchBox::showEvent(QShowEvent
* event
)
203 if (!event
->spontaneous()) {
204 m_searchInput
->setFocus();
205 m_startedSearching
= false;
209 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
211 QWidget::keyReleaseEvent(event
);
212 if (event
->key() == Qt::Key_Escape
) {
213 if (m_searchInput
->text().isEmpty()) {
216 m_searchInput
->clear();
221 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
223 switch (event
->type()) {
224 case QEvent::FocusIn
:
233 return QObject::eventFilter(obj
, event
);
236 void DolphinSearchBox::emitSearchRequest()
238 m_startSearchTimer
->stop();
239 m_startedSearching
= true;
240 emit
searchRequest();
243 void DolphinSearchBox::emitCloseRequest()
245 m_startSearchTimer
->stop();
246 m_startedSearching
= false;
250 void DolphinSearchBox::slotConfigurationChanged()
253 if (m_startedSearching
) {
258 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
261 if (text
.isEmpty()) {
262 m_startSearchTimer
->stop();
264 m_startSearchTimer
->start();
266 emit
searchTextChanged(text
);
269 void DolphinSearchBox::slotReturnPressed()
272 emit
returnPressed();
275 void DolphinSearchBox::slotFacetsButtonToggled()
277 const bool facetsIsVisible
= !m_facetsWidget
->isVisible();
278 m_facetsWidget
->setVisible(facetsIsVisible
);
279 updateFacetsToggleButton();
282 void DolphinSearchBox::slotFacetChanged()
284 m_startedSearching
= true;
285 m_startSearchTimer
->stop();
286 emit
searchRequest();
289 void DolphinSearchBox::initButton(QToolButton
* button
)
291 button
->installEventFilter(this);
292 button
->setAutoExclusive(true);
293 button
->setAutoRaise(true);
294 button
->setCheckable(true);
295 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
298 void DolphinSearchBox::loadSettings()
300 if (SearchSettings::location() == QLatin1String("Everywhere")) {
301 m_everywhereButton
->setChecked(true);
303 m_fromHereButton
->setChecked(true);
306 if (SearchSettings::what() == QLatin1String("Content")) {
307 m_contentButton
->setChecked(true);
309 m_fileNameButton
->setChecked(true);
312 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
315 void DolphinSearchBox::saveSettings()
317 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
318 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
319 SearchSettings::setShowFacetsWidget(m_facetsToggleButton
->isChecked());
320 SearchSettings::self()->save();
323 void DolphinSearchBox::init()
325 // Create close button
326 QToolButton
* closeButton
= new QToolButton(this);
327 closeButton
->setAutoRaise(true);
328 closeButton
->setIcon(QIcon::fromTheme("dialog-close"));
329 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
330 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
332 // Create search label
333 m_searchLabel
= new QLabel(this);
336 m_searchInput
= new QLineEdit(this);
337 m_searchInput
->installEventFilter(this);
338 m_searchInput
->setClearButtonEnabled(true);
339 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
340 connect(m_searchInput
, &QLineEdit::returnPressed
,
341 this, &DolphinSearchBox::slotReturnPressed
);
342 connect(m_searchInput
, &QLineEdit::textChanged
,
343 this, &DolphinSearchBox::slotSearchTextChanged
);
344 setFocusProxy(m_searchInput
);
346 // Apply layout for the search input
347 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
348 searchInputLayout
->setMargin(0);
349 searchInputLayout
->addWidget(closeButton
);
350 searchInputLayout
->addWidget(m_searchLabel
);
351 searchInputLayout
->addWidget(m_searchInput
);
353 // Create "Filename" and "Content" button
354 m_fileNameButton
= new QToolButton(this);
355 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
356 initButton(m_fileNameButton
);
358 m_contentButton
= new QToolButton();
359 m_contentButton
->setText(i18nc("action:button", "Content"));
360 initButton(m_contentButton
);
362 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
363 searchWhatGroup
->addButton(m_fileNameButton
);
364 searchWhatGroup
->addButton(m_contentButton
);
366 m_separator
= new KSeparator(Qt::Vertical
, this);
368 // Create "From Here" and "Everywhere"button
369 m_fromHereButton
= new QToolButton(this);
370 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
371 initButton(m_fromHereButton
);
373 m_everywhereButton
= new QToolButton(this);
374 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
375 initButton(m_everywhereButton
);
377 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
378 searchLocationGroup
->addButton(m_fromHereButton
);
379 searchLocationGroup
->addButton(m_everywhereButton
);
381 // Create "Facets" widgets
382 m_facetsToggleButton
= new QToolButton(this);
383 m_facetsToggleButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
384 initButton(m_facetsToggleButton
);
385 connect(m_facetsToggleButton
, &QToolButton::clicked
, this, &DolphinSearchBox::slotFacetsButtonToggled
);
387 m_facetsWidget
= new DolphinFacetsWidget(this);
388 m_facetsWidget
->installEventFilter(this);
389 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
390 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
392 // Apply layout for the options
393 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
394 optionsLayout
->setMargin(0);
395 optionsLayout
->addWidget(m_fileNameButton
);
396 optionsLayout
->addWidget(m_contentButton
);
397 optionsLayout
->addWidget(m_separator
);
398 optionsLayout
->addWidget(m_fromHereButton
);
399 optionsLayout
->addWidget(m_everywhereButton
);
400 optionsLayout
->addStretch(1);
401 optionsLayout
->addWidget(m_facetsToggleButton
);
403 // Put the options into a QScrollArea. This prevents increasing the view width
404 // in case that not enough width for the options is available.
405 QWidget
* optionsContainer
= new QWidget(this);
406 optionsContainer
->setLayout(optionsLayout
);
408 m_optionsScrollArea
= new QScrollArea(this);
409 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
410 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
411 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
412 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
413 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
414 m_optionsScrollArea
->setWidget(optionsContainer
);
415 m_optionsScrollArea
->setWidgetResizable(true);
417 m_topLayout
= new QVBoxLayout(this);
418 m_topLayout
->setMargin(0);
419 m_topLayout
->addLayout(searchInputLayout
);
420 m_topLayout
->addWidget(m_optionsScrollArea
);
421 m_topLayout
->addWidget(m_facetsWidget
);
425 // The searching should be started automatically after the user did not change
426 // the text within one second
427 m_startSearchTimer
= new QTimer(this);
428 m_startSearchTimer
->setSingleShot(true);
429 m_startSearchTimer
->setInterval(1000);
430 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
432 updateFacetsToggleButton();
435 QUrl
DolphinSearchBox::balooUrlForSearching() const
438 const QString text
= m_searchInput
->text();
441 query
.addType(m_facetsWidget
->facetType());
443 QStringList queryStrings
;
444 QString ratingQuery
= m_facetsWidget
->ratingTerm();
445 if (!ratingQuery
.isEmpty()) {
446 queryStrings
<< ratingQuery
;
449 if (m_contentButton
->isChecked()) {
450 queryStrings
<< text
;
451 } else if (!text
.isEmpty()) {
452 queryStrings
<< QString::fromLatin1("filename:\"%1\"").arg(text
);
455 if (m_fromHereButton
->isChecked()) {
456 query
.setIncludeFolder(m_searchPath
.toLocalFile());
459 query
.setSearchString(queryStrings
.join(" "));
461 return query
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
462 "Query Results from '%1'", text
));
468 void DolphinSearchBox::fromBalooSearchUrl(const QUrl
& url
)
471 const Baloo::Query query
= Baloo::Query::fromSearchUrl(url
);
473 // Block all signals to avoid unnecessary "searchRequest" signals
474 // while we adjust the search text and the facet widget.
477 const QString customDir
= query
.includeFolder();
478 if (!customDir
.isEmpty()) {
479 setSearchPath(QUrl::fromLocalFile(customDir
));
481 setSearchPath(QUrl::fromLocalFile(QDir::homePath()));
484 setText(query
.searchString());
486 QStringList types
= query
.types();
487 if (!types
.isEmpty()) {
488 m_facetsWidget
->setFacetType(types
.first());
491 const QStringList subTerms
= query
.searchString().split(' ', QString::SkipEmptyParts
);
492 foreach (const QString
& subTerm
, subTerms
) {
493 if (subTerm
.startsWith("filename:")) {
494 const QString value
= subTerm
.mid(9);
496 } else if (m_facetsWidget
->isRatingTerm(subTerm
)) {
497 m_facetsWidget
->setRatingTerm(subTerm
);
501 m_startSearchTimer
->stop();
508 void DolphinSearchBox::updateFacetsToggleButton()
510 const bool facetsIsVisible
= SearchSettings::showFacetsWidget();
511 m_facetsToggleButton
->setChecked(facetsIsVisible
? true : false);
512 m_facetsToggleButton
->setIcon(QIcon::fromTheme(facetsIsVisible
? "arrow-up-double" : "arrow-down-double"));
513 m_facetsToggleButton
->setText(facetsIsVisible
? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));