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>
15 #include <QHBoxLayout>
18 #include <QToolButton>
20 DolphinFacetsWidget::DolphinFacetsWidget(QWidget
* parent
) :
22 m_typeSelector(nullptr),
23 m_dateSelector(nullptr),
24 m_ratingSelector(nullptr),
25 m_tagsSelector(nullptr)
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
);
36 const QDate currentDate
= QDate::currentDate();
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
);
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
);
56 m_clearTagsAction
= new QAction(QIcon::fromTheme(QStringLiteral("edit-clear-all")), i18nc("@action:inmenu", "Clear Selection"), this);
57 connect(m_clearTagsAction
, &QAction::triggered
, this, [this]() {
59 Q_EMIT
facetChanged();
62 m_tagsSelector
= new QToolButton(this);
63 m_tagsSelector
->setIcon(QIcon::fromTheme(QStringLiteral("tag")));
64 m_tagsSelector
->setMenu(new QMenu(this));
65 m_tagsSelector
->setToolButtonStyle(Qt::ToolButtonTextBesideIcon
);
66 m_tagsSelector
->setPopupMode(QToolButton::MenuButtonPopup
);
67 m_tagsSelector
->setAutoRaise(true);
70 connect(m_tagsSelector
, &QToolButton::clicked
, m_tagsSelector
, &QToolButton::showMenu
);
71 connect(m_tagsSelector
->menu(), &QMenu::aboutToShow
, this, &DolphinFacetsWidget::updateTagsMenu
);
72 connect(&m_tagsLister
, &KCoreDirLister::itemsAdded
, this, &DolphinFacetsWidget::updateTagsMenuItems
);
75 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
76 topLayout
->setContentsMargins(0, 0, 0, 0);
77 topLayout
->addWidget(m_typeSelector
);
78 topLayout
->addWidget(m_dateSelector
);
79 topLayout
->addWidget(m_ratingSelector
);
80 topLayout
->addWidget(m_tagsSelector
);
85 DolphinFacetsWidget::~DolphinFacetsWidget()
89 void DolphinFacetsWidget::changeEvent(QEvent
*event
)
91 if (event
->type() == QEvent::EnabledChange
) {
100 void DolphinFacetsWidget::resetSearchTerms()
102 m_typeSelector
->setCurrentIndex(0);
103 m_dateSelector
->setCurrentIndex(0);
104 m_ratingSelector
->setCurrentIndex(0);
109 QStringList
DolphinFacetsWidget::searchTerms() const
113 if (m_ratingSelector
->currentIndex() > 0) {
114 const int rating
= m_ratingSelector
->currentData().toInt() * 2;
115 terms
<< QStringLiteral("rating>=%1").arg(rating
);
118 if (m_dateSelector
->currentIndex() > 0) {
119 const QDate date
= m_dateSelector
->currentData().toDate();
120 terms
<< QStringLiteral("modified>=%1").arg(date
.toString(Qt::ISODate
));
123 if (!m_searchTags
.isEmpty()) {
124 for (auto const &tag
: m_searchTags
) {
125 if (tag
.contains(QLatin1Char(' '))) {
126 terms
<< QStringLiteral("tag:\"%1\"").arg(tag
);
128 terms
<< QStringLiteral("tag:%1").arg(tag
);
136 QString
DolphinFacetsWidget::facetType() const
138 return m_typeSelector
->currentData().toString();
141 bool DolphinFacetsWidget::isSearchTerm(const QString
& term
) const
143 static const QLatin1String searchTokens
[] {
144 QLatin1String("modified>="),
145 QLatin1String("rating>="),
146 QLatin1String("tag:"), QLatin1String("tag=")
149 for (const auto &searchToken
: searchTokens
) {
150 if (term
.startsWith(searchToken
)) {
157 void DolphinFacetsWidget::setSearchTerm(const QString
& term
)
159 if (term
.startsWith(QLatin1String("modified>="))) {
160 const QString value
= term
.mid(10);
161 const QDate date
= QDate::fromString(value
, Qt::ISODate
);
163 } else if (term
.startsWith(QLatin1String("rating>="))) {
164 const QString value
= term
.mid(8);
165 const int stars
= value
.toInt() / 2;
167 } else if (term
.startsWith(QLatin1String("tag:")) ||
168 term
.startsWith(QLatin1String("tag="))) {
169 const QString value
= term
.mid(4);
174 void DolphinFacetsWidget::setFacetType(const QString
& type
)
176 for (int index
= 0; index
<= m_typeSelector
->count(); index
++) {
177 if (type
== m_typeSelector
->itemData(index
).toString()) {
178 m_typeSelector
->setCurrentIndex(index
);
184 void DolphinFacetsWidget::setRating(const int stars
)
186 if (stars
< 0 || stars
> 5) {
189 m_ratingSelector
->setCurrentIndex(stars
);
192 void DolphinFacetsWidget::setTimespan(const QDate
& date
)
194 if (!date
.isValid()) {
197 m_dateSelector
->setCurrentIndex(0);
198 for (int index
= 1; index
<= m_dateSelector
->count(); index
++) {
199 if (date
>= m_dateSelector
->itemData(index
).toDate()) {
200 m_dateSelector
->setCurrentIndex(index
);
206 void DolphinFacetsWidget::addSearchTag(const QString
& tag
)
208 if (tag
.isEmpty() || m_searchTags
.contains(tag
)) {
211 m_searchTags
.append(tag
);
213 updateTagsSelector();
216 void DolphinFacetsWidget::removeSearchTag(const QString
& tag
)
218 if (tag
.isEmpty() || !m_searchTags
.contains(tag
)) {
221 m_searchTags
.removeAll(tag
);
222 updateTagsSelector();
225 void DolphinFacetsWidget::resetSearchTags()
227 m_searchTags
= QStringList();
228 updateTagsSelector();
232 void DolphinFacetsWidget::initComboBox(QComboBox
* combo
)
234 combo
->setFrame(false);
235 combo
->setMinimumHeight(parentWidget()->height());
236 combo
->setCurrentIndex(0);
237 connect(combo
, QOverload
<int>::of(&QComboBox::activated
), this, &DolphinFacetsWidget::facetChanged
);
240 void DolphinFacetsWidget::updateTagsSelector()
242 const bool hasListedTags
= !m_tagsSelector
->menu()->isEmpty();
243 const bool hasSelectedTags
= !m_searchTags
.isEmpty();
245 if (hasSelectedTags
) {
246 const QString tagsText
= m_searchTags
.join(i18nc("String list separator", ", "));
247 m_tagsSelector
->setText(i18ncp("@action:button %2 is a list of tags",
248 "Tag: %2", "Tags: %2",m_searchTags
.count(), tagsText
));
250 m_tagsSelector
->setText(i18nc("@action:button", "Add Tags"));
253 m_tagsSelector
->setEnabled(isEnabled() && (hasListedTags
|| hasSelectedTags
));
254 m_clearTagsAction
->setEnabled(hasSelectedTags
);
257 void DolphinFacetsWidget::updateTagsMenu()
259 updateTagsMenuItems({}, {});
260 m_tagsLister
.openUrl(QUrl(QStringLiteral("tags:/")), KCoreDirLister::OpenUrlFlag::Reload
);
263 void DolphinFacetsWidget::updateTagsMenuItems(const QUrl
&, const KFileItemList
& items
)
265 QMenu
*tagsMenu
= m_tagsSelector
->menu();
268 QStringList allTags
= QStringList(m_searchTags
);
269 for (const KFileItem
&item
: items
) {
270 allTags
.append(item
.name());
272 allTags
.sort(Qt::CaseInsensitive
);
273 allTags
.removeDuplicates();
275 const bool onlyOneTag
= allTags
.count() == 1;
277 for (const QString
& tagName
: qAsConst(allTags
)) {
278 QAction
*action
= tagsMenu
->addAction(QIcon::fromTheme(QStringLiteral("tag")), tagName
);
279 action
->setCheckable(true);
280 action
->setChecked(m_searchTags
.contains(tagName
));
282 connect(action
, &QAction::triggered
, this, [this, tagName
, onlyOneTag
](bool isChecked
) {
284 addSearchTag(tagName
);
286 removeSearchTag(tagName
);
288 Q_EMIT
facetChanged();
291 m_tagsSelector
->menu()->show();
296 if (allTags
.count() > 1) {
297 tagsMenu
->addSeparator();
298 tagsMenu
->addAction(m_clearTagsAction
);
301 updateTagsSelector();