]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
Merge branch 'release/19.12'
[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->setContentsMargins(0, 0, 0, 0);
68 topLayout->addWidget(m_typeSelector);
69 topLayout->addWidget(m_dateSelector);
70 topLayout->addWidget(m_ratingSelector);
71
72 resetOptions();
73 }
74
75 DolphinFacetsWidget::~DolphinFacetsWidget()
76 {
77 }
78
79 void DolphinFacetsWidget::changeEvent(QEvent *event)
80 {
81 if (event->type() == QEvent::EnabledChange && !isEnabled()) {
82 resetOptions();
83 }
84 }
85
86 void DolphinFacetsWidget::resetOptions()
87 {
88 m_typeSelector->setCurrentIndex(0);
89 m_dateSelector->setCurrentIndex(0);
90 m_ratingSelector->setCurrentIndex(0);
91 }
92
93 QString DolphinFacetsWidget::ratingTerm() const
94 {
95 QStringList terms;
96
97 if (m_ratingSelector->currentIndex() > 0) {
98 const int rating = m_ratingSelector->currentData().toInt() * 2;
99 terms << QStringLiteral("rating>=%1").arg(rating);
100 }
101
102 if (m_dateSelector->currentIndex() > 0) {
103 const QDate date = m_dateSelector->currentData().toDate();
104 terms << QStringLiteral("modified>=%1").arg(date.toString(Qt::ISODate));
105 }
106
107 return terms.join(QLatin1String(" AND "));
108 }
109
110 QString DolphinFacetsWidget::facetType() const
111 {
112 return m_typeSelector->currentData().toString();
113 }
114
115 bool DolphinFacetsWidget::isRatingTerm(const QString& term) const
116 {
117 const QStringList subTerms = term.split(' ', QString::SkipEmptyParts);
118
119 // If term has sub terms, then sone of the sub terms are always "rating" and "modified" terms.
120 bool containsRating = false;
121 bool containsModified = false;
122
123 foreach (const QString& subTerm, subTerms) {
124 if (subTerm.startsWith(QLatin1String("rating>="))) {
125 containsRating = true;
126 } else if (subTerm.startsWith(QLatin1String("modified>="))) {
127 containsModified = true;
128 }
129 }
130
131 return containsModified || containsRating;
132 }
133
134 void DolphinFacetsWidget::setRatingTerm(const QString& term)
135 {
136 // If term has sub terms, then the sub terms are always "rating" and "modified" terms.
137 // If term has no sub terms, then the term itself is either a "rating" term or a "modified"
138 // term. To avoid code duplication we add term to subTerms list, if the list is empty.
139 QStringList subTerms = term.split(' ', QString::SkipEmptyParts);
140
141 foreach (const QString& subTerm, subTerms) {
142 if (subTerm.startsWith(QLatin1String("modified>="))) {
143 const QString value = subTerm.mid(10);
144 const QDate date = QDate::fromString(value, Qt::ISODate);
145 setTimespan(date);
146 } else if (subTerm.startsWith(QLatin1String("rating>="))) {
147 const QString value = subTerm.mid(8);
148 const int stars = value.toInt() / 2;
149 setRating(stars);
150 }
151 }
152 }
153
154 void DolphinFacetsWidget::setFacetType(const QString& type)
155 {
156 for (int index = 0; index <= m_typeSelector->count(); index++) {
157 if (type == m_typeSelector->itemData(index).toString()) {
158 m_typeSelector->setCurrentIndex(index);
159 break;
160 }
161 }
162 }
163
164 void DolphinFacetsWidget::setRating(const int stars)
165 {
166 if (stars < 0 || stars > 5) {
167 return;
168 }
169 m_ratingSelector->setCurrentIndex(stars);
170 }
171
172 void DolphinFacetsWidget::setTimespan(const QDate& date)
173 {
174 if (!date.isValid()) {
175 return;
176 }
177 m_dateSelector->setCurrentIndex(0);
178 for (int index = 1; index <= m_dateSelector->count(); index++) {
179 if (date >= m_dateSelector->itemData(index).toDate()) {
180 m_dateSelector->setCurrentIndex(index);
181 break;
182 }
183 }
184 }
185
186 void DolphinFacetsWidget::initComboBox(QComboBox* combo)
187 {
188 combo->setFrame(false);
189 combo->setMinimumHeight(parentWidget()->height());
190 combo->setCurrentIndex(0);
191 connect(combo, QOverload<int>::of(&QComboBox::activated), this, &DolphinFacetsWidget::facetChanged);
192 }
193