typecombo.h
00001 #ifndef TYPECOMBO_H
00002 #define TYPECOMBO_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
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
This file is part of the documentation for kdelibs Version 3.1.5.