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