]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
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>
28 #include <QHBoxLayout>
29 #include <QRadioButton>
31 DolphinFacetsWidget::DolphinFacetsWidget(QWidget
* parent
) :
47 m_threeOrMore(nullptr),
48 m_fourOrMore(nullptr),
51 QButtonGroup
* filetypeGroup
= new QButtonGroup(this);
52 m_anyType
= createRadioButton(i18nc("@option:check", "Any"), filetypeGroup
);
53 m_folders
= createRadioButton(i18nc("@option:check", "Folders"), filetypeGroup
);
54 m_documents
= createRadioButton(i18nc("@option:check", "Documents"), filetypeGroup
);
55 m_images
= createRadioButton(i18nc("@option:check", "Images"), filetypeGroup
);
56 m_audio
= createRadioButton(i18nc("@option:check", "Audio Files"), filetypeGroup
);
57 m_videos
= createRadioButton(i18nc("@option:check", "Videos"), filetypeGroup
);
59 QVBoxLayout
* typeLayout
= new QVBoxLayout();
60 typeLayout
->setSpacing(0);
61 typeLayout
->addWidget(m_anyType
);
62 typeLayout
->addWidget(m_folders
);
63 typeLayout
->addWidget(m_documents
);
64 typeLayout
->addWidget(m_images
);
65 typeLayout
->addWidget(m_audio
);
66 typeLayout
->addWidget(m_videos
);
67 typeLayout
->addStretch();
69 QButtonGroup
* timespanGroup
= new QButtonGroup(this);
70 m_anytime
= createRadioButton(i18nc("@option:option", "Anytime"), timespanGroup
);
71 m_today
= createRadioButton(i18nc("@option:option", "Today"), timespanGroup
);
72 m_yesterday
= createRadioButton(i18nc("@option:option", "Yesterday"), timespanGroup
);
73 m_thisWeek
= createRadioButton(i18nc("@option:option", "This Week"), timespanGroup
);
74 m_thisMonth
= createRadioButton(i18nc("@option:option", "This Month"), timespanGroup
);
75 m_thisYear
= createRadioButton(i18nc("@option:option", "This Year"), timespanGroup
);
77 QVBoxLayout
* timespanLayout
= new QVBoxLayout();
78 timespanLayout
->setSpacing(0);
79 timespanLayout
->addWidget(m_anytime
);
80 timespanLayout
->addWidget(m_today
);
81 timespanLayout
->addWidget(m_yesterday
);
82 timespanLayout
->addWidget(m_thisWeek
);
83 timespanLayout
->addWidget(m_thisMonth
);
84 timespanLayout
->addWidget(m_thisYear
);
85 timespanLayout
->addStretch();
87 QButtonGroup
* ratingGroup
= new QButtonGroup(this);
88 m_anyRating
= createRadioButton(i18nc("@option:option", "Any Rating"), ratingGroup
);
89 m_oneOrMore
= createRadioButton(i18nc("@option:option", "1 or more"), ratingGroup
);
90 m_twoOrMore
= createRadioButton(i18nc("@option:option", "2 or more"), ratingGroup
);
91 m_threeOrMore
= createRadioButton(i18nc("@option:option", "3 or more"), ratingGroup
);
92 m_fourOrMore
= createRadioButton(i18nc("@option:option", "4 or more"), ratingGroup
);
93 m_maxRating
= createRadioButton(i18nc("@option:option", "Highest Rating"), ratingGroup
);
95 QVBoxLayout
* ratingLayout
= new QVBoxLayout();
96 ratingLayout
->setSpacing(0);
97 ratingLayout
->addWidget(m_anyRating
);
98 ratingLayout
->addWidget(m_oneOrMore
);
99 ratingLayout
->addWidget(m_twoOrMore
);
100 ratingLayout
->addWidget(m_threeOrMore
);
101 ratingLayout
->addWidget(m_fourOrMore
);
102 ratingLayout
->addWidget(m_maxRating
);
104 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
105 topLayout
->addLayout(typeLayout
);
106 topLayout
->addLayout(timespanLayout
);
107 topLayout
->addLayout(ratingLayout
);
108 topLayout
->addStretch();
113 DolphinFacetsWidget::~DolphinFacetsWidget()
117 void DolphinFacetsWidget::changeEvent(QEvent
*event
)
119 if (event
->type() == QEvent::EnabledChange
&& !isEnabled()) {
124 void DolphinFacetsWidget::resetOptions()
126 m_anyType
->setChecked(true);
127 m_anytime
->setChecked(true);
128 m_anyRating
->setChecked(true);
131 QString
DolphinFacetsWidget::ratingTerm() const
135 if (!m_anyRating
->isChecked()) {
136 int stars
= 1; // represents m_oneOrMore
137 if (m_twoOrMore
->isChecked()) {
139 } else if (m_threeOrMore
->isChecked()) {
141 } else if (m_fourOrMore
->isChecked()) {
143 } else if (m_maxRating
->isChecked()) {
147 const int rating
= stars
* 2;
148 terms
<< QStringLiteral("rating>=%1").arg(rating
);
151 if (!m_anytime
->isChecked()) {
152 QDate date
= QDate::currentDate(); // represents m_today
153 if (m_yesterday
->isChecked()) {
154 date
= date
.addDays(-1);
155 } else if (m_thisWeek
->isChecked()) {
156 date
= date
.addDays(1 - date
.dayOfWeek());
157 } else if (m_thisMonth
->isChecked()) {
158 date
= date
.addDays(1 - date
.day());
159 } else if (m_thisYear
->isChecked()) {
160 date
= date
.addDays(1 - date
.dayOfYear());
163 terms
<< QStringLiteral("modified>=%1").arg(date
.toString(Qt::ISODate
));
166 return terms
.join(QLatin1String(" AND "));
169 QString
DolphinFacetsWidget::facetType() const
171 if (m_folders
->isChecked()) {
172 return QStringLiteral("Folder");
173 } else if (m_documents
->isChecked()) {
174 return QStringLiteral("Document");
175 } else if (m_images
->isChecked()) {
176 return QStringLiteral("Image");
177 } else if (m_audio
->isChecked()) {
178 return QStringLiteral("Audio");
179 } else if (m_videos
->isChecked()) {
180 return QStringLiteral("Video");
186 bool DolphinFacetsWidget::isRatingTerm(const QString
& term
) const
188 const QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
190 // If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms.
191 bool containsRating
= false;
192 bool containsModified
= false;
194 foreach (const QString
& subTerm
, subTerms
) {
195 if (subTerm
.startsWith(QLatin1String("rating>="))) {
196 containsRating
= true;
197 } else if (subTerm
.startsWith(QLatin1String("modified>="))) {
198 containsModified
= true;
202 return containsModified
|| containsRating
;
205 void DolphinFacetsWidget::setRatingTerm(const QString
& term
)
207 // If term has sub terms, then the sub terms are always "rating" and "modified" terms.
208 // If term has no sub terms, then the term itself is either a "rating" term or a "modified"
209 // term. To avoid code duplication we add term to subTerms list, if the list is empty.
210 QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
212 foreach (const QString
& subTerm
, subTerms
) {
213 if (subTerm
.startsWith(QLatin1String("modified>="))) {
214 const QString value
= subTerm
.mid(10);
215 const QDate date
= QDate::fromString(value
, Qt::ISODate
);
217 } else if (subTerm
.startsWith(QLatin1String("rating>="))) {
218 const QString value
= subTerm
.mid(8);
219 const int stars
= value
.toInt() / 2;
225 void DolphinFacetsWidget::setFacetType(const QString
& type
)
227 if (type
== QLatin1String("Folder")) {
228 m_folders
->setChecked(true);
229 } else if (type
== QLatin1String("Document")) {
230 m_documents
->setChecked(true);
231 } else if (type
== QLatin1String("Image")) {
232 m_images
->setChecked(true);
233 } else if (type
== QLatin1String("Audio")) {
234 m_audio
->setChecked(true);
235 } else if (type
== QLatin1String("Video")) {
236 m_videos
->setChecked(true);
238 m_anyType
->setChecked(true);
242 void DolphinFacetsWidget::setRating(const int stars
)
246 m_maxRating
->setChecked(true);
250 m_fourOrMore
->setChecked(true);
254 m_threeOrMore
->setChecked(true);
258 m_twoOrMore
->setChecked(true);
262 m_oneOrMore
->setChecked(true);
266 m_anyRating
->setChecked(true);
270 void DolphinFacetsWidget::setTimespan(const QDate
& date
)
272 const QDate currentDate
= QDate::currentDate();
273 const int days
= date
.daysTo(currentDate
);
276 m_today
->setChecked(true);
277 } else if (days
<= 1) {
278 m_yesterday
->setChecked(true);
279 } else if (days
<= currentDate
.dayOfWeek()) {
280 m_thisWeek
->setChecked(true);
281 } else if (days
<= currentDate
.day()) {
282 m_thisMonth
->setChecked(true);
283 } else if (days
<= currentDate
.dayOfYear()) {
284 m_thisYear
->setChecked(true);
286 m_anytime
->setChecked(true);
290 QRadioButton
* DolphinFacetsWidget::createRadioButton(const QString
& text
,
293 QRadioButton
* button
= new QRadioButton(text
);
294 connect(button
, &QRadioButton::clicked
, this, &DolphinFacetsWidget::facetChanged
);
295 group
->addButton(button
);