]>
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/FileQuery>
46 #include <Nepomuk/Query/LiteralTerm>
47 #include <Nepomuk/Query/OrTerm>
48 #include <Nepomuk/Query/Query>
49 #include <Nepomuk/Query/QueryParser>
50 #include <Nepomuk/Query/ResourceTypeTerm>
51 #include <Nepomuk/Query/ComparisonTerm>
52 #include <Nepomuk/ResourceManager>
53 #include <Nepomuk/Vocabulary/NFO>
56 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
58 m_startedSearching(false),
63 m_optionsScrollArea(0),
68 m_everywhereButton(0),
69 m_facetsToggleButton(0),
77 DolphinSearchBox::~DolphinSearchBox()
82 void DolphinSearchBox::setText(const QString
& text
)
84 m_searchInput
->setText(text
);
87 QString
DolphinSearchBox::text() const
89 return m_searchInput
->text();
92 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
96 QFontMetrics
metrics(m_fromHereButton
->font());
97 const int maxWidth
= metrics
.height() * 8;
99 QString location
= url
.fileName();
100 if (location
.isEmpty()) {
101 if (url
.isLocalFile()) {
102 location
= QLatin1String("/");
104 location
= url
.protocol() + QLatin1String(" - ") + url
.host();
108 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
109 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
111 const bool showSearchFromButtons
= url
.isLocalFile() && !m_readOnly
;
112 m_separator
->setVisible(showSearchFromButtons
);
113 m_fromHereButton
->setVisible(showSearchFromButtons
);
114 m_everywhereButton
->setVisible(showSearchFromButtons
);
116 const DolphinSearchInformation
& searchInfo
= DolphinSearchInformation::instance();
117 const bool hasFacetsSupport
= searchInfo
.isIndexingEnabled() && searchInfo
.isPathIndexed(m_searchPath
);
118 m_facetsWidget
->setEnabled(hasFacetsSupport
);
121 KUrl
DolphinSearchBox::searchPath() const
126 KUrl
DolphinSearchBox::urlForSearching() const
129 const DolphinSearchInformation
& searchInfo
= DolphinSearchInformation::instance();
130 if (searchInfo
.isIndexingEnabled() && searchInfo
.isPathIndexed(m_searchPath
)) {
131 url
= nepomukUrlForSearching();
133 url
.setProtocol("filenamesearch");
134 url
.addQueryItem("search", m_searchInput
->text());
135 if (m_contentButton
->isChecked()) {
136 url
.addQueryItem("checkContent", "yes");
140 if (m_everywhereButton
->isChecked()) {
141 // It is very unlikely, that the majority of Dolphins target users
142 // mean "the whole harddisk" instead of "my home folder" when
143 // selecting the "Everywhere" button.
144 encodedUrl
= QDir::homePath();
146 encodedUrl
= m_searchPath
.url();
148 url
.addQueryItem("url", encodedUrl
);
154 void DolphinSearchBox::selectAll()
156 m_searchInput
->selectAll();
159 void DolphinSearchBox::setReadOnly(bool readOnly
, const KUrl
& query
)
161 if (m_readOnly
!= readOnly
) {
162 m_readOnly
= readOnly
;
163 m_readOnlyQuery
= query
;
164 applyReadOnlyState();
168 bool DolphinSearchBox::isReadOnly() const
173 bool DolphinSearchBox::event(QEvent
* event
)
175 if (event
->type() == QEvent::Polish
) {
178 return QWidget::event(event
);
181 void DolphinSearchBox::showEvent(QShowEvent
* event
)
183 if (!event
->spontaneous()) {
184 m_searchInput
->setFocus();
185 m_startedSearching
= false;
189 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
191 QWidget::keyReleaseEvent(event
);
192 if (event
->key() == Qt::Key_Escape
) {
193 if (m_searchInput
->text().isEmpty()) {
196 m_searchInput
->clear();
201 void DolphinSearchBox::emitSearchSignal()
203 m_startSearchTimer
->stop();
204 m_startedSearching
= true;
205 emit
search(m_searchInput
->text());
208 void DolphinSearchBox::slotSearchLocationChanged()
210 emit
searchLocationChanged(m_fromHereButton
->isChecked() ? SearchFromHere
: SearchEverywhere
);
213 void DolphinSearchBox::slotSearchContextChanged()
215 emit
searchContextChanged(m_fileNameButton
->isChecked() ? SearchFileName
: SearchContent
);
218 void DolphinSearchBox::slotConfigurationChanged()
221 if (m_startedSearching
) {
226 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
228 m_startSearchTimer
->start();
229 emit
searchTextChanged(text
);
232 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
235 emit
returnPressed(text
);
238 void DolphinSearchBox::slotFacetsButtonToggled()
240 const bool visible
= !m_facetsWidget
->isVisible();
241 m_facetsWidget
->setVisible(visible
);
242 SearchSettings::setShowFacetsWidget(visible
);
243 updateFacetsToggleButtonIcon();
246 void DolphinSearchBox::initButton(QToolButton
* button
)
248 button
->setAutoExclusive(true);
249 button
->setAutoRaise(true);
250 button
->setCheckable(true);
251 connect(button
, SIGNAL(clicked(bool)), this, SLOT(slotConfigurationChanged()));
254 void DolphinSearchBox::loadSettings()
256 if (SearchSettings::location() == QLatin1String("Everywhere")) {
257 m_everywhereButton
->setChecked(true);
259 m_fromHereButton
->setChecked(true);
262 if (SearchSettings::what() == QLatin1String("Content")) {
263 m_contentButton
->setChecked(true);
265 m_fileNameButton
->setChecked(true);
268 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
271 void DolphinSearchBox::saveSettings()
273 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
274 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
275 SearchSettings::self()->writeConfig();
278 void DolphinSearchBox::init()
280 // Create close button
281 QToolButton
* closeButton
= new QToolButton(this);
282 closeButton
->setAutoRaise(true);
283 closeButton
->setIcon(KIcon("dialog-close"));
284 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
285 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
287 // Create search label
288 m_searchLabel
= new QLabel(this);
291 m_searchInput
= new KLineEdit(this);
292 m_searchInput
->setClearButtonShown(true);
293 m_searchInput
->setFont(KGlobalSettings::generalFont());
294 setFocusProxy(m_searchInput
);
295 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
296 this, SLOT(slotReturnPressed(QString
)));
297 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
298 this, SLOT(slotSearchTextChanged(QString
)));
300 // Apply layout for the search input
301 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
302 searchInputLayout
->setMargin(0);
303 searchInputLayout
->addWidget(closeButton
);
304 searchInputLayout
->addWidget(m_searchLabel
);
305 searchInputLayout
->addWidget(m_searchInput
);
307 // Create "Filename" and "Content" button
308 m_fileNameButton
= new QToolButton(this);
309 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
310 initButton(m_fileNameButton
);
312 m_contentButton
= new QToolButton();
313 m_contentButton
->setText(i18nc("action:button", "Content"));
314 initButton(m_contentButton
);;
316 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
317 searchWhatGroup
->addButton(m_fileNameButton
);
318 searchWhatGroup
->addButton(m_contentButton
);
319 connect(m_fileNameButton
, SIGNAL(clicked()), this, SLOT(slotSearchContextChanged()));
320 connect(m_contentButton
, SIGNAL(clicked()), this, SLOT(slotSearchContextChanged()));
322 m_separator
= new KSeparator(Qt::Vertical
, this);
324 // Create "From Here" and "Everywhere"button
325 m_fromHereButton
= new QToolButton(this);
326 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
327 initButton(m_fromHereButton
);
329 m_everywhereButton
= new QToolButton(this);
330 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
331 initButton(m_everywhereButton
);
333 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
334 searchLocationGroup
->addButton(m_fromHereButton
);
335 searchLocationGroup
->addButton(m_everywhereButton
);
336 connect(m_fromHereButton
, SIGNAL(clicked()), this, SLOT(slotSearchLocationChanged()));
337 connect(m_everywhereButton
, SIGNAL(clicked()), this, SLOT(slotSearchLocationChanged()));
339 // Create "Facets" widgets
340 m_facetsToggleButton
= new QToolButton(this);
341 m_facetsToggleButton
->setAutoRaise(true);
342 connect(m_facetsToggleButton
, SIGNAL(clicked()), this, SLOT(slotFacetsButtonToggled()));
344 m_facetsWidget
= new DolphinFacetsWidget(this);
345 m_facetsWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
347 // Apply layout for the options
348 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
349 optionsLayout
->setMargin(0);
350 optionsLayout
->addWidget(m_fileNameButton
);
351 optionsLayout
->addWidget(m_contentButton
);
352 optionsLayout
->addWidget(m_separator
);
353 optionsLayout
->addWidget(m_fromHereButton
);
354 optionsLayout
->addWidget(m_everywhereButton
);
355 optionsLayout
->addStretch(1);
356 optionsLayout
->addWidget(m_facetsToggleButton
);
358 // Put the options into a QScrollArea. This prevents increasing the view width
359 // in case that not enough width for the options is available.
360 QWidget
* optionsContainer
= new QWidget(this);
361 optionsContainer
->setLayout(optionsLayout
);
363 m_optionsScrollArea
= new QScrollArea(this);
364 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
365 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
366 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
367 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
368 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
369 m_optionsScrollArea
->setWidget(optionsContainer
);
370 m_optionsScrollArea
->setWidgetResizable(true);
372 m_topLayout
= new QVBoxLayout(this);
373 m_topLayout
->setMargin(0);
374 m_topLayout
->addLayout(searchInputLayout
);
375 m_topLayout
->addWidget(m_optionsScrollArea
);
376 m_topLayout
->addWidget(m_facetsWidget
);
380 // The searching should be started automatically after the user did not change
381 // the text within one second
382 m_startSearchTimer
= new QTimer(this);
383 m_startSearchTimer
->setSingleShot(true);
384 m_startSearchTimer
->setInterval(1000);
385 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
387 updateFacetsToggleButtonIcon();
388 applyReadOnlyState();
391 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
394 Nepomuk::Query::Term term
;
396 const QString text
= m_searchInput
->text();
398 if (m_contentButton
->isChecked()) {
399 // Let Nepomuk parse the query
400 term
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
).term();
403 // Search the text in the filename only
404 QString regex
= QRegExp::escape(text
);
405 regex
.replace("\\*", QLatin1String(".*"));
406 regex
.replace("\\?", QLatin1String("."));
407 regex
.replace("\\", "\\\\");
408 term
= Nepomuk::Query::ComparisonTerm(
409 Nepomuk::Vocabulary::NFO::fileName(),
410 Nepomuk::Query::LiteralTerm(regex
),
411 Nepomuk::Query::ComparisonTerm::Regexp
);
414 Nepomuk::Query::FileQuery fileQuery
;
415 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFilesAndFolders
);
416 fileQuery
.setTerm(term
);
417 if (m_fromHereButton
->isChecked()) {
418 const bool recursive
= true;
419 fileQuery
.addIncludeFolder(m_searchPath
, recursive
);
422 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
423 "Query Results from '%1'",
430 void DolphinSearchBox::applyReadOnlyState()
434 m_searchLabel
->setText(Nepomuk::Query::Query::titleFromQueryUrl(m_readOnlyQuery
));
439 m_searchLabel
->setText(i18nc("@label:textbox", "Find:"));
442 m_searchInput
->setVisible(!m_readOnly
);
443 m_optionsScrollArea
->setVisible(!m_readOnly
);
446 m_facetsWidget
->hide();
448 m_facetsWidget
->setVisible(SearchSettings::showFacetsWidget());
452 void DolphinSearchBox::updateFacetsToggleButtonIcon()
454 const bool visible
= SearchSettings::showFacetsWidget();
455 m_facetsToggleButton
->setIcon(KIcon(visible
? "list-remove" : "list-add"));
458 #include "dolphinsearchbox.moc"