]>
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 "dolphinsearchinformation.h"
30 #include <QButtonGroup>
33 #include <QFormLayout>
34 #include <QHBoxLayout>
37 #include <QScrollArea>
39 #include <QToolButton>
40 #include <QVBoxLayout>
42 #include <config-nepomuk.h>
44 #include <Nepomuk/Query/FileQuery>
45 #include <Nepomuk/Query/LiteralTerm>
46 #include <Nepomuk/Query/OrTerm>
47 #include <Nepomuk/Query/Query>
48 #include <Nepomuk/Query/QueryParser>
49 #include <Nepomuk/Query/ResourceTypeTerm>
50 #include <Nepomuk/Query/ComparisonTerm>
51 #include <Nepomuk/ResourceManager>
52 #include <Nepomuk/Vocabulary/NFO>
55 DolphinSearchBox::DolphinSearchBox(QWidget
* parent
) :
57 m_startedSearching(false),
62 m_optionsScrollArea(0),
67 m_everywhereButton(0),
74 DolphinSearchBox::~DolphinSearchBox()
79 void DolphinSearchBox::setText(const QString
& text
)
81 m_searchInput
->setText(text
);
84 QString
DolphinSearchBox::text() const
86 return m_searchInput
->text();
89 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
93 QFontMetrics
metrics(m_fromHereButton
->font());
94 const int maxWidth
= metrics
.height() * 8;
96 QString location
= url
.fileName();
97 if (location
.isEmpty()) {
98 if (url
.isLocalFile()) {
99 location
= QLatin1String("/");
101 location
= url
.protocol() + QLatin1String(" - ") + url
.host();
105 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
106 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
108 const bool showSearchFromButtons
= url
.isLocalFile() && !m_readOnly
;
109 m_separator
->setVisible(showSearchFromButtons
);
110 m_fromHereButton
->setVisible(showSearchFromButtons
);
111 m_everywhereButton
->setVisible(showSearchFromButtons
);
114 KUrl
DolphinSearchBox::searchPath() const
119 KUrl
DolphinSearchBox::urlForSearching() const
122 const DolphinSearchInformation
& searchInfo
= DolphinSearchInformation::instance();
123 if (searchInfo
.isIndexingEnabled() && searchInfo
.isPathIndexed(m_searchPath
)) {
124 url
= nepomukUrlForSearching();
126 url
.setProtocol("filenamesearch");
127 url
.addQueryItem("search", m_searchInput
->text());
128 if (m_contentButton
->isChecked()) {
129 url
.addQueryItem("checkContent", "yes");
133 if (m_everywhereButton
->isChecked()) {
134 // It is very unlikely, that the majority of Dolphins target users
135 // mean "the whole harddisk" instead of "my home folder" when
136 // selecting the "Everywhere" button.
137 encodedUrl
= QDir::homePath();
139 encodedUrl
= m_searchPath
.url();
141 url
.addQueryItem("url", encodedUrl
);
147 void DolphinSearchBox::selectAll()
149 m_searchInput
->selectAll();
152 void DolphinSearchBox::setReadOnly(bool readOnly
, const KUrl
& query
)
154 if (m_readOnly
!= readOnly
) {
155 m_readOnly
= readOnly
;
156 m_readOnlyQuery
= query
;
157 applyReadOnlyState();
161 bool DolphinSearchBox::isReadOnly() const
166 bool DolphinSearchBox::event(QEvent
* event
)
168 if (event
->type() == QEvent::Polish
) {
171 return QWidget::event(event
);
174 void DolphinSearchBox::showEvent(QShowEvent
* event
)
176 if (!event
->spontaneous()) {
177 m_searchInput
->setFocus();
178 m_startedSearching
= false;
182 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
184 QWidget::keyReleaseEvent(event
);
185 if (event
->key() == Qt::Key_Escape
) {
186 if (m_searchInput
->text().isEmpty()) {
189 m_searchInput
->clear();
194 void DolphinSearchBox::emitSearchSignal()
196 m_startSearchTimer
->stop();
197 m_startedSearching
= true;
198 emit
search(m_searchInput
->text());
201 void DolphinSearchBox::slotSearchLocationChanged()
203 emit
searchLocationChanged(m_fromHereButton
->isChecked() ? SearchFromHere
: SearchEverywhere
);
206 void DolphinSearchBox::slotSearchContextChanged()
208 emit
searchContextChanged(m_fileNameButton
->isChecked() ? SearchFileName
: SearchContent
);
211 void DolphinSearchBox::slotConfigurationChanged()
214 if (m_startedSearching
) {
219 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
221 m_startSearchTimer
->start();
222 emit
searchTextChanged(text
);
225 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
228 emit
returnPressed(text
);
231 void DolphinSearchBox::initButton(QToolButton
* button
)
233 button
->setAutoExclusive(true);
234 button
->setAutoRaise(true);
235 button
->setCheckable(true);
236 connect(button
, SIGNAL(clicked(bool)), this, SLOT(slotConfigurationChanged()));
239 void DolphinSearchBox::loadSettings()
241 if (SearchSettings::location() == QLatin1String("Everywhere")) {
242 m_everywhereButton
->setChecked(true);
244 m_fromHereButton
->setChecked(true);
247 if (SearchSettings::what() == QLatin1String("Content")) {
248 m_contentButton
->setChecked(true);
250 m_fileNameButton
->setChecked(true);
254 void DolphinSearchBox::saveSettings()
256 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
257 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
258 SearchSettings::self()->writeConfig();
261 void DolphinSearchBox::init()
263 // Create close button
264 QToolButton
* closeButton
= new QToolButton(this);
265 closeButton
->setAutoRaise(true);
266 closeButton
->setIcon(KIcon("dialog-close"));
267 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
268 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
270 // Create search label
271 m_searchLabel
= new QLabel(this);
274 m_searchInput
= new KLineEdit(this);
275 m_searchInput
->setClearButtonShown(true);
276 m_searchInput
->setFont(KGlobalSettings::generalFont());
277 setFocusProxy(m_searchInput
);
278 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
279 this, SLOT(slotReturnPressed(QString
)));
280 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
281 this, SLOT(slotSearchTextChanged(QString
)));
283 // Apply layout for the search input
284 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
285 searchInputLayout
->setMargin(0);
286 searchInputLayout
->addWidget(closeButton
);
287 searchInputLayout
->addWidget(m_searchLabel
);
288 searchInputLayout
->addWidget(m_searchInput
);
290 // Create "Filename" and "Content" button
291 m_fileNameButton
= new QToolButton(this);
292 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
293 initButton(m_fileNameButton
);
295 m_contentButton
= new QToolButton();
296 m_contentButton
->setText(i18nc("action:button", "Content"));
297 initButton(m_contentButton
);;
299 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
300 searchWhatGroup
->addButton(m_fileNameButton
);
301 searchWhatGroup
->addButton(m_contentButton
);
302 connect(m_fileNameButton
, SIGNAL(clicked()), this, SLOT(slotSearchContextChanged()));
303 connect(m_contentButton
, SIGNAL(clicked()), this, SLOT(slotSearchContextChanged()));
305 m_separator
= new KSeparator(Qt::Vertical
, this);
307 // Create "From Here" and "Everywhere"button
308 m_fromHereButton
= new QToolButton(this);
309 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
310 initButton(m_fromHereButton
);
312 m_everywhereButton
= new QToolButton(this);
313 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
314 initButton(m_everywhereButton
);
316 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
317 searchLocationGroup
->addButton(m_fromHereButton
);
318 searchLocationGroup
->addButton(m_everywhereButton
);
319 connect(m_fromHereButton
, SIGNAL(clicked()), this, SLOT(slotSearchLocationChanged()));
320 connect(m_everywhereButton
, SIGNAL(clicked()), this, SLOT(slotSearchLocationChanged()));
322 // Apply layout for the options
323 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
324 optionsLayout
->setMargin(0);
325 optionsLayout
->addWidget(m_fileNameButton
);
326 optionsLayout
->addWidget(m_contentButton
);
327 optionsLayout
->addWidget(m_separator
);
328 optionsLayout
->addWidget(m_fromHereButton
);
329 optionsLayout
->addWidget(m_everywhereButton
);
330 optionsLayout
->addStretch(1);
332 // Put the options into a QScrollArea. This prevents increasing the view width
333 // in case that not enough width for the options is available.
334 QWidget
* optionsContainer
= new QWidget(this);
335 optionsContainer
->setLayout(optionsLayout
);
337 m_optionsScrollArea
= new QScrollArea(this);
338 m_optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
339 m_optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
340 m_optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
341 m_optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
342 m_optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
343 m_optionsScrollArea
->setWidget(optionsContainer
);
344 m_optionsScrollArea
->setWidgetResizable(true);
346 m_topLayout
= new QVBoxLayout(this);
347 m_topLayout
->setMargin(0);
348 m_topLayout
->addLayout(searchInputLayout
);
349 m_topLayout
->addWidget(m_optionsScrollArea
);
353 // The searching should be started automatically after the user did not change
354 // the text within one second
355 m_startSearchTimer
= new QTimer(this);
356 m_startSearchTimer
->setSingleShot(true);
357 m_startSearchTimer
->setInterval(1000);
358 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
360 applyReadOnlyState();
363 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
366 Nepomuk::Query::Term term
;
368 const QString text
= m_searchInput
->text();
370 if (m_contentButton
->isChecked()) {
371 // Let Nepomuk parse the query
372 term
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
).term();
375 // Search the text in the filename only
376 QString regex
= QRegExp::escape(text
);
377 regex
.replace("\\*", QLatin1String(".*"));
378 regex
.replace("\\?", QLatin1String("."));
379 regex
.replace("\\", "\\\\");
380 term
= Nepomuk::Query::ComparisonTerm(
381 Nepomuk::Vocabulary::NFO::fileName(),
382 Nepomuk::Query::LiteralTerm(regex
),
383 Nepomuk::Query::ComparisonTerm::Regexp
);
386 Nepomuk::Query::FileQuery fileQuery
;
387 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFilesAndFolders
);
388 fileQuery
.setTerm(term
);
389 if (m_fromHereButton
->isChecked()) {
390 const bool recursive
= true;
391 fileQuery
.addIncludeFolder(m_searchPath
, recursive
);
394 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
395 "Query Results from '%1'",
402 void DolphinSearchBox::applyReadOnlyState()
406 m_searchLabel
->setText(Nepomuk::Query::Query::titleFromQueryUrl(m_readOnlyQuery
));
411 m_searchLabel
->setText(i18nc("@label:textbox", "Find:"));
414 m_searchInput
->setVisible(!m_readOnly
);
415 m_optionsScrollArea
->setVisible(!m_readOnly
);
418 #include "dolphinsearchbox.moc"