kaddressbook
typecombo.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
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
|