]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinfacetswidget.cpp
Merge branch 'release/20.08' into master
[dolphin.git] / src / search / dolphinfacetswidget.cpp
1 /*
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2019 Ismael Asensio <isma.af@mgmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "dolphinfacetswidget.h"
9
10 #include <KLocalizedString>
11
12 #include <QComboBox>
13 #include <QDate>
14 #include <QEvent>
15 #include <QHBoxLayout>
16 #include <QIcon>
17 #include <QMenu>
18 #include <QToolButton>
19
20 DolphinFacetsWidget::DolphinFacetsWidget(QWidget* parent) :
21 QWidget(parent),
22 m_typeSelector(nullptr),
23 m_dateSelector(nullptr),
24 m_ratingSelector(nullptr),
25 m_tagsSelector(nullptr)
26 {
27 m_typeSelector = new QComboBox(this);
28 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("none")), i18nc("@item:inlistbox", "Any Type"), QString());
29 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("inode-directory")), i18nc("@item:inlistbox", "Folders") , QStringLiteral("Folder"));
30 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("text-x-generic")), i18nc("@item:inlistbox", "Documents") , QStringLiteral("Document"));
31 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("image-x-generic")), i18nc("@item:inlistbox", "Images") , QStringLiteral("Image"));
32 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("audio-x-generic")), i18nc("@item:inlistbox", "Audio Files"), QStringLiteral("Audio"));
33 m_typeSelector->addItem(QIcon::fromTheme(QStringLiteral("video-x-generic")), i18nc("@item:inlistbox", "Videos") , QStringLiteral("Video"));
34 initComboBox(m_typeSelector);
35
36 const QDate currentDate = QDate::currentDate();
37
38 m_dateSelector = new QComboBox(this);
39 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar")), i18nc("@item:inlistbox", "Any Date"), QDate());
40 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Today") , currentDate);
41 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Yesterday") , currentDate.addDays(-1));
42 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-week")), i18nc("@item:inlistbox", "This Week") , currentDate.addDays(1 - currentDate.dayOfWeek()));
43 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-month")), i18nc("@item:inlistbox", "This Month"), currentDate.addDays(1 - currentDate.day()));
44 m_dateSelector->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-year")), i18nc("@item:inlistbox", "This Year") , currentDate.addDays(1 - currentDate.dayOfYear()));
45 initComboBox(m_dateSelector);
46
47 m_ratingSelector = new QComboBox(this);
48 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("non-starred-symbolic")), i18nc("@item:inlistbox", "Any Rating"), 0);
49 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "1 or more"), 1);
50 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "2 or more"), 2);
51 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "3 or more"), 3);
52 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "4 or more"), 4);
53 m_ratingSelector->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "Highest Rating"), 5);
54 initComboBox(m_ratingSelector);
55
56 m_tagsSelector = new QToolButton(this);
57 m_tagsSelector->setIcon(QIcon::fromTheme(QStringLiteral("tag")));
58 m_tagsSelector->setMenu(new QMenu(this));
59 m_tagsSelector->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
60 m_tagsSelector->setPopupMode(QToolButton::MenuButtonPopup);
61 m_tagsSelector->setAutoRaise(true);
62 updateTagsSelector();
63
64 connect(m_tagsSelector, &QToolButton::clicked, m_tagsSelector, &QToolButton::showMenu);
65 connect(m_tagsSelector->menu(), &QMenu::aboutToShow, this, &DolphinFacetsWidget::updateTagsMenu);
66 connect(&m_tagsLister, &KCoreDirLister::itemsAdded, this, &DolphinFacetsWidget::updateTagsMenuItems);
67 updateTagsMenu();
68
69 QHBoxLayout* topLayout = new QHBoxLayout(this);
70 topLayout->setContentsMargins(0, 0, 0, 0);
71 topLayout->addWidget(m_typeSelector);
72 topLayout->addWidget(m_dateSelector);
73 topLayout->addWidget(m_ratingSelector);
74 topLayout->addWidget(m_tagsSelector);
75
76 resetSearchTerms();
77 }
78
79 DolphinFacetsWidget::~DolphinFacetsWidget()
80 {
81 }
82
83 void DolphinFacetsWidget::changeEvent(QEvent *event)
84 {
85 if (event->type() == QEvent::EnabledChange) {
86 if (isEnabled()) {
87 updateTagsSelector();
88 } else {
89 resetSearchTerms();
90 }
91 }
92 }
93
94 void DolphinFacetsWidget::resetSearchTerms()
95 {
96 m_typeSelector->setCurrentIndex(0);
97 m_dateSelector->setCurrentIndex(0);
98 m_ratingSelector->setCurrentIndex(0);
99
100 m_searchTags = QStringList();
101 updateTagsSelector();
102 updateTagsMenu();
103 }
104
105 QStringList DolphinFacetsWidget::searchTerms() const
106 {
107 QStringList terms;
108
109 if (m_ratingSelector->currentIndex() > 0) {
110 const int rating = m_ratingSelector->currentData().toInt() * 2;
111 terms << QStringLiteral("rating>=%1").arg(rating);
112 }
113
114 if (m_dateSelector->currentIndex() > 0) {
115 const QDate date = m_dateSelector->currentData().toDate();
116 terms << QStringLiteral("modified>=%1").arg(date.toString(Qt::ISODate));
117 }
118
119 if (!m_searchTags.isEmpty()) {
120 for (auto const &tag : m_searchTags) {
121 if (tag.contains(QLatin1Char(' '))) {
122 terms << QStringLiteral("tag:\"%1\"").arg(tag);
123 } else {
124 terms << QStringLiteral("tag:%1").arg(tag);
125 }
126 }
127 }
128
129 return terms;
130 }
131
132 QString DolphinFacetsWidget::facetType() const
133 {
134 return m_typeSelector->currentData().toString();
135 }
136
137 bool DolphinFacetsWidget::isSearchTerm(const QString& term) const
138 {
139 static const QLatin1String searchTokens[] {
140 QLatin1String("modified>="),
141 QLatin1String("rating>="),
142 QLatin1String("tag:"), QLatin1String("tag=")
143 };
144
145 for (const auto &searchToken : searchTokens) {
146 if (term.startsWith(searchToken)) {
147 return true;
148 }
149 }
150 return false;
151 }
152
153 void DolphinFacetsWidget::setSearchTerm(const QString& term)
154 {
155 if (term.startsWith(QLatin1String("modified>="))) {
156 const QString value = term.mid(10);
157 const QDate date = QDate::fromString(value, Qt::ISODate);
158 setTimespan(date);
159 } else if (term.startsWith(QLatin1String("rating>="))) {
160 const QString value = term.mid(8);
161 const int stars = value.toInt() / 2;
162 setRating(stars);
163 } else if (term.startsWith(QLatin1String("tag:")) ||
164 term.startsWith(QLatin1String("tag="))) {
165 const QString value = term.mid(4);
166 addSearchTag(value);
167 }
168 }
169
170 void DolphinFacetsWidget::setFacetType(const QString& type)
171 {
172 for (int index = 0; index <= m_typeSelector->count(); index++) {
173 if (type == m_typeSelector->itemData(index).toString()) {
174 m_typeSelector->setCurrentIndex(index);
175 break;
176 }
177 }
178 }
179
180 void DolphinFacetsWidget::setRating(const int stars)
181 {
182 if (stars < 0 || stars > 5) {
183 return;
184 }
185 m_ratingSelector->setCurrentIndex(stars);
186 }
187
188 void DolphinFacetsWidget::setTimespan(const QDate& date)
189 {
190 if (!date.isValid()) {
191 return;
192 }
193 m_dateSelector->setCurrentIndex(0);
194 for (int index = 1; index <= m_dateSelector->count(); index++) {
195 if (date >= m_dateSelector->itemData(index).toDate()) {
196 m_dateSelector->setCurrentIndex(index);
197 break;
198 }
199 }
200 }
201
202 void DolphinFacetsWidget::addSearchTag(const QString& tag)
203 {
204 if (tag.isEmpty() || m_searchTags.contains(tag)) {
205 return;
206 }
207 m_searchTags.append(tag);
208 m_searchTags.sort();
209 updateTagsSelector();
210 }
211
212 void DolphinFacetsWidget::removeSearchTag(const QString& tag)
213 {
214 if (tag.isEmpty() || !m_searchTags.contains(tag)) {
215 return;
216 }
217 m_searchTags.removeAll(tag);
218 updateTagsSelector();
219 }
220
221 void DolphinFacetsWidget::initComboBox(QComboBox* combo)
222 {
223 combo->setFrame(false);
224 combo->setMinimumHeight(parentWidget()->height());
225 combo->setCurrentIndex(0);
226 connect(combo, QOverload<int>::of(&QComboBox::activated), this, &DolphinFacetsWidget::facetChanged);
227 }
228
229 void DolphinFacetsWidget::updateTagsSelector()
230 {
231 const bool hasListedTags = !m_tagsSelector->menu()->isEmpty();
232 const bool hasSelectedTags = !m_searchTags.isEmpty();
233
234 if (hasSelectedTags) {
235 const QString tagsText = m_searchTags.join(i18nc("String list separator", ", "));
236 m_tagsSelector->setText(i18ncp("@action:button %2 is a list of tags",
237 "Tag: %2", "Tags: %2",m_searchTags.count(), tagsText));
238 } else {
239 m_tagsSelector->setText(i18nc("@action:button", "Add Tags"));
240 }
241
242 m_tagsSelector->setEnabled(isEnabled() && (hasListedTags || hasSelectedTags));
243 }
244
245 void DolphinFacetsWidget::updateTagsMenu()
246 {
247 updateTagsMenuItems({}, {});
248 m_tagsLister.openUrl(QUrl(QStringLiteral("tags:/")), KCoreDirLister::OpenUrlFlag::Reload);
249 }
250
251 void DolphinFacetsWidget::updateTagsMenuItems(const QUrl&, const KFileItemList& items)
252 {
253 m_tagsSelector->menu()->clear();
254
255 QStringList allTags = QStringList(m_searchTags);
256 for (const KFileItem &item: items) {
257 allTags.append(item.name());
258 }
259 allTags.sort(Qt::CaseInsensitive);
260 allTags.removeDuplicates();
261
262 const bool onlyOneTag = allTags.count() == 1;
263
264 for (const QString& tagName : qAsConst(allTags)) {
265 QAction* action = m_tagsSelector->menu()->addAction(QIcon::fromTheme(QStringLiteral("tag")), tagName);
266 action->setCheckable(true);
267 action->setChecked(m_searchTags.contains(tagName));
268
269 connect(action, &QAction::triggered, this, [this, tagName, onlyOneTag](bool isChecked) {
270 if (isChecked) {
271 addSearchTag(tagName);
272 } else {
273 removeSearchTag(tagName);
274 }
275 emit facetChanged();
276
277 if (!onlyOneTag) {
278 m_tagsSelector->menu()->show();
279 }
280 });
281 }
282
283 updateTagsSelector();
284 }