]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
[dolphin/search] Reset search options when needed
[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 <QEvent>
28 #include <QHBoxLayout>
29 #include <QRadioButton>
30
31 DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
32 QWidget(parent),
33 m_folders(nullptr),
34 m_documents(nullptr),
35 m_images(nullptr),
36 m_audio(nullptr),
37 m_videos(nullptr),
38 m_anytime(nullptr),
39 m_today(nullptr),
40 m_yesterday(nullptr),
41 m_thisWeek(nullptr),
42 m_thisMonth(nullptr),
43 m_thisYear(nullptr),
44 m_anyRating(nullptr),
45 m_oneOrMore(nullptr),
46 m_twoOrMore(nullptr),
47 m_threeOrMore(nullptr),
48 m_fourOrMore(nullptr),
49 m_maxRating(nullptr)
50 {
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);
58
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();
68
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);
76
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();
86
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);
94
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);
103
104 QHBoxLayout* topLayout = new QHBoxLayout(this);
105 topLayout->addLayout(typeLayout);
106 topLayout->addLayout(timespanLayout);
107 topLayout->addLayout(ratingLayout);
108 topLayout->addStretch();
109
110 resetOptions();
111 }
112
113 DolphinFacetsWidget::~DolphinFacetsWidget()
114 {
115 }
116
117 void DolphinFacetsWidget::changeEvent(QEvent *event)
118 {
119 if (event->type() == QEvent::EnabledChange && !isEnabled()) {
120 resetOptions();
121 }
122 }
123
124 void DolphinFacetsWidget::resetOptions()
125 {
126 m_anyType->setChecked(true);
127 m_anytime->setChecked(true);
128 m_anyRating->setChecked(true);
129 }
130
131 QString DolphinFacetsWidget::ratingTerm() const
132 {
133 QStringList terms;
134
135 if (!m_anyRating->isChecked()) {
136 int stars = 1; // represents m_oneOrMore
137 if (m_twoOrMore->isChecked()) {
138 stars = 2;
139 } else if (m_threeOrMore->isChecked()) {
140 stars = 3;
141 } else if (m_fourOrMore->isChecked()) {
142 stars = 4;
143 } else if (m_maxRating->isChecked()) {
144 stars = 5;
145 }
146
147 const int rating = stars * 2;
148 terms << QStringLiteral("rating>=%1").arg(rating);
149 }
150
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());
161 }
162
163 terms << QStringLiteral("modified>=%1").arg(date.toString(Qt::ISODate));
164 }
165
166 return terms.join(QLatin1String(" AND "));
167 }
168
169 QString DolphinFacetsWidget::facetType() const
170 {
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");
181 }
182
183 return QString();
184 }
185
186 bool DolphinFacetsWidget::isRatingTerm(const QString& term) const
187 {
188 const QStringList subTerms = term.split(' ', QString::SkipEmptyParts);
189
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;
193
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;
199 }
200 }
201
202 return containsModified || containsRating;
203 }
204
205 void DolphinFacetsWidget::setRatingTerm(const QString& term)
206 {
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);
211
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);
216 setTimespan(date);
217 } else if (subTerm.startsWith(QLatin1String("rating>="))) {
218 const QString value = subTerm.mid(8);
219 const int stars = value.toInt() / 2;
220 setRating(stars);
221 }
222 }
223 }
224
225 void DolphinFacetsWidget::setFacetType(const QString& type)
226 {
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);
237 } else {
238 m_anyType->setChecked(true);
239 }
240 }
241
242 void DolphinFacetsWidget::setRating(const int stars)
243 {
244 switch (stars) {
245 case 5:
246 m_maxRating->setChecked(true);
247 break;
248
249 case 4:
250 m_fourOrMore->setChecked(true);
251 break;
252
253 case 3:
254 m_threeOrMore->setChecked(true);
255 break;
256
257 case 2:
258 m_twoOrMore->setChecked(true);
259 break;
260
261 case 1:
262 m_oneOrMore->setChecked(true);
263 break;
264
265 default:
266 m_anyRating->setChecked(true);
267 }
268 }
269
270 void DolphinFacetsWidget::setTimespan(const QDate& date)
271 {
272 const QDate currentDate = QDate::currentDate();
273 const int days = date.daysTo(currentDate);
274
275 if (days <= 0) {
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);
285 } else {
286 m_anytime->setChecked(true);
287 }
288 }
289
290 QRadioButton* DolphinFacetsWidget::createRadioButton(const QString& text,
291 QButtonGroup* group)
292 {
293 QRadioButton* button = new QRadioButton(text);
294 connect(button, &QRadioButton::clicked, this, &DolphinFacetsWidget::facetChanged);
295 group->addButton(button);
296 return button;
297 }
298