From e3578ee3b770a1b8f359ed1a3cc6e3ac7f6c700e Mon Sep 17 00:00:00 2001 From: Denis Steckelmacher Date: Wed, 10 Sep 2014 10:16:11 +0200 Subject: [PATCH] Use the Baloo Query Builder widget to add syntax-highlighting in Dolphin search REVIEW: 112589 --- src/CMakeLists.txt | 1 + src/search/dolphinsearchbox.cpp | 53 ++++++++++++++++++++++++--------- src/search/dolphinsearchbox.h | 17 +++++++++-- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fec365abf..3ad68c7f2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -106,6 +106,7 @@ if(HAVE_BALOO) KF5::FileMetaData KF5::BalooCore KF5::BalooFiles + KF5::BalooNaturalQueryParser KF5::BalooWidgets ) endif() diff --git a/src/search/dolphinsearchbox.cpp b/src/search/dolphinsearchbox.cpp index ae93030ac..27a251b7d 100644 --- a/src/search/dolphinsearchbox.cpp +++ b/src/search/dolphinsearchbox.cpp @@ -40,8 +40,9 @@ #include #include -#include #ifdef HAVE_BALOO + #include + #include #include #include #include @@ -252,8 +253,10 @@ void DolphinSearchBox::slotConfigurationChanged() } } -void DolphinSearchBox::slotSearchTextChanged(const QString& text) +void DolphinSearchBox::slotSearchTextChanged() { + const QString text = m_searchInput->text(); + if (text.isEmpty()) { m_startSearchTimer->stop(); } else { @@ -262,10 +265,17 @@ void DolphinSearchBox::slotSearchTextChanged(const QString& text) emit searchTextChanged(text); } -void DolphinSearchBox::slotReturnPressed(const QString& text) +void DolphinSearchBox::slotReturnPressed() { emitSearchRequest(); - emit returnPressed(text); + emit returnPressed(m_searchInput->text()); +} + +void DolphinSearchBox::updateSearchInputParsing() +{ +#ifdef HAVE_NEPOMUK + m_searchInput->setParsingEnabled(m_contentButton->isChecked()); +#endif } void DolphinSearchBox::slotFacetsButtonToggled() @@ -306,6 +316,7 @@ void DolphinSearchBox::loadSettings() } m_facetsWidget->setVisible(SearchSettings::showFacetsWidget()); + updateSearchInputParsing(); } void DolphinSearchBox::saveSettings() @@ -329,15 +340,24 @@ void DolphinSearchBox::init() m_searchLabel = new QLabel(this); // Create search box +#ifdef HAVE_BALOO + m_queryParser.reset(new Baloo::NaturalFileQueryParser); + m_searchInput = new Baloo::QueryBuilder(m_queryParser.data(), this); + connect(m_searchInput, &Baloo::QueryBuilder::editingFinished, + this, &DolphinSearchBox::slotReturnPressed); + connect(m_searchInput, &Baloo::QueryBuilder::textChanged, + this, &DolphinSearchBox::slotSearchTextChanged); +#else m_searchInput = new KLineEdit(this); m_searchInput->installEventFilter(this); m_searchInput->setClearButtonShown(true); m_searchInput->setFont(KGlobalSettings::generalFont()); - setFocusProxy(m_searchInput); - connect(m_searchInput, static_cast(&KLineEdit::returnPressed), + connect(m_searchInput, &KLineEdit::returnPressed, this, &DolphinSearchBox::slotReturnPressed); connect(m_searchInput, &KLineEdit::textChanged, this, &DolphinSearchBox::slotSearchTextChanged); +#endif + setFocusProxy(m_searchInput); // Apply layout for the search input QHBoxLayout* searchInputLayout = new QHBoxLayout(); @@ -358,6 +378,8 @@ void DolphinSearchBox::init() QButtonGroup* searchWhatGroup = new QButtonGroup(this); searchWhatGroup->addButton(m_fileNameButton); searchWhatGroup->addButton(m_contentButton); + connect(searchWhatGroup, static_cast(&QButtonGroup::buttonClicked), + this, &DolphinSearchBox::updateSearchInputParsing); m_separator = new KSeparator(Qt::Vertical, this); @@ -434,28 +456,31 @@ KUrl DolphinSearchBox::balooUrlForSearching() const const QString text = m_searchInput->text(); Baloo::Query query; + + if (m_contentButton->isChecked()) { + query = m_queryParser->parse(text, Baloo::NaturalQueryParser::DetectFilenamePattern); + } else { + query.setTerm(Baloo::Term(QLatin1String("filename"), text)); + } + + // Configure the query so that it returns files and takes the rating into account query.addType("File"); query.addType(m_facetsWidget->facetType()); Baloo::Term term(Baloo::Term::And); - Baloo::Term ratingTerm = m_facetsWidget->ratingTerm(); + if (ratingTerm.isValid()) { + term.addSubTerm(query.term()); term.addSubTerm(ratingTerm); - } - if (m_contentButton->isChecked()) { - query.setSearchString(text); - } else if (!text.isEmpty()) { - term.addSubTerm(Baloo::Term(QLatin1String("filename"), text)); + query.setTerm(term); } if (m_fromHereButton->isChecked()) { query.addCustomOption("includeFolder", m_searchPath.toLocalFile()); } - query.setTerm(term); - return query.toSearchUrl(i18nc("@title UDS_DISPLAY_NAME for a KIO directory listing. %1 is the query the user entered.", "Query Results from '%1'", text)); #else diff --git a/src/search/dolphinsearchbox.h b/src/search/dolphinsearchbox.h index 53b12ffab..eba81346d 100644 --- a/src/search/dolphinsearchbox.h +++ b/src/search/dolphinsearchbox.h @@ -24,6 +24,8 @@ #include #include +#include + class DolphinFacetsWidget; class KLineEdit; class KSeparator; @@ -32,6 +34,11 @@ class QScrollArea; class QLabel; class QVBoxLayout; +namespace Baloo { + class QueryBuilder; + class NaturalQueryParser; +} + /** * @brief Input box for searching files with or without Baloo. * @@ -134,10 +141,11 @@ private slots: void emitSearchRequest(); void emitCloseRequest(); void slotConfigurationChanged(); - void slotSearchTextChanged(const QString& text); - void slotReturnPressed(const QString& text); + void slotSearchTextChanged(); + void slotReturnPressed(); void slotFacetsButtonToggled(); void slotFacetChanged(); + void updateSearchInputParsing(); private: void initButton(QToolButton* button); @@ -164,7 +172,12 @@ private: QVBoxLayout* m_topLayout; QLabel* m_searchLabel; +#ifdef HAVE_BALOO + Baloo::QueryBuilder* m_searchInput; + QScopedPointer m_queryParser; +#else KLineEdit* m_searchInput; +#endif QScrollArea* m_optionsScrollArea; QToolButton* m_fileNameButton; QToolButton* m_contentButton; -- 2.47.3