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.
27 #include <kabc/phonenumber.h>
28 #include <kcombobox.h>
31 Combo box for type information of Addresses and Phone numbers.
34 class TypeCombo
: public KComboBox
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
; }
47 void selectType( int type
);
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
);
65 TypeCombo
<T
>::TypeCombo( TypeCombo::List
&list
, QWidget
*parent
,
67 : KComboBox( parent
, name
),
73 void TypeCombo
<T
>::updateTypes()
75 // Remember current item
77 int current
= currentItem();
78 if ( current
>= 0 ) currentId
= mTypeList
[ current
].id();
82 QMap
<int,int> labelCount
;
85 for ( i
= 0; i
< mTypeList
.count(); ++i
) {
86 int type
= ( mTypeList
[ i
].type() & ~( T::Pref
) );
87 QString label
= mTypeList
[ i
].typeLabel( type
);
89 if ( labelCount
.contains( type
) ) {
90 count
= labelCount
[ type
] + 1;
92 labelCount
[ type
] = count
;
94 label
= i18n("label (number)", "%1 (%2)").arg( label
)
95 .arg( QString::number( count
) );
100 // Restore previous current item
101 if ( !currentId
.isEmpty() ) {
102 for ( i
= 0; i
< mTypeList
.count(); ++i
) {
103 if ( mTypeList
[ i
].id() == currentId
) {
112 void TypeCombo
<T
>::selectType( int type
)
115 for ( i
= 0; i
< mTypeList
.count(); ++i
) {
116 if ( (mTypeList
[ i
].type() & ~T::Pref
) == type
) {
124 int TypeCombo
<T
>::selectedType()
126 return mTypeList
[ currentItem() ].type();
130 typename TypeCombo
<T
>::Iterator TypeCombo
<T
>::selectedElement()
132 return mTypeList
.at( currentItem() );
136 void TypeCombo
<T
>::insertType( const TypeCombo::List
&list
, int type
,
137 const T
&defaultObject
)
140 for ( i
= 0; i
< list
.count(); ++i
) {
141 if ( list
[ i
].type() == type
) {
142 mTypeList
.append( list
[ i
] );
146 if ( i
== list
.count() ) {
147 mTypeList
.append( defaultObject
);
152 void TypeCombo
<T
>::insertTypeList( const TypeCombo::List
&list
)
155 for ( i
= 0; i
< list
.count(); ++i
) {
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
] );
167 bool TypeCombo
<T
>::hasType( int type
)
169 for ( uint i
= 0; i
< mTypeList
.count(); ++i
) {
170 if ( ( mTypeList
[ i
].type() & ~T::Pref
) == type
)