1 /* This file is part of the KDE libraries
2 Copyright (C) Stephan Kulow <coolo@kde.org>
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.
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.
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.
20 #include "kfilefiltercombo.h"
24 #include <kmimetype.h>
25 #include <config-kfile.h>
26 #include <QtCore/QEvent>
27 #include <QtGui/QLineEdit>
29 class KFileFilterCombo::Private
32 Private( KFileFilterCombo
*_parent
)
34 hasAllSupportedFiles(false),
36 defaultFilter(i18n("*|All Files"))
40 void _k_slotFilterChanged();
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
51 QString defaultFilter
;
53 QStringList m_filters
;
57 KFileFilterCombo::KFileFilterCombo( QWidget
*parent
)
58 : KComboBox(true, parent
), d( new Private(this) )
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;
68 KFileFilterCombo::~KFileFilterCombo()
73 void KFileFilterCombo::setFilter(const QString
& filter
)
77 d
->hasAllSupportedFiles
= false;
79 if (!filter
.isEmpty()) {
81 int index
= tmp
.indexOf('\n');
83 d
->m_filters
.append(tmp
.left(index
));
84 tmp
= tmp
.mid(index
+ 1);
85 index
= tmp
.indexOf('\n');
87 d
->m_filters
.append(tmp
);
90 d
->m_filters
.append( d
->defaultFilter
);
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
:
100 d
->lastFilter
= currentText();
101 d
->isMimeFilter
= false;
104 QString
KFileFilterCombo::currentFilter() const
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
114 int tab
= f
.indexOf('|');
121 bool KFileFilterCombo::showsAllTypes() const
123 return d
->m_allTypes
;
126 QStringList
KFileFilterCombo::filters() const
131 void KFileFilterCombo::setCurrentFilter( const QString
& filter
)
133 setCurrentIndex(d
->m_filters
.indexOf(filter
));
137 void KFileFilterCombo::setMimeFilter( const QStringList
& types
,
138 const QString
& defaultType
)
141 d
->m_filters
.clear();
142 QString delim
= QLatin1String(", ");
143 d
->hasAllSupportedFiles
= false;
145 d
->m_allTypes
= defaultType
.isEmpty() && (types
.count() > 1);
147 QString allComments
, allTypes
;
148 for(QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
)
150 if ( d
->m_allTypes
&& it
!= types
.begin() ) {
151 allComments
+= delim
;
155 kDebug(kfile_area
) << *it
;
156 KMimeType::Ptr type
= KMimeType::mimeType( *it
);
159 kDebug(kfile_area
) << "Could not create mimetype!\n";
164 d
->m_filters
.append( type
->name() );
167 allTypes
+= type
->name();
168 allComments
+= type
->comment();
170 addItem( type
->comment() );
171 if ( type
->name() == defaultType
)
172 setCurrentIndex( count() - 1 );
177 if ( count() <= 3 ) // show the mime-comments of at max 3 types
178 insertItem(0, allComments
);
180 insertItem(0, i18n("All Supported Files"));
181 d
->hasAllSupportedFiles
= true;
183 setCurrentIndex( 0 );
185 d
->m_filters
.prepend( allTypes
);
188 d
->lastFilter
= currentText();
189 d
->isMimeFilter
= true;
192 void KFileFilterCombo::Private::_k_slotFilterChanged()
194 lastFilter
= parent
->currentText();
197 bool KFileFilterCombo::eventFilter( QObject
*o
, QEvent
*e
)
199 if ( o
== lineEdit() && e
->type() == QEvent::FocusOut
) {
200 if ( currentText() != d
->lastFilter
)
201 emit
filterChanged();
204 return KComboBox::eventFilter( o
, e
);
207 void KFileFilterCombo::setDefaultFilter( const QString
& filter
)
209 d
->defaultFilter
= filter
;
212 QString
KFileFilterCombo::defaultFilter() const
214 return d
->defaultFilter
;
217 #include "kfilefiltercombo.moc"