kaddressbook Library API Documentation

typecombo.h

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