# kio Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kurlcombobox.cpp

    00001 /* This file is part of the KDE libraries
    00002     Copyright (C) 2000,2001 Carsten Pfeiffer <pfeiffer@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 version 2, as published by the Free Software Foundation.
    00007
    00008     This library is distributed in the hope that it will be useful,
    00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00011     Library General Public License for more details.
    00012
    00013     You should have received a copy of the GNU Library General Public License
    00014     along with this library; see the file COPYING.LIB.  If not, write to
    00015     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00016     Boston, MA 02111-1307, USA.
    00017 */
    00018
    00019 #include <qdir.h>
    00020 #include <qlistbox.h>
    00021
    00022 #include <kdebug.h>
    00023 #include <kglobal.h>
    00024 #include <kiconloader.h>
    00025 #include <klocale.h>
    00026 #include <kmimetype.h>
    00027
    00028 #include <kurlcombobox.h>
    00029
    00030 class KURLComboBox::KURLComboBoxPrivate
    00031 {
    00032 public:
    00033     KURLComboBoxPrivate() {
    00034         dirpix = SmallIcon(QString::fromLatin1("folder"));
    00035     }
    00036
    00037     QPixmap dirpix;
    00038 };
    00039
    00040
    00041 KURLComboBox::KURLComboBox( Mode mode, QWidget *parent, const char *name )
    00042     : KComboBox( parent, name )
    00043 {
    00044     init( mode );
    00045 }
    00046
    00047
    00048 KURLComboBox::KURLComboBox( Mode mode, bool rw, QWidget *parent,
    00049                             const char *name )
    00050     : KComboBox( rw, parent, name )
    00051 {
    00052     init( mode );
    00053 }
    00054
    00055
    00056 KURLComboBox::~KURLComboBox()
    00057 {
    00058     delete d;
    00059 }
    00060
    00061
    00062 void KURLComboBox::init( Mode mode )
    00063 {
    00064     d = new KURLComboBoxPrivate();
    00065
    00066     myMode    = mode;
    00067     urlAdded  = false;
    00068     myMaximum = 10; // default
    00069     itemList.setAutoDelete( true );
    00070     defaultList.setAutoDelete( true );
    00071     setInsertionPolicy( NoInsertion );
    00072     setTrapReturnKey( true );
    00073     setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ));
    00074
    00075     opendirPix = SmallIcon(QString::fromLatin1("folder_open"));
    00076
    00077     connect( this, SIGNAL( activated( int )), SLOT( slotActivated( int )));
    00078 }
    00079
    00080
    00081 QStringList KURLComboBox::urls() const
    00082 {
    00083     kdDebug(250) << "::urls()" << endl;
    00084     //static const QString &fileProt = KGlobal::staticQString("file:");
    00085     QStringList list;
    00086     QString url;
    00087     for ( int i = defaultList.count(); i < count(); i++ ) {
    00088         url = text( i );
    00089         if ( !url.isEmpty() ) {
    00090             //if ( url.at(0) == '/' )
    00091             //    list.append( url.prepend( fileProt ) );
    00092             //else
    00093                 list.append( url );
    00094         }
    00095     }
    00096
    00097     return list;
    00098 }
    00099
    00100
    00101 void KURLComboBox::addDefaultURL( const KURL& url, const QString& text )
    00102 {
    00103     addDefaultURL( url, getPixmap( url ), text );
    00104 }
    00105
    00106
    00107 void KURLComboBox::addDefaultURL( const KURL& url, const QPixmap& pix,
    00108                                   const QString& text )
    00109 {
    00110     KURLComboItem *item = new KURLComboItem;
    00111     item->url = url;
    00112     item->pixmap = pix;
    00113     if ( text.isEmpty() )
    00114         if ( url.isLocalFile() )
    00115           item->text = url.path( myMode );
    00116         else
    00117           item->text = url.prettyURL( myMode );
    00118     else
    00119         item->text = text;
    00120
    00121     defaultList.append( item );
    00122 }
    00123
    00124
    00125 void KURLComboBox::setDefaults()
    00126 {
    00127     clear();
    00128     itemMapper.clear();
    00129
    00130     KURLComboItem *item;
    00131     for ( unsigned int id = 0; id < defaultList.count(); id++ ) {
    00132         item = defaultList.at( id );
    00133         insertURLItem( item );
    00134     }
    00135 }
    00136
    00137 void KURLComboBox::setURLs( QStringList urls )
    00138 {
    00139     setURLs( urls, RemoveBottom );
    00140 }
    00141
    00142 void KURLComboBox::setURLs( QStringList urls, OverLoadResolving remove )
    00143 {
    00144     setDefaults();
    00145     itemList.clear();
    00146
    00147     if ( urls.isEmpty() )
    00148         return;
    00149
    00150     QStringList::Iterator it = urls.begin();
    00151
    00152     // kill duplicates
    00153     QString text;
    00154     while ( it != urls.end() ) {
    00155         while ( urls.contains( *it ) > 1 ) {
    00156             it = urls.remove( it );
    00157             continue;
    00158         }
    00159         ++it;
    00160     }
    00161
    00162     // limit to myMaximum items
    00163     /* Note: overload is an (old) C++ keyword, some compilers (KCC) choke
    00164        on that, so call it Overload (capital 'O').  (matz) */
    00165     int Overload = urls.count() - myMaximum + defaultList.count();
    00166     while ( Overload > 0 ) {
    00167         urls.remove((remove == RemoveBottom) ? urls.fromLast() : urls.begin());
    00168         Overload--;
    00169     }
    00170
    00171     it = urls.begin();
    00172
    00173     KURLComboItem *item = 0L;
    00174     KURL u;
    00175
    00176     while ( it != urls.end() ) {
    00177         if ( (*it).isEmpty() ) {
    00178             ++it;
    00179             continue;
    00180         }
    00181         u = KURL::fromPathOrURL( *it );
    00182
    00183         item = new KURLComboItem;
    00184         item->url = u;
    00185         item->pixmap = getPixmap( u );
    00186
    00187         if ( u.isLocalFile() )
    00188             item->text = u.path( myMode ); // don't show file:/
    00189         else
    00190             item->text = *it;
    00191
    00192         insertURLItem( item );
    00193         itemList.append( item );
    00194         ++it;
    00195     }
    00196 }
    00197
    00198
    00199 void KURLComboBox::setURL( const KURL& url )
    00200 {
    00201     if ( url.isEmpty() )
    00202         return;
    00203
    00204     blockSignals( true );
    00205
    00206     // check for duplicates
    00207     QMap<int,const KURLComboItem*>::ConstIterator mit = itemMapper.begin();
    00208     QString urlToInsert = url.url(-1);
    00209     while ( mit != itemMapper.end() ) {
    00210         if ( urlToInsert == mit.data()->url.url(-1) ) {
    00211             setCurrentItem( mit.key() );
    00212
    00213             if ( myMode == Directories )
    00214                 updateItem( mit.data(), mit.key(), opendirPix );
    00215
    00216             blockSignals( false );
    00217             return;
    00218         }
    00219         ++mit;
    00220     }
    00221
    00222     // not in the combo yet -> create a new item and insert it
    00223
    00224     // first remove the old item
    00225     if ( urlAdded ) {
    00226         itemList.removeLast();
    00227         urlAdded = false;
    00228     }
    00229
    00230     setDefaults();
    00231
    00232     QPtrListIterator<KURLComboItem> it( itemList );
    00233     for( ; it.current(); ++it )
    00234         insertURLItem( it.current() );
    00235
    00236     KURLComboItem *item = new KURLComboItem;
    00237     item->url = url;
    00238     item->pixmap = getPixmap( url );
    00239     if ( url.isLocalFile() )
    00240         item->text = url.path( myMode );
    00241     else
    00242         item->text = url.prettyURL( myMode );
    00243      kdDebug(250) << "setURL: text=" << item->text << endl;
    00244
    00245     int id = count();
    00246     QString text = /*isEditable() ? item->url.prettyURL( myMode ) : */ item->text;
    00247
    00248     if ( myMode == Directories )
    00249         KComboBox::insertItem( opendirPix, text, id );
    00250     else
    00251         KComboBox::insertItem( item->pixmap, text, id );
    00252     itemMapper.insert( id, item );
    00253     itemList.append( item );
    00254
    00255     setCurrentItem( id );
    00256     urlAdded = true;
    00257     blockSignals( false );
    00258 }
    00259
    00260
    00261 void KURLComboBox::slotActivated( int index )
    00262 {
    00263     const KURLComboItem *item = itemMapper[ index ];
    00264
    00265     if ( item ) {
    00266         setURL( item->url );
    00267         emit urlActivated( item->url );
    00268     }
    00269 }
    00270
    00271
    00272 void KURLComboBox::insertURLItem( const KURLComboItem *item )
    00273 {
    00274 // kdDebug(250) << "insertURLItem " << item->text << endl;
    00275     int id = count();
    00276     KComboBox::insertItem( item->pixmap, item->text, id );
    00277     itemMapper.insert( id, item );
    00278 }
    00279
    00280
    00281 void KURLComboBox::setMaxItems( int max )
    00282 {
    00283     myMaximum = max;
    00284
    00285     if ( count() > myMaximum ) {
    00286         int oldCurrent = currentItem();
    00287
    00288         setDefaults();
    00289
    00290         QPtrListIterator<KURLComboItem> it( itemList );
    00291         int Overload = itemList.count() - myMaximum + defaultList.count();
    00292         for ( int i = 0; i <= Overload; i++ )
    00293             ++it;
    00294
    00295         for( ; it.current(); ++it )
    00296             insertURLItem( it.current() );
    00297
    00298         if ( count() > 0 ) { // restore the previous currentItem
    00299             if ( oldCurrent >= count() )
    00300                 oldCurrent = count() -1;
    00301             setCurrentItem( oldCurrent );
    00302         }
    00303     }
    00304 }
    00305
    00306
    00307 void KURLComboBox::removeURL( const KURL& url, bool checkDefaultURLs )
    00308 {
    00309     QMap<int,const KURLComboItem*>::ConstIterator mit = itemMapper.begin();
    00310     while ( mit != itemMapper.end() ) {
    00311         if ( url.url(-1) == mit.data()->url.url(-1) ) {
    00312             if ( !itemList.remove( mit.data() ) && checkDefaultURLs )
    00313                 defaultList.remove( mit.data() );
    00314         }
    00315         ++mit;
    00316     }
    00317
    00318     blockSignals( true );
    00319     setDefaults();
    00320     QPtrListIterator<KURLComboItem> it( itemList );
    00321     while ( it.current() ) {
    00322         insertURLItem( *it );
    00323         ++it;
    00324     }
    00325     blockSignals( false );
    00326 }
    00327
    00328
    00329 QPixmap KURLComboBox::getPixmap( const KURL& url ) const
    00330 {
    00331     if ( myMode == Directories )
    00332         return d->dirpix;
    00333     else
    00334         return KMimeType::pixmapForURL( url, 0, KIcon::Small );
    00335 }
    00336
    00337
    00338 // updates "item" with pixmap "pixmap" and sets the URL instead of text
    00339 // works around a Qt bug.
    00340 void KURLComboBox::updateItem( const KURLComboItem *item,
    00341                                int index, const QPixmap& pixmap )
    00342 {
    00343     // QComboBox::changeItem() doesn't honour the pixmap when
    00344     // using an editable combobox, so we just remove and insert
    00345     if ( editable() ) {
    00346         removeItem( index );
    00347         insertItem( pixmap,
    00348                     item->url.isLocalFile() ? item->url.path( myMode ) :
    00349                                               item->url.prettyURL( myMode ),
    00350                     index );
    00351     }
    00352     else
    00353         changeItem( pixmap, item->text, index );
    00354 }
    00355
    00356
    00357 #include "kurlcombobox.moc"

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:14:33 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001