• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KFile

  • kfile
kfilefiltercombo.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) Stephan Kulow <coolo@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kfilefiltercombo.h"
21
22#include <kdebug.h>
23#include <klocale.h>
24#include <kmimetype.h>
25#include <config-kfile.h>
26#include <QtCore/QEvent>
27#include <QtGui/QLineEdit>
28
29class KFileFilterCombo::Private
30{
31public:
32 Private( KFileFilterCombo *_parent )
33 : parent(_parent),
34 hasAllSupportedFiles(false),
35 isMimeFilter(false),
36 defaultFilter(i18n("*|All Files"))
37 {
38 }
39
40 void _k_slotFilterChanged();
41
42 KFileFilterCombo *parent;
43 // when we have more than 3 mimefilters and no default-filter,
44 // we don't show the comments of all mimefilters in one line,
45 // instead we show "All supported files". We have to translate
46 // that back to the list of mimefilters in currentFilter() tho.
47 bool hasAllSupportedFiles;
48 // true when setMimeFilter was called
49 bool isMimeFilter;
50 QString lastFilter;
51 QString defaultFilter;
52
53 QStringList m_filters;
54 bool m_allTypes;
55};
56
57KFileFilterCombo::KFileFilterCombo( QWidget *parent)
58 : KComboBox(true, parent), d( new Private(this) )
59{
60 setTrapReturnKey( true );
61 setInsertPolicy(QComboBox::NoInsert);
62 connect( this, SIGNAL(activated(int)), this, SIGNAL(filterChanged()));
63 connect( this, SIGNAL(returnPressed()), this, SIGNAL(filterChanged()));
64 connect( this, SIGNAL(filterChanged()), SLOT(_k_slotFilterChanged()));
65 d->m_allTypes = false;
66}
67
68KFileFilterCombo::~KFileFilterCombo()
69{
70 delete d;
71}
72
73void KFileFilterCombo::setFilter(const QString& filter)
74{
75 clear();
76 d->m_filters.clear();
77 d->hasAllSupportedFiles = false;
78
79 if (!filter.isEmpty()) {
80 QString tmp = filter;
81 int index = tmp.indexOf('\n');
82 while (index > 0) {
83 d->m_filters.append(tmp.left(index));
84 tmp = tmp.mid(index + 1);
85 index = tmp.indexOf('\n');
86 }
87 d->m_filters.append(tmp);
88 }
89 else
90 d->m_filters.append( d->defaultFilter );
91
92 QStringList::ConstIterator it;
93 QStringList::ConstIterator end(d->m_filters.constEnd());
94 for (it = d->m_filters.constBegin(); it != end; ++it) {
95 int tab = (*it).indexOf('|');
96 addItem((tab < 0) ? *it :
97 (*it).mid(tab + 1));
98 }
99
100 d->lastFilter = currentText();
101 d->isMimeFilter = false;
102}
103
104QString KFileFilterCombo::currentFilter() const
105{
106 QString f = currentText();
107 if (f == itemText(currentIndex())) { // user didn't edit the text
108 f = d->m_filters.value(currentIndex());
109 if ( d->isMimeFilter || (currentIndex() == 0 && d->hasAllSupportedFiles) ) {
110 return f; // we have a mimetype as filter
111 }
112 }
113
114 int tab = f.indexOf('|');
115 if (tab < 0)
116 return f;
117 else
118 return f.left(tab);
119}
120
121bool KFileFilterCombo::showsAllTypes() const
122{
123 return d->m_allTypes;
124}
125
126QStringList KFileFilterCombo::filters() const
127{
128 return d->m_filters;
129}
130
131void KFileFilterCombo::setCurrentFilter( const QString& filter )
132{
133 setCurrentIndex(d->m_filters.indexOf(filter));
134 filterChanged();
135}
136
137void KFileFilterCombo::setMimeFilter( const QStringList& types,
138 const QString& defaultType )
139{
140 clear();
141 d->m_filters.clear();
142 QString delim = QLatin1String(", ");
143 d->hasAllSupportedFiles = false;
144 bool hasAllFilesFilter = false;
145
146 d->m_allTypes = defaultType.isEmpty() && (types.count() > 1);
147
148 QString allComments, allTypes;
149 for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
150 {
151 kDebug(kfile_area) << *it;
152 KMimeType::Ptr type = KMimeType::mimeType( *it );
153
154 if (!type) {
155 kDebug(kfile_area) << "Could not create mimetype!\n";
156 continue;
157 }
158
159 if ( type->name().startsWith( QLatin1String( "all/" ) ) ) {
160 hasAllFilesFilter = true;
161 continue;
162 }
163
164 if ( d->m_allTypes && it != types.begin() ) {
165 allComments += delim;
166 allTypes += ' ';
167 }
168
169 d->m_filters.append( type->name() );
170 if ( d->m_allTypes )
171 {
172 allTypes += type->name();
173 allComments += type->comment();
174 }
175 addItem( type->comment() );
176 if ( type->name() == defaultType )
177 setCurrentIndex( count() - 1 );
178 }
179
180 if ( d->m_allTypes )
181 {
182 if ( count() <= 3 ) // show the mime-comments of at max 3 types
183 insertItem(0, allComments);
184 else {
185 insertItem(0, i18n("All Supported Files"));
186 d->hasAllSupportedFiles = true;
187 }
188 setCurrentIndex( 0 );
189
190 d->m_filters.prepend( allTypes );
191 }
192
193 if ( hasAllFilesFilter ) {
194 addItem(i18n("All Files"));
195 d->m_filters.append( QLatin1String("all/allfiles") );
196 }
197
198 d->lastFilter = currentText();
199 d->isMimeFilter = true;
200}
201
202void KFileFilterCombo::Private::_k_slotFilterChanged()
203{
204 lastFilter = parent->currentText();
205}
206
207bool KFileFilterCombo::eventFilter( QObject *o, QEvent *e )
208{
209 if ( o == lineEdit() && e->type() == QEvent::FocusOut ) {
210 if ( currentText() != d->lastFilter )
211 emit filterChanged();
212 }
213
214 return KComboBox::eventFilter( o, e );
215}
216
217void KFileFilterCombo::setDefaultFilter( const QString& filter )
218{
219 d->defaultFilter = filter;
220}
221
222QString KFileFilterCombo::defaultFilter() const
223{
224 return d->defaultFilter;
225}
226
227bool KFileFilterCombo::isMimeFilter() const
228{
229 return d->isMimeFilter;
230}
231
232#include "kfilefiltercombo.moc"
KComboBox
KComboBox::eventFilter
virtual bool eventFilter(QObject *, QEvent *)
KComboBox::setTrapReturnKey
void setTrapReturnKey(bool trap)
KComboBox::returnPressed
void returnPressed()
KFileFilterCombo
Definition: kfilefiltercombo.h:30
KFileFilterCombo::setMimeFilter
void setMimeFilter(const QStringList &types, const QString &defaultType)
Sets a list of mimetypes.
Definition: kfilefiltercombo.cpp:137
KFileFilterCombo::filters
QStringList filters() const
Definition: kfilefiltercombo.cpp:126
KFileFilterCombo::~KFileFilterCombo
~KFileFilterCombo()
Destroys the filter combo box.
Definition: kfilefiltercombo.cpp:68
KFileFilterCombo::KFileFilterCombo
KFileFilterCombo(QWidget *parent=0)
Creates a new filter combo box.
Definition: kfilefiltercombo.cpp:57
KFileFilterCombo::showsAllTypes
bool showsAllTypes() const
Definition: kfilefiltercombo.cpp:121
KFileFilterCombo::filterChanged
void filterChanged()
This signal is emitted whenever the filter has been changed.
KFileFilterCombo::isMimeFilter
bool isMimeFilter() const
Returns true if the filter has been set using setMimeFilter().
Definition: kfilefiltercombo.cpp:227
KFileFilterCombo::setDefaultFilter
void setDefaultFilter(const QString &filter)
This method allows you to set a default-filter, that is used when an empty filter is set.
Definition: kfilefiltercombo.cpp:217
KFileFilterCombo::setCurrentFilter
void setCurrentFilter(const QString &filter)
Sets the current filter.
Definition: kfilefiltercombo.cpp:131
KFileFilterCombo::currentFilter
QString currentFilter() const
Definition: kfilefiltercombo.cpp:104
KFileFilterCombo::setFilter
void setFilter(const QString &filter)
Sets the filter string.
Definition: kfilefiltercombo.cpp:73
KFileFilterCombo::defaultFilter
QString defaultFilter() const
Definition: kfilefiltercombo.cpp:222
KFileFilterCombo::eventFilter
virtual bool eventFilter(QObject *, QEvent *)
Definition: kfilefiltercombo.cpp:207
KMimeType::mimeType
static Ptr mimeType(const QString &name, FindByNameOption options=ResolveAliases)
KSharedPtr
QObject
QWidget
config-kfile.h
kfile_area
const int kfile_area
kDebug
#define kDebug
kdebug.h
kfilefiltercombo.h
klocale.h
i18n
QString i18n(const char *text)
kmimetype.h
types
QStringList types(Mode mode=Writing)
clear
KAction * clear(const QObject *recvr, const char *slot, QObject *parent)
end
const KShortcut & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KFile

Skip menu "KFile"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal