• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KFile

kfilefiltercombo.cpp

Go to the documentation of this file.
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 "kfilefiltercombo.h"
00021 
00022 #include <kdebug.h>
00023 #include <klocale.h>
00024 #include <kmimetype.h>
00025 #include <config-kfile.h>
00026 #include <QtCore/QEvent>
00027 #include <QtGui/QLineEdit>
00028 
00029 class KFileFilterCombo::Private
00030 {
00031 public:
00032     Private( KFileFilterCombo *_parent )
00033         : parent(_parent),
00034           hasAllSupportedFiles(false),
00035           isMimeFilter(false),
00036           defaultFilter(i18n("*|All Files"))
00037     {
00038     }
00039 
00040     void _k_slotFilterChanged();
00041 
00042     KFileFilterCombo *parent;
00043     // when we have more than 3 mimefilters and no default-filter,
00044     // we don't show the comments of all mimefilters in one line,
00045     // instead we show "All supported files". We have to translate
00046     // that back to the list of mimefilters in currentFilter() tho.
00047     bool hasAllSupportedFiles;
00048     // true when setMimeFilter was called
00049     bool isMimeFilter;
00050     QString lastFilter;
00051     QString defaultFilter;
00052 
00053     QStringList m_filters;
00054     bool m_allTypes;
00055 };
00056 
00057 KFileFilterCombo::KFileFilterCombo( QWidget *parent)
00058     : KComboBox(true, parent), d( new Private(this) )
00059 {
00060     setTrapReturnKey( true );
00061     setInsertPolicy(QComboBox::NoInsert);
00062     connect( this, SIGNAL( activated( int )), this, SIGNAL( filterChanged() ));
00063     connect( this, SIGNAL( returnPressed() ), this, SIGNAL( filterChanged() ));
00064     connect( this, SIGNAL( filterChanged() ), SLOT( _k_slotFilterChanged() ));
00065     d->m_allTypes = false;
00066 }
00067 
00068 KFileFilterCombo::~KFileFilterCombo()
00069 {
00070     delete d;
00071 }
00072 
00073 void KFileFilterCombo::setFilter(const QString& filter)
00074 {
00075     clear();
00076     d->m_filters.clear();
00077     d->hasAllSupportedFiles = false;
00078 
00079     if (!filter.isEmpty()) {
00080     QString tmp = filter;
00081     int index = tmp.indexOf('\n');
00082     while (index > 0) {
00083         d->m_filters.append(tmp.left(index));
00084         tmp = tmp.mid(index + 1);
00085         index = tmp.indexOf('\n');
00086     }
00087     d->m_filters.append(tmp);
00088     }
00089     else
00090     d->m_filters.append( d->defaultFilter );
00091 
00092     QStringList::ConstIterator it;
00093     QStringList::ConstIterator end(d->m_filters.end());
00094     for (it = d->m_filters.begin(); it != end; ++it) {
00095     int tab = (*it).indexOf('|');
00096     addItem((tab < 0) ? *it :
00097            (*it).mid(tab + 1));
00098     }
00099 
00100     d->lastFilter = currentText();
00101     d->isMimeFilter = false;
00102 }
00103 
00104 QString KFileFilterCombo::currentFilter() const
00105 {
00106     QString f = currentText();
00107     if (f == itemText(currentIndex())) { // user didn't edit the text
00108     f = d->m_filters.value(currentIndex());
00109         if ( d->isMimeFilter || (currentIndex() == 0 && d->hasAllSupportedFiles) ) {
00110             return f; // we have a mimetype as filter
00111         }
00112     }
00113 
00114     int tab = f.indexOf('|');
00115     if (tab < 0)
00116     return f;
00117     else
00118     return f.left(tab);
00119 }
00120 
00121 bool KFileFilterCombo::showsAllTypes() const
00122 {
00123     return d->m_allTypes;
00124 }
00125 
00126 QStringList KFileFilterCombo::filters() const
00127 {
00128     return d->m_filters;
00129 }
00130 
00131 void KFileFilterCombo::setCurrentFilter( const QString& filter )
00132 {
00133     setCurrentIndex(d->m_filters.indexOf(filter));
00134     filterChanged();
00135 }
00136 
00137 void KFileFilterCombo::setMimeFilter( const QStringList& types,
00138                                       const QString& defaultType )
00139 {
00140     clear();
00141     d->m_filters.clear();
00142     QString delim = QLatin1String(", ");
00143     d->hasAllSupportedFiles = false;
00144 
00145     d->m_allTypes = defaultType.isEmpty() && (types.count() > 1);
00146 
00147     QString allComments, allTypes;
00148     int i = 0;
00149     for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it,  ++i)
00150     {
00151         if ( d->m_allTypes && it != types.begin() ) {
00152             allComments += delim;
00153             allTypes += ' ';
00154         }
00155 
00156         kDebug(kfile_area) << *it;
00157         KMimeType::Ptr type = KMimeType::mimeType( *it );
00158 
00159         if (!type) {
00160             kDebug(kfile_area) << "Could not create mimetype!\n";
00161             continue;
00162         }
00163 
00164 
00165         d->m_filters.append( type->name() );
00166         if ( d->m_allTypes )
00167         {
00168             allTypes += type->name();
00169             allComments += type->comment();
00170         }
00171         addItem( type->comment() );
00172         if ( type->name() == defaultType )
00173             setCurrentIndex( i );
00174     }
00175 
00176     if ( d->m_allTypes )
00177     {
00178         if ( i < 3 ) // show the mime-comments of at max 3 types
00179             insertItem(0, allComments);
00180         else {
00181             insertItem(0, i18n("All Supported Files"));
00182             d->hasAllSupportedFiles = true;
00183         }
00184         setCurrentIndex( 0 );
00185 
00186         d->m_filters.prepend( allTypes );
00187     }
00188 
00189     d->lastFilter = currentText();
00190     d->isMimeFilter = true;
00191 }
00192 
00193 void KFileFilterCombo::Private::_k_slotFilterChanged()
00194 {
00195     lastFilter = parent->currentText();
00196 }
00197 
00198 bool KFileFilterCombo::eventFilter( QObject *o, QEvent *e )
00199 {
00200     if ( o == lineEdit() && e->type() == QEvent::FocusOut ) {
00201         if ( currentText() != d->lastFilter )
00202             emit filterChanged();
00203     }
00204 
00205     return KComboBox::eventFilter( o, e );
00206 }
00207 
00208 void KFileFilterCombo::setDefaultFilter( const QString& filter )
00209 {
00210     d->defaultFilter = filter;
00211 }
00212 
00213 QString KFileFilterCombo::defaultFilter() const
00214 {
00215     return d->defaultFilter;
00216 }
00217 
00218 #include "kfilefiltercombo.moc"

KFile

Skip menu "KFile"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal