]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 * **************************************************************************/
20 #include "dolphinfacetswidget.h"
22 #include <KLocalizedString>
23 #include <QButtonGroup>
26 #include <QRadioButton>
27 #include <QHBoxLayout>
28 #include <QVBoxLayout>
30 DolphinFacetsWidget::DolphinFacetsWidget(QWidget
* parent
) :
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
);
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();
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
);
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();
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
);
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
);
100 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
101 topLayout
->addLayout(typeLayout
);
102 topLayout
->addLayout(timespanLayout
);
103 topLayout
->addLayout(ratingLayout
);
104 topLayout
->addStretch();
106 m_anyType
->setChecked(true);
107 m_anytime
->setChecked(true);
108 m_anyRating
->setChecked(true);
111 DolphinFacetsWidget::~DolphinFacetsWidget()
115 QString
DolphinFacetsWidget::ratingTerm() const
119 if (!m_anyRating
->isChecked()) {
120 int stars
= 1; // represents m_oneOrMore
121 if (m_twoOrMore
->isChecked()) {
123 } else if (m_threeOrMore
->isChecked()) {
125 } else if (m_fourOrMore
->isChecked()) {
127 } else if (m_maxRating
->isChecked()) {
131 const int rating
= stars
* 2;
132 terms
<< QStringLiteral("rating>=%1").arg(rating
);
135 if (!m_anytime
->isChecked()) {
136 QDate date
= QDate::currentDate(); // represents m_today
137 if (m_yesterday
->isChecked()) {
138 date
= date
.addDays(-1);
139 } else if (m_thisWeek
->isChecked()) {
140 date
= date
.addDays(1 - date
.dayOfWeek());
141 } else if (m_thisMonth
->isChecked()) {
142 date
= date
.addDays(1 - date
.day());
143 } else if (m_thisYear
->isChecked()) {
144 date
= date
.addDays(1 - date
.dayOfYear());
147 terms
<< QStringLiteral("modified>=%1").arg(date
.toString(Qt::ISODate
));
150 return terms
.join(QStringLiteral(" AND "));
153 QString
DolphinFacetsWidget::facetType() const
155 if (m_documents
->isChecked()) {
156 return QStringLiteral("Document");
157 } else if (m_images
->isChecked()) {
158 return QStringLiteral("Image");
159 } else if (m_audio
->isChecked()) {
160 return QStringLiteral("Audio");
161 } else if (m_videos
->isChecked()) {
162 return QStringLiteral("Video");
168 bool DolphinFacetsWidget::isRatingTerm(const QString
& term
) const
170 const QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
172 // If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms.
173 bool containsRating
= false;
174 bool containsModified
= false;
176 foreach (const QString
& subTerm
, subTerms
) {
177 if (subTerm
.startsWith(QLatin1String("rating>="))) {
178 containsRating
= true;
179 } else if (subTerm
.startsWith(QLatin1String("modified>="))) {
180 containsModified
= true;
184 return containsModified
|| containsRating
;
187 void DolphinFacetsWidget::setRatingTerm(const QString
& term
)
189 // If term has sub terms, then the sub terms are always "rating" and "modified" terms.
190 // If term has no sub terms, then the term itself is either a "rating" term or a "modified"
191 // term. To avoid code duplication we add term to subTerms list, if the list is empty.
192 QStringList subTerms
= term
.split(' ', QString::SkipEmptyParts
);
194 foreach (const QString
& subTerm
, subTerms
) {
195 if (subTerm
.startsWith(QLatin1String("modified>="))) {
196 const QString value
= subTerm
.mid(10);
197 const QDate date
= QDate::fromString(value
, Qt::ISODate
);
199 } else if (subTerm
.startsWith(QLatin1String("rating>="))) {
200 const QString value
= subTerm
.mid(8);
201 const int stars
= value
.toInt() / 2;
207 void DolphinFacetsWidget::setFacetType(const QString
& type
)
209 if (type
== QLatin1String("Document")) {
210 m_documents
->setChecked(true);
211 } else if (type
== QLatin1String("Image")) {
212 m_images
->setChecked(true);
213 } else if (type
== QLatin1String("Audio")) {
214 m_audio
->setChecked(true);
215 } else if (type
== QLatin1String("Video")) {
216 m_videos
->setChecked(true);
218 m_anyType
->setChecked(true);
222 void DolphinFacetsWidget::setRating(const int stars
)
226 m_maxRating
->setChecked(true);
230 m_fourOrMore
->setChecked(true);
234 m_threeOrMore
->setChecked(true);
238 m_twoOrMore
->setChecked(true);
242 m_oneOrMore
->setChecked(true);
246 m_anyRating
->setChecked(true);
250 void DolphinFacetsWidget::setTimespan(const QDate
& date
)
252 const QDate currentDate
= QDate::currentDate();
253 const int days
= date
.daysTo(currentDate
);
256 m_today
->setChecked(true);
257 } else if (days
<= 1) {
258 m_yesterday
->setChecked(true);
259 } else if (days
<= currentDate
.dayOfWeek()) {
260 m_thisWeek
->setChecked(true);
261 } else if (days
<= currentDate
.day()) {
262 m_thisMonth
->setChecked(true);
263 } else if (days
<= currentDate
.dayOfYear()) {
264 m_thisYear
->setChecked(true);
266 m_anytime
->setChecked(true);
270 QRadioButton
* DolphinFacetsWidget::createRadioButton(const QString
& text
,
273 QRadioButton
* button
= new QRadioButton(text
);
274 connect(button
, &QRadioButton::clicked
, this, &DolphinFacetsWidget::facetChanged
);
275 group
->addButton(button
);