]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchbox.cpp
Merge remote-tracking branch 'origin/KDE/4.11'
[dolphin.git] / src / search / dolphinsearchbox.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 * **************************************************************************/
19
20 #include "dolphinsearchbox.h"
21
22 #include "dolphin_searchsettings.h"
23 #include "dolphinfacetswidget.h"
24 #include "dolphinsearchinformation.h"
25
26 #include <KIcon>
27 #include <KLineEdit>
28 #include <KLocale>
29 #include <KSeparator>
30
31 #include <QButtonGroup>
32 #include <QDir>
33 #include <QEvent>
34 #include <QFormLayout>
35 #include <QHBoxLayout>
36 #include <QKeyEvent>
37 #include <QLabel>
38 #include <QScrollArea>
39 #include <QTimer>
40 #include <QToolButton>
41 #include <QVBoxLayout>
42
43 #include <config-nepomuk.h>
44 #ifdef HAVE_NEPOMUK
45 #include <Nepomuk2/Query/AndTerm>
46 #include <Nepomuk2/Query/FileQuery>
47 #include <Nepomuk2/Query/LiteralTerm>
48 #include <Nepomuk2/Query/OrTerm>
49 #include <Nepomuk2/Query/Query>
50 #include <Nepomuk2/Query/QueryParser>
51 #include <Nepomuk2/Query/ResourceTypeTerm>
52 #include <Nepomuk2/Query/ComparisonTerm>
53 #include <Nepomuk2/ResourceManager>
54 #include <Nepomuk2/Vocabulary/NFO>
55 #endif
56
57 DolphinSearchBox::DolphinSearchBox(QWidget* parent) :
58 QWidget(parent),
59 m_startedSearching(false),
60 m_readOnly(false),
61 m_active(true),
62 m_topLayout(0),
63 m_searchLabel(0),
64 m_searchInput(0),
65 m_optionsScrollArea(0),
66 m_fileNameButton(0),
67 m_contentButton(0),
68 m_separator(0),
69 m_fromHereButton(0),
70 m_everywhereButton(0),
71 m_facetsToggleButton(0),
72 m_facetsWidget(0),
73 m_searchPath(),
74 m_readOnlyQuery(),
75 m_startSearchTimer(0)
76 {
77 }
78
79 DolphinSearchBox::~DolphinSearchBox()
80 {
81 saveSettings();
82 }
83
84 void DolphinSearchBox::setText(const QString& text)
85 {
86 m_searchInput->setText(text);
87 }
88
89 QString DolphinSearchBox::text() const
90 {
91 return m_searchInput->text();
92 }
93
94 void DolphinSearchBox::setSearchPath(const KUrl& url)
95 {
96 m_searchPath = url;
97
98 QFontMetrics metrics(m_fromHereButton->font());
99 const int maxWidth = metrics.height() * 8;
100
101 QString location = url.fileName();
102 if (location.isEmpty()) {
103 if (url.isLocalFile()) {
104 location = QLatin1String("/");
105 } else {
106 location = url.protocol() + QLatin1String(" - ") + url.host();
107 }
108 }
109
110 const QString elidedLocation = metrics.elidedText(location, Qt::ElideMiddle, maxWidth);
111 m_fromHereButton->setText(i18nc("action:button", "From Here (%1)", elidedLocation));
112
113 const bool showSearchFromButtons = url.isLocalFile() && !m_readOnly;
114 m_separator->setVisible(showSearchFromButtons);
115 m_fromHereButton->setVisible(showSearchFromButtons);
116 m_everywhereButton->setVisible(showSearchFromButtons);
117
118 const DolphinSearchInformation& searchInfo = DolphinSearchInformation::instance();
119 const bool hasFacetsSupport = searchInfo.isIndexingEnabled() && searchInfo.isPathIndexed(m_searchPath);
120 m_facetsWidget->setEnabled(hasFacetsSupport);
121 }
122
123 KUrl DolphinSearchBox::searchPath() const
124 {
125 return m_searchPath;
126 }
127
128 KUrl DolphinSearchBox::urlForSearching() const
129 {
130 KUrl url;
131 const DolphinSearchInformation& searchInfo = DolphinSearchInformation::instance();
132 if (searchInfo.isIndexingEnabled() && searchInfo.isPathIndexed(m_searchPath)) {
133 url = nepomukUrlForSearching();
134 } else {
135 url.setProtocol("filenamesearch");
136 url.addQueryItem("search", m_searchInput->text());
137 if (m_contentButton->isChecked()) {
138 url.addQueryItem("checkContent", "yes");
139 }
140
141 QString encodedUrl;
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();
147 } else {
148 encodedUrl = m_searchPath.url();
149 }
150 url.addQueryItem("url", encodedUrl);
151 }
152
153 return url;
154 }
155
156 void DolphinSearchBox::selectAll()
157 {
158 m_searchInput->selectAll();
159 }
160
161 void DolphinSearchBox::setReadOnly(bool readOnly, const KUrl& query)
162 {
163 if (m_readOnly != readOnly || m_readOnlyQuery != query) {
164 m_readOnly = readOnly;
165 m_readOnlyQuery = query;
166 applyReadOnlyState();
167 }
168 }
169
170 bool DolphinSearchBox::isReadOnly() const
171 {
172 return m_readOnly;
173 }
174
175 void DolphinSearchBox::setActive(bool active)
176 {
177 if (active != m_active) {
178 m_active = active;
179
180 if (active) {
181 emit activated();
182 }
183 }
184 }
185
186 bool DolphinSearchBox::isActive() const
187 {
188 return m_active;
189 }
190
191 bool DolphinSearchBox::event(QEvent* event)
192 {
193 if (event->type() == QEvent::Polish) {
194 init();
195 }
196 return QWidget::event(event);
197 }
198
199 void DolphinSearchBox::showEvent(QShowEvent* event)
200 {
201 if (!event->spontaneous()) {
202 m_searchInput->setFocus();
203 m_startedSearching = false;
204 }
205 }
206
207 void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
208 {
209 QWidget::keyReleaseEvent(event);
210 if (event->key() == Qt::Key_Escape) {
211 if (m_searchInput->text().isEmpty()) {
212 emit closeRequest();
213 } else {
214 m_searchInput->clear();
215 }
216 }
217 }
218
219 bool DolphinSearchBox::eventFilter(QObject* obj, QEvent* event)
220 {
221 switch (event->type()) {
222 case QEvent::FocusIn:
223 setActive(true);
224 setFocus();
225 break;
226
227 default:
228 break;
229 }
230
231 return QObject::eventFilter(obj, event);
232 }
233
234 void DolphinSearchBox::emitSearchRequest()
235 {
236 m_startSearchTimer->stop();
237 m_startedSearching = true;
238 emit searchRequest();
239 }
240
241 void DolphinSearchBox::emitCloseRequest()
242 {
243 m_startSearchTimer->stop();
244 m_startedSearching = false;
245 emit closeRequest();
246 }
247
248 void DolphinSearchBox::slotConfigurationChanged()
249 {
250 saveSettings();
251 if (m_startedSearching) {
252 emitSearchRequest();
253 }
254 }
255
256 void DolphinSearchBox::slotSearchTextChanged(const QString& text)
257 {
258 if (text.isEmpty()) {
259 m_startSearchTimer->stop();
260 } else {
261 m_startSearchTimer->start();
262 }
263 emit searchTextChanged(text);
264 }
265
266 void DolphinSearchBox::slotReturnPressed(const QString& text)
267 {
268 emitSearchRequest();
269 emit returnPressed(text);
270 }
271
272 void DolphinSearchBox::slotFacetsButtonToggled()
273 {
274 const bool facetsIsVisible = !m_facetsWidget->isVisible();
275 m_facetsWidget->setVisible(facetsIsVisible);
276 updateFacetsToggleButton();
277 }
278
279 void DolphinSearchBox::slotFacetChanged()
280 {
281 m_startedSearching = true;
282 m_startSearchTimer->stop();
283 emit searchRequest();
284 }
285
286 void DolphinSearchBox::initButton(QToolButton* button)
287 {
288 button->installEventFilter(this);
289 button->setAutoExclusive(true);
290 button->setAutoRaise(true);
291 button->setCheckable(true);
292 connect(button, SIGNAL(clicked(bool)), this, SLOT(slotConfigurationChanged()));
293 }
294
295 void DolphinSearchBox::loadSettings()
296 {
297 if (SearchSettings::location() == QLatin1String("Everywhere")) {
298 m_everywhereButton->setChecked(true);
299 } else {
300 m_fromHereButton->setChecked(true);
301 }
302
303 if (SearchSettings::what() == QLatin1String("Content")) {
304 m_contentButton->setChecked(true);
305 } else {
306 m_fileNameButton->setChecked(true);
307 }
308
309 m_facetsWidget->setVisible(SearchSettings::showFacetsWidget());
310 }
311
312 void DolphinSearchBox::saveSettings()
313 {
314 SearchSettings::setLocation(m_fromHereButton->isChecked() ? "FromHere" : "Everywhere");
315 SearchSettings::setWhat(m_fileNameButton->isChecked() ? "FileName" : "Content");
316 SearchSettings::setShowFacetsWidget(m_facetsToggleButton->isChecked());
317 SearchSettings::self()->writeConfig();
318 }
319
320 void DolphinSearchBox::init()
321 {
322 // Create close button
323 QToolButton* closeButton = new QToolButton(this);
324 closeButton->setAutoRaise(true);
325 closeButton->setIcon(KIcon("dialog-close"));
326 closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
327 connect(closeButton, SIGNAL(clicked()), this, SLOT(emitCloseRequest()));
328
329 // Create search label
330 m_searchLabel = new QLabel(this);
331
332 // Create search box
333 m_searchInput = new KLineEdit(this);
334 m_searchInput->installEventFilter(this);
335 m_searchInput->setClearButtonShown(true);
336 m_searchInput->setFont(KGlobalSettings::generalFont());
337 setFocusProxy(m_searchInput);
338 connect(m_searchInput, SIGNAL(returnPressed(QString)),
339 this, SLOT(slotReturnPressed(QString)));
340 connect(m_searchInput, SIGNAL(textChanged(QString)),
341 this, SLOT(slotSearchTextChanged(QString)));
342
343 // Apply layout for the search input
344 QHBoxLayout* searchInputLayout = new QHBoxLayout();
345 searchInputLayout->setMargin(0);
346 searchInputLayout->addWidget(closeButton);
347 searchInputLayout->addWidget(m_searchLabel);
348 searchInputLayout->addWidget(m_searchInput);
349
350 // Create "Filename" and "Content" button
351 m_fileNameButton = new QToolButton(this);
352 m_fileNameButton->setText(i18nc("action:button", "Filename"));
353 initButton(m_fileNameButton);
354
355 m_contentButton = new QToolButton();
356 m_contentButton->setText(i18nc("action:button", "Content"));
357 initButton(m_contentButton);
358
359 QButtonGroup* searchWhatGroup = new QButtonGroup(this);
360 searchWhatGroup->addButton(m_fileNameButton);
361 searchWhatGroup->addButton(m_contentButton);
362
363 m_separator = new KSeparator(Qt::Vertical, this);
364
365 // Create "From Here" and "Everywhere"button
366 m_fromHereButton = new QToolButton(this);
367 m_fromHereButton->setText(i18nc("action:button", "From Here"));
368 initButton(m_fromHereButton);
369
370 m_everywhereButton = new QToolButton(this);
371 m_everywhereButton->setText(i18nc("action:button", "Everywhere"));
372 initButton(m_everywhereButton);
373
374 QButtonGroup* searchLocationGroup = new QButtonGroup(this);
375 searchLocationGroup->addButton(m_fromHereButton);
376 searchLocationGroup->addButton(m_everywhereButton);
377
378 // Create "Facets" widgets
379 m_facetsToggleButton = new QToolButton(this);
380 m_facetsToggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
381 initButton(m_facetsToggleButton);
382 connect(m_facetsToggleButton, SIGNAL(clicked()), this, SLOT(slotFacetsButtonToggled()));
383
384 m_facetsWidget = new DolphinFacetsWidget(this);
385 m_facetsWidget->installEventFilter(this);
386 m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
387 connect(m_facetsWidget, SIGNAL(facetChanged()), this, SLOT(slotFacetChanged()));
388
389 // Apply layout for the options
390 QHBoxLayout* optionsLayout = new QHBoxLayout();
391 optionsLayout->setMargin(0);
392 optionsLayout->addWidget(m_fileNameButton);
393 optionsLayout->addWidget(m_contentButton);
394 optionsLayout->addWidget(m_separator);
395 optionsLayout->addWidget(m_fromHereButton);
396 optionsLayout->addWidget(m_everywhereButton);
397 optionsLayout->addStretch(1);
398 optionsLayout->addWidget(m_facetsToggleButton);
399
400 // Put the options into a QScrollArea. This prevents increasing the view width
401 // in case that not enough width for the options is available.
402 QWidget* optionsContainer = new QWidget(this);
403 optionsContainer->setLayout(optionsLayout);
404
405 m_optionsScrollArea = new QScrollArea(this);
406 m_optionsScrollArea->setFrameShape(QFrame::NoFrame);
407 m_optionsScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
408 m_optionsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
409 m_optionsScrollArea->setMaximumHeight(optionsContainer->sizeHint().height());
410 m_optionsScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
411 m_optionsScrollArea->setWidget(optionsContainer);
412 m_optionsScrollArea->setWidgetResizable(true);
413
414 m_topLayout = new QVBoxLayout(this);
415 m_topLayout->setMargin(0);
416 m_topLayout->addLayout(searchInputLayout);
417 m_topLayout->addWidget(m_optionsScrollArea);
418 m_topLayout->addWidget(m_facetsWidget);
419
420 loadSettings();
421
422 // The searching should be started automatically after the user did not change
423 // the text within one second
424 m_startSearchTimer = new QTimer(this);
425 m_startSearchTimer->setSingleShot(true);
426 m_startSearchTimer->setInterval(1000);
427 connect(m_startSearchTimer, SIGNAL(timeout()), this, SLOT(emitSearchRequest()));
428
429 updateFacetsToggleButton();
430 applyReadOnlyState();
431 }
432
433 KUrl DolphinSearchBox::nepomukUrlForSearching() const
434 {
435 #ifdef HAVE_NEPOMUK
436 // Create the term for the text from the input-field
437 // dependent on whether a searching for content or
438 // filename is done
439 const QString text = m_searchInput->text();
440 Nepomuk2::Query::Term searchLabelTerm;
441 if (m_contentButton->isChecked()) {
442 // Let Nepomuk parse the query
443 searchLabelTerm = Nepomuk2::Query::QueryParser::parseQuery(text, Nepomuk2::Query::QueryParser::DetectFilenamePattern).term();
444 } else {
445 // Search the text in the filename only
446 QString regex = QRegExp::escape(text);
447 regex.replace("\\*", QLatin1String(".*"));
448 regex.replace("\\?", QLatin1String("."));
449 regex.replace("\\", "\\\\");
450 searchLabelTerm = Nepomuk2::Query::ComparisonTerm(
451 Nepomuk2::Vocabulary::NFO::fileName(),
452 Nepomuk2::Query::LiteralTerm(regex),
453 Nepomuk2::Query::ComparisonTerm::Regexp);
454 }
455
456 // Get the term from the facets and merge it with the
457 // created term from the input-field.
458 Nepomuk2::Query::Term facetsTerm = m_facetsWidget->facetsTerm();
459
460 Nepomuk2::Query::FileQuery fileQuery;
461 fileQuery.setFileMode(Nepomuk2::Query::FileQuery::QueryFilesAndFolders);
462 if (facetsTerm.isValid()) {
463 Nepomuk2::Query::AndTerm andTerm;
464 andTerm.addSubTerm(searchLabelTerm);
465 andTerm.addSubTerm(facetsTerm);
466 fileQuery.setTerm(andTerm);
467 } else {
468 fileQuery.setTerm(searchLabelTerm);
469 }
470
471 if (m_fromHereButton->isChecked()) {
472 const bool recursive = true;
473 fileQuery.addIncludeFolder(m_searchPath, recursive);
474 }
475
476 return fileQuery.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
477 "Query Results from '%1'",
478 text));
479 #else
480 return KUrl();
481 #endif
482 }
483
484 void DolphinSearchBox::applyReadOnlyState()
485 {
486 #ifdef HAVE_NEPOMUK
487 if (m_readOnly) {
488 m_searchLabel->setText(Nepomuk2::Query::Query::titleFromQueryUrl(m_readOnlyQuery));
489 } else {
490 #else
491 {
492 #endif
493 m_searchLabel->setText(i18nc("@label:textbox", "Find:"));
494 }
495
496 m_searchInput->setVisible(!m_readOnly);
497 m_optionsScrollArea->setVisible(!m_readOnly);
498
499 if (m_readOnly) {
500 m_facetsWidget->hide();
501 } else {
502 m_facetsWidget->setVisible(SearchSettings::showFacetsWidget());
503 }
504 }
505
506 void DolphinSearchBox::updateFacetsToggleButton()
507 {
508 const bool facetsIsVisible = SearchSettings::showFacetsWidget();
509 m_facetsToggleButton->setChecked(facetsIsVisible ? true : false);
510 m_facetsToggleButton->setIcon(KIcon(facetsIsVisible ? "arrow-up-double" : "arrow-down-double"));
511 m_facetsToggleButton->setText(facetsIsVisible ? i18nc("action:button", "Fewer Options") : i18nc("action:button", "More Options"));
512 }
513
514 #include "dolphinsearchbox.moc"