KIO

kfilefiltercombo.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: Stephan Kulow <coolo@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kfilefiltercombo.h"
9#include "kfilefilter.h"
10#include "kfilefiltercombo_debug.h"
11
12#include <KLocalizedString>
13#include <QDebug>
14#include <QEvent>
15#include <QLineEdit>
16#include <QMimeDatabase>
17
18#include <config-kiofilewidgets.h>
19
20#include <algorithm>
21#include <utility>
22
23class KFileFilterComboPrivate
24{
25public:
26 explicit KFileFilterComboPrivate(KFileFilterCombo *qq)
27 : q(qq)
28 {
29 }
30
31 void slotFilterChanged();
32
33 KFileFilterCombo *const q;
34 // when we have more than 3 mimefilters and no default-filter,
35 // we don't show the comments of all mimefilters in one line,
36 // instead we show "All supported files". We have to translate
37 // that back to the list of mimefilters in currentFilter() tho.
38 bool m_hasAllSupportedFiles = false;
39 // true when setMimeFilter was called
40 bool m_isMimeFilter = false;
41 QString m_lastFilter;
42 KFileFilter m_defaultFilter = KFileFilter::fromFilterString(i18nc("Default mime type filter that shows all file types", "*|All Files")).first();
43
44 QList<KFileFilter> m_filters;
45 bool m_allTypes;
46};
47
49 : KComboBox(true, parent)
50 , d(new KFileFilterComboPrivate(this))
51{
52 setTrapReturnKey(true);
56 connect(this, &KFileFilterCombo::filterChanged, this, [this]() {
57 d->slotFilterChanged();
58 });
59 d->m_allTypes = false;
60}
61
63
64void KFileFilterCombo::setFilters(const QList<KFileFilter> &types, const KFileFilter &defaultFilter)
65{
66 clear();
67 d->m_filters.clear();
68 QString delim = QStringLiteral(", ");
69 d->m_hasAllSupportedFiles = false;
70 bool hasAllFilesFilter = false;
72
73 if (types.isEmpty()) {
74 d->m_filters = {d->m_defaultFilter};
75 addItem(d->m_defaultFilter.label());
76
77 d->m_lastFilter = currentText();
78 return;
79 }
80
81 d->m_allTypes = defaultFilter.isEmpty() && (types.count() > 1);
82
83 if (!types.isEmpty() && types.first().mimePatterns().isEmpty()) {
84 d->m_allTypes = false;
85 }
86
87 // If there's MIME types that have the same comment, we will show the extension
88 // in addition to the MIME type comment
89 QHash<QString, int> allTypeComments;
90 for (const KFileFilter &filter : types) {
91 allTypeComments[filter.label()] += 1;
92 }
93
94 for (const KFileFilter &filter : types) {
95 if (!filter.isValid()) {
96 continue;
97 }
98
99 const QStringList mimeTypes = filter.mimePatterns();
100
101 const bool isAllFileFilters = std::any_of(mimeTypes.cbegin(), mimeTypes.cend(), [&db](const QString &mimeTypeName) {
102 const QMimeType type = db.mimeTypeForName(mimeTypeName);
103
104 if (!type.isValid()) {
105 qCWarning(KIO_KFILEWIDGETS_KFILEFILTERCOMBO) << mimeTypeName << "is not a valid MIME type";
106 return false;
107 }
108
109 return type.name().startsWith(QLatin1String("all/")) || type.isDefault();
110 });
111
112 if (isAllFileFilters) {
113 hasAllFilesFilter = true;
114 continue;
115 }
116
117 if (allTypeComments.value(filter.label()) > 1) {
118 QStringList mimeSuffixes;
119
120 for (const QString &mimeTypeName : filter.mimePatterns()) {
121 const QMimeType type = db.mimeTypeForName(mimeTypeName);
122 mimeSuffixes << type.suffixes();
123 }
124
125 const QString label = i18nc("%1 is the mimetype name, %2 is the extensions", "%1 (%2)", filter.label(), mimeSuffixes.join(QLatin1String(", ")));
126 KFileFilter newFilter(label, filter.filePatterns(), filter.mimePatterns());
127
128 d->m_filters.append(newFilter);
129 addItem(newFilter.label());
130 } else {
131 d->m_filters.append(filter);
132 addItem(filter.label());
133 }
134
135 if (filter == defaultFilter) {
136 setCurrentIndex(count() - 1);
137 }
138 }
139
140 if (count() == 1) {
141 d->m_allTypes = false;
142 }
143
144 if (d->m_allTypes) {
145 QStringList allMimePatterns;
146 QStringList allFilePatterns;
147 for (const KFileFilter &filter : std::as_const(d->m_filters)) {
148 allMimePatterns << filter.mimePatterns();
149 allFilePatterns << filter.filePatterns();
150 }
151
152 KFileFilter allSupportedFilesFilter;
153
154 if (count() <= 3) { // show the MIME type comments of at max 3 types
155 QStringList allComments;
156 for (const KFileFilter &filter : std::as_const(d->m_filters)) {
157 allComments << filter.label();
158 }
159
160 allSupportedFilesFilter = KFileFilter(allComments.join(delim), allFilePatterns, allMimePatterns);
161 } else {
162 allSupportedFilesFilter = KFileFilter(i18n("All Supported Files"), allMimePatterns, allMimePatterns);
163 d->m_hasAllSupportedFiles = true;
164 }
165
166 insertItem(0, allSupportedFilesFilter.label());
167 d->m_filters.prepend(allSupportedFilesFilter);
168 setCurrentIndex(0);
169 }
170
171 if (hasAllFilesFilter) {
172 addItem(i18n("All Files"));
173
174 KFileFilter allFilter(i18n("All Files"), {}, {QStringLiteral("application/octet-stream")});
175
176 d->m_filters.append(allFilter);
177
178 if (defaultFilter == allFilter) {
179 setCurrentIndex(count() - 1);
180 }
181 }
182
183 d->m_lastFilter = currentText();
184}
185
187{
188 if (currentText() != itemText(currentIndex())) {
189 // The user edited the text
190
191 const QList<KFileFilter> filter = KFileFilter::fromFilterString(currentText());
192
193 if (!filter.isEmpty()) {
194 return filter.first();
195 } else {
196 return KFileFilter();
197 }
198 } else {
199 if (currentIndex() == -1) {
200 return KFileFilter();
201 }
202
203 return d->m_filters[currentIndex()];
204 }
205}
206
208{
209 return d->m_allTypes;
210}
211
213{
214 return d->m_filters;
215}
216
218{
219 auto it = std::find(d->m_filters.cbegin(), d->m_filters.cend(), filter);
220
221 if (it == d->m_filters.cend()) {
222 qCWarning(KIO_KFILEWIDGETS_KFILEFILTERCOMBO) << "KFileFilterCombo::setCurrentFilter: Could not find file filter" << filter;
223 setCurrentIndex(-1);
225 return;
226 }
227
228 setCurrentIndex(std::distance(d->m_filters.cbegin(), it));
230}
231
232void KFileFilterComboPrivate::slotFilterChanged()
233{
234 m_lastFilter = q->currentText();
235}
236
237bool KFileFilterCombo::eventFilter(QObject *o, QEvent *e)
238{
239 if (o == lineEdit() && e->type() == QEvent::FocusOut) {
240 if (currentText() != d->m_lastFilter) {
242 }
243 }
244
245 return KComboBox::eventFilter(o, e);
246}
247
249{
250 d->m_defaultFilter = filter;
251}
252
254{
255 return d->m_defaultFilter;
256}
257
258#include "moc_kfilefiltercombo.cpp"
void returnPressed(const QString &text)
void setTrapReturnKey(bool trap)
File filter combo box.
void setFilters(const QList< KFileFilter > &filters, const KFileFilter &defaultFilter=KFileFilter())
Sets the filters to be used.
bool showsAllTypes() const
KFileFilter defaultFilter() const
KFileFilter currentFilter() const
The currently selected/active filter.
QList< KFileFilter > filters() const
The current filters.
void setCurrentFilter(const KFileFilter &filter)
Sets the current filter.
void setDefaultFilter(const KFileFilter &filter)
This method allows to set a default-filter, that is used when an empty filter is set.
void filterChanged()
This signal is emitted whenever the filter has been changed.
~KFileFilterCombo() override
Destroys the filter combo box.
KFileFilterCombo(QWidget *parent=nullptr)
Creates a new filter combo box.
Encapsulates rules to filter a list of files.
Definition kfilefilter.h:29
QString label() const
The user-facing label for this filter.
bool isEmpty() const
Whether the filer is empty, i.e. matches all files.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void activated(int index)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void clear()
void setCurrentIndex(int index)
void setInsertPolicy(InsertPolicy policy)
QString itemText(int index) const const
QLineEdit * lineEdit() const const
Type type() const const
T value(const Key &key) const const
const_iterator cbegin() const const
const_iterator cend() const const
qsizetype count() const const
T & first()
bool isEmpty() const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool eventFilter(QObject *watched, QEvent *event)
QString join(QChar separator) const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.