]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
Merge remote-tracking branch 'origin/master' into frameworks
[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 <KLocale>
23 #include <QButtonGroup>
24 #include <QCheckBox>
25 #include <QDate>
26 #include <QRadioButton>
27 #include <QHBoxLayout>
28 #include <QVBoxLayout>
29
30 DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
31 QWidget(parent),
32 m_documents(0),
33 m_images(0),
34 m_audio(0),
35 m_videos(0),
36 m_anytime(0),
37 m_today(0),
38 m_yesterday(0),
39 m_thisWeek(0),
40 m_thisMonth(0),
41 m_thisYear(0),
42 m_anyRating(0),
43 m_oneOrMore(0),
44 m_twoOrMore(0),
45 m_threeOrMore(0),
46 m_fourOrMore(0),
47 m_maxRating(0)
48 {
49 QButtonGroup* filetypeGroup = new QButtonGroup(this);
50 m_anyType = createRadioButton(i18nc("@option:check", "Any"), filetypeGroup);
51 m_documents = createRadioButton(i18nc("@option:check", "Documents"), filetypeGroup);
52 m_images = createRadioButton(i18nc("@option:check", "Images"), filetypeGroup);
53 m_audio = createRadioButton(i18nc("@option:check", "Audio Files"), filetypeGroup);
54 m_videos = createRadioButton(i18nc("@option:check", "Videos"), filetypeGroup);
55
56 QVBoxLayout* typeLayout = new QVBoxLayout();
57 typeLayout->setSpacing(0);
58 typeLayout->addWidget(m_anyType);
59 typeLayout->addWidget(m_documents);
60 typeLayout->addWidget(m_images);
61 typeLayout->addWidget(m_audio);
62 typeLayout->addWidget(m_videos);
63 typeLayout->addStretch();
64
65 QButtonGroup* timespanGroup = new QButtonGroup(this);
66 m_anytime = createRadioButton(i18nc("@option:option", "Anytime"), timespanGroup);
67 m_today = createRadioButton(i18nc("@option:option", "Today"), timespanGroup);
68 m_yesterday = createRadioButton(i18nc("@option:option", "Yesterday"), timespanGroup);
69 m_thisWeek = createRadioButton(i18nc("@option:option", "This Week"), timespanGroup);
70 m_thisMonth = createRadioButton(i18nc("@option:option", "This Month"), timespanGroup);
71 m_thisYear = createRadioButton(i18nc("@option:option", "This Year"), timespanGroup);
72
73 QVBoxLayout* timespanLayout = new QVBoxLayout();
74 timespanLayout->setSpacing(0);
75 timespanLayout->addWidget(m_anytime);
76 timespanLayout->addWidget(m_today);
77 timespanLayout->addWidget(m_yesterday);
78 timespanLayout->addWidget(m_thisWeek);
79 timespanLayout->addWidget(m_thisMonth);
80 timespanLayout->addWidget(m_thisYear);
81 timespanLayout->addStretch();
82
83 QButtonGroup* ratingGroup = new QButtonGroup(this);
84 m_anyRating = createRadioButton(i18nc("@option:option", "Any Rating"), ratingGroup);
85 m_oneOrMore = createRadioButton(i18nc("@option:option", "1 or more"), ratingGroup);
86 m_twoOrMore = createRadioButton(i18nc("@option:option", "2 or more"), ratingGroup);
87 m_threeOrMore = createRadioButton(i18nc("@option:option", "3 or more"), ratingGroup);
88 m_fourOrMore = createRadioButton(i18nc("@option:option", "4 or more"), ratingGroup);
89 m_maxRating = createRadioButton(i18nc("@option:option", "Highest Rating"), ratingGroup);
90
91 QVBoxLayout* ratingLayout = new QVBoxLayout();
92 ratingLayout->setSpacing(0);
93 ratingLayout->addWidget(m_anyRating);
94 ratingLayout->addWidget(m_oneOrMore);
95 ratingLayout->addWidget(m_twoOrMore);
96 ratingLayout->addWidget(m_threeOrMore);
97 ratingLayout->addWidget(m_fourOrMore);
98 ratingLayout->addWidget(m_maxRating);
99
100 QHBoxLayout* topLayout = new QHBoxLayout(this);
101 topLayout->addLayout(typeLayout);
102 topLayout->addLayout(timespanLayout);
103 topLayout->addLayout(ratingLayout);
104 topLayout->addStretch();
105
106 m_anyType->setChecked(true);
107 m_anytime->setChecked(true);
108 m_anyRating->setChecked(true);
109 }
110
111 DolphinFacetsWidget::~DolphinFacetsWidget()
112 {
113 }
114
115 #ifdef HAVE_BALOO
116 Baloo::Term DolphinFacetsWidget::ratingTerm() const
117 {
118 Baloo::Term ratingTerm;
119 Baloo::Term modifiedTerm;
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 ratingTerm = Baloo::Term("rating", rating, Baloo::Term::GreaterEqual);
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 modifiedTerm = Baloo::Term("modified", date, Baloo::Term::GreaterEqual);
150 }
151
152 if (ratingTerm.isValid() && modifiedTerm.isValid()) {
153 Baloo::Term term(Baloo::Term::And);
154 term.addSubTerm(ratingTerm);
155 term.addSubTerm(modifiedTerm);
156
157 return term;
158 } else if (modifiedTerm.isValid()) {
159 return modifiedTerm;
160 } else if (ratingTerm.isValid()) {
161 return ratingTerm;
162 }
163
164 return Baloo::Term();
165 }
166
167 QString DolphinFacetsWidget::facetType() const
168 {
169 if (m_documents->isChecked()) {
170 return QLatin1String("Document");
171 } else if (m_images->isChecked()) {
172 return QLatin1String("Image");
173 } else if (m_audio->isChecked()) {
174 return QLatin1String("Audio");
175 } else if (m_videos->isChecked()) {
176 return QLatin1String("Video");
177 }
178
179 return QString();
180 }
181
182 bool DolphinFacetsWidget::isRatingTerm(const Baloo::Term& term) const
183 {
184 const QList<Baloo::Term> subTerms = term.subTerms();
185 if (subTerms.isEmpty()) {
186 // If term has no sub terms, then the term itself is either a "rating" term
187 // or a "modified" term.
188 return term.property() == QLatin1String("modified") ||
189 term.property() == QLatin1String("rating");
190
191 } else if (subTerms.size() == 2) {
192 // If term has sub terms, then the sub terms are always "rating" and "modified" terms.
193
194 QStringList properties;
195 foreach (const Baloo::Term& subTerm, subTerms) {
196 properties << subTerm.property();
197 }
198
199 return properties.contains(QLatin1String("modified")) &&
200 properties.contains(QLatin1String("rating"));
201 }
202
203 return false;
204 }
205
206 void DolphinFacetsWidget::setRatingTerm(const Baloo::Term& term)
207 {
208 // If term has sub terms, then the sub terms are always "rating" and "modified" terms.
209 // If term has no sub terms, then the term itself is either a "rating" term or a "modified"
210 // term. To avoid code duplication we add term to subTerms list, if the list is empty.
211 QList<Baloo::Term> subTerms = term.subTerms();
212 if (subTerms.isEmpty()) {
213 subTerms << term;
214 }
215
216 foreach (const Baloo::Term& subTerm, subTerms) {
217 const QString property = subTerm.property();
218
219 if (property == QLatin1String("modified")) {
220 const QDate date = subTerm.value().toDate();
221 setTimespan(date);
222 } else if (property == QLatin1String("rating")) {
223 const int stars = subTerm.value().toInt() / 2;
224 setRating(stars);
225 }
226 }
227 }
228
229 #endif
230
231 void DolphinFacetsWidget::setFacetType(const QString& type)
232 {
233 if (type == QLatin1String("Document")) {
234 m_documents->setChecked(true);
235 } else if (type == QLatin1String("Image")) {
236 m_images->setChecked(true);
237 } else if (type == QLatin1String("Audio")) {
238 m_audio->setChecked(true);
239 } else if (type == QLatin1String("Video")) {
240 m_videos->setChecked(true);
241 } else {
242 m_anyType->setChecked(true);
243 }
244 }
245
246 void DolphinFacetsWidget::setRating(const int stars)
247 {
248 switch (stars) {
249 case 5:
250 m_maxRating->setChecked(true);
251 break;
252
253 case 4:
254 m_fourOrMore->setChecked(true);
255 break;
256
257 case 3:
258 m_threeOrMore->setChecked(true);
259 break;
260
261 case 2:
262 m_twoOrMore->setChecked(true);
263 break;
264
265 case 1:
266 m_oneOrMore->setChecked(true);
267 break;
268
269 default:
270 m_anyRating->setChecked(true);
271 }
272 }
273
274 void DolphinFacetsWidget::setTimespan(const QDate& date)
275 {
276 const QDate currentDate = QDate::currentDate();
277 const int days = date.daysTo(currentDate);
278
279 if (days <= 0) {
280 m_today->setChecked(true);
281 } else if (days <= 1) {
282 m_yesterday->setChecked(true);
283 } else if (days <= currentDate.dayOfWeek()) {
284 m_thisWeek->setChecked(true);
285 } else if (days <= currentDate.day()) {
286 m_thisMonth->setChecked(true);
287 } else if (days <= currentDate.dayOfYear()) {
288 m_thisYear->setChecked(true);
289 } else {
290 m_anytime->setChecked(true);
291 }
292 }
293
294 QRadioButton* DolphinFacetsWidget::createRadioButton(const QString& text,
295 QButtonGroup* group)
296 {
297 QRadioButton* button = new QRadioButton(text);
298 connect(button, &QRadioButton::clicked, this, &DolphinFacetsWidget::facetChanged);
299 group->addButton(button);
300 return button;
301 }
302
303 #include "dolphinfacetswidget.moc"