• Skip to content
  • Skip to link menu
KDE 4.2 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.constEnd());
00094     for (it = d->m_filters.constBegin(); 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     for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
00149     {
00150         if ( d->m_allTypes && it != types.begin() ) {
00151             allComments += delim;
00152             allTypes += ' ';
00153         }
00154 
00155         kDebug(kfile_area) << *it;
00156         KMimeType::Ptr type = KMimeType::mimeType( *it );
00157 
00158         if (!type) {
00159             kDebug(kfile_area) << "Could not create mimetype!\n";
00160             continue;
00161         }
00162 
00163 
00164         d->m_filters.append( type->name() );
00165         if ( d->m_allTypes )
00166         {
00167             allTypes += type->name();
00168             allComments += type->comment();
00169         }
00170         addItem( type->comment() );
00171         if ( type->name() == defaultType )
00172             setCurrentIndex( count() - 1 );
00173     }
00174 
00175     if ( d->m_allTypes )
00176     {
00177         if ( count() <= 3 ) // show the mime-comments of at max 3 types
00178             insertItem(0, allComments);
00179         else {
00180             insertItem(0, i18n("All Supported Files"));
00181             d->hasAllSupportedFiles = true;
00182         }
00183         setCurrentIndex( 0 );
00184 
00185         d->m_filters.prepend( allTypes );
00186     }
00187 
00188     d->lastFilter = currentText();
00189     d->isMimeFilter = true;
00190 }
00191 
00192 void KFileFilterCombo::Private::_k_slotFilterChanged()
00193 {
00194     lastFilter = parent->currentText();
00195 }
00196 
00197 bool KFileFilterCombo::eventFilter( QObject *o, QEvent *e )
00198 {
00199     if ( o == lineEdit() && e->type() == QEvent::FocusOut ) {
00200         if ( currentText() != d->lastFilter )
00201             emit filterChanged();
00202     }
00203 
00204     return KComboBox::eventFilter( o, e );
00205 }
00206 
00207 void KFileFilterCombo::setDefaultFilter( const QString& filter )
00208 {
00209     d->defaultFilter = filter;
00210 }
00211 
00212 QString KFileFilterCombo::defaultFilter() const
00213 {
00214     return d->defaultFilter;
00215 }
00216 
00217 #include "kfilefiltercombo.moc"

KFile

Skip menu "KFile"
  • Main Page
  • Namespace List
  • 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
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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