]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
1827192259dcd4e6afa1fbe6aff2db1eae594e94
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2019 by Ismael Asensio <isma.af@mgmail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 * **************************************************************************/
21 #include "dolphinfacetswidget.h"
23 #include <KLocalizedString>
28 #include <QHBoxLayout>
31 DolphinFacetsWidget::DolphinFacetsWidget(QWidget
* parent
) :
33 m_typeSelector(nullptr),
34 m_dateSelector(nullptr),
35 m_ratingSelector(nullptr)
37 m_typeSelector
= new QComboBox(this);
38 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("none")), i18nc("@item:inlistbox", "Any Type"), QString());
39 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("inode-directory")), i18nc("@item:inlistbox", "Folders") , QStringLiteral("Folder"));
40 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("text-x-generic")), i18nc("@item:inlistbox", "Documents") , QStringLiteral("Document"));
41 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("image-x-generic")), i18nc("@item:inlistbox", "Images") , QStringLiteral("Image"));
42 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("audio-x-generic")), i18nc("@item:inlistbox", "Audio Files"), QStringLiteral("Audio"));
43 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("video-x-generic")), i18nc("@item:inlistbox", "Videos") , QStringLiteral("Video"));
44 initComboBox(m_typeSelector
);
46 const QDate currentDate
= QDate::currentDate();
48 m_dateSelector
= new QComboBox(this);
49 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("view-calendar")), i18nc("@item:inlistbox", "Any Date"), QDate());
50 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Today") , currentDate
);
51 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Yesterday") , currentDate
.addDays(-1));
52 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-week")), i18nc("@item:inlistbox", "This Week") , currentDate
.addDays(1 - currentDate
.dayOfWeek()));
53 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-month")), i18nc("@item:inlistbox", "This Month"), currentDate
.addDays(1 - currentDate
.day()));
54 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-year")), i18nc("@item:inlistbox", "This Year") , currentDate
.addDays(1 - currentDate
.dayOfYear()));
55 initComboBox(m_dateSelector
);
57 m_ratingSelector
= new QComboBox(this);
58 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("non-starred-symbolic")), i18nc("@item:inlistbox", "Any Rating"), 0);
59 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "1 or more"), 1);
60 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "2 or more"), 2);
61 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "3 or more"), 3);
62 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "4 or more"), 4);
63 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "Highest Rating"), 5);
64 initComboBox(m_ratingSelector
);
66 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
67 topLayout
->setContentsMargins(0, 0, 0, 0);
68 topLayout
->addWidget(m_typeSelector
);
69 topLayout
->addWidget(m_dateSelector
);
70 topLayout
->addWidget(m_ratingSelector
);
75 DolphinFacetsWidget::~DolphinFacetsWidget()
79 void DolphinFacetsWidget::changeEvent(QEvent
*event
)
81 if (event
->type() == QEvent::EnabledChange
&& !isEnabled()) {
86 void DolphinFacetsWidget::resetOptions()
88 m_typeSelector
->setCurrentIndex(0);
89 m_dateSelector
->setCurrentIndex(0);
90 m_ratingSelector
->setCurrentIndex(0);
93 QString
DolphinFacetsWidget::ratingTerm() const
97 if (m_ratingSelector
->currentIndex() > 0) {
98 const int rating
= m_ratingSelector
->currentData().toInt() * 2;
99 terms
<< QStringLiteral("rating>=%1").arg(rating
);
102 if (m_dateSelector
->currentIndex() > 0) {
103 const QDate date
= m_dateSelector
->currentData().toDate();
104 terms
<< QStringLiteral("modified>=%1").arg(date
.toString(Qt::ISODate
));
107 return terms
.join(QLatin1String(" AND "));
110 QString
DolphinFacetsWidget::facetType() const
112 return m_typeSelector
->currentData().toString();
115 bool DolphinFacetsWidget::isRatingTerm(const QString
& term
) const
117 const QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
119 // If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms.
120 bool containsRating
= false;
121 bool containsModified
= false;
123 foreach (const QString
& subTerm
, subTerms
) {
124 if (subTerm
.startsWith(QLatin1String("rating>="))) {
125 containsRating
= true;
126 } else if (subTerm
.startsWith(QLatin1String("modified>="))) {
127 containsModified
= true;
131 return containsModified
|| containsRating
;
134 void DolphinFacetsWidget::setRatingTerm(const QString
& term
)
136 // If term has sub terms, then the sub terms are always "rating" and "modified" terms.
137 // If term has no sub terms, then the term itself is either a "rating" term or a "modified"
138 // term. To avoid code duplication we add term to subTerms list, if the list is empty.
139 QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
141 foreach (const QString
& subTerm
, subTerms
) {
142 if (subTerm
.startsWith(QLatin1String("modified>="))) {
143 const QString value
= subTerm
.mid(10);
144 const QDate date
= QDate::fromString(value
, Qt::ISODate
);
146 } else if (subTerm
.startsWith(QLatin1String("rating>="))) {
147 const QString value
= subTerm
.mid(8);
148 const int stars
= value
.toInt() / 2;
154 void DolphinFacetsWidget::setFacetType(const QString
& type
)
156 for (int index
= 1; index
<= m_typeSelector
->count(); index
++) {
157 if (type
== m_typeSelector
->itemData(index
).toString()) {
158 m_typeSelector
->setCurrentIndex(index
);
164 void DolphinFacetsWidget::setRating(const int stars
)
166 if (stars
< 0 || stars
> 5) {
169 m_ratingSelector
->setCurrentIndex(stars
);
172 void DolphinFacetsWidget::setTimespan(const QDate
& date
)
174 if (!date
.isValid()) {
177 m_dateSelector
->setCurrentIndex(0);
178 for (int index
= 1; index
<= m_dateSelector
->count(); index
++) {
179 if (date
>= m_dateSelector
->itemData(index
).toDate()) {
180 m_dateSelector
->setCurrentIndex(index
);
186 void DolphinFacetsWidget::initComboBox(QComboBox
* combo
)
188 combo
->setFrame(false);
189 combo
->setMinimumHeight(parentWidget()->height());
190 combo
->setCurrentIndex(0);
191 connect(combo
, QOverload
<int>::of(&QComboBox::activated
), this, &DolphinFacetsWidget::facetChanged
);