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 // true when setMimeFilter was called
35 bool m_isMimeFilter = false;
36 QString m_lastFilter;
37 KFileFilter m_defaultFilter = KFileFilter::fromFilterString(i18nc("Default mime type filter that shows all file types", "*|All Files")).first();
38
39 QList<KFileFilter> m_filters;
40 bool m_allTypes;
41};
42
44 : KComboBox(true, parent)
45 , d(new KFileFilterComboPrivate(this))
46{
47 setTrapReturnKey(true);
51 connect(this, &KFileFilterCombo::filterChanged, this, [this]() {
52 d->slotFilterChanged();
53 });
54 d->m_allTypes = false;
55}
56
58
60{
61 clear();
62 d->m_filters.clear();
63 QString delim = QStringLiteral(", ");
64 bool hasAllFilesFilter = false;
66
67 if (types.isEmpty()) {
68 d->m_filters = {d->m_defaultFilter};
69 addItem(d->m_defaultFilter.label());
70
71 d->m_lastFilter = currentText();
72 return;
73 }
74
75 d->m_allTypes = defaultFilter.isEmpty() && (types.count() > 1);
76
77 if (!types.isEmpty() && types.first().mimePatterns().isEmpty()) {
78 d->m_allTypes = false;
79 }
80
81 // If there's MIME types that have the same comment, we will show the extension
82 // in addition to the MIME type comment
83 QHash<QString, int> allTypeComments;
84 for (const KFileFilter &filter : types) {
85 allTypeComments[filter.label()] += 1;
86 }
87
88 for (const KFileFilter &filter : types) {
89 if (!filter.isValid()) {
90 continue;
91 }
92
93 const QStringList mimeTypes = filter.mimePatterns();
94
95 const bool isAllFileFilters = std::any_of(mimeTypes.cbegin(), mimeTypes.cend(), [&db](const QString &mimeTypeName) {
96 const QMimeType type = db.mimeTypeForName(mimeTypeName);
97
98 if (!type.isValid()) {
99 qCWarning(KIO_KFILEWIDGETS_KFILEFILTERCOMBO) << mimeTypeName << "is not a valid MIME type";
100 return false;
101 }
102
103 return type.name().startsWith(QLatin1String("all/")) || type.isDefault();
104 });
105
106 if (isAllFileFilters) {
107 hasAllFilesFilter = true;
108 continue;
109 }
110
111 if (allTypeComments.value(filter.label()) > 1) {
112 QStringList mimeSuffixes;
113
114 for (const QString &mimeTypeName : filter.mimePatterns()) {
115 const QMimeType type = db.mimeTypeForName(mimeTypeName);
116 mimeSuffixes << type.suffixes();
117 }
118
119 const QString label = i18nc("%1 is the mimetype name, %2 is the extensions", "%1 (%2)", filter.label(), mimeSuffixes.join(QLatin1String(", ")));
120 KFileFilter newFilter(label, filter.filePatterns(), filter.mimePatterns());
121
122 d->m_filters.append(newFilter);
123 addItem(newFilter.label());
124 } else {
125 d->m_filters.append(filter);
126 addItem(filter.label());
127 }
128
129 if (filter == defaultFilter) {
130 setCurrentIndex(count() - 1);
131 }
132 }
133
134 if (count() == 1) {
135 d->m_allTypes = false;
136 }
137
138 if (d->m_allTypes) {
139 QStringList allMimePatterns;
140 QStringList allFilePatterns;
141 for (const KFileFilter &filter : std::as_const(d->m_filters)) {
142 allMimePatterns << filter.mimePatterns();
143 allFilePatterns << filter.filePatterns();
144 }
145
146 KFileFilter allSupportedFilesFilter;
147
148 if (count() <= 3) { // show the MIME type comments of at max 3 types
149 QStringList allComments;
150 for (const KFileFilter &filter : std::as_const(d->m_filters)) {
151 allComments << filter.label();
152 }
153
154 allSupportedFilesFilter = KFileFilter(allComments.join(delim), allFilePatterns, allMimePatterns);
155 } else {
156 allSupportedFilesFilter = KFileFilter(i18n("All Supported Files"), allFilePatterns, allMimePatterns);
157 }
158
159 insertItem(0, allSupportedFilesFilter.label());
160 d->m_filters.prepend(allSupportedFilesFilter);
161 setCurrentIndex(0);
162 }
163
164 if (hasAllFilesFilter) {
165 addItem(i18n("All Files"));
166
167 KFileFilter allFilter(i18n("All Files"), {}, {QStringLiteral("application/octet-stream")});
168
169 d->m_filters.append(allFilter);
170
171 if (defaultFilter == allFilter) {
172 setCurrentIndex(count() - 1);
173 }
174 }
175
176 d->m_lastFilter = currentText();
177}
178
180{
181 if (currentText() != itemText(currentIndex())) {
182 // The user edited the text
183
184 const QList<KFileFilter> filter = KFileFilter::fromFilterString(currentText());
185
186 if (!filter.isEmpty()) {
187 return filter.first();
188 } else {
189 return KFileFilter();
190 }
191 } else {
192 if (currentIndex() == -1) {
193 return KFileFilter();
194 }
195
196 return d->m_filters[currentIndex()];
197 }
198}
199
201{
202 return d->m_allTypes;
203}
204
206{
207 return d->m_filters;
208}
209
211{
212 auto it = std::find(d->m_filters.cbegin(), d->m_filters.cend(), filter);
213
214 if (it == d->m_filters.cend()) {
215 qCWarning(KIO_KFILEWIDGETS_KFILEFILTERCOMBO) << "KFileFilterCombo::setCurrentFilter: Could not find file filter" << filter;
216 setCurrentIndex(-1);
218 return;
219 }
220
221 setCurrentIndex(std::distance(d->m_filters.cbegin(), it));
223}
224
225void KFileFilterComboPrivate::slotFilterChanged()
226{
227 m_lastFilter = q->currentText();
228}
229
230bool KFileFilterCombo::eventFilter(QObject *o, QEvent *e)
231{
232 if (o == lineEdit() && e->type() == QEvent::FocusOut) {
233 if (currentText() != d->m_lastFilter) {
235 }
236 }
237
238 return KComboBox::eventFilter(o, e);
239}
240
242{
243 d->m_defaultFilter = filter;
244}
245
247{
248 return d->m_defaultFilter;
249}
250
251#include "moc_kfilefiltercombo.cpp"
void returnPressed(const QString &text)
void setTrapReturnKey(bool trap)
KComboBox(bool rw, QWidget *parent=nullptr)
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.
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)
QObject * parent() const const
QString join(QChar separator) const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QWidget(QWidget *parent, Qt::WindowFlags f)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:49:37 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.