]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Use the Baloo Query Builder widget to add syntax-highlighting in Dolphin search
authorDenis Steckelmacher <steckdenis@yahoo.fr>
Wed, 10 Sep 2014 08:16:11 +0000 (10:16 +0200)
committerDenis Steckelmacher <steckdenis@yahoo.fr>
Wed, 10 Sep 2014 08:16:11 +0000 (10:16 +0200)
REVIEW: 112589

src/CMakeLists.txt
src/search/dolphinsearchbox.cpp
src/search/dolphinsearchbox.h

index fec365abfe3db622f25d4db396394afe202a7a81..3ad68c7f2e47c8f8de0668c7ed3ccf8eecf78408 100644 (file)
@@ -106,6 +106,7 @@ if(HAVE_BALOO)
         KF5::FileMetaData
         KF5::BalooCore
         KF5::BalooFiles
+        KF5::BalooNaturalQueryParser
         KF5::BalooWidgets
     )
 endif()
index ae93030acf7c8a322ee8447e725e7da4bd32c5e6..27a251b7d7629f62de179cb7e8de9d6f9d077f07 100644 (file)
@@ -40,8 +40,9 @@
 #include <QToolButton>
 #include <QVBoxLayout>
 
-#include <config-baloo.h>
 #ifdef HAVE_BALOO
+    #include <Baloo/NaturalFileQueryParser>
+    #include <Baloo/QueryBuilder>
     #include <Baloo/Query>
     #include <Baloo/Term>
     #include <Baloo/IndexerConfig>
@@ -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<void(KLineEdit::*)(const QString&)>(&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<void (QButtonGroup::*)(int)>(&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
index 53b12ffabbb8ff15d6143e6f5014cd2a8906758b..eba81346d5ceb8a8909bca4ff220f68c6793f593 100644 (file)
@@ -24,6 +24,8 @@
 #include <QList>
 #include <QWidget>
 
+#include <config-baloo.h>
+
 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<Baloo::NaturalQueryParser> m_queryParser;
+#else
     KLineEdit* m_searchInput;
+#endif
     QScrollArea* m_optionsScrollArea;
     QToolButton* m_fileNameButton;
     QToolButton* m_contentButton;