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