Need to store config when changing units, if not very bad things happen
[kdepim.git] / kaddressbook / typecombo.h
blob3b411c86c788b2dddc8e3c7862039381a95f95a7
1 /*
2 This file is part of KAddressBook.
3 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution.
24 #ifndef TYPECOMBO_H
25 #define TYPECOMBO_H
27 #include <kabc/phonenumber.h>
28 #include <kcombobox.h>
30 /**
31 Combo box for type information of Addresses and Phone numbers.
33 template <class T>
34 class TypeCombo : public KComboBox
36 public:
37 typedef typename T::List List;
38 typedef typename T::List::Iterator Iterator;
40 TypeCombo( List &list, QWidget *parent, const char *name = 0 );
42 void setLineEdit( QLineEdit *edit ) { mLineEdit = edit; }
43 QLineEdit *lineEdit() const { return mLineEdit; }
45 void updateTypes();
47 void selectType( int type );
49 int selectedType();
51 Iterator selectedElement();
53 void insertType( const List &list, int type,
54 const T &defaultObject );
55 void insertTypeList( const List &list );
57 bool hasType( int type );
59 private:
60 List &mTypeList;
61 QLineEdit *mLineEdit;
64 template <class T>
65 TypeCombo<T>::TypeCombo( TypeCombo::List &list, QWidget *parent,
66 const char *name )
67 : KComboBox( parent, name ),
68 mTypeList( list )
72 template <class T>
73 void TypeCombo<T>::updateTypes()
75 // Remember current item
76 QString currentId;
77 int current = currentItem();
78 if ( current >= 0 ) currentId = mTypeList[ current ].id();
80 clear();
82 QMap<int,int> labelCount;
84 uint i;
85 for ( i = 0; i < mTypeList.count(); ++i ) {
86 int type = ( mTypeList[ i ].type() & ~( T::Pref ) );
87 QString label = mTypeList[ i ].typeLabel( type );
88 int count = 1;
89 if ( labelCount.contains( type ) ) {
90 count = labelCount[ type ] + 1;
92 labelCount[ type ] = count;
93 if ( count > 1 ) {
94 label = i18n("label (number)", "%1 (%2)").arg( label )
95 .arg( QString::number( count ) );
97 insertItem( label );
100 // Restore previous current item
101 if ( !currentId.isEmpty() ) {
102 for ( i = 0; i < mTypeList.count(); ++i ) {
103 if ( mTypeList[ i ].id() == currentId ) {
104 setCurrentItem( i );
105 break;
111 template <class T>
112 void TypeCombo<T>::selectType( int type )
114 uint i;
115 for ( i = 0; i < mTypeList.count(); ++i ) {
116 if ( (mTypeList[ i ].type() & ~T::Pref) == type ) {
117 setCurrentItem( i );
118 break;
123 template <class T>
124 int TypeCombo<T>::selectedType()
126 return mTypeList[ currentItem() ].type();
129 template <class T>
130 typename TypeCombo<T>::Iterator TypeCombo<T>::selectedElement()
132 return mTypeList.at( currentItem() );
135 template <class T>
136 void TypeCombo<T>::insertType( const TypeCombo::List &list, int type,
137 const T &defaultObject )
139 uint i;
140 for ( i = 0; i < list.count(); ++i ) {
141 if ( list[ i ].type() == type ) {
142 mTypeList.append( list[ i ] );
143 break;
146 if ( i == list.count() ) {
147 mTypeList.append( defaultObject );
151 template <class T>
152 void TypeCombo<T>::insertTypeList( const TypeCombo::List &list )
154 uint i;
155 for ( i = 0; i < list.count(); ++i ) {
156 uint j;
157 for ( j = 0; j < mTypeList.count(); ++j ) {
158 if ( list[ i ].id() == mTypeList[ j ].id() ) break;
160 if ( j == mTypeList.count() ) {
161 mTypeList.append( list[ i ] );
166 template <class T>
167 bool TypeCombo<T>::hasType( int type )
169 for ( uint i = 0; i < mTypeList.count(); ++i ) {
170 if ( ( mTypeList[ i ].type() & ~T::Pref ) == type )
171 return true;
174 return false;
177 #endif