]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
Get rid of search "More Options" toggle button
[dolphin.git] / src / search / dolphinfacetswidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2019 by Ismael Asensio <isma.af@mgmail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 * **************************************************************************/
20
21 #include "dolphinfacetswidget.h"
22
23 #include <KLocalizedString>
24
25 #include <QComboBox>
26 #include <QDate>
27 #include <QEvent>
28 #include <QHBoxLayout>
29 #include <QIcon>
30
31 DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
32 QWidget(parent),
33 m_typeSelector(nullptr),
34 m_dateSelector(nullptr),
35 m_ratingSelector(nullptr)
36 {
37 m_typeSelector = new QComboBox(this);
38 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("none")), i18nc("@item:inlistbox", "Any Type"), QString());
39 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("inode-directory")), i18nc("@item:inlistbox", "Folders") , QStringLiteral("Folder"));
40 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("text-x-generic")), i18nc("@item:inlistbox", "Documents") , QStringLiteral("Document"));
41 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("image-x-generic")), i18nc("@item:inlistbox", "Images") , QStringLiteral("Image"));
42 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("audio-x-generic")), i18nc("@item:inlistbox", "Audio Files"), QStringLiteral("Audio"));
43 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("video-x-generic")), i18nc("@item:inlistbox", "Videos") , QStringLiteral("Video"));
44 initComboBox(m_typeSelector);
45
46 const QDate currentDate = QDate::currentDate();
47
48 m_dateSelector = new QComboBox(this);
49 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar")), i18nc("@item:inlistbox", "Any Date"), QDate());
50 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Today") , currentDate);
51 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Yesterday") , currentDate.addDays(-1));
52 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-week")), i18nc("@item:inlistbox", "This Week") , currentDate.addDays(1 - currentDate.dayOfWeek()));
53 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-month")), i18nc("@item:inlistbox", "This Month"), currentDate.addDays(1 - currentDate.day()));
54 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-year")), i18nc("@item:inlistbox", "This Year") , currentDate.addDays(1 - currentDate.dayOfYear()));
55 initComboBox(m_dateSelector);
56
57 m_ratingSelector = new QComboBox(this);
58 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("non-starred-symbolic")), i18nc("@item:inlistbox", "Any Rating"), 0);
59 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "1 or more"), 1);
60 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "2 or more"), 2);
61 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "3 or more"), 3);
62 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "4 or more"), 4);
63 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "Highest Rating"), 5);
64 initComboBox(m_ratingSelector);
65
66 QHBoxLayout* topLayout = new QHBoxLayout(this);
67 topLayout->addWidget(m_typeSelector);
68 topLayout->addWidget(m_dateSelector);
69 topLayout->addWidget(m_ratingSelector);
70
71 resetOptions();
72 }
73
74 DolphinFacetsWidget::~DolphinFacetsWidget()
75 {
76 }
77
78 void DolphinFacetsWidget::changeEvent(QEvent *event)
79 {
80 if (event->type() == QEvent::EnabledChange && !isEnabled()) {
81 resetOptions();
82 }
83 }
84
85 void DolphinFacetsWidget::resetOptions()
86 {
87 m_typeSelector->setCurrentIndex(0);
88 m_dateSelector->setCurrentIndex(0);
89 m_ratingSelector->setCurrentIndex(0);
90 }
91
92 QString DolphinFacetsWidget::ratingTerm() const
93 {
94 QStringList terms;
95
96 if (m_ratingSelector->currentIndex() > 0) {
97 const int rating = m_ratingSelector->currentData().toInt() * 2;
98 terms << QStringLiteral("rating>=%1").arg(rating);
99 }
100
101 if (m_dateSelector->currentIndex() > 0) {
102 const QDate date = m_dateSelector->currentData().toDate();
103 terms << QStringLiteral("modified>=%1").arg(date.toString(Qt::ISODate));
104 }
105
106 return terms.join(QLatin1String(" AND "));
107 }
108
109 QString DolphinFacetsWidget::facetType() const
110 {
111 return m_typeSelector->currentData().toString();
112 }
113
114 bool DolphinFacetsWidget::isRatingTerm(const QString& term) const
115 {
116 const QStringList subTerms = term.split(' ', QString::SkipEmptyParts);
117
118 // If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms.
119 bool containsRating = false;
120 bool containsModified = false;
121
122 foreach (const QString& subTerm, subTerms) {
123 if (subTerm.startsWith(QLatin1String("rating>="))) {
124 containsRating = true;
125 } else if (subTerm.startsWith(QLatin1String("modified>="))) {
126 containsModified = true;
127 }
128 }
129
130 return containsModified || containsRating;
131 }
132
133 void DolphinFacetsWidget::setRatingTerm(const QString& term)
134 {
135 // If term has sub terms, then the sub terms are always "rating" and "modified" terms.
136 // If term has no sub terms, then the term itself is either a "rating" term or a "modified"
137 // term. To avoid code duplication we add term to subTerms list, if the list is empty.
138 QStringList subTerms = term.split(' ', QString::SkipEmptyParts);
139
140 foreach (const QString& subTerm, subTerms) {
141 if (subTerm.startsWith(QLatin1String("modified>="))) {
142 const QString value = subTerm.mid(10);
143 const QDate date = QDate::fromString(value, Qt::ISODate);
144 setTimespan(date);
145 } else if (subTerm.startsWith(QLatin1String("rating>="))) {
146 const QString value = subTerm.mid(8);
147 const int stars = value.toInt() / 2;
148 setRating(stars);
149 }
150 }
151 }
152
153 void DolphinFacetsWidget::setFacetType(const QString& type)
154 {
155 for (int index = 1; index <= m_typeSelector->count(); index++) {
156 if (type == m_typeSelector->itemData(index).toString()) {
157 m_typeSelector->setCurrentIndex(index);
158 break;
159 }
160 }
161 }
162
163 void DolphinFacetsWidget::setRating(const int stars)
164 {
165 if (stars < 0 || stars > 5) {
166 return;
167 }
168 m_ratingSelector->setCurrentIndex(stars);
169 }
170
171 void DolphinFacetsWidget::setTimespan(const QDate& date)
172 {
173 if (!date.isValid()) {
174 return;
175 }
176 m_dateSelector->setCurrentIndex(0);
177 for (int index = 1; index <= m_dateSelector->count(); index++) {
178 if (date >= m_dateSelector->itemData(index).toDate()) {
179 m_dateSelector->setCurrentIndex(index);
180 break;
181 }
182 }
183 }
184
185 void DolphinFacetsWidget::initComboBox(QComboBox* combo)
186 {
187 combo->setFrame(false);
188 combo->setMinimumHeight(parentWidget()->height());
189 combo->setCurrentIndex(0);
190 connect(combo, QOverload<int>::of(&QComboBox::activated), this, &DolphinFacetsWidget::facetChanged);
191 }
192