kfilefiltercombo.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) Stephan Kulow <coolo@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 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 // when we have more than 3 mimefilters and no default-filter, 00037 // we don't show the comments of all mimefilters in one line, 00038 // instead we show "All supported files". We have to translate 00039 // that back to the list of mimefilters in currentFilter() tho. 00040 bool hasAllSupportedFiles; 00041 // true when setMimeFilter was called 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 QStringList::ConstIterator end(filters.end()); 00084 for (it = filters.begin(); it != end; ++it) { 00085 int tab = (*it).find('|'); 00086 insertItem((tab < 0) ? *it : 00087 (*it).mid(tab + 1)); 00088 } 00089 00090 d->lastFilter = currentText(); 00091 d->isMimeFilter = false; 00092 } 00093 00094 QString KFileFilterCombo::currentFilter() const 00095 { 00096 QString f = currentText(); 00097 if (f == text(currentItem())) { // user didn't edit the text 00098 f = *filters.at(currentItem()); 00099 if ( d->isMimeFilter || (currentItem() == 0 && d->hasAllSupportedFiles) ) { 00100 return f; // we have a mimetype as filter 00101 } 00102 } 00103 00104 int tab = f.find('|'); 00105 if (tab < 0) 00106 return f; 00107 else 00108 return f.left(tab); 00109 } 00110 00111 void KFileFilterCombo::setCurrentFilter( const QString& filter ) 00112 { 00113 int pos = 0; 00114 for( QStringList::ConstIterator it = filters.begin(); 00115 it != filters.end(); 00116 ++it, ++pos ) { 00117 if( *it == filter ) { 00118 setCurrentItem( pos ); 00119 filterChanged(); 00120 return; 00121 } 00122 } 00123 setCurrentText( filter ); 00124 filterChanged(); 00125 } 00126 00127 void KFileFilterCombo::setMimeFilter( const QStringList& types, 00128 const QString& defaultType ) 00129 { 00130 clear(); 00131 filters.clear(); 00132 QString delim = QString::fromLatin1(", "); 00133 d->hasAllSupportedFiles = false; 00134 00135 m_allTypes = defaultType.isEmpty() && (types.count() > 1); 00136 00137 QString allComments, allTypes; 00138 int i = 0; 00139 for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it, ++i) 00140 { 00141 if ( m_allTypes && it != types.begin() ) { 00142 allComments += delim; 00143 allTypes += ' '; 00144 } 00145 00146 kdDebug(kfile_area) << *it << endl; 00147 KMimeType::Ptr type = KMimeType::mimeType( *it ); 00148 filters.append( type->name() ); 00149 if ( m_allTypes ) 00150 { 00151 allTypes += type->name(); 00152 allComments += type->comment(); 00153 } 00154 insertItem( type->comment() ); 00155 if ( type->name() == defaultType ) 00156 setCurrentItem( i ); 00157 } 00158 00159 if ( m_allTypes ) 00160 { 00161 if ( i < 3 ) // show the mime-comments of at max 3 types 00162 insertItem( allComments, 0 ); 00163 else { 00164 insertItem( i18n("All Supported Files"), 0 ); 00165 d->hasAllSupportedFiles = true; 00166 } 00167 00168 filters.prepend( allTypes ); 00169 } 00170 00171 d->lastFilter = currentText(); 00172 d->isMimeFilter = true; 00173 } 00174 00175 void KFileFilterCombo::slotFilterChanged() 00176 { 00177 d->lastFilter = currentText(); 00178 } 00179 00180 bool KFileFilterCombo::eventFilter( QObject *o, QEvent *e ) 00181 { 00182 if ( o == lineEdit() && e->type() == QEvent::FocusOut ) { 00183 if ( currentText() != d->lastFilter ) 00184 emit filterChanged(); 00185 } 00186 00187 return KComboBox::eventFilter( o, e ); 00188 } 00189 00190 void KFileFilterCombo::setDefaultFilter( const QString& filter ) 00191 { 00192 d->defaultFilter = filter; 00193 } 00194 00195 QString KFileFilterCombo::defaultFilter() const 00196 { 00197 return d->defaultFilter; 00198 } 00199 00200 void KFileFilterCombo::virtual_hook( int id, void* data ) 00201 { KComboBox::virtual_hook( id, data ); } 00202 00203 #include "kfilefiltercombo.moc"