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