Clean-up DolphinFacetsWidget

Summary:
Small refactor patch to clean-up some logic on setter and getter:
- Search terms are previously splitted and set separately, so no need
  for extra splitting and `foreach` loop
- Return search terms on a `QStringList` rather than join first

Depends on: D26029

Test Plan: No behavior changes

Reviewers: #dolphin, elvisangelaccio

Reviewed By: #dolphin, elvisangelaccio

Subscribers: kfm-devel

Tags: #dolphin

Differential Revision: https://phabricator.kde.org/D26150
This commit is contained in:
Ismael Asensio 2019-12-15 23:15:46 +01:00
parent 732697d0d6
commit c331d70685
3 changed files with 25 additions and 43 deletions

View file

@ -115,7 +115,7 @@ void DolphinFacetsWidget::resetSearchTerms()
updateTagsMenu(); updateTagsMenu();
} }
QString DolphinFacetsWidget::searchTerms() const QStringList DolphinFacetsWidget::searchTerms() const
{ {
QStringList terms; QStringList terms;
@ -135,7 +135,7 @@ QString DolphinFacetsWidget::searchTerms() const
} }
} }
return terms.join(QLatin1String(" AND ")); return terms;
} }
QString DolphinFacetsWidget::facetType() const QString DolphinFacetsWidget::facetType() const
@ -145,48 +145,34 @@ QString DolphinFacetsWidget::facetType() const
bool DolphinFacetsWidget::isSearchTerm(const QString& term) const bool DolphinFacetsWidget::isSearchTerm(const QString& term) const
{ {
const QStringList subTerms = term.split(' ', QString::SkipEmptyParts); static const QLatin1String searchTokens[] {
QLatin1String("modified>="),
QLatin1String("rating>="),
QLatin1String("tag:"), QLatin1String("tag=")
};
// If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms. for (const auto &searchToken : searchTokens) {
bool containsRating = false; if (term.startsWith(searchToken)) {
bool containsModified = false; return true;
bool containsTag = false;
foreach (const QString& subTerm, subTerms) {
if (subTerm.startsWith(QLatin1String("rating>="))) {
containsRating = true;
} else if (subTerm.startsWith(QLatin1String("modified>="))) {
containsModified = true;
} else if (subTerm.startsWith(QLatin1String("tag:")) ||
subTerm.startsWith(QLatin1String("tag="))) {
containsTag = true;
} }
} }
return false;
return containsModified || containsRating || containsTag;
} }
void DolphinFacetsWidget::setSearchTerm(const QString& term) void DolphinFacetsWidget::setSearchTerm(const QString& term)
{ {
// If term has sub terms, then the sub terms are always "rating" and "modified" terms. if (term.startsWith(QLatin1String("modified>="))) {
// If term has no sub terms, then the term itself is either a "rating" term or a "modified" const QString value = term.mid(10);
// term. To avoid code duplication we add term to subTerms list, if the list is empty. const QDate date = QDate::fromString(value, Qt::ISODate);
QStringList subTerms = term.split(' ', QString::SkipEmptyParts); setTimespan(date);
} else if (term.startsWith(QLatin1String("rating>="))) {
foreach (const QString& subTerm, subTerms) { const QString value = term.mid(8);
if (subTerm.startsWith(QLatin1String("modified>="))) { const int stars = value.toInt() / 2;
const QString value = subTerm.mid(10); setRating(stars);
const QDate date = QDate::fromString(value, Qt::ISODate); } else if (term.startsWith(QLatin1String("tag:")) ||
setTimespan(date); term.startsWith(QLatin1String("tag="))) {
} else if (subTerm.startsWith(QLatin1String("rating>="))) { const QString value = term.mid(4);
const QString value = subTerm.mid(8); addSearchTag(value);
const int stars = value.toInt() / 2;
setRating(stars);
} else if (subTerm.startsWith(QLatin1String("tag:")) ||
subTerm.startsWith(QLatin1String("tag="))) {
const QString value = subTerm.mid(4);
addSearchTag(value);
}
} }
} }

View file

@ -52,7 +52,7 @@ public:
explicit DolphinFacetsWidget(QWidget* parent = nullptr); explicit DolphinFacetsWidget(QWidget* parent = nullptr);
~DolphinFacetsWidget() override; ~DolphinFacetsWidget() override;
QString searchTerms() const; QStringList searchTerms() const;
QString facetType() const; QString facetType() const;
bool isSearchTerm(const QString& term) const; bool isSearchTerm(const QString& term) const;

View file

@ -477,11 +477,7 @@ QUrl DolphinSearchBox::balooUrlForSearching() const
Baloo::Query query; Baloo::Query query;
query.addType(m_facetsWidget->facetType()); query.addType(m_facetsWidget->facetType());
QStringList queryStrings; QStringList queryStrings = m_facetsWidget->searchTerms();
QString ratingQuery = m_facetsWidget->searchTerms();
if (!ratingQuery.isEmpty()) {
queryStrings << ratingQuery;
}
if (m_contentButton->isChecked()) { if (m_contentButton->isChecked()) {
queryStrings << text; queryStrings << text;