]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
Dolphin Facet Widgets: Implement date based filtering
[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 m_documents = createCheckBox(i18nc("@option:check", "Documents"));
50 m_images = createCheckBox(i18nc("@option:check", "Images"));
51 m_audio = createCheckBox(i18nc("@option:check", "Audio Files"));
52 m_videos = createCheckBox(i18nc("@option:check", "Videos"));
53
54 QVBoxLayout* typeLayout = new QVBoxLayout();
55 typeLayout->setSpacing(0);
56 typeLayout->addWidget(m_documents);
57 typeLayout->addWidget(m_images);
58 typeLayout->addWidget(m_audio);
59 typeLayout->addWidget(m_videos);
60 typeLayout->addStretch();
61
62 QButtonGroup* timespanGroup = new QButtonGroup(this);
63 m_anytime = createRadioButton(i18nc("@option:option", "Anytime"), timespanGroup);
64 m_today = createRadioButton(i18nc("@option:option", "Today"), timespanGroup);
65 m_yesterday = createRadioButton(i18nc("@option:option", "Yesterday"), timespanGroup);
66 m_thisWeek = createRadioButton(i18nc("@option:option", "This Week"), timespanGroup);
67 m_thisMonth = createRadioButton(i18nc("@option:option", "This Month"), timespanGroup);
68 m_thisYear = createRadioButton(i18nc("@option:option", "This Year"), timespanGroup);
69
70 QVBoxLayout* timespanLayout = new QVBoxLayout();
71 timespanLayout->setSpacing(0);
72 timespanLayout->addWidget(m_anytime);
73 timespanLayout->addWidget(m_today);
74 timespanLayout->addWidget(m_yesterday);
75 timespanLayout->addWidget(m_thisWeek);
76 timespanLayout->addWidget(m_thisMonth);
77 timespanLayout->addWidget(m_thisYear);
78 timespanLayout->addStretch();
79
80 QButtonGroup* ratingGroup = new QButtonGroup(this);
81 m_anyRating = createRadioButton(i18nc("@option:option", "Any Rating"), ratingGroup);
82 m_oneOrMore = createRadioButton(i18nc("@option:option", "1 or more"), ratingGroup);
83 m_twoOrMore = createRadioButton(i18nc("@option:option", "2 or more"), ratingGroup);
84 m_threeOrMore = createRadioButton(i18nc("@option:option", "3 or more"), ratingGroup);
85 m_fourOrMore = createRadioButton(i18nc("@option:option", "4 or more"), ratingGroup);
86 m_maxRating = createRadioButton(i18nc("@option:option", "Highest Rating"), ratingGroup);
87
88 QVBoxLayout* ratingLayout = new QVBoxLayout();
89 ratingLayout->setSpacing(0);
90 ratingLayout->addWidget(m_anyRating);
91 ratingLayout->addWidget(m_oneOrMore);
92 ratingLayout->addWidget(m_twoOrMore);
93 ratingLayout->addWidget(m_threeOrMore);
94 ratingLayout->addWidget(m_fourOrMore);
95 ratingLayout->addWidget(m_maxRating);
96
97 QHBoxLayout* topLayout = new QHBoxLayout(this);
98 topLayout->addLayout(typeLayout);
99 topLayout->addLayout(timespanLayout);
100 topLayout->addLayout(ratingLayout);
101 topLayout->addStretch();
102
103 m_anytime->setChecked(true);
104 m_anyRating->setChecked(true);
105 }
106
107 DolphinFacetsWidget::~DolphinFacetsWidget()
108 {
109 }
110
111 #ifdef HAVE_BALOO
112 Baloo::Term DolphinFacetsWidget::ratingTerm() const
113 {
114 Baloo::Term ratingTerm;
115 Baloo::Term modifiedTerm;
116
117 if (!m_anyRating->isChecked()) {
118 int stars = 1; // represents m_oneOrMore
119 if (m_twoOrMore->isChecked()) {
120 stars = 2;
121 } else if (m_threeOrMore->isChecked()) {
122 stars = 3;
123 } else if (m_fourOrMore->isChecked()) {
124 stars = 4;
125 } else if (m_maxRating->isChecked()) {
126 stars = 5;
127 }
128
129 const int rating = stars * 2;
130 ratingTerm = Baloo::Term("rating", rating, Baloo::Term::GreaterEqual);
131 }
132
133 if (!m_anytime->isChecked()) {
134 QDate date = QDate::currentDate(); // represents m_today
135 if (m_yesterday->isChecked()) {
136 date = date.addDays(-1);
137 } else if (m_thisWeek->isChecked()) {
138 date = date.addDays(1 - date.dayOfWeek());
139 } else if (m_thisMonth->isChecked()) {
140 date = date.addDays(1 - date.day());
141 } else if (m_thisYear->isChecked()) {
142 date = date.addDays(1 - date.dayOfYear());
143 }
144
145 modifiedTerm = Baloo::Term("modified", date, Baloo::Term::GreaterEqual);
146 }
147
148 if (ratingTerm.isValid() && modifiedTerm.isValid()) {
149 Baloo::Term term(Baloo::Term::And);
150 term.addSubTerm(ratingTerm);
151 term.addSubTerm(modifiedTerm);
152
153 return term;
154 } else if (modifiedTerm.isValid()) {
155 return modifiedTerm;
156 } else if (ratingTerm.isValid()) {
157 return ratingTerm;
158 }
159
160 return Baloo::Term();
161 }
162
163 QStringList DolphinFacetsWidget::facetTypes() const
164 {
165 QStringList types;
166 if (m_documents->isChecked()) {
167 types << "Document";
168 }
169
170 if (m_images->isChecked()) {
171 types << "Image";
172 }
173
174 if (m_audio->isChecked()) {
175 types << "Audio";
176 }
177
178 if (m_videos->isChecked()) {
179 types << "Video";
180 }
181
182 return types;
183 }
184
185 #endif
186
187
188 QCheckBox* DolphinFacetsWidget::createCheckBox(const QString& text)
189 {
190 QCheckBox* checkBox = new QCheckBox(text);
191 connect(checkBox, SIGNAL(clicked()), this, SIGNAL(facetChanged()));
192 return checkBox;
193 }
194
195 QRadioButton* DolphinFacetsWidget::createRadioButton(const QString& text,
196 QButtonGroup* group)
197 {
198 QRadioButton* button = new QRadioButton(text);
199 connect(button, SIGNAL(clicked()), this, SIGNAL(facetChanged()));
200 group->addButton(button);
201 return button;
202 }
203
204 #include "dolphinfacetswidget.moc"