]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Reimplement search-filters
authorPeter Penz <peter.penz19@gmail.com>
Thu, 10 May 2012 20:40:51 +0000 (22:40 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 10 May 2012 20:45:12 +0000 (22:45 +0200)
BUG: 270721
BUG: 280377
FIXED-IN: 4.9.0

src/dolphinviewcontainer.cpp
src/dolphinviewcontainer.h
src/search/dolphinfacetswidget.cpp
src/search/dolphinfacetswidget.h
src/search/dolphinsearchbox.cpp
src/search/dolphinsearchbox.h

index 5f8b8f3b4504cfeddc50cddbd057a3f499a30923..51f2b4218f902ff12ad8f20b5d260dc1da3c45f8 100644 (file)
@@ -90,7 +90,7 @@ DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) :
     m_searchBox = new DolphinSearchBox(this);
     m_searchBox->hide();
     connect(m_searchBox, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
-    connect(m_searchBox, SIGNAL(search(QString)), this, SLOT(startSearching(QString)));
+    connect(m_searchBox, SIGNAL(searchRequest()), this, SLOT(startSearching()));
     connect(m_searchBox, SIGNAL(returnPressed(QString)), this, SLOT(requestFocus()));
 
     m_messageWidget = new KMessageWidget(this);
@@ -585,9 +585,8 @@ void DolphinViewContainer::slotHistoryChanged()
     }
 }
 
-void DolphinViewContainer::startSearching(const QString &text)
+void DolphinViewContainer::startSearching()
 {
-    Q_UNUSED(text);
     const KUrl url = m_searchBox->urlForSearching();
     if (url.isValid() && !url.isEmpty()) {
         m_urlNavigator->setLocationUrl(url);
index a1569ea8b99b014b4466fc6e86d6d88bfb9bf238..f0a0960d2d953c8ded729ff9ab0ccd8804f88b7e 100644 (file)
@@ -253,9 +253,8 @@ private slots:
 
     /**
      * Gets the search URL from the searchbox and starts searching.
-     * @param text Text the user has entered into the searchbox.
      */
-    void startSearching(const QString& text);
+    void startSearching();
     void closeSearchBox();
 
     /**
index 1a912af481b19a39debd8584c45081e429659109..678cb6b14189c41709064f60046d0987c00cb6c3 100644 (file)
 #include "dolphinfacetswidget.h"
 
 #include <KLocale>
+#include <QButtonGroup>
 #include <QCheckBox>
+#include <QDate>
 #include <QRadioButton>
 #include <QHBoxLayout>
 #include <QVBoxLayout>
 
+#ifdef HAVE_NEPOMUK
+    #include <Nepomuk/Query/AndTerm>
+    #include <Nepomuk/Query/ComparisonTerm>
+    #include <Nepomuk/Query/LiteralTerm>
+    #include <Nepomuk/Query/OrTerm>
+    #include <Nepomuk/Query/Query>
+    #include <Nepomuk/Query/ResourceTypeTerm>
+    #include <Nepomuk/Vocabulary/NFO>
+    #include <Nepomuk/Vocabulary/NIE>
+    #include <Soprano/Vocabulary/NAO>
+#endif
+
 DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
     QWidget(parent),
     m_documents(0),
@@ -44,10 +58,10 @@ DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
     m_fourOrMore(0),
     m_maxRating(0)
 {
-    m_documents = new QCheckBox(i18nc("@option:check", "Documents"));
-    m_images = new QCheckBox(i18nc("@option:check", "Images"));
-    m_audio = new QCheckBox(i18nc("@option:check", "Audio"));
-    m_videos = new QCheckBox(i18nc("@option:check", "Videos"));
+    m_documents = createCheckBox(i18nc("@option:check", "Documents"));
+    m_images    = createCheckBox(i18nc("@option:check", "Images"));
+    m_audio     = createCheckBox(i18nc("@option:check", "Audio"));
+    m_videos    = createCheckBox(i18nc("@option:check", "Videos"));
 
     QVBoxLayout* typeLayout = new QVBoxLayout();
     typeLayout->setSpacing(0);
@@ -57,12 +71,13 @@ DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
     typeLayout->addWidget(m_videos);
     typeLayout->addStretch();
 
-    m_anytime = new QRadioButton(i18nc("@option:option", "Anytime"));
-    m_today = new QRadioButton(i18nc("@option:option", "Today"));
-    m_yesterday = new QRadioButton(i18nc("@option:option", "Yesterday"));
-    m_thisWeek = new QRadioButton(i18nc("@option:option", "This Week"));
-    m_thisMonth = new QRadioButton(i18nc("@option:option", "This Month"));
-    m_thisYear = new QRadioButton(i18nc("@option:option", "This Year"));
+    QButtonGroup* timespanGroup = new QButtonGroup(this);
+    m_anytime   = createRadioButton(i18nc("@option:option", "Anytime"), timespanGroup);
+    m_today     = createRadioButton(i18nc("@option:option", "Today"), timespanGroup);
+    m_yesterday = createRadioButton(i18nc("@option:option", "Yesterday"), timespanGroup);
+    m_thisWeek  = createRadioButton(i18nc("@option:option", "This Week"), timespanGroup);
+    m_thisMonth = createRadioButton(i18nc("@option:option", "This Month"), timespanGroup);
+    m_thisYear  = createRadioButton(i18nc("@option:option", "This Year"), timespanGroup);
 
     QVBoxLayout* timespanLayout = new QVBoxLayout();
     timespanLayout->setSpacing(0);
@@ -74,12 +89,13 @@ DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
     timespanLayout->addWidget(m_thisYear);
     timespanLayout->addStretch();
 
-    m_anyRating = new QRadioButton(i18nc("@option:option", "Any Rating"));
-    m_oneOrMore = new QRadioButton(i18nc("@option:option", "1 or more"));
-    m_twoOrMore = new QRadioButton(i18nc("@option:option", "2 or more"));
-    m_threeOrMore = new QRadioButton(i18nc("@option:option", "3 or more"));
-    m_fourOrMore = new QRadioButton(i18nc("@option:option", "4 or more"));
-    m_maxRating = new QRadioButton(i18nc("@option:option", "Maximum Rating"));
+    QButtonGroup* ratingGroup = new QButtonGroup(this);
+    m_anyRating   = createRadioButton(i18nc("@option:option", "Any Rating"), ratingGroup);
+    m_oneOrMore   = createRadioButton(i18nc("@option:option", "1 or more"), ratingGroup);
+    m_twoOrMore   = createRadioButton(i18nc("@option:option", "2 or more"), ratingGroup);
+    m_threeOrMore = createRadioButton(i18nc("@option:option", "3 or more"), ratingGroup);
+    m_fourOrMore  = createRadioButton(i18nc("@option:option", "4 or more"), ratingGroup);
+    m_maxRating   = createRadioButton(i18nc("@option:option", "Maximum Rating"), ratingGroup);
 
     QVBoxLayout* ratingLayout = new QVBoxLayout();
     ratingLayout->setSpacing(0);
@@ -96,7 +112,6 @@ DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
     topLayout->addLayout(ratingLayout);
     topLayout->addStretch();
 
-    // TODO:
     m_anytime->setChecked(true);
     m_anyRating->setChecked(true);
 }
@@ -105,4 +120,94 @@ DolphinFacetsWidget::~DolphinFacetsWidget()
 {
 }
 
+#ifdef HAVE_NEPOMUK
+Nepomuk::Query::Term DolphinFacetsWidget::facetsTerm() const
+{
+    Nepomuk::Query::AndTerm andTerm;
+
+    const bool hasTypeFilter = m_documents->isChecked() ||
+                               m_images->isChecked() ||
+                               m_audio->isChecked() ||
+                               m_videos->isChecked();
+    if (hasTypeFilter) {
+        Nepomuk::Query::OrTerm orTerm;
+
+        if (m_documents->isChecked()) {
+            orTerm.addSubTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Document()));
+        }
+
+        if (m_images->isChecked()) {
+            orTerm.addSubTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
+        }
+
+        if (m_audio->isChecked()) {
+            orTerm.addSubTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
+                                                             Nepomuk::Query::LiteralTerm("audio")));
+        }
+
+        if (m_videos->isChecked()) {
+            orTerm.addSubTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
+                                                             Nepomuk::Query::LiteralTerm("video")));
+        }
+
+        andTerm.addSubTerm(orTerm);
+    }
+
+    if (!m_anyRating->isChecked()) {
+        int stars = 1; // represents m_oneOrMore
+        if (m_twoOrMore->isChecked()) {
+            stars = 2;
+        } else if (m_threeOrMore->isChecked()) {
+            stars = 3;
+        } else if (m_fourOrMore->isChecked()) {
+            stars = 4;
+        } else if (m_maxRating->isChecked()) {
+            stars = 5;
+        }
+
+        const int rating = stars * 2;
+        Nepomuk::Query::ComparisonTerm term(Soprano::Vocabulary::NAO::numericRating(),
+                                            Nepomuk::Query::LiteralTerm(rating),
+                                            Nepomuk::Query::ComparisonTerm::GreaterOrEqual);
+        andTerm.addSubTerm(term);
+    }
+
+    if (!m_anytime->isChecked()) {
+        QDate date = QDate::currentDate(); // represents m_today
+        if (m_yesterday->isChecked()) {
+            date.addDays(-1);
+        } else if (m_thisWeek->isChecked()) {
+            date.addDays(1 - date.dayOfWeek());
+        } else if (m_thisMonth->isChecked()) {
+            date.addDays(1 - date.day());
+        } else if (m_thisYear->isChecked()) {
+            date.addDays(1 - date.dayOfYear());
+        }
+
+        Nepomuk::Query::ComparisonTerm term(Nepomuk::Vocabulary::NIE::lastModified(),
+                                            Nepomuk::Query::LiteralTerm(QDateTime(date)),
+                                            Nepomuk::Query::ComparisonTerm::GreaterOrEqual);
+        andTerm.addSubTerm(term);
+    }
+
+    return andTerm;
+}
+#endif
+
+QCheckBox* DolphinFacetsWidget::createCheckBox(const QString& text)
+{
+    QCheckBox* checkBox = new QCheckBox(text);
+    connect(checkBox, SIGNAL(clicked()), this, SIGNAL(facetChanged()));
+    return checkBox;
+}
+
+QRadioButton* DolphinFacetsWidget::createRadioButton(const QString& text,
+                                                     QButtonGroup* group)
+{
+    QRadioButton* button = new QRadioButton(text);
+    connect(button, SIGNAL(clicked()), this, SIGNAL(facetChanged()));
+    group->addButton(button);
+    return button;
+}
+
 #include "dolphinfacetswidget.moc"
index 916e056081e98a44130a0937884084fec00813b0..65734aa66f7a66c6f0b68e1ee786ee8c99d1899d 100644 (file)
 
 #include <QWidget>
 
+#include <config-nepomuk.h>
+#ifdef HAVE_NEPOMUK
+    #include <Nepomuk/Query/Term>
+#endif
+
+class QButtonGroup;
 class QCheckBox;
 class QRadioButton;
 
@@ -29,7 +35,7 @@ class QRadioButton;
  * @brief Allows to filter search-queries by facets.
  *
  * TODO: The current implementation is a temporary
- * workaround for the 4.10 release and represents no
+ * workaround for the 4.9 release and represents no
  * real facets-implementation yet: There have been
  * some Dolphin specific user-interface and interaction
  * issues since 4.6 by embedding the Nepomuk facet-widget
@@ -49,6 +55,29 @@ public:
     explicit DolphinFacetsWidget(QWidget* parent = 0);
     virtual ~DolphinFacetsWidget();
 
+#ifdef HAVE_NEPOMUK
+    Nepomuk::Query::Term facetsTerm() const;
+#endif
+
+signals:
+    void facetChanged();
+
+private:
+    /**
+     * @return New checkbox which is connected to the
+     *         slotFacedChanged() slot whenever it has
+     *         been toggled.
+     */
+    QCheckBox* createCheckBox(const QString& text);
+
+    /**
+     * @return New radiobutton which is connected to the
+     *         slotFacedChanged() slot whenever it has
+     *         been toggled.
+     */
+    QRadioButton* createRadioButton(const QString& text,
+                                    QButtonGroup* group);
+
 private:
     QCheckBox* m_documents;
     QCheckBox* m_images;
index a9c960c24f4085f3979420f647a70232e18ca33c..4dee43cd54a9c469f56f77d95f887cf112011d87 100644 (file)
@@ -42,6 +42,7 @@
 
 #include <config-nepomuk.h>
 #ifdef HAVE_NEPOMUK
+    #include <Nepomuk/Query/AndTerm>
     #include <Nepomuk/Query/FileQuery>
     #include <Nepomuk/Query/LiteralTerm>
     #include <Nepomuk/Query/OrTerm>
@@ -198,40 +199,41 @@ void DolphinSearchBox::keyReleaseEvent(QKeyEvent* event)
     }
 }
 
-void DolphinSearchBox::emitSearchSignal()
+void DolphinSearchBox::emitSearchRequest()
 {
     m_startSearchTimer->stop();
     m_startedSearching = true;
-    emit search(m_searchInput->text());
+    emit searchRequest();
 }
 
-void DolphinSearchBox::slotSearchLocationChanged()
+void DolphinSearchBox::emitCloseRequest()
 {
-    emit searchLocationChanged(m_fromHereButton->isChecked() ? SearchFromHere : SearchEverywhere);
-}
-
-void DolphinSearchBox::slotSearchContextChanged()
-{
-    emit searchContextChanged(m_fileNameButton->isChecked() ? SearchFileName : SearchContent);
+    m_startSearchTimer->stop();
+    m_startedSearching = false;
+    emit closeRequest();
 }
 
 void DolphinSearchBox::slotConfigurationChanged()
 {
     saveSettings();
     if (m_startedSearching) {
-        emitSearchSignal();
+        emitSearchRequest();
     }
 }
 
 void DolphinSearchBox::slotSearchTextChanged(const QString& text)
 {
-    m_startSearchTimer->start();
+    if (text.isEmpty()) {
+        m_startSearchTimer->stop();
+    } else {
+        m_startSearchTimer->start();
+    }
     emit searchTextChanged(text);
 }
 
 void DolphinSearchBox::slotReturnPressed(const QString& text)
 {
-    emitSearchSignal();
+    emitSearchRequest();
     emit returnPressed(text);
 }
 
@@ -243,6 +245,13 @@ void DolphinSearchBox::slotFacetsButtonToggled()
     updateFacetsToggleButtonIcon();
 }
 
+void DolphinSearchBox::slotFacetChanged()
+{
+    m_startedSearching = true;
+    m_startSearchTimer->stop();
+    emit searchRequest();
+}
+
 void DolphinSearchBox::initButton(QToolButton* button)
 {
     button->setAutoExclusive(true);
@@ -282,7 +291,7 @@ void DolphinSearchBox::init()
     closeButton->setAutoRaise(true);
     closeButton->setIcon(KIcon("dialog-close"));
     closeButton->setToolTip(i18nc("@info:tooltip", "Quit searching"));
-    connect(closeButton, SIGNAL(clicked()), SIGNAL(closeRequest()));
+    connect(closeButton, SIGNAL(clicked()), this, SLOT(emitCloseRequest()));
 
     // Create search label
     m_searchLabel = new QLabel(this);
@@ -333,8 +342,6 @@ void DolphinSearchBox::init()
     QButtonGroup* searchLocationGroup = new QButtonGroup(this);
     searchLocationGroup->addButton(m_fromHereButton);
     searchLocationGroup->addButton(m_everywhereButton);
-    connect(m_fromHereButton, SIGNAL(clicked()), this, SLOT(slotSearchLocationChanged()));
-    connect(m_everywhereButton, SIGNAL(clicked()), this, SLOT(slotSearchLocationChanged()));
 
     // Create "Facets" widgets
     m_facetsToggleButton = new QToolButton(this);
@@ -343,6 +350,7 @@ void DolphinSearchBox::init()
 
     m_facetsWidget = new DolphinFacetsWidget(this);
     m_facetsWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+    connect(m_facetsWidget, SIGNAL(facetChanged()), this, SLOT(slotFacetChanged()));
 
     // Apply layout for the options
     QHBoxLayout* optionsLayout = new QHBoxLayout();
@@ -382,7 +390,7 @@ void DolphinSearchBox::init()
     m_startSearchTimer = new QTimer(this);
     m_startSearchTimer->setSingleShot(true);
     m_startSearchTimer->setInterval(1000);
-    connect(m_startSearchTimer, SIGNAL(timeout()), this, SLOT(emitSearchSignal()));
+    connect(m_startSearchTimer, SIGNAL(timeout()), this, SLOT(emitSearchRequest()));
 
     updateFacetsToggleButtonIcon();
     applyReadOnlyState();
@@ -391,29 +399,41 @@ void DolphinSearchBox::init()
 KUrl DolphinSearchBox::nepomukUrlForSearching() const
 {
 #ifdef HAVE_NEPOMUK
-    Nepomuk::Query::Term term;
-
+    // Create the term for the text from the input-field
+    // dependent on whether a searching for content or
+    // filename is done
     const QString text = m_searchInput->text();
-
+    Nepomuk::Query::Term searchLabelTerm;
     if (m_contentButton->isChecked()) {
         // Let Nepomuk parse the query
-        term = Nepomuk::Query::QueryParser::parseQuery(text, Nepomuk::Query::QueryParser::DetectFilenamePattern).term();
-    }
-    else {
+        searchLabelTerm = Nepomuk::Query::QueryParser::parseQuery(text, Nepomuk::Query::QueryParser::DetectFilenamePattern).term();
+    } else {
         // Search the text in the filename only
         QString regex = QRegExp::escape(text);
         regex.replace("\\*", QLatin1String(".*"));
         regex.replace("\\?", QLatin1String("."));
         regex.replace("\\", "\\\\");
-        term = Nepomuk::Query::ComparisonTerm(
-                    Nepomuk::Vocabulary::NFO::fileName(),
-                    Nepomuk::Query::LiteralTerm(regex),
-                    Nepomuk::Query::ComparisonTerm::Regexp);
+        searchLabelTerm = Nepomuk::Query::ComparisonTerm(
+                                Nepomuk::Vocabulary::NFO::fileName(),
+                                Nepomuk::Query::LiteralTerm(regex),
+                                Nepomuk::Query::ComparisonTerm::Regexp);
     }
 
+    // Get the term from the facets and merge it with the
+    // created term from the input-field.
+    Nepomuk::Query::Term facetsTerm = m_facetsWidget->facetsTerm();
+
     Nepomuk::Query::FileQuery fileQuery;
     fileQuery.setFileMode(Nepomuk::Query::FileQuery::QueryFilesAndFolders);
-    fileQuery.setTerm(term);
+    if (facetsTerm.isValid()) {
+        Nepomuk::Query::AndTerm andTerm;
+        andTerm.addSubTerm(searchLabelTerm);
+        andTerm.addSubTerm(facetsTerm);
+        fileQuery.setTerm(andTerm);
+    } else {
+        fileQuery.setTerm(searchLabelTerm);
+    }
+
     if (m_fromHereButton->isChecked()) {
         const bool recursive = true;
         fileQuery.addIncludeFolder(m_searchPath, recursive);
index 2b4632bb1aa7fc3f00000f0b95bb51b9fc1dbc17..ee9987a3899896e9d53301c74f8afa32c4b63abe 100644 (file)
@@ -46,16 +46,6 @@ class DolphinSearchBox : public QWidget {
     Q_OBJECT
 
 public:
-    enum SearchContext {
-        SearchFileName,
-        SearchContent
-    };
-
-    enum SearchLocation {
-        SearchFromHere,
-        SearchEverywhere
-    };
-
     explicit DolphinSearchBox(QWidget* parent = 0);
     virtual ~DolphinSearchBox();
 
@@ -104,11 +94,9 @@ protected:
 
 signals:
     /**
-     * Is emitted when a searching should be triggered
-     * and provides the text that should be used as input
-     * for searching.
+     * Is emitted when a searching should be triggered.
      */
-    void search(const QString& text);
+    void searchRequest();
 
     /**
      * Is emitted when the user has changed a character of
@@ -118,29 +106,19 @@ signals:
 
     void returnPressed(const QString& text);
 
-    /**
-     * Is emitted if the search location has been changed by the user.
-     */
-    void searchLocationChanged(SearchLocation location);
-
-    /**
-     * Is emitted if the search context has been changed by the user.
-     */
-    void searchContextChanged(SearchContext context);
-
     /**
      * Emitted as soon as the search box should get closed.
      */
     void closeRequest();
 
 private slots:
-    void emitSearchSignal();
-    void slotSearchLocationChanged();
-    void slotSearchContextChanged();
+    void emitSearchRequest();
+    void emitCloseRequest();
     void slotConfigurationChanged();
     void slotSearchTextChanged(const QString& text);
     void slotReturnPressed(const QString& text);
     void slotFacetsButtonToggled();
+    void slotFacetChanged();
 
 private:
     void initButton(QToolButton* button);