kaddressbook Library API Documentation

typecombo.h

00001 /*                                                                      
00002     This file is part of KAddressBook.                                  
00003     Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>                   
00004                                                                         
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or   
00008     (at your option) any later version.                                 
00009                                                                         
00010     This program is distributed in the hope that it will be useful,     
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of      
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        
00013     GNU General Public License for more details.                        
00014                                                                         
00015     You should have received a copy of the GNU General Public License   
00016     along with this program; if not, write to the Free Software         
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           
00018                                                                         
00019     As a special exception, permission is given to link this program    
00020     with any edition of Qt, and distribute the resulting executable,    
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #ifndef TYPECOMBO_H
00025 #define TYPECOMBO_H
00026 
00027 #include <kabc/phonenumber.h>
00028 #include <kcombobox.h>
00029 
00033 template <class T>
00034 class TypeCombo : public KComboBox
00035 {
00036   public:
00037     typedef typename T::List List;
00038     typedef typename T::List::Iterator Iterator;
00039   
00040     TypeCombo( List &list, QWidget *parent, const char *name = 0 );
00041 
00042     void setLineEdit( QLineEdit *edit ) { mLineEdit = edit; }
00043     QLineEdit *lineEdit() const { return mLineEdit; }
00044 
00045     void updateTypes();
00046 
00047     void selectType( int type );
00048 
00049     int selectedType();
00050 
00051     Iterator selectedElement();
00052 
00053     void insertType( const List &list, int type,
00054                      const T &defaultObject );
00055     void insertTypeList( const List &list );
00056 
00057     bool hasType( int type );
00058 
00059   private:
00060     List &mTypeList;
00061     QLineEdit *mLineEdit;
00062 };
00063 
00064 template <class T>
00065 TypeCombo<T>::TypeCombo( TypeCombo::List &list, QWidget *parent,
00066                       const char *name )
00067   : KComboBox( parent, name ),
00068     mTypeList( list )
00069 {
00070 }
00071 
00072 template <class T>
00073 void TypeCombo<T>::updateTypes()
00074 {
00075   // Remember current item
00076   QString currentId;
00077   int current = currentItem();  
00078   if ( current >= 0 ) currentId = mTypeList[ current ].id();
00079 
00080   clear();
00081 
00082   QMap<int,int> labelCount;
00083 
00084   uint i;
00085   for( i = 0; i < mTypeList.count(); ++i ) {
00086     int type = ( mTypeList[ i ].type() & ~( T::Pref ) );
00087     QString label = mTypeList[ i ].typeLabel( type );
00088     int count = 1;
00089     if ( labelCount.contains( type ) ) {
00090       count = labelCount[ type ] + 1;
00091     }
00092     labelCount[ type ] = count;
00093     if ( count > 1 ) {
00094       label = i18n("label (number)", "%1 (%2)").arg( label )
00095                                            .arg( QString::number( count ) );
00096     }
00097     insertItem( label );
00098   }
00099 
00100   // Restore previous current item
00101   if ( !currentId.isEmpty() ) {
00102     for( i = 0; i < mTypeList.count(); ++i ) {
00103       if ( mTypeList[ i ].id() == currentId ) {
00104         setCurrentItem( i );
00105         break;
00106       }
00107     }
00108   }
00109 }
00110 
00111 template <class T>
00112 void TypeCombo<T>::selectType( int type )
00113 {
00114   uint i;
00115   for( i = 0; i < mTypeList.count(); ++i ) {
00116     if ( (mTypeList[ i ].type() & ~T::Pref) == type ) {
00117       setCurrentItem( i );
00118       break;
00119     }
00120   }
00121 }
00122 
00123 template <class T>
00124 int TypeCombo<T>::selectedType()
00125 {
00126   return mTypeList[ currentItem() ].type();
00127 }
00128 
00129 template <class T>
00130 typename TypeCombo<T>::Iterator TypeCombo<T>::selectedElement()
00131 {
00132   return mTypeList.at( currentItem() );
00133 }
00134 
00135 template <class T>
00136 void TypeCombo<T>::insertType( const TypeCombo::List &list, int type,
00137                                const T &defaultObject )
00138 {
00139   uint i;
00140   for ( i = 0; i < list.count(); ++i ) {
00141     if ( list[ i ].type() == type ) {
00142       mTypeList.append( list[ i ] );
00143       break;
00144     }
00145   }
00146   if ( i == list.count() ) {
00147     mTypeList.append( defaultObject );
00148   }
00149 }
00150 
00151 template <class T>
00152 void TypeCombo<T>::insertTypeList( const TypeCombo::List &list )
00153 {
00154   uint i;
00155   for ( i = 0; i < list.count(); ++i ) {
00156     uint j;
00157     for( j = 0; j < mTypeList.count(); ++j ) {
00158       if ( list[ i ].id() == mTypeList[ j ].id() ) break;
00159     }
00160     if ( j == mTypeList.count() ) {
00161       mTypeList.append( list[ i ] );
00162     }
00163   }
00164 }
00165 
00166 template <class T>
00167 bool TypeCombo<T>::hasType( int type )
00168 {
00169   for( uint i = 0; i < mTypeList.count(); ++i ) {
00170     if ( ( mTypeList[ i ].type() & ~T::Pref ) == type )
00171       return true;
00172   }
00173 
00174   return false;
00175 }
00176 
00177 #endif
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:53 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003