]>
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"
30 #include <QButtonGroup>
33 #include <QFormLayout>
34 #include <QHBoxLayout>
37 #include <QScrollArea>
39 #include <QToolButton>
40 #include <QVBoxLayout>
42 #include <config-baloo.h>
44 #include <baloo/query.h>
45 #include <baloo/term.h>
46 #include <baloo/indexerconfig.h>
49 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
51 m_startedSearching(false),
57 m_optionsScrollArea(0),
62 m_everywhereButton(0),
63 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() && !m_readOnly
;
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::selectAll()
157 m_searchInput
->selectAll();
160 void DolphinSearchBox::setReadOnly(bool readOnly
, const KUrl
& query
)
162 if (m_readOnly
!= readOnly
|| m_readOnlyQuery
!= query
) {
163 m_readOnly
= readOnly
;
164 m_readOnlyQuery
= query
;
165 applyReadOnlyState();
169 bool DolphinSearchBox::isReadOnly() const
174 void DolphinSearchBox::setActive(bool active
)
176 if (active
!= m_active
) {
185 bool DolphinSearchBox::isActive() const
190 bool DolphinSearchBox::event(QEvent
* event
)
192 if (event
->type() == QEvent::Polish
) {
195 return QWidget::event(event
);
198 void DolphinSearchBox::showEvent(QShowEvent
* event
)
200 if (!event
->spontaneous()) {
201 m_searchInput
->setFocus();
202 m_startedSearching
= false;
206 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
208 QWidget::keyReleaseEvent(event
);
209 if (event
->key() == Qt::Key_Escape
) {
210 if (m_searchInput
->text().isEmpty()) {
213 m_searchInput
->clear();
218 bool DolphinSearchBox::eventFilter(QObject
* obj
, QEvent
* event
)
220 switch (event
->type()) {
221 case QEvent::FocusIn
:
230 return QObject::eventFilter(obj
, event
);
233 void DolphinSearchBox::emitSearchRequest()
235 m_startSearchTimer
->stop();
236 m_startedSearching
= true;
237 emit
searchRequest();
240 void DolphinSearchBox::emitCloseRequest()
242 m_startSearchTimer
->stop();
243 m_startedSearching
= false;
247 void DolphinSearchBox::slotConfigurationChanged()
250 if (m_startedSearching
) {
255 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
257 if (text
.isEmpty()) {
258 m_startSearchTimer
->stop();
260 m_startSearchTimer
->start();
262 emit
searchTextChanged(text
);
265 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
268 emit
returnPressed(text
);
271 void DolphinSearchBox::slotFacetsButtonToggled()
273 const bool facetsIsVisible
= !m_facetsWidget
->isVisible();
274 m_facetsWidget
->setVisible(facetsIsVisible
);
275 updateFacetsToggleButton();
278 void DolphinSearchBox::slotFacetChanged()
280 m_startedSearching
= true;
281 m_startSearchTimer
->stop();
282 emit
searchRequest();
285 void DolphinSearchBox::initButton(QToolButton
* button
)
287 button
->installEventFilter(this);
288 button
->setAutoExclusive(true);
289 button
->setAutoRaise(true);
290 button
->setCheckable(true);
291 connect(button
, SIGNAL(clicked(bool)), this, SLOT(slotConfigurationChanged()));
294 void DolphinSearchBox::loadSettings()
296 if (SearchSettings::location() == QLatin1String("Everywhere")) {
297 m_everywhereButton
->setChecked(true);
299 m_fromHereButton
->setChecked(true);
302 if (SearchSettings::what() == QLatin1String("Content")) {
303 m_contentButton
->setChecked(true);
305 m_fileNameButton
->setChecked(true);
308 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
311 void DolphinSearchBox::saveSettings()
313 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
314 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
315 SearchSettings::setShowFacetsWidget(m_facetsToggleButton
->isChecked());
316 SearchSettings::self()->writeConfig();
319 void DolphinSearchBox::init()
321 // Create close button
322 QToolButton
* closeButton
= new QToolButton(this);
323 closeButton
->setAutoRaise(true);
324 closeButton
->setIcon(KIcon("dialog-close"));
325 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
326 connect(closeButton
, SIGNAL(clicked()), this, SLOT(emitCloseRequest()));
328 // Create search label
329 m_searchLabel
= new QLabel(this);
332 m_searchInput
= new KLineEdit(this);
333 m_searchInput
->installEventFilter(this);
334 m_searchInput
->setClearButtonShown(true);
335 m_searchInput
->setFont(KGlobalSettings::generalFont());
336 setFocusProxy(m_searchInput
);
337 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
338 this, SLOT(slotReturnPressed(QString
)));
339 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
340 this, SLOT(slotSearchTextChanged(QString
)));
342 // Apply layout for the search input
343 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
344 searchInputLayout
->setMargin(0);
345 searchInputLayout
->addWidget(closeButton
);
346 searchInputLayout
->addWidget(m_searchLabel
);
347 searchInputLayout
->addWidget(m_searchInput
);
349 // Create "Filename" and "Content" button
350 m_fileNameButton
= new QToolButton(this);
351 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
352 initButton(m_fileNameButton
);
354 m_contentButton
= new QToolButton();
355 m_contentButton
->setText(i18nc("action:button", "Content"));
356 initButton(m_contentButton
);
358 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
359 searchWhatGroup
->addButton(m_fileNameButton
);
360 searchWhatGroup
->addButton(m_contentButton
);
362 m_separator
= new KSeparator(Qt::Vertical
, this);
364 // Create "From Here" and "Everywhere"button
365 m_fromHereButton
= new QToolButton(this);
366 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
367 initButton(m_fromHereButton
);
369 m_everywhereButton
= new QToolButton(this);
370 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
371 initButton(m_everywhereButton
);
373 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
374 searchLocationGroup
->addButton(m_fromHereButton
);
375 searchLocationGroup
->addButton(m_everywhereButton
);
377 // Create "Facets" widgets
378 m_facetsToggleButton
= new QToolButton(this);
379 m_facetsToggleButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
380 initButton(m_facetsToggleButton
);
381 connect(m_facetsToggleButton
, SIGNAL(clicked()), this, SLOT(slotFacetsButtonToggled()));
383 m_facetsWidget
= new DolphinFacetsWidget(this);
384 m_facetsWidget
->installEventFilter(this);
385 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
386 connect(m_facetsWidget
, SIGNAL(facetChanged()), this, SLOT(slotFacetChanged()));
388 // Apply layout for the options
389 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
390 optionsLayout
->setMargin(0);
391 optionsLayout
->addWidget(m_fileNameButton
);
392 optionsLayout
->addWidget(m_contentButton
);
393 optionsLayout
->addWidget(m_separator
);
394 optionsLayout
->addWidget(m_fromHereButton
);
395 optionsLayout
->addWidget(m_everywhereButton
);
396 optionsLayout
->addStretch(1);
397 optionsLayout
->addWidget(m_facetsToggleButton
);
399 // Put the options into a QScrollArea. This prevents increasing the view width
400 // in case that not enough width for the options is available.
401 QWidget
* optionsContainer
= new QWidget(this);
402 optionsContainer
->setLayout(optionsLayout
);
404 m_optionsScrollArea
= new QScrollArea(this);
405 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
406 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
407 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
408 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
409 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
410 m_optionsScrollArea
->setWidget(optionsContainer
);
411 m_optionsScrollArea
->setWidgetResizable(true);
413 m_topLayout
= new QVBoxLayout(this);
414 m_topLayout
->setMargin(0);
415 m_topLayout
->addLayout(searchInputLayout
);
416 m_topLayout
->addWidget(m_optionsScrollArea
);
417 m_topLayout
->addWidget(m_facetsWidget
);
421 // The searching should be started automatically after the user did not change
422 // the text within one second
423 m_startSearchTimer
= new QTimer(this);
424 m_startSearchTimer
->setSingleShot(true);
425 m_startSearchTimer
->setInterval(1000);
426 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchRequest()));
428 updateFacetsToggleButton();
429 applyReadOnlyState();
432 KUrl
DolphinSearchBox::balooUrlForSearching() const
435 const QString text
= m_searchInput
->text();
438 query
.addType("File");
439 query
.addTypes(m_facetsWidget
->facetTypes());
441 Baloo::Term
term(Baloo::Term::And
);
443 Baloo::Term ratingTerm
= m_facetsWidget
->ratingTerm();
444 if (ratingTerm
.isValid()) {
445 term
.addSubTerm(ratingTerm
);
448 if (m_contentButton
->isChecked()) {
449 query
.setSearchString(text
);
451 term
.addSubTerm(Baloo::Term(QLatin1String("filename"), text
));
454 if (m_fromHereButton
->isChecked()) {
455 query
.addCustomOption("includeFolder", m_searchPath
.toLocalFile());
460 return query
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
461 "Query Results from '%1'", text
));
467 void DolphinSearchBox::applyReadOnlyState()
471 m_searchLabel
->setText(Baloo::Query::titleFromQueryUrl(m_readOnlyQuery
));
476 m_searchLabel
->setText(i18nc("@label:textbox", "Find:"));
479 m_searchInput
->setVisible(!m_readOnly
);
480 m_optionsScrollArea
->setVisible(!m_readOnly
);
483 m_facetsWidget
->hide();
485 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
489 void DolphinSearchBox::updateFacetsToggleButton()
491 const bool facetsIsVisible
= SearchSettings::showFacetsWidget();
492 m_facetsToggleButton
->setChecked(facetsIsVisible
? true : false);
493 m_facetsToggleButton
->setIcon(KIcon(facetsIsVisible
? "arrow-up-double" : "arrow-down-double"));
494 m_facetsToggleButton
->setText(facetsIsVisible
? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));
497 #include "dolphinsearchbox.moc"