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 allTypes;
146 for (const KFileFilter &filter : std::as_const(d->m_filters)) {
147 allTypes << filter.mimePatterns().join(QLatin1Char(' '));
148 }
149
150 KFileFilter allSupportedFilesFilter;
151
152 if (count() <= 3) { // show the MIME type comments of at max 3 types
153 QStringList allComments;
154 for (const KFileFilter &filter : std::as_const(d->m_filters)) {
155 allComments << filter.label();
156 }
157
158 allSupportedFilesFilter = KFileFilter(allComments.join(delim), {}, allTypes);
159 } else {
160 allSupportedFilesFilter = KFileFilter(i18n("All Supported Files"), {}, allTypes);
161 d->m_hasAllSupportedFiles = true;
162 }
163
164 insertItem(0, allSupportedFilesFilter.label());
165 d->m_filters.prepend(allSupportedFilesFilter);
166 setCurrentIndex(0);
167 }
168
169 if (hasAllFilesFilter) {
170 addItem(i18n("All Files"));
171
172 KFileFilter allFilter(i18n("All Files"), {}, {QStringLiteral("application/octet-stream")});
173
174 d->m_filters.append(allFilter);
175
176 if (defaultFilter == allFilter) {
177 setCurrentIndex(count() - 1);
178 }
179 }
180
181 d->m_lastFilter = currentText();
182}
183
185{
186 if (currentText() != itemText(currentIndex())) {
187 // The user edited the text
188
189 const QList<KFileFilter> filter = KFileFilter::fromFilterString(currentText());
190
191 if (!filter.isEmpty()) {
192 return filter.first();
193 } else {
194 return KFileFilter();
195 }
196 } else {
197 if (currentIndex() == -1) {
198 return KFileFilter();
199 }
200
201 return d->m_filters[currentIndex()];
202 }
203}
204
206{
207 return d->m_allTypes;
208}
209
211{
212 return d->m_filters;
213}
214
216{
217 auto it = std::find(d->m_filters.cbegin(), d->m_filters.cend(), filter);
218
219 if (it == d->m_filters.cend()) {
220 qCWarning(KIO_KFILEWIDGETS_KFILEFILTERCOMBO) << "KFileFilterCombo::setCurrentFilter: Could not find file filter" << filter;
221 setCurrentIndex(-1);
223 return;
224 }
225
226 setCurrentIndex(std::distance(d->m_filters.cbegin(), it));
228}
229
230void KFileFilterComboPrivate::slotFilterChanged()
231{
232 m_lastFilter = q->currentText();
233}
234
235bool KFileFilterCombo::eventFilter(QObject *o, QEvent *e)
236{
237 if (o == lineEdit() && e->type() == QEvent::FocusOut) {
238 if (currentText() != d->m_lastFilter) {
240 }
241 }
242
243 return KComboBox::eventFilter(o, e);
244}
245
247{
248 d->m_defaultFilter = filter;
249}
250
252{
253 return d->m_defaultFilter;
254}
255
256#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 Mon Nov 18 2024 12:16:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.