]>
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"
24 #include "dolphinsearchinformation.h"
31 #include <QButtonGroup>
34 #include <QFormLayout>
35 #include <QHBoxLayout>
38 #include <QScrollArea>
40 #include <QToolButton>
41 #include <QVBoxLayout>
43 #include <config-nepomuk.h>
45 #include <Nepomuk/Query/AndTerm>
46 #include <Nepomuk/Query/FileQuery>
47 #include <Nepomuk/Query/LiteralTerm>
48 #include <Nepomuk/Query/OrTerm>
49 #include <Nepomuk/Query/Query>
50 #include <Nepomuk/Query/QueryParser>
51 #include <Nepomuk/Query/ResourceTypeTerm>
52 #include <Nepomuk/Query/ComparisonTerm>
53 #include <Nepomuk/ResourceManager>
54 #include <Nepomuk/Vocabulary/NFO>
57 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
59 m_startedSearching(false),
64 m_optionsScrollArea(0),
69 m_everywhereButton(0),
70 m_facetsToggleButton(0),
78 DolphinSearchBox::~DolphinSearchBox()
83 void DolphinSearchBox::setText(const QString
& text
)
85 m_searchInput
->setText(text
);
88 QString
DolphinSearchBox::text() const
90 return m_searchInput
->text();
93 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
97 QFontMetrics
metrics(m_fromHereButton
->font());
98 const int maxWidth
= metrics
.height() * 8;
100 QString location
= url
.fileName();
101 if (location
.isEmpty()) {
102 if (url
.isLocalFile()) {
103 location
= QLatin1String("/");
105 location
= url
.protocol() + QLatin1String(" - ") + url
.host();
109 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
110 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
112 const bool showSearchFromButtons
= url
.isLocalFile() && !m_readOnly
;
113 m_separator
->setVisible(showSearchFromButtons
);
114 m_fromHereButton
->setVisible(showSearchFromButtons
);
115 m_everywhereButton
->setVisible(showSearchFromButtons
);
117 const DolphinSearchInformation
& searchInfo
= DolphinSearchInformation::instance();
118 const bool hasFacetsSupport
= searchInfo
.isIndexingEnabled() && searchInfo
.isPathIndexed(m_searchPath
);
119 m_facetsWidget
->setEnabled(hasFacetsSupport
);
122 KUrl
DolphinSearchBox::searchPath() const
127 KUrl
DolphinSearchBox::urlForSearching() const
130 const DolphinSearchInformation
& searchInfo
= DolphinSearchInformation::instance();
131 if (searchInfo
.isIndexingEnabled() && searchInfo
.isPathIndexed(m_searchPath
)) {
132 url
= nepomukUrlForSearching();
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
) {
163 m_readOnly
= readOnly
;
164 m_readOnlyQuery
= query
;
165 applyReadOnlyState();
169 bool DolphinSearchBox::isReadOnly() const
174 bool DolphinSearchBox::event(QEvent
* event
)
176 if (event
->type() == QEvent::Polish
) {
179 return QWidget::event(event
);
182 void DolphinSearchBox::showEvent(QShowEvent
* event
)
184 if (!event
->spontaneous()) {
185 m_searchInput
->setFocus();
186 m_startedSearching
= false;
190 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
192 QWidget::keyReleaseEvent(event
);
193 if (event
->key() == Qt::Key_Escape
) {
194 if (m_searchInput
->text().isEmpty()) {
197 m_searchInput
->clear();
202 void DolphinSearchBox::emitSearchRequest()
204 m_startSearchTimer
->stop();
205 m_startedSearching
= true;
206 emit
searchRequest();
209 void DolphinSearchBox::emitCloseRequest()
211 m_startSearchTimer
->stop();
212 m_startedSearching
= false;
216 void DolphinSearchBox::slotConfigurationChanged()
219 if (m_startedSearching
) {
224 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
226 if (text
.isEmpty()) {
227 m_startSearchTimer
->stop();
229 m_startSearchTimer
->start();
231 emit
searchTextChanged(text
);
234 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
237 emit
returnPressed(text
);
240 void DolphinSearchBox::slotFacetsButtonToggled()
242 const bool facetsIsVisible
= !m_facetsWidget
->isVisible();
243 m_facetsWidget
->setVisible(facetsIsVisible
);
244 updateFacetsToggleButton();
247 void DolphinSearchBox::slotFacetChanged()
249 m_startedSearching
= true;
250 m_startSearchTimer
->stop();
251 emit
searchRequest();
254 void DolphinSearchBox::initButton(QToolButton
* button
)
256 button
->setAutoExclusive(true);
257 button
->setAutoRaise(true);
258 button
->setCheckable(true);
259 connect(button
, SIGNAL(clicked(bool)), this, SLOT(slotConfigurationChanged()));
262 void DolphinSearchBox::loadSettings()
264 if (SearchSettings::location() == QLatin1String("Everywhere")) {
265 m_everywhereButton
->setChecked(true);
267 m_fromHereButton
->setChecked(true);
270 if (SearchSettings::what() == QLatin1String("Content")) {
271 m_contentButton
->setChecked(true);
273 m_fileNameButton
->setChecked(true);
276 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
279 void DolphinSearchBox::saveSettings()
281 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
282 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
283 SearchSettings::setShowFacetsWidget(m_facetsToggleButton
->isChecked());
284 SearchSettings::self()->writeConfig();
287 void DolphinSearchBox::init()
289 // Create close button
290 QToolButton
* closeButton
= new QToolButton(this);
291 closeButton
->setAutoRaise(true);
292 closeButton
->setIcon(KIcon("dialog-close"));
293 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
294 connect(closeButton
, SIGNAL(clicked()), this, SLOT(emitCloseRequest()));
296 // Create search label
297 m_searchLabel
= new QLabel(this);
300 m_searchInput
= new KLineEdit(this);
301 m_searchInput
->setClearButtonShown(true);
302 m_searchInput
->setFont(KGlobalSettings::generalFont());
303 setFocusProxy(m_searchInput
);
304 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
305 this, SLOT(slotReturnPressed(QString
)));
306 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
307 this, SLOT(slotSearchTextChanged(QString
)));
309 // Apply layout for the search input
310 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
311 searchInputLayout
->setMargin(0);
312 searchInputLayout
->addWidget(closeButton
);
313 searchInputLayout
->addWidget(m_searchLabel
);
314 searchInputLayout
->addWidget(m_searchInput
);
316 // Create "Filename" and "Content" button
317 m_fileNameButton
= new QToolButton(this);
318 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
319 initButton(m_fileNameButton
);
321 m_contentButton
= new QToolButton();
322 m_contentButton
->setText(i18nc("action:button", "Content"));
323 initButton(m_contentButton
);
325 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
326 searchWhatGroup
->addButton(m_fileNameButton
);
327 searchWhatGroup
->addButton(m_contentButton
);
329 m_separator
= new KSeparator(Qt::Vertical
, this);
331 // Create "From Here" and "Everywhere"button
332 m_fromHereButton
= new QToolButton(this);
333 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
334 initButton(m_fromHereButton
);
336 m_everywhereButton
= new QToolButton(this);
337 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
338 initButton(m_everywhereButton
);
340 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
341 searchLocationGroup
->addButton(m_fromHereButton
);
342 searchLocationGroup
->addButton(m_everywhereButton
);
344 // Create "Facets" widgets
345 m_facetsToggleButton
= new QToolButton(this);
346 m_facetsToggleButton
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
347 initButton(m_facetsToggleButton
);
348 connect(m_facetsToggleButton
, SIGNAL(clicked()), this, SLOT(slotFacetsButtonToggled()));
350 m_facetsWidget
= new DolphinFacetsWidget(this);
351 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
352 connect(m_facetsWidget
, SIGNAL(facetChanged()), this, SLOT(slotFacetChanged()));
354 // Apply layout for the options
355 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
356 optionsLayout
->setMargin(0);
357 optionsLayout
->addWidget(m_fileNameButton
);
358 optionsLayout
->addWidget(m_contentButton
);
359 optionsLayout
->addWidget(m_separator
);
360 optionsLayout
->addWidget(m_fromHereButton
);
361 optionsLayout
->addWidget(m_everywhereButton
);
362 optionsLayout
->addStretch(1);
363 optionsLayout
->addWidget(m_facetsToggleButton
);
365 // Put the options into a QScrollArea. This prevents increasing the view width
366 // in case that not enough width for the options is available.
367 QWidget
* optionsContainer
= new QWidget(this);
368 optionsContainer
->setLayout(optionsLayout
);
370 m_optionsScrollArea
= new QScrollArea(this);
371 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
372 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
373 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
374 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
375 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
376 m_optionsScrollArea
->setWidget(optionsContainer
);
377 m_optionsScrollArea
->setWidgetResizable(true);
379 m_topLayout
= new QVBoxLayout(this);
380 m_topLayout
->setMargin(0);
381 m_topLayout
->addLayout(searchInputLayout
);
382 m_topLayout
->addWidget(m_optionsScrollArea
);
383 m_topLayout
->addWidget(m_facetsWidget
);
387 // The searching should be started automatically after the user did not change
388 // the text within one second
389 m_startSearchTimer
= new QTimer(this);
390 m_startSearchTimer
->setSingleShot(true);
391 m_startSearchTimer
->setInterval(1000);
392 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchRequest()));
394 updateFacetsToggleButton();
395 applyReadOnlyState();
398 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
401 // Create the term for the text from the input-field
402 // dependent on whether a searching for content or
404 const QString text
= m_searchInput
->text();
405 Nepomuk::Query::Term searchLabelTerm
;
406 if (m_contentButton
->isChecked()) {
407 // Let Nepomuk parse the query
408 searchLabelTerm
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
).term();
410 // Search the text in the filename only
411 QString regex
= QRegExp::escape(text
);
412 regex
.replace("\\*", QLatin1String(".*"));
413 regex
.replace("\\?", QLatin1String("."));
414 regex
.replace("\\", "\\\\");
415 searchLabelTerm
= Nepomuk::Query::ComparisonTerm(
416 Nepomuk::Vocabulary::NFO::fileName(),
417 Nepomuk::Query::LiteralTerm(regex
),
418 Nepomuk::Query::ComparisonTerm::Regexp
);
421 // Get the term from the facets and merge it with the
422 // created term from the input-field.
423 Nepomuk::Query::Term facetsTerm
= m_facetsWidget
->facetsTerm();
425 Nepomuk::Query::FileQuery fileQuery
;
426 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFilesAndFolders
);
427 if (facetsTerm
.isValid()) {
428 Nepomuk::Query::AndTerm andTerm
;
429 andTerm
.addSubTerm(searchLabelTerm
);
430 andTerm
.addSubTerm(facetsTerm
);
431 fileQuery
.setTerm(andTerm
);
433 fileQuery
.setTerm(searchLabelTerm
);
436 if (m_fromHereButton
->isChecked()) {
437 const bool recursive
= true;
438 fileQuery
.addIncludeFolder(m_searchPath
, recursive
);
441 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
442 "Query Results from '%1'",
449 void DolphinSearchBox::applyReadOnlyState()
453 m_searchLabel
->setText(Nepomuk::Query::Query::titleFromQueryUrl(m_readOnlyQuery
));
458 m_searchLabel
->setText(i18nc("@label:textbox", "Find:"));
461 m_searchInput
->setVisible(!m_readOnly
);
462 m_optionsScrollArea
->setVisible(!m_readOnly
);
465 m_facetsWidget
->hide();
467 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
471 void DolphinSearchBox::updateFacetsToggleButton()
473 const bool facetsIsVisible
= SearchSettings::showFacetsWidget();
474 m_facetsToggleButton
->setChecked(facetsIsVisible
? true : false);
475 m_facetsToggleButton
->setIcon(KIcon(facetsIsVisible
? "arrow-up-double" : "arrow-down-double"));
476 m_facetsToggleButton
->setText(facetsIsVisible
? i18nc("action:button", "Less Options") : i18nc("action:button", "More Options"));
479 #include "dolphinsearchbox.moc"