kaddressbook

typecombo.h
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
4
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.
9
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.
14
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.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#ifndef TYPECOMBO_H
25#define TYPECOMBO_H
26
27#include <tdeabc/phonenumber.h>
28#include <kcombobox.h>
29
33template <class T>
34class TypeCombo : public KComboBox
35{
36 public:
37 typedef typename T::List List;
38 typedef typename T::List::Iterator Iterator;
39
40 TypeCombo( List &list, TQWidget *parent, const char *name = 0 );
41
42 void setLineEdit( TQLineEdit *edit ) { mLineEdit = edit; }
43 TQLineEdit *lineEdit() const { return mLineEdit; }
44
45 void updateTypes();
46
47 void selectType( int type );
48
49 int selectedType();
50
51 Iterator selectedElement();
52
53 void insertType( const List &list, int type,
54 const T &defaultObject );
55 void insertTypeList( const List &list );
56
57 bool hasType( int type );
58
59 private:
60 List &mTypeList;
61 TQLineEdit *mLineEdit;
62};
63
64template <class T>
65TypeCombo<T>::TypeCombo( TypeCombo::List &list, TQWidget *parent,
66 const char *name )
67 : KComboBox( parent, name ),
68 mTypeList( list )
69{
70}
71
72template <class T>
74{
75 // Remember current item
76 TQString currentId;
77 int current = currentItem();
78 if ( current >= 0 ) currentId = mTypeList[ current ].id();
79
80 clear();
81
82 TQMap<int,int> labelCount;
83
84 uint i;
85 for ( i = 0; i < mTypeList.count(); ++i ) {
86 int type = ( mTypeList[ i ].type() & ~( T::Pref ) );
87 TQString label = mTypeList[ i ].typeLabel( type );
88 int count = 1;
89 if ( labelCount.contains( type ) ) {
90 count = labelCount[ type ] + 1;
91 }
92 labelCount[ type ] = count;
93 if ( count > 1 ) {
94 label = i18n("label (number)", "%1 (%2)").arg( label )
95 .arg( TQString::number( count ) );
96 }
97 insertItem( label );
98 }
99
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;
106 }
107 }
108 }
109}
110
111template <class T>
112void TypeCombo<T>::selectType( int type )
113{
114 uint i;
115 for ( i = 0; i < mTypeList.count(); ++i ) {
116 if ( (mTypeList[ i ].type() & ~T::Pref) == type ) {
117 setCurrentItem( i );
118 break;
119 }
120 }
121}
122
123template <class T>
125{
126 return mTypeList[ currentItem() ].type();
127}
128
129template <class T>
130typename TypeCombo<T>::Iterator TypeCombo<T>::selectedElement()
131{
132 return mTypeList.at( currentItem() );
133}
134
135template <class T>
136void TypeCombo<T>::insertType( const TypeCombo::List &list, int type,
137 const T &defaultObject )
138{
139 uint i;
140 for ( i = 0; i < list.count(); ++i ) {
141 if ( list[ i ].type() == type ) {
142 mTypeList.append( list[ i ] );
143 break;
144 }
145 }
146 if ( i == list.count() ) {
147 mTypeList.append( defaultObject );
148 }
149}
150
151template <class T>
152void TypeCombo<T>::insertTypeList( const TypeCombo::List &list )
153{
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;
159 }
160 if ( j == mTypeList.count() ) {
161 mTypeList.append( list[ i ] );
162 }
163 }
164}
165
166template <class T>
167bool TypeCombo<T>::hasType( int type )
168{
169 for ( uint i = 0; i < mTypeList.count(); ++i ) {
170 if ( ( mTypeList[ i ].type() & ~T::Pref ) == type )
171 return true;
172 }
173
174 return false;
175}
176
177#endif
Combo box for type information of Addresses and Phone numbers.
Definition: typecombo.h:35