]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
7c0e97f86ca4f4f12a7e65fa6d0f267487094fe2
[dolphin.git] / src / search / dolphinfacetswidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 * **************************************************************************/
19
20 #include "dolphinfacetswidget.h"
21
22 #include <KLocalizedString>
23
24 #include <QButtonGroup>
25 #include <QCheckBox>
26 #include <QDate>
27 #include <QHBoxLayout>
28 #include <QRadioButton>
29
30 DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
31 QWidget(parent),
32 m_folders(nullptr),
33 m_documents(nullptr),
34 m_images(nullptr),
35 m_audio(nullptr),
36 m_videos(nullptr),
37 m_anytime(nullptr),
38 m_today(nullptr),
39 m_yesterday(nullptr),
40 m_thisWeek(nullptr),
41 m_thisMonth(nullptr),
42 m_thisYear(nullptr),
43 m_anyRating(nullptr),
44 m_oneOrMore(nullptr),
45 m_twoOrMore(nullptr),
46 m_threeOrMore(nullptr),
47 m_fourOrMore(nullptr),
48 m_maxRating(nullptr)
49 {
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);
57
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();
67
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);
75
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();
85
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);
93
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);
102
103 QHBoxLayout* topLayout = new QHBoxLayout(this);
104 topLayout->addLayout(typeLayout);
105 topLayout->addLayout(timespanLayout);
106 topLayout->addLayout(ratingLayout);
107 topLayout->addStretch();
108
109 m_anyType->setChecked(true);
110 m_anytime->setChecked(true);
111 m_anyRating->setChecked(true);
112 }
113
114 DolphinFacetsWidget::~DolphinFacetsWidget()
115 {
116 }
117
118 QString DolphinFacetsWidget::ratingTerm() const
119 {
120 QStringList terms;
121
122 if (!m_anyRating->isChecked()) {
123 int stars = 1; // represents m_oneOrMore
124 if (m_twoOrMore->isChecked()) {
125 stars = 2;
126 } else if (m_threeOrMore->isChecked()) {
127 stars = 3;
128 } else if (m_fourOrMore->isChecked()) {
129 stars = 4;
130 } else if (m_maxRating->isChecked()) {
131 stars = 5;
132 }
133
134 const int rating = stars * 2;
135 terms << QStringLiteral("rating>=%1").arg(rating);
136 }
137
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());
148 }
149
150 terms << QStringLiteral("modified>=%1").arg(date.toString(Qt::ISODate));
151 }
152
153 return terms.join(QStringLiteral(" AND "));
154 }
155
156 QString DolphinFacetsWidget::facetType() const
157 {
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");
168 }
169
170 return QString();
171 }
172
173 bool DolphinFacetsWidget::isRatingTerm(const QString& term) const
174 {
175 const QStringList subTerms = term.split(' ', QString::SkipEmptyParts);
176
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;
180
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;
186 }
187 }
188
189 return containsModified || containsRating;
190 }
191
192 void DolphinFacetsWidget::setRatingTerm(const QString& term)
193 {
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);
198
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);
203 setTimespan(date);
204 } else if (subTerm.startsWith(QLatin1String("rating>="))) {
205 const QString value = subTerm.mid(8);
206 const int stars = value.toInt() / 2;
207 setRating(stars);
208 }
209 }
210 }
211
212 void DolphinFacetsWidget::setFacetType(const QString& type)
213 {
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);
222 } else {
223 m_anyType->setChecked(true);
224 }
225 }
226
227 void DolphinFacetsWidget::setRating(const int stars)
228 {
229 switch (stars) {
230 case 5:
231 m_maxRating->setChecked(true);
232 break;
233
234 case 4:
235 m_fourOrMore->setChecked(true);
236 break;
237
238 case 3:
239 m_threeOrMore->setChecked(true);
240 break;
241
242 case 2:
243 m_twoOrMore->setChecked(true);
244 break;
245
246 case 1:
247 m_oneOrMore->setChecked(true);
248 break;
249
250 default:
251 m_anyRating->setChecked(true);
252 }
253 }
254
255 void DolphinFacetsWidget::setTimespan(const QDate& date)
256 {
257 const QDate currentDate = QDate::currentDate();
258 const int days = date.daysTo(currentDate);
259
260 if (days <= 0) {
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);
270 } else {
271 m_anytime->setChecked(true);
272 }
273 }
274
275 QRadioButton* DolphinFacetsWidget::createRadioButton(const QString& text,
276 QButtonGroup* group)
277 {
278 QRadioButton* button = new QRadioButton(text);
279 connect(button, &QRadioButton::clicked, this, &DolphinFacetsWidget::facetChanged);
280 group->addButton(button);
281 return button;
282 }
283