]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
21a45cd9b8ec2419a05540d67a746d30b97658d8
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>
23 #include <QButtonGroup>
26 #include <QRadioButton>
27 #include <QHBoxLayout>
29 DolphinFacetsWidget::DolphinFacetsWidget(QWidget
* parent
) :
45 m_threeOrMore(nullptr),
46 m_fourOrMore(nullptr),
49 QButtonGroup
* filetypeGroup
= new QButtonGroup(this);
50 m_anyType
= createRadioButton(i18nc("@option:check", "Any"), filetypeGroup
);
51 m_folders
= createRadioButton(i18nc("@option:check", "Folders"), filetypeGroup
);
52 m_documents
= createRadioButton(i18nc("@option:check", "Documents"), filetypeGroup
);
53 m_images
= createRadioButton(i18nc("@option:check", "Images"), filetypeGroup
);
54 m_audio
= createRadioButton(i18nc("@option:check", "Audio Files"), filetypeGroup
);
55 m_videos
= createRadioButton(i18nc("@option:check", "Videos"), filetypeGroup
);
57 QVBoxLayout
* typeLayout
= new QVBoxLayout();
58 typeLayout
->setSpacing(0);
59 typeLayout
->addWidget(m_anyType
);
60 typeLayout
->addWidget(m_folders
);
61 typeLayout
->addWidget(m_documents
);
62 typeLayout
->addWidget(m_images
);
63 typeLayout
->addWidget(m_audio
);
64 typeLayout
->addWidget(m_videos
);
65 typeLayout
->addStretch();
67 QButtonGroup
* timespanGroup
= new QButtonGroup(this);
68 m_anytime
= createRadioButton(i18nc("@option:option", "Anytime"), timespanGroup
);
69 m_today
= createRadioButton(i18nc("@option:option", "Today"), timespanGroup
);
70 m_yesterday
= createRadioButton(i18nc("@option:option", "Yesterday"), timespanGroup
);
71 m_thisWeek
= createRadioButton(i18nc("@option:option", "This Week"), timespanGroup
);
72 m_thisMonth
= createRadioButton(i18nc("@option:option", "This Month"), timespanGroup
);
73 m_thisYear
= createRadioButton(i18nc("@option:option", "This Year"), timespanGroup
);
75 QVBoxLayout
* timespanLayout
= new QVBoxLayout();
76 timespanLayout
->setSpacing(0);
77 timespanLayout
->addWidget(m_anytime
);
78 timespanLayout
->addWidget(m_today
);
79 timespanLayout
->addWidget(m_yesterday
);
80 timespanLayout
->addWidget(m_thisWeek
);
81 timespanLayout
->addWidget(m_thisMonth
);
82 timespanLayout
->addWidget(m_thisYear
);
83 timespanLayout
->addStretch();
85 QButtonGroup
* ratingGroup
= new QButtonGroup(this);
86 m_anyRating
= createRadioButton(i18nc("@option:option", "Any Rating"), ratingGroup
);
87 m_oneOrMore
= createRadioButton(i18nc("@option:option", "1 or more"), ratingGroup
);
88 m_twoOrMore
= createRadioButton(i18nc("@option:option", "2 or more"), ratingGroup
);
89 m_threeOrMore
= createRadioButton(i18nc("@option:option", "3 or more"), ratingGroup
);
90 m_fourOrMore
= createRadioButton(i18nc("@option:option", "4 or more"), ratingGroup
);
91 m_maxRating
= createRadioButton(i18nc("@option:option", "Highest Rating"), ratingGroup
);
93 QVBoxLayout
* ratingLayout
= new QVBoxLayout();
94 ratingLayout
->setSpacing(0);
95 ratingLayout
->addWidget(m_anyRating
);
96 ratingLayout
->addWidget(m_oneOrMore
);
97 ratingLayout
->addWidget(m_twoOrMore
);
98 ratingLayout
->addWidget(m_threeOrMore
);
99 ratingLayout
->addWidget(m_fourOrMore
);
100 ratingLayout
->addWidget(m_maxRating
);
102 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
103 topLayout
->addLayout(typeLayout
);
104 topLayout
->addLayout(timespanLayout
);
105 topLayout
->addLayout(ratingLayout
);
106 topLayout
->addStretch();
108 m_anyType
->setChecked(true);
109 m_anytime
->setChecked(true);
110 m_anyRating
->setChecked(true);
113 DolphinFacetsWidget::~DolphinFacetsWidget()
117 QString
DolphinFacetsWidget::ratingTerm() const
121 if (!m_anyRating
->isChecked()) {
122 int stars
= 1; // represents m_oneOrMore
123 if (m_twoOrMore
->isChecked()) {
125 } else if (m_threeOrMore
->isChecked()) {
127 } else if (m_fourOrMore
->isChecked()) {
129 } else if (m_maxRating
->isChecked()) {
133 const int rating
= stars
* 2;
134 terms
<< QStringLiteral("rating>=%1").arg(rating
);
137 if (!m_anytime
->isChecked()) {
138 QDate date
= QDate::currentDate(); // represents m_today
139 if (m_yesterday
->isChecked()) {
140 date
= date
.addDays(-1);
141 } else if (m_thisWeek
->isChecked()) {
142 date
= date
.addDays(1 - date
.dayOfWeek());
143 } else if (m_thisMonth
->isChecked()) {
144 date
= date
.addDays(1 - date
.day());
145 } else if (m_thisYear
->isChecked()) {
146 date
= date
.addDays(1 - date
.dayOfYear());
149 terms
<< QStringLiteral("modified>=%1").arg(date
.toString(Qt::ISODate
));
152 return terms
.join(QStringLiteral(" AND "));
155 QString
DolphinFacetsWidget::facetType() const
157 if (m_folders
->isChecked()) {
158 return QStringLiteral("Folder");
159 } else if (m_documents
->isChecked()) {
160 return QStringLiteral("Document");
161 } else if (m_images
->isChecked()) {
162 return QStringLiteral("Image");
163 } else if (m_audio
->isChecked()) {
164 return QStringLiteral("Audio");
165 } else if (m_videos
->isChecked()) {
166 return QStringLiteral("Video");
172 bool DolphinFacetsWidget::isRatingTerm(const QString
& term
) const
174 const QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
176 // If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms.
177 bool containsRating
= false;
178 bool containsModified
= false;
180 foreach (const QString
& subTerm
, subTerms
) {
181 if (subTerm
.startsWith(QLatin1String("rating>="))) {
182 containsRating
= true;
183 } else if (subTerm
.startsWith(QLatin1String("modified>="))) {
184 containsModified
= true;
188 return containsModified
|| containsRating
;
191 void DolphinFacetsWidget::setRatingTerm(const QString
& term
)
193 // If term has sub terms, then the sub terms are always "rating" and "modified" terms.
194 // If term has no sub terms, then the term itself is either a "rating" term or a "modified"
195 // term. To avoid code duplication we add term to subTerms list, if the list is empty.
196 QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
198 foreach (const QString
& subTerm
, subTerms
) {
199 if (subTerm
.startsWith(QLatin1String("modified>="))) {
200 const QString value
= subTerm
.mid(10);
201 const QDate date
= QDate::fromString(value
, Qt::ISODate
);
203 } else if (subTerm
.startsWith(QLatin1String("rating>="))) {
204 const QString value
= subTerm
.mid(8);
205 const int stars
= value
.toInt() / 2;
211 void DolphinFacetsWidget::setFacetType(const QString
& type
)
213 if (type
== QLatin1String("Document")) {
214 m_documents
->setChecked(true);
215 } else if (type
== QLatin1String("Image")) {
216 m_images
->setChecked(true);
217 } else if (type
== QLatin1String("Audio")) {
218 m_audio
->setChecked(true);
219 } else if (type
== QLatin1String("Video")) {
220 m_videos
->setChecked(true);
222 m_anyType
->setChecked(true);
226 void DolphinFacetsWidget::setRating(const int stars
)
230 m_maxRating
->setChecked(true);
234 m_fourOrMore
->setChecked(true);
238 m_threeOrMore
->setChecked(true);
242 m_twoOrMore
->setChecked(true);
246 m_oneOrMore
->setChecked(true);
250 m_anyRating
->setChecked(true);
254 void DolphinFacetsWidget::setTimespan(const QDate
& date
)
256 const QDate currentDate
= QDate::currentDate();
257 const int days
= date
.daysTo(currentDate
);
260 m_today
->setChecked(true);
261 } else if (days
<= 1) {
262 m_yesterday
->setChecked(true);
263 } else if (days
<= currentDate
.dayOfWeek()) {
264 m_thisWeek
->setChecked(true);
265 } else if (days
<= currentDate
.day()) {
266 m_thisMonth
->setChecked(true);
267 } else if (days
<= currentDate
.dayOfYear()) {
268 m_thisYear
->setChecked(true);
270 m_anytime
->setChecked(true);
274 QRadioButton
* DolphinFacetsWidget::createRadioButton(const QString
& text
,
277 QRadioButton
* button
= new QRadioButton(text
);
278 connect(button
, &QRadioButton::clicked
, this, &DolphinFacetsWidget::facetChanged
);
279 group
->addButton(button
);