]>
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>
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
= QLatin1String("/");
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("filenamesearch");
136 query
.addQueryItem("search", m_searchInput
->text());
137 if (m_contentButton
->isChecked()) {
138 query
.addQueryItem("checkContent", "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("url", encodedUrl
);
158 void DolphinSearchBox::fromSearchUrl(const QUrl
& url
)
160 if (url
.scheme() == "baloosearch") {
161 fromBalooSearchUrl(url
);
162 } else if (url
.scheme() == "filenamesearch") {
163 const QUrlQuery
query(url
);
164 setText(query
.queryItemValue("search"));
165 setSearchPath(QUrl::fromUserInput(query
.queryItemValue("url"), QString(), QUrl::AssumeLocalFile
));
166 m_contentButton
->setChecked(query
.queryItemValue("checkContent") == "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::keyReleaseEvent(QKeyEvent
* event
)
212 QWidget::keyReleaseEvent(event
);
213 if (event
->key() == Qt::Key_Escape
) {
214 if (m_searchInput
->text().isEmpty()) {
217 m_searchInput
->clear();
222 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
224 switch (event
->type()) {
225 case QEvent::FocusIn
:
234 return QObject::eventFilter(obj
, event
);
237 void DolphinSearchBox::emitSearchRequest()
239 m_startSearchTimer
->stop();
240 m_startedSearching
= true;
241 emit
searchRequest();
244 void DolphinSearchBox::emitCloseRequest()
246 m_startSearchTimer
->stop();
247 m_startedSearching
= false;
251 void DolphinSearchBox::slotConfigurationChanged()
254 if (m_startedSearching
) {
259 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
262 if (text
.isEmpty()) {
263 m_startSearchTimer
->stop();
265 m_startSearchTimer
->start();
267 emit
searchTextChanged(text
);
270 void DolphinSearchBox::slotReturnPressed()
273 emit
returnPressed();
276 void DolphinSearchBox::slotFacetsButtonToggled()
278 const bool facetsIsVisible
= !m_facetsWidget
->isVisible();
279 m_facetsWidget
->setVisible(facetsIsVisible
);
280 updateFacetsToggleButton();
283 void DolphinSearchBox::slotFacetChanged()
285 m_startedSearching
= true;
286 m_startSearchTimer
->stop();
287 emit
searchRequest();
290 void DolphinSearchBox::initButton(QToolButton
* button
)
292 button
->installEventFilter(this);
293 button
->setAutoExclusive(true);
294 button
->setAutoRaise(true);
295 button
->setCheckable(true);
296 connect(button
, &QToolButton::clicked
, this, &DolphinSearchBox::slotConfigurationChanged
);
299 void DolphinSearchBox::loadSettings()
301 if (SearchSettings::location() == QLatin1String("Everywhere")) {
302 m_everywhereButton
->setChecked(true);
304 m_fromHereButton
->setChecked(true);
307 if (SearchSettings::what() == QLatin1String("Content")) {
308 m_contentButton
->setChecked(true);
310 m_fileNameButton
->setChecked(true);
313 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
316 void DolphinSearchBox::saveSettings()
318 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
319 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
320 SearchSettings::setShowFacetsWidget(m_facetsToggleButton
->isChecked());
321 SearchSettings::self()->save();
324 void DolphinSearchBox::init()
326 // Create close button
327 QToolButton
* closeButton
= new QToolButton(this);
328 closeButton
->setAutoRaise(true);
329 closeButton
->setIcon(QIcon::fromTheme("dialog-close"));
330 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
331 connect(closeButton
, &QToolButton::clicked
, this, &DolphinSearchBox::emitCloseRequest
);
333 // Create search label
334 m_searchLabel
= new QLabel(this);
337 m_searchInput
= new QLineEdit(this);
338 m_searchInput
->installEventFilter(this);
339 m_searchInput
->setClearButtonEnabled(true);
340 m_searchInput
->setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont
));
341 connect(m_searchInput
, &QLineEdit::returnPressed
,
342 this, &DolphinSearchBox::slotReturnPressed
);
343 connect(m_searchInput
, &QLineEdit::textChanged
,
344 this, &DolphinSearchBox::slotSearchTextChanged
);
345 setFocusProxy(m_searchInput
);
347 // Apply layout for the search input
348 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
349 searchInputLayout
->setMargin(0);
350 searchInputLayout
->addWidget(closeButton
);
351 searchInputLayout
->addWidget(m_searchLabel
);
352 searchInputLayout
->addWidget(m_searchInput
);
354 // Create "Filename" and "Content" button
355 m_fileNameButton
= new QToolButton(this);
356 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
357 initButton(m_fileNameButton
);
359 m_contentButton
= new QToolButton();
360 m_contentButton
->setText(i18nc("action:button", "Content"));
361 initButton(m_contentButton
);
363 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
364 searchWhatGroup
->addButton(m_fileNameButton
);
365 searchWhatGroup
->addButton(m_contentButton
);
367 m_separator
= new KSeparator(Qt::Vertical
, this);
369 // Create "From Here" and "Everywhere"button
370 m_fromHereButton
= new QToolButton(this);
371 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
372 initButton(m_fromHereButton
);
374 m_everywhereButton
= new QToolButton(this);
375 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
376 initButton(m_everywhereButton
);
378 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
379 searchLocationGroup
->addButton(m_fromHereButton
);
380 searchLocationGroup
->addButton(m_everywhereButton
);
382 // Create "Facets" widgets
383 m_facetsToggleButton
= new QToolButton(this);
384 m_facetsToggleButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
385 initButton(m_facetsToggleButton
);
386 connect(m_facetsToggleButton
, &QToolButton::clicked
, this, &DolphinSearchBox::slotFacetsButtonToggled
);
388 m_facetsWidget
= new DolphinFacetsWidget(this);
389 m_facetsWidget
->installEventFilter(this);
390 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
391 connect(m_facetsWidget
, &DolphinFacetsWidget::facetChanged
, this, &DolphinSearchBox::slotFacetChanged
);
393 // Apply layout for the options
394 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
395 optionsLayout
->setMargin(0);
396 optionsLayout
->addWidget(m_fileNameButton
);
397 optionsLayout
->addWidget(m_contentButton
);
398 optionsLayout
->addWidget(m_separator
);
399 optionsLayout
->addWidget(m_fromHereButton
);
400 optionsLayout
->addWidget(m_everywhereButton
);
401 optionsLayout
->addStretch(1);
402 optionsLayout
->addWidget(m_facetsToggleButton
);
404 // Put the options into a QScrollArea. This prevents increasing the view width
405 // in case that not enough width for the options is available.
406 QWidget
* optionsContainer
= new QWidget(this);
407 optionsContainer
->setLayout(optionsLayout
);
409 m_optionsScrollArea
= new QScrollArea(this);
410 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
411 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
412 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
413 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
414 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
415 m_optionsScrollArea
->setWidget(optionsContainer
);
416 m_optionsScrollArea
->setWidgetResizable(true);
418 m_topLayout
= new QVBoxLayout(this);
419 m_topLayout
->setMargin(0);
420 m_topLayout
->addLayout(searchInputLayout
);
421 m_topLayout
->addWidget(m_optionsScrollArea
);
422 m_topLayout
->addWidget(m_facetsWidget
);
426 // The searching should be started automatically after the user did not change
427 // the text within one second
428 m_startSearchTimer
= new QTimer(this);
429 m_startSearchTimer
->setSingleShot(true);
430 m_startSearchTimer
->setInterval(1000);
431 connect(m_startSearchTimer
, &QTimer::timeout
, this, &DolphinSearchBox::emitSearchRequest
);
433 updateFacetsToggleButton();
436 QUrl
DolphinSearchBox::balooUrlForSearching() const
439 const QString text
= m_searchInput
->text();
442 query
.addType("File");
443 query
.addType(m_facetsWidget
->facetType());
445 Baloo::Term
term(Baloo::Term::And
);
447 Baloo::Term ratingTerm
= m_facetsWidget
->ratingTerm();
448 if (ratingTerm
.isValid()) {
449 term
.addSubTerm(ratingTerm
);
452 if (m_contentButton
->isChecked()) {
453 query
.setSearchString(text
);
454 } else if (!text
.isEmpty()) {
455 term
.addSubTerm(Baloo::Term(QLatin1String("filename"), text
));
458 if (m_fromHereButton
->isChecked()) {
459 query
.setIncludeFolder(m_searchPath
.toLocalFile());
464 return query
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
465 "Query Results from '%1'", text
));
471 void DolphinSearchBox::fromBalooSearchUrl(const QUrl
& url
)
474 const Baloo::Query query
= Baloo::Query::fromSearchUrl(url
);
475 const Baloo::Term term
= query
.term();
477 // Block all signals to avoid unnecessary "searchRequest" signals
478 // while we adjust the search text and the facet widget.
481 const QString customDir
= query
.includeFolder();
482 if (!customDir
.isEmpty()) {
483 setSearchPath(QUrl::fromLocalFile(customDir
));
485 setSearchPath(QDir::homePath());
488 setText(query
.searchString());
490 QStringList types
= query
.types();
491 types
.removeOne("File"); // We are only interested in facet widget types
492 if (!types
.isEmpty()) {
493 m_facetsWidget
->setFacetType(types
.first());
496 foreach (const Baloo::Term
& subTerm
, term
.subTerms()) {
497 const QString property
= subTerm
.property();
499 if (property
== QLatin1String("filename")) {
500 setText(subTerm
.value().toString());
501 } else if (m_facetsWidget
->isRatingTerm(subTerm
)) {
502 m_facetsWidget
->setRatingTerm(subTerm
);
506 m_startSearchTimer
->stop();
513 void DolphinSearchBox::updateFacetsToggleButton()
515 const bool facetsIsVisible
= SearchSettings::showFacetsWidget();
516 m_facetsToggleButton
->setChecked(facetsIsVisible
? true : false);
517 m_facetsToggleButton
->setIcon(QIcon::fromTheme(facetsIsVisible
? "arrow-up-double" : "arrow-down-double"));
518 m_facetsToggleButton
->setText(facetsIsVisible
? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));