kio Library API Documentation

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., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, 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     }
00034 
00035     // when we have more than 3 mimefilters and no default-filter,
00036     // we don't show the comments of all mimefilters in one line,
00037     // instead we show "All supported files". We have to translate
00038     // that back to the list of mimefilters in currentFilter() tho.
00039     bool hasAllSupportedFiles;
00040     QString lastFilter;
00041     QString defaultFilter;
00042 };
00043 
00044 KFileFilterCombo::KFileFilterCombo( QWidget *parent, const char *name)
00045     : KComboBox(true, parent, name), d( new KFileFilterComboPrivate )
00046 {
00047     setTrapReturnKey( true );
00048     setInsertionPolicy(NoInsertion);
00049     connect( this, SIGNAL( activated( int )), this, SIGNAL( filterChanged() ));
00050     connect( this, SIGNAL( returnPressed() ), this, SIGNAL( filterChanged() ));
00051     connect( this, SIGNAL( filterChanged() ), SLOT( slotFilterChanged() ));
00052     m_allTypes = false;
00053 }
00054 
00055 KFileFilterCombo::~KFileFilterCombo()
00056 {
00057     delete d;
00058 }
00059 
00060 void KFileFilterCombo::setFilter(const QString& filter)
00061 {
00062     clear();
00063     filters.clear();
00064     d->hasAllSupportedFiles = false;
00065 
00066     if (!filter.isEmpty()) {
00067         QString tmp = filter;
00068         int index = tmp.find('\n');
00069         while (index > 0) {
00070             filters.append(tmp.left(index));
00071             tmp = tmp.mid(index + 1);
00072             index = tmp.find('\n');
00073         }
00074         filters.append(tmp);
00075     } 
00076     else
00077         filters.append( d->defaultFilter );
00078 
00079     QStringList::ConstIterator it;
00080     for (it = filters.begin(); it != filters.end(); it++) {
00081         int tab = (*it).find('|');
00082         insertItem((tab < 0) ? *it :
00083                    (*it).mid(tab + 1));
00084     }
00085 
00086     d->lastFilter = currentText();
00087 }
00088 
00089 QString KFileFilterCombo::currentFilter() const
00090 {
00091     QString f = currentText();
00092     if (f == text(currentItem())) { // user didn't edit the text
00093         f = *filters.at(currentItem());
00094         int mime = f.contains( '/' );
00095         if ( mime > 0 || (currentItem() == 0 && d->hasAllSupportedFiles) ) {
00096             return f; // we have a mimetype as filter
00097         }
00098     }
00099 
00100     int tab = f.find('|');
00101     if (tab < 0)
00102         return f;
00103     else
00104         return f.left(tab);
00105 }
00106 
00107 void KFileFilterCombo::setMimeFilter( const QStringList& types, 
00108                                       const QString& defaultType )
00109 {
00110     clear();
00111     filters.clear();
00112     QString delim = QString::fromLatin1(", ");
00113     d->hasAllSupportedFiles = false;
00114 
00115     m_allTypes = defaultType.isEmpty() && (types.count() > 1);
00116 
00117     QString allComments, allTypes;
00118     int i = 0;
00119     for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it,  ++i)
00120     {
00121         if ( m_allTypes && it != types.begin() ) {
00122             allComments += delim;
00123             allTypes += ' ';
00124         }
00125 
00126         kdDebug(kfile_area) << *it << endl;
00127         KMimeType::Ptr type = KMimeType::mimeType( *it );
00128         filters.append( type->name() );
00129         if ( m_allTypes )
00130         {
00131             allTypes += type->name();
00132             allComments += type->comment();
00133         }
00134         insertItem( type->comment() );
00135         if ( type->name() == defaultType )
00136             setCurrentItem( i );
00137     }
00138 
00139     if ( m_allTypes )
00140     {
00141         if ( i < 3 ) // show the mime-comments of at max 3 types
00142             insertItem( allComments, 0 );
00143         else {
00144             insertItem( i18n("All supported files"), 0 );
00145             d->hasAllSupportedFiles = true;
00146         }
00147 
00148         filters.prepend( allTypes );
00149     }
00150 
00151     d->lastFilter = currentText();
00152 }
00153 
00154 void KFileFilterCombo::slotFilterChanged()
00155 {
00156     d->lastFilter = currentText();
00157 }
00158 
00159 bool KFileFilterCombo::eventFilter( QObject *o, QEvent *e )
00160 {
00161     if ( o == lineEdit() && e->type() == QEvent::FocusOut ) {
00162         if ( currentText() != d->lastFilter )
00163             emit filterChanged();
00164     }
00165 
00166     return KComboBox::eventFilter( o, e );
00167 }
00168 
00169 void KFileFilterCombo::setDefaultFilter( const QString& filter )
00170 {
00171     d->defaultFilter = filter;
00172 }
00173 
00174 QString KFileFilterCombo::defaultFilter() const
00175 {
00176     return d->defaultFilter;
00177 }
00178 
00179 void KFileFilterCombo::virtual_hook( int id, void* data )
00180 { KComboBox::virtual_hook( id, data ); }
00181 
00182 #include "kfilefiltercombo.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:27 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001