]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
7c0e97f86ca4f4f12a7e65fa6d0f267487094fe2
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 * **************************************************************************/
20 #include "dolphinfacetswidget.h"
22 #include <KLocalizedString>
24 #include <QButtonGroup>
27 #include <QHBoxLayout>
28 #include <QRadioButton>
30 DolphinFacetsWidget::DolphinFacetsWidget(QWidget
* parent
) :
46 m_threeOrMore(nullptr),
47 m_fourOrMore(nullptr),
50 QButtonGroup
* filetypeGroup
= new QButtonGroup(this);
51 m_anyType
= createRadioButton(i18nc("@option:check", "Any"), filetypeGroup
);
52 m_folders
= createRadioButton(i18nc("@option:check", "Folders"), filetypeGroup
);
53 m_documents
= createRadioButton(i18nc("@option:check", "Documents"), filetypeGroup
);
54 m_images
= createRadioButton(i18nc("@option:check", "Images"), filetypeGroup
);
55 m_audio
= createRadioButton(i18nc("@option:check", "Audio Files"), filetypeGroup
);
56 m_videos
= createRadioButton(i18nc("@option:check", "Videos"), filetypeGroup
);
58 QVBoxLayout
* typeLayout
= new QVBoxLayout();
59 typeLayout
->setSpacing(0);
60 typeLayout
->addWidget(m_anyType
);
61 typeLayout
->addWidget(m_folders
);
62 typeLayout
->addWidget(m_documents
);
63 typeLayout
->addWidget(m_images
);
64 typeLayout
->addWidget(m_audio
);
65 typeLayout
->addWidget(m_videos
);
66 typeLayout
->addStretch();
68 QButtonGroup
* timespanGroup
= new QButtonGroup(this);
69 m_anytime
= createRadioButton(i18nc("@option:option", "Anytime"), timespanGroup
);
70 m_today
= createRadioButton(i18nc("@option:option", "Today"), timespanGroup
);
71 m_yesterday
= createRadioButton(i18nc("@option:option", "Yesterday"), timespanGroup
);
72 m_thisWeek
= createRadioButton(i18nc("@option:option", "This Week"), timespanGroup
);
73 m_thisMonth
= createRadioButton(i18nc("@option:option", "This Month"), timespanGroup
);
74 m_thisYear
= createRadioButton(i18nc("@option:option", "This Year"), timespanGroup
);
76 QVBoxLayout
* timespanLayout
= new QVBoxLayout();
77 timespanLayout
->setSpacing(0);
78 timespanLayout
->addWidget(m_anytime
);
79 timespanLayout
->addWidget(m_today
);
80 timespanLayout
->addWidget(m_yesterday
);
81 timespanLayout
->addWidget(m_thisWeek
);
82 timespanLayout
->addWidget(m_thisMonth
);
83 timespanLayout
->addWidget(m_thisYear
);
84 timespanLayout
->addStretch();
86 QButtonGroup
* ratingGroup
= new QButtonGroup(this);
87 m_anyRating
= createRadioButton(i18nc("@option:option", "Any Rating"), ratingGroup
);
88 m_oneOrMore
= createRadioButton(i18nc("@option:option", "1 or more"), ratingGroup
);
89 m_twoOrMore
= createRadioButton(i18nc("@option:option", "2 or more"), ratingGroup
);
90 m_threeOrMore
= createRadioButton(i18nc("@option:option", "3 or more"), ratingGroup
);
91 m_fourOrMore
= createRadioButton(i18nc("@option:option", "4 or more"), ratingGroup
);
92 m_maxRating
= createRadioButton(i18nc("@option:option", "Highest Rating"), ratingGroup
);
94 QVBoxLayout
* ratingLayout
= new QVBoxLayout();
95 ratingLayout
->setSpacing(0);
96 ratingLayout
->addWidget(m_anyRating
);
97 ratingLayout
->addWidget(m_oneOrMore
);
98 ratingLayout
->addWidget(m_twoOrMore
);
99 ratingLayout
->addWidget(m_threeOrMore
);
100 ratingLayout
->addWidget(m_fourOrMore
);
101 ratingLayout
->addWidget(m_maxRating
);
103 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
104 topLayout
->addLayout(typeLayout
);
105 topLayout
->addLayout(timespanLayout
);
106 topLayout
->addLayout(ratingLayout
);
107 topLayout
->addStretch();
109 m_anyType
->setChecked(true);
110 m_anytime
->setChecked(true);
111 m_anyRating
->setChecked(true);
114 DolphinFacetsWidget::~DolphinFacetsWidget()
118 QString
DolphinFacetsWidget::ratingTerm() const
122 if (!m_anyRating
->isChecked()) {
123 int stars
= 1; // represents m_oneOrMore
124 if (m_twoOrMore
->isChecked()) {
126 } else if (m_threeOrMore
->isChecked()) {
128 } else if (m_fourOrMore
->isChecked()) {
130 } else if (m_maxRating
->isChecked()) {
134 const int rating
= stars
* 2;
135 terms
<< QStringLiteral("rating>=%1").arg(rating
);
138 if (!m_anytime
->isChecked()) {
139 QDate date
= QDate::currentDate(); // represents m_today
140 if (m_yesterday
->isChecked()) {
141 date
= date
.addDays(-1);
142 } else if (m_thisWeek
->isChecked()) {
143 date
= date
.addDays(1 - date
.dayOfWeek());
144 } else if (m_thisMonth
->isChecked()) {
145 date
= date
.addDays(1 - date
.day());
146 } else if (m_thisYear
->isChecked()) {
147 date
= date
.addDays(1 - date
.dayOfYear());
150 terms
<< QStringLiteral("modified>=%1").arg(date
.toString(Qt::ISODate
));
153 return terms
.join(QStringLiteral(" AND "));
156 QString
DolphinFacetsWidget::facetType() const
158 if (m_folders
->isChecked()) {
159 return QStringLiteral("Folder");
160 } else if (m_documents
->isChecked()) {
161 return QStringLiteral("Document");
162 } else if (m_images
->isChecked()) {
163 return QStringLiteral("Image");
164 } else if (m_audio
->isChecked()) {
165 return QStringLiteral("Audio");
166 } else if (m_videos
->isChecked()) {
167 return QStringLiteral("Video");
173 bool DolphinFacetsWidget::isRatingTerm(const QString
& term
) const
175 const QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
177 // If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms.
178 bool containsRating
= false;
179 bool containsModified
= false;
181 foreach (const QString
& subTerm
, subTerms
) {
182 if (subTerm
.startsWith(QLatin1String("rating>="))) {
183 containsRating
= true;
184 } else if (subTerm
.startsWith(QLatin1String("modified>="))) {
185 containsModified
= true;
189 return containsModified
|| containsRating
;
192 void DolphinFacetsWidget::setRatingTerm(const QString
& term
)
194 // If term has sub terms, then the sub terms are always "rating" and "modified" terms.
195 // If term has no sub terms, then the term itself is either a "rating" term or a "modified"
196 // term. To avoid code duplication we add term to subTerms list, if the list is empty.
197 QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
199 foreach (const QString
& subTerm
, subTerms
) {
200 if (subTerm
.startsWith(QLatin1String("modified>="))) {
201 const QString value
= subTerm
.mid(10);
202 const QDate date
= QDate::fromString(value
, Qt::ISODate
);
204 } else if (subTerm
.startsWith(QLatin1String("rating>="))) {
205 const QString value
= subTerm
.mid(8);
206 const int stars
= value
.toInt() / 2;
212 void DolphinFacetsWidget::setFacetType(const QString
& type
)
214 if (type
== QLatin1String("Document")) {
215 m_documents
->setChecked(true);
216 } else if (type
== QLatin1String("Image")) {
217 m_images
->setChecked(true);
218 } else if (type
== QLatin1String("Audio")) {
219 m_audio
->setChecked(true);
220 } else if (type
== QLatin1String("Video")) {
221 m_videos
->setChecked(true);
223 m_anyType
->setChecked(true);
227 void DolphinFacetsWidget::setRating(const int stars
)
231 m_maxRating
->setChecked(true);
235 m_fourOrMore
->setChecked(true);
239 m_threeOrMore
->setChecked(true);
243 m_twoOrMore
->setChecked(true);
247 m_oneOrMore
->setChecked(true);
251 m_anyRating
->setChecked(true);
255 void DolphinFacetsWidget::setTimespan(const QDate
& date
)
257 const QDate currentDate
= QDate::currentDate();
258 const int days
= date
.daysTo(currentDate
);
261 m_today
->setChecked(true);
262 } else if (days
<= 1) {
263 m_yesterday
->setChecked(true);
264 } else if (days
<= currentDate
.dayOfWeek()) {
265 m_thisWeek
->setChecked(true);
266 } else if (days
<= currentDate
.day()) {
267 m_thisMonth
->setChecked(true);
268 } else if (days
<= currentDate
.dayOfYear()) {
269 m_thisYear
->setChecked(true);
271 m_anytime
->setChecked(true);
275 QRadioButton
* DolphinFacetsWidget::createRadioButton(const QString
& text
,
278 QRadioButton
* button
= new QRadioButton(text
);
279 connect(button
, &QRadioButton::clicked
, this, &DolphinFacetsWidget::facetChanged
);
280 group
->addButton(button
);