kfilefiltercombo.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <klocale.h>
00021 #include <kdebug.h>
00022 #include <kstaticdeleter.h>
00023 #include <config-kfile.h>
00024
00025 #include "kfilefiltercombo.h"
00026
00027 class KFileFilterCombo::KFileFilterComboPrivate
00028 {
00029 public:
00030 KFileFilterComboPrivate() {
00031 hasAllSupportedFiles = false;
00032 defaultFilter = i18n("*|All Files");
00033 isMimeFilter = false;
00034 }
00035
00036
00037
00038
00039
00040 bool hasAllSupportedFiles;
00041 bool isMimeFilter;
00042 QString lastFilter;
00043 QString defaultFilter;
00044 };
00045
00046 KFileFilterCombo::KFileFilterCombo( QWidget *parent, const char *name)
00047 : KComboBox(true, parent, name), d( new KFileFilterComboPrivate )
00048 {
00049 setTrapReturnKey( true );
00050 setInsertionPolicy(NoInsertion);
00051 connect( this, SIGNAL( activated( int )), this, SIGNAL( filterChanged() ));
00052 connect( this, SIGNAL( returnPressed() ), this, SIGNAL( filterChanged() ));
00053 connect( this, SIGNAL( filterChanged() ), SLOT( slotFilterChanged() ));
00054 m_allTypes = false;
00055 }
00056
00057 KFileFilterCombo::~KFileFilterCombo()
00058 {
00059 delete d;
00060 }
00061
00062 void KFileFilterCombo::setFilter(const QString& filter)
00063 {
00064 clear();
00065 filters.clear();
00066 d->hasAllSupportedFiles = false;
00067
00068 if (!filter.isEmpty()) {
00069 QString tmp = filter;
00070 int index = tmp.find('\n');
00071 while (index > 0) {
00072 filters.append(tmp.left(index));
00073 tmp = tmp.mid(index + 1);
00074 index = tmp.find('\n');
00075 }
00076 filters.append(tmp);
00077 }
00078 else
00079 filters.append( d->defaultFilter );
00080
00081 QStringList::ConstIterator it;
00082 for (it = filters.begin(); it != filters.end(); it++) {
00083 int tab = (*it).find('|');
00084 insertItem((tab < 0) ? *it :
00085 (*it).mid(tab + 1));
00086 }
00087
00088 d->lastFilter = currentText();
00089 d->isMimeFilter = false;
00090 }
00091
00092 QString KFileFilterCombo::currentFilter() const
00093 {
00094 QString f = currentText();
00095 if (f == text(currentItem())) {
00096 f = *filters.at(currentItem());
00097 if ( d->isMimeFilter || (currentItem() == 0 && d->hasAllSupportedFiles) ) {
00098 return f;
00099 }
00100 }
00101
00102 int tab = f.find('|');
00103 if (tab < 0)
00104 return f;
00105 else
00106 return f.left(tab);
00107 }
00108
00109 void KFileFilterCombo::setMimeFilter( const QStringList& types,
00110 const QString& defaultType )
00111 {
00112 clear();
00113 filters.clear();
00114 QString delim = QString::fromLatin1(", ");
00115 d->hasAllSupportedFiles = false;
00116
00117 m_allTypes = defaultType.isEmpty() && (types.count() > 1);
00118
00119 QString allComments, allTypes;
00120 int i = 0;
00121 for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it, ++i)
00122 {
00123 if ( m_allTypes && it != types.begin() ) {
00124 allComments += delim;
00125 allTypes += ' ';
00126 }
00127
00128 kdDebug(kfile_area) << *it << endl;
00129 KMimeType::Ptr type = KMimeType::mimeType( *it );
00130 filters.append( type->name() );
00131 if ( m_allTypes )
00132 {
00133 allTypes += type->name();
00134 allComments += type->comment();
00135 }
00136 insertItem( type->comment() );
00137 if ( type->name() == defaultType )
00138 setCurrentItem( i );
00139 }
00140
00141 if ( m_allTypes )
00142 {
00143 if ( i < 3 )
00144 insertItem( allComments, 0 );
00145 else {
00146 insertItem( i18n("All Supported Files"), 0 );
00147 d->hasAllSupportedFiles = true;
00148 }
00149
00150 filters.prepend( allTypes );
00151 }
00152
00153 d->lastFilter = currentText();
00154 d->isMimeFilter = true;
00155 }
00156
00157 void KFileFilterCombo::slotFilterChanged()
00158 {
00159 d->lastFilter = currentText();
00160 }
00161
00162 bool KFileFilterCombo::eventFilter( QObject *o, QEvent *e )
00163 {
00164 if ( o == lineEdit() && e->type() == QEvent::FocusOut ) {
00165 if ( currentText() != d->lastFilter )
00166 emit filterChanged();
00167 }
00168
00169 return KComboBox::eventFilter( o, e );
00170 }
00171
00172 void KFileFilterCombo::setDefaultFilter( const QString& filter )
00173 {
00174 d->defaultFilter = filter;
00175 }
00176
00177 QString KFileFilterCombo::defaultFilter() const
00178 {
00179 return d->defaultFilter;
00180 }
00181
00182 void KFileFilterCombo::virtual_hook( int id, void* data )
00183 { KComboBox::virtual_hook( id, data ); }
00184
00185 #include "kfilefiltercombo.moc"
This file is part of the documentation for kio Library Version 3.2.2.