2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2019 Ismael Asensio <isma.af@mgmail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "dolphinfacetswidget.h"
10 #include <KLocalizedString>
11 #include <KProtocolInfo>
16 #include <QHBoxLayout>
19 #include <QToolButton>
21 DolphinFacetsWidget::DolphinFacetsWidget(QWidget
* parent
) :
23 m_typeSelector(nullptr),
24 m_dateSelector(nullptr),
25 m_ratingSelector(nullptr),
26 m_tagsSelector(nullptr)
28 m_typeSelector
= new QComboBox(this);
29 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("none")), i18nc("@item:inlistbox", "Any Type"), QString());
30 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("inode-directory")), i18nc("@item:inlistbox", "Folders") , QStringLiteral("Folder"));
31 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("text-x-generic")), i18nc("@item:inlistbox", "Documents") , QStringLiteral("Document"));
32 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("image-x-generic")), i18nc("@item:inlistbox", "Images") , QStringLiteral("Image"));
33 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("audio-x-generic")), i18nc("@item:inlistbox", "Audio Files"), QStringLiteral("Audio"));
34 m_typeSelector
->addItem(QIcon::fromTheme(QStringLiteral("video-x-generic")), i18nc("@item:inlistbox", "Videos") , QStringLiteral("Video"));
35 initComboBox(m_typeSelector
);
37 const QDate currentDate
= QDate::currentDate();
39 m_dateSelector
= new QComboBox(this);
40 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("view-calendar")), i18nc("@item:inlistbox", "Any Date"), QDate());
41 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Today") , currentDate
);
42 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("go-jump-today")), i18nc("@item:inlistbox", "Yesterday") , currentDate
.addDays(-1));
43 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-week")), i18nc("@item:inlistbox", "This Week") , currentDate
.addDays(1 - currentDate
.dayOfWeek()));
44 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-month")), i18nc("@item:inlistbox", "This Month"), currentDate
.addDays(1 - currentDate
.day()));
45 m_dateSelector
->addItem(QIcon::fromTheme(QStringLiteral("view-calendar-year")), i18nc("@item:inlistbox", "This Year") , currentDate
.addDays(1 - currentDate
.dayOfYear()));
46 initComboBox(m_dateSelector
);
48 m_ratingSelector
= new QComboBox(this);
49 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("non-starred-symbolic")), i18nc("@item:inlistbox", "Any Rating"), 0);
50 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "1 or more"), 1);
51 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "2 or more"), 2);
52 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "3 or more"), 3);
53 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "4 or more"), 4);
54 m_ratingSelector
->addItem(QIcon::fromTheme(QStringLiteral("starred-symbolic")), i18nc("@item:inlistbox", "Highest Rating"), 5);
55 initComboBox(m_ratingSelector
);
57 m_tagsSelector
= new QToolButton(this);
58 m_tagsSelector
->setIcon(QIcon::fromTheme(QStringLiteral("tag")));
59 m_tagsSelector
->setMenu(new QMenu(this));
60 m_tagsSelector
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
61 m_tagsSelector
->setPopupMode(QToolButton::MenuButtonPopup
);
62 m_tagsSelector
->setAutoRaise(true);
65 connect(m_tagsSelector
, &QToolButton::clicked
, m_tagsSelector
, &QToolButton::showMenu
);
66 connect(m_tagsSelector
->menu(), &QMenu::aboutToShow
, this, &DolphinFacetsWidget::updateTagsMenu
);
67 connect(&m_tagsLister
, &KCoreDirLister::itemsAdded
, this, &DolphinFacetsWidget::updateTagsMenuItems
);
70 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
71 topLayout
->setContentsMargins(0, 0, 0, 0);
72 topLayout
->addWidget(m_typeSelector
);
73 topLayout
->addWidget(m_dateSelector
);
74 topLayout
->addWidget(m_ratingSelector
);
75 topLayout
->addWidget(m_tagsSelector
);
80 DolphinFacetsWidget::~DolphinFacetsWidget()
84 void DolphinFacetsWidget::changeEvent(QEvent
*event
)
86 if (event
->type() == QEvent::EnabledChange
) {
95 void DolphinFacetsWidget::resetSearchTerms()
97 m_typeSelector
->setCurrentIndex(0);
98 m_dateSelector
->setCurrentIndex(0);
99 m_ratingSelector
->setCurrentIndex(0);
101 m_searchTags
= QStringList();
102 updateTagsSelector();
106 QStringList
DolphinFacetsWidget::searchTerms() const
110 if (m_ratingSelector
->currentIndex() > 0) {
111 const int rating
= m_ratingSelector
->currentData().toInt() * 2;
112 terms
<< QStringLiteral("rating>=%1").arg(rating
);
115 if (m_dateSelector
->currentIndex() > 0) {
116 const QDate date
= m_dateSelector
->currentData().toDate();
117 terms
<< QStringLiteral("modified>=%1").arg(date
.toString(Qt::ISODate
));
120 if (!m_searchTags
.isEmpty()) {
121 for (auto const &tag
: m_searchTags
) {
122 if (tag
.contains(QLatin1Char(' '))) {
123 terms
<< QStringLiteral("tag:\"%1\"").arg(tag
);
125 terms
<< QStringLiteral("tag:%1").arg(tag
);
133 QString
DolphinFacetsWidget::facetType() const
135 return m_typeSelector
->currentData().toString();
138 bool DolphinFacetsWidget::isSearchTerm(const QString
& term
) const
140 static const QLatin1String searchTokens
[] {
141 QLatin1String("modified>="),
142 QLatin1String("rating>="),
143 QLatin1String("tag:"), QLatin1String("tag=")
146 for (const auto &searchToken
: searchTokens
) {
147 if (term
.startsWith(searchToken
)) {
154 void DolphinFacetsWidget::setSearchTerm(const QString
& term
)
156 if (term
.startsWith(QLatin1String("modified>="))) {
157 const QString value
= term
.mid(10);
158 const QDate date
= QDate::fromString(value
, Qt::ISODate
);
160 } else if (term
.startsWith(QLatin1String("rating>="))) {
161 const QString value
= term
.mid(8);
162 const int stars
= value
.toInt() / 2;
164 } else if (term
.startsWith(QLatin1String("tag:")) ||
165 term
.startsWith(QLatin1String("tag="))) {
166 const QString value
= term
.mid(4);
171 void DolphinFacetsWidget::setFacetType(const QString
& type
)
173 for (int index
= 0; index
<= m_typeSelector
->count(); index
++) {
174 if (type
== m_typeSelector
->itemData(index
).toString()) {
175 m_typeSelector
->setCurrentIndex(index
);
181 void DolphinFacetsWidget::setRating(const int stars
)
183 if (stars
< 0 || stars
> 5) {
186 m_ratingSelector
->setCurrentIndex(stars
);
189 void DolphinFacetsWidget::setTimespan(const QDate
& date
)
191 if (!date
.isValid()) {
194 m_dateSelector
->setCurrentIndex(0);
195 for (int index
= 1; index
<= m_dateSelector
->count(); index
++) {
196 if (date
>= m_dateSelector
->itemData(index
).toDate()) {
197 m_dateSelector
->setCurrentIndex(index
);
203 void DolphinFacetsWidget::addSearchTag(const QString
& tag
)
205 if (tag
.isEmpty() || m_searchTags
.contains(tag
)) {
208 m_searchTags
.append(tag
);
210 updateTagsSelector();
213 void DolphinFacetsWidget::removeSearchTag(const QString
& tag
)
215 if (tag
.isEmpty() || !m_searchTags
.contains(tag
)) {
218 m_searchTags
.removeAll(tag
);
219 updateTagsSelector();
222 void DolphinFacetsWidget::initComboBox(QComboBox
* combo
)
224 combo
->setFrame(false);
225 combo
->setMinimumHeight(parentWidget()->height());
226 combo
->setCurrentIndex(0);
227 connect(combo
, QOverload
<int>::of(&QComboBox::activated
), this, &DolphinFacetsWidget::facetChanged
);
230 void DolphinFacetsWidget::updateTagsSelector()
232 const bool hasListedTags
= !m_tagsSelector
->menu()->isEmpty();
233 const bool hasSelectedTags
= !m_searchTags
.isEmpty();
235 if (hasSelectedTags
) {
236 const QString tagsText
= m_searchTags
.join(i18nc("String list separator", ", "));
237 m_tagsSelector
->setText(i18ncp("@action:button %2 is a list of tags",
238 "Tag: %2", "Tags: %2",m_searchTags
.count(), tagsText
));
240 m_tagsSelector
->setText(i18nc("@action:button", "Add Tags"));
243 m_tagsSelector
->setEnabled(isEnabled() && (hasListedTags
|| hasSelectedTags
));
246 void DolphinFacetsWidget::updateTagsMenu()
248 updateTagsMenuItems({}, {});
249 if (KProtocolInfo::isKnownProtocol(QStringLiteral("tags"))) {
250 m_tagsLister
.openUrl(QUrl(QStringLiteral("tags:/")), KCoreDirLister::OpenUrlFlag::Reload
);
254 void DolphinFacetsWidget::updateTagsMenuItems(const QUrl
&, const KFileItemList
& items
)
256 m_tagsSelector
->menu()->clear();
258 QStringList allTags
= QStringList(m_searchTags
);
259 for (const KFileItem
&item
: items
) {
260 allTags
.append(item
.name());
262 allTags
.sort(Qt::CaseInsensitive
);
263 allTags
.removeDuplicates();
265 const bool onlyOneTag
= allTags
.count() == 1;
267 for (const QString
& tagName
: qAsConst(allTags
)) {
268 QAction
* action
= m_tagsSelector
->menu()->addAction(QIcon::fromTheme(QStringLiteral("tag")), tagName
);
269 action
->setCheckable(true);
270 action
->setChecked(m_searchTags
.contains(tagName
));
272 connect(action
, &QAction::triggered
, this, [this, tagName
, onlyOneTag
](bool isChecked
) {
274 addSearchTag(tagName
);
276 removeSearchTag(tagName
);
278 Q_EMIT
facetChanged();
281 m_tagsSelector
->menu()->show();
286 updateTagsSelector();