]>
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"
28 #include <kseparator.h>
30 #include <QButtonGroup>
33 #include <QFormLayout>
34 #include <QHBoxLayout>
37 #include <QPushButton>
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/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),
65 m_everywhereButton(0),
71 DolphinSearchBox::~DolphinSearchBox()
76 QString
DolphinSearchBox::text() const
78 return m_searchInput
->text();
81 void DolphinSearchBox::setSearchPath(const KUrl
& url
)
85 QFontMetrics
metrics(m_fromHereButton
->font());
86 const int maxWidth
= metrics
.averageCharWidth() * 15;
88 QString location
= url
.fileName();
89 if (location
.isEmpty()) {
90 if (url
.isLocalFile()) {
91 location
= QLatin1String("/");
93 location
= url
.protocol() + QLatin1String(" - ") + url
.host();
97 const QString elidedLocation
= metrics
.elidedText(location
, Qt::ElideMiddle
, maxWidth
);
98 m_fromHereButton
->setText(i18nc("action:button", "From Here (%1)", elidedLocation
));
100 const bool showSearchFromButtons
= url
.isLocalFile();
101 m_separator
->setVisible(showSearchFromButtons
);
102 m_fromHereButton
->setVisible(showSearchFromButtons
);
103 m_everywhereButton
->setVisible(showSearchFromButtons
);
106 KUrl
DolphinSearchBox::searchPath() const
111 KUrl
DolphinSearchBox::urlForSearching() const
114 const DolphinSearchInformation
& searchInfo
= DolphinSearchInformation::instance();
115 if (searchInfo
.isIndexingEnabled() && searchInfo
.isPathIndexed(url
)) {
116 url
= nepomukUrlForSearching();
118 url
.setProtocol("filenamesearch");
119 url
.addQueryItem("search", m_searchInput
->text());
120 if (m_contentButton
->isChecked()) {
121 url
.addQueryItem("checkContent", "yes");
125 if (m_everywhereButton
->isChecked()) {
126 // It is very unlikely, that the majority of Dolphins target users
127 // mean "the whole harddisk" instead of "my home folder" when
128 // selecting the "Everywhere" button.
129 encodedUrl
= QDir::homePath();
131 encodedUrl
= m_searchPath
.url();
133 url
.addQueryItem("url", encodedUrl
);
139 void DolphinSearchBox::selectAll()
141 m_searchInput
->selectAll();
144 void DolphinSearchBox::clearText()
146 m_searchInput
->clear();
149 bool DolphinSearchBox::event(QEvent
* event
)
151 if (event
->type() == QEvent::Polish
) {
154 return QWidget::event(event
);
157 void DolphinSearchBox::showEvent(QShowEvent
* event
)
159 if (!event
->spontaneous()) {
160 m_searchInput
->setFocus();
161 m_startedSearching
= false;
165 void DolphinSearchBox::keyReleaseEvent(QKeyEvent
* event
)
167 QWidget::keyReleaseEvent(event
);
168 if (event
->key() == Qt::Key_Escape
) {
169 if (m_searchInput
->text().isEmpty()) {
172 m_searchInput
->clear();
177 void DolphinSearchBox::emitSearchSignal()
179 m_startSearchTimer
->stop();
180 m_startedSearching
= true;
181 emit
search(m_searchInput
->text());
184 void DolphinSearchBox::slotConfigurationChanged()
186 if (m_startedSearching
) {
191 void DolphinSearchBox::slotSearchTextChanged(const QString
& text
)
193 if (text
.isEmpty()) {
194 m_startSearchTimer
->stop();
196 m_startSearchTimer
->start();
198 emit
searchTextChanged(text
);
201 void DolphinSearchBox::slotReturnPressed(const QString
& text
)
204 emit
returnPressed(text
);
207 void DolphinSearchBox::initButton(QPushButton
* button
)
209 button
->setAutoExclusive(true);
210 button
->setFlat(true);
211 button
->setCheckable(true);
212 connect(button
, SIGNAL(toggled(bool)), this, SLOT(slotConfigurationChanged()));
215 void DolphinSearchBox::loadSettings()
217 if (SearchSettings::location() == QLatin1String("Everywhere")) {
218 m_everywhereButton
->setChecked(true);
220 m_fromHereButton
->setChecked(true);
223 if (SearchSettings::what() == QLatin1String("Content")) {
224 m_contentButton
->setChecked(true);
226 m_fileNameButton
->setChecked(true);
230 void DolphinSearchBox::saveSettings()
232 SearchSettings::setLocation(m_fromHereButton
->isChecked() ? "FromHere" : "Everywhere");
233 SearchSettings::setWhat(m_fileNameButton
->isChecked() ? "FileName" : "Content");
234 SearchSettings::self()->writeConfig();
237 void DolphinSearchBox::init()
239 // Create close button
240 QToolButton
* closeButton
= new QToolButton(this);
241 closeButton
->setAutoRaise(true);
242 closeButton
->setIcon(KIcon("dialog-close"));
243 closeButton
->setToolTip(i18nc("@info:tooltip", "Quit searching"));
244 connect(closeButton
, SIGNAL(clicked()), SIGNAL(closeRequest()));
246 // Create search label
247 QLabel
* searchLabel
= new QLabel(i18nc("@label:textbox", "Find:"), this);
250 m_searchInput
= new KLineEdit(this);
251 m_searchInput
->setClearButtonShown(true);
252 m_searchInput
->setFont(KGlobalSettings::generalFont());
253 setFocusProxy(m_searchInput
);
254 connect(m_searchInput
, SIGNAL(returnPressed(QString
)),
255 this, SLOT(slotReturnPressed(QString
)));
256 connect(m_searchInput
, SIGNAL(textChanged(QString
)),
257 this, SLOT(slotSearchTextChanged(QString
)));
259 // Apply layout for the search input
260 QHBoxLayout
* searchInputLayout
= new QHBoxLayout();
261 searchInputLayout
->setMargin(0);
262 searchInputLayout
->addWidget(closeButton
);
263 searchInputLayout
->addWidget(searchLabel
);
264 searchInputLayout
->addWidget(m_searchInput
);
266 // Create "Filename" and "Content" button
267 m_fileNameButton
= new QPushButton(this);
268 m_fileNameButton
->setText(i18nc("action:button", "Filename"));
269 initButton(m_fileNameButton
);
271 m_contentButton
= new QPushButton();
272 m_contentButton
->setText(i18nc("action:button", "Content"));
273 initButton(m_contentButton
);;
275 QButtonGroup
* searchWhatGroup
= new QButtonGroup(this);
276 searchWhatGroup
->addButton(m_fileNameButton
);
277 searchWhatGroup
->addButton(m_contentButton
);
279 m_separator
= new KSeparator(Qt::Vertical
, this);
281 // Create "From Here" and "Everywhere"button
282 m_fromHereButton
= new QPushButton(this);
283 m_fromHereButton
->setText(i18nc("action:button", "From Here"));
284 initButton(m_fromHereButton
);
286 m_everywhereButton
= new QPushButton(this);
287 m_everywhereButton
->setText(i18nc("action:button", "Everywhere"));
288 initButton(m_everywhereButton
);
290 QButtonGroup
* searchLocationGroup
= new QButtonGroup(this);
291 searchLocationGroup
->addButton(m_fromHereButton
);
292 searchLocationGroup
->addButton(m_everywhereButton
);
294 // Apply layout for the options
295 QHBoxLayout
* optionsLayout
= new QHBoxLayout();
296 optionsLayout
->setMargin(0);
297 optionsLayout
->addWidget(m_fileNameButton
);
298 optionsLayout
->addWidget(m_contentButton
);
299 optionsLayout
->addWidget(m_separator
);
300 optionsLayout
->addWidget(m_fromHereButton
);
301 optionsLayout
->addWidget(m_everywhereButton
);
302 optionsLayout
->addStretch(1);
304 // Put the options into a QScrollArea. This prevents increasing the view width
305 // in case that not enough width for the options is available.
306 QWidget
* optionsContainer
= new QWidget(this);
307 optionsContainer
->setLayout(optionsLayout
);
308 QScrollArea
* optionsScrollArea
= new QScrollArea(this);
309 optionsScrollArea
->setFrameShape(QFrame::NoFrame
);
310 optionsScrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
311 optionsScrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
312 optionsScrollArea
->setMaximumHeight(optionsContainer
->sizeHint().height());
313 optionsScrollArea
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
314 optionsScrollArea
->setWidget(optionsContainer
);
315 optionsScrollArea
->setWidgetResizable(true);
317 m_topLayout
= new QVBoxLayout(this);
318 m_topLayout
->addLayout(searchInputLayout
);
319 m_topLayout
->addWidget(optionsScrollArea
);
321 searchLabel
->setBuddy(m_searchInput
);
324 // The searching should be started automatically after the user did not change
325 // the text within one second
326 m_startSearchTimer
= new QTimer(this);
327 m_startSearchTimer
->setSingleShot(true);
328 m_startSearchTimer
->setInterval(1000);
329 connect(m_startSearchTimer
, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
332 KUrl
DolphinSearchBox::nepomukUrlForSearching() const
335 Nepomuk::Query::AndTerm andTerm
;
337 // Add input from search filter
338 const QString text
= m_searchInput
->text();
339 if (!text
.isEmpty()) {
340 if (m_fileNameButton
->isChecked()) {
341 QString regex
= QRegExp::escape(text
);
342 regex
.replace("\\*", QLatin1String(".*"));
343 regex
.replace("\\?", QLatin1String("."));
344 regex
.replace("\\", "\\\\");
345 andTerm
.addSubTerm(Nepomuk::Query::ComparisonTerm(
346 Nepomuk::Vocabulary::NFO::fileName(),
347 Nepomuk::Query::LiteralTerm(regex
),
348 Nepomuk::Query::ComparisonTerm::Regexp
));
350 const Nepomuk::Query::Query customQuery
= Nepomuk::Query::QueryParser::parseQuery(text
, Nepomuk::Query::QueryParser::DetectFilenamePattern
);
351 if (customQuery
.isValid()) {
352 andTerm
.addSubTerm(customQuery
.term());
357 Nepomuk::Query::FileQuery fileQuery
;
358 fileQuery
.setFileMode(Nepomuk::Query::FileQuery::QueryFilesAndFolders
);
359 fileQuery
.setTerm(andTerm
);
360 if (m_fromHereButton
->isChecked()) {
361 const bool recursive
= true;
362 fileQuery
.addIncludeFolder(m_searchPath
, recursive
);
365 return fileQuery
.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.",
366 "Query Results from '%1'",
373 #include "dolphinsearchbox.moc"