26 #include <tqlistbox.h>
27 #include <tqpushbutton.h>
28 #include <tqtoolbutton.h>
29 #include <tqapplication.h>
31 #include <kcombobox.h>
34 #include <kiconloader.h>
35 #include <tdelocale.h>
37 #include "viewconfigurefieldspage.h"
39 class FieldItem :
public TQListBoxText
42 FieldItem( TQListBox *parent, TDEABC::Field *field )
43 : TQListBoxText( parent, field->label() ), mField( field ) {}
45 FieldItem( TQListBox *parent, TDEABC::Field *field,
int index )
46 : TQListBoxText( parent, field->label(), parent->item( index ) ),
49 TDEABC::Field *field() {
return mField; }
52 TDEABC::Field *mField;
56 ViewConfigureFieldsPage::ViewConfigureFieldsPage( TDEABC::AddressBook *ab,
59 : TQWidget( parent, name ), mAddressBook( ab )
64 void ViewConfigureFieldsPage::restoreSettings( TDEConfig *config )
66 TDEABC::Field::List fields = TDEABC::Field::restoreFields( config,
"KABCFields" );
68 if ( fields.isEmpty() )
69 fields = TDEABC::Field::defaultFields();
71 TDEABC::Field::List::ConstIterator it;
72 for ( it = fields.begin(); it != fields.end(); ++it )
73 new FieldItem( mSelectedBox, *it );
75 slotShowFields( mCategoryCombo->currentItem() );
78 void ViewConfigureFieldsPage::saveSettings( TDEConfig *config )
80 TDEABC::Field::List fields;
82 for ( uint i = 0; i < mSelectedBox->count(); ++i ) {
83 FieldItem *fieldItem =
static_cast<FieldItem *
>( mSelectedBox->item( i ) );
84 fields.append( fieldItem->field() );
87 TDEABC::Field::saveFields( config,
"KABCFields", fields );
90 void ViewConfigureFieldsPage::slotShowFields(
int index )
92 int currentPos = mUnSelectedBox->currentItem();
93 mUnSelectedBox->clear();
96 if ( index == 0 ) category = TDEABC::Field::All;
97 else category = 1 << ( index - 1 );
99 TDEABC::Field::List allFields = mAddressBook->fields( category );
101 TDEABC::Field::List::ConstIterator it;
102 for ( it = allFields.begin(); it != allFields.end(); ++it ) {
103 TQListBoxItem *item = mSelectedBox->firstItem();
105 FieldItem *fieldItem =
static_cast<FieldItem *
>( item );
106 if ( (*it)->equals( fieldItem->field() ) )
112 new FieldItem( mUnSelectedBox, *it );
115 mUnSelectedBox->sort();
116 mUnSelectedBox->setCurrentItem( currentPos );
119 void ViewConfigureFieldsPage::slotSelect()
124 int where = mSelectedBox->currentItem();
125 if ( !(where > -1 && mSelectedBox->item( where )->isSelected()) )
126 where = mSelectedBox->count() - 1;
128 for ( uint i = 0; i < mUnSelectedBox->count(); ++i )
129 if ( mUnSelectedBox->isSelected( mUnSelectedBox->item( i ) ) ) {
130 FieldItem *fieldItem =
static_cast<FieldItem *
>( mUnSelectedBox->item( i ) );
131 new FieldItem( mSelectedBox, fieldItem->field(), where );
135 slotShowFields( mCategoryCombo->currentItem() );
138 void ViewConfigureFieldsPage::slotUnSelect()
140 for ( uint i = 0; i < mSelectedBox->count(); ++i )
141 if ( mSelectedBox->isSelected( mSelectedBox->item( i ) ) ) {
142 mSelectedBox->removeItem( i );
146 slotShowFields( mCategoryCombo->currentItem() );
149 void ViewConfigureFieldsPage::slotButtonsEnabled()
153 for ( uint i = 0; i < mUnSelectedBox->count(); ++i )
154 if ( mUnSelectedBox->item( i )->isSelected() ) {
158 mAddButton->setEnabled( state );
160 int j = mSelectedBox->currentItem();
161 state = ( j > -1 && mSelectedBox->isSelected( j ) );
164 mUpButton->setEnabled( ( j > 0 && state ) );
167 mDownButton->setEnabled( ( j > -1 && j < (
int)mSelectedBox->count() - 1 && state ) );
171 for ( uint i = 0; i < mSelectedBox->count(); ++i )
172 if ( mSelectedBox->item( i )->isSelected() ) {
176 mRemoveButton->setEnabled( state );
179 void ViewConfigureFieldsPage::slotMoveUp()
181 int i = mSelectedBox->currentItem();
183 TQListBoxItem *item = mSelectedBox->item( i );
184 mSelectedBox->takeItem( item );
185 mSelectedBox->insertItem( item, i - 1 );
186 mSelectedBox->setCurrentItem( item );
187 mSelectedBox->setSelected( i - 1,
true );
191 void ViewConfigureFieldsPage::slotMoveDown()
193 int i = mSelectedBox->currentItem();
194 if ( i > -1 && i < (
int)mSelectedBox->count() - 1 ) {
195 TQListBoxItem *item = mSelectedBox->item( i );
196 mSelectedBox->takeItem( item );
197 mSelectedBox->insertItem( item, i + 1 );
198 mSelectedBox->setCurrentItem( item );
199 mSelectedBox->setSelected( i + 1,
true );
203 void ViewConfigureFieldsPage::initGUI()
205 setCaption( i18n(
"Select Fields to Display") );
207 TQGridLayout *gl =
new TQGridLayout(
this , 6, 4, 0, KDialog::spacingHint() );
209 mCategoryCombo =
new KComboBox(
false,
this );
210 mCategoryCombo->insertItem( TDEABC::Field::categoryLabel( TDEABC::Field::All ) );
211 mCategoryCombo->insertItem( TDEABC::Field::categoryLabel( TDEABC::Field::Frequent ) );
212 mCategoryCombo->insertItem( TDEABC::Field::categoryLabel( TDEABC::Field::Address ) );
213 mCategoryCombo->insertItem( TDEABC::Field::categoryLabel( TDEABC::Field::Email ) );
214 mCategoryCombo->insertItem( TDEABC::Field::categoryLabel( TDEABC::Field::Personal ) );
215 mCategoryCombo->insertItem( TDEABC::Field::categoryLabel( TDEABC::Field::Organization ) );
216 mCategoryCombo->insertItem( TDEABC::Field::categoryLabel( TDEABC::Field::CustomCategory ) );
217 connect( mCategoryCombo, TQ_SIGNAL( activated(
int) ), TQ_SLOT( slotShowFields(
int) ) );
218 gl->addWidget( mCategoryCombo, 0, 0 );
220 TQLabel *label =
new TQLabel( i18n(
"&Selected fields:" ),
this );
221 gl->addWidget( label, 0, 2 );
223 mUnSelectedBox =
new TQListBox(
this );
224 mUnSelectedBox->setSelectionMode( TQListBox::Extended );
225 mUnSelectedBox->setMinimumHeight( 100 );
226 gl->addWidget( mUnSelectedBox, 1, 0 );
228 mSelectedBox =
new TQListBox(
this );
229 mSelectedBox->setSelectionMode( TQListBox::Extended );
230 label->setBuddy( mSelectedBox );
231 gl->addWidget( mSelectedBox, 1, 2 );
233 TQBoxLayout *vb1 =
new TQBoxLayout( TQBoxLayout::TopToBottom, KDialog::spacingHint() );
236 mAddButton =
new TQToolButton(
this );
237 mAddButton->setIconSet( TQApplication::reverseLayout() ? SmallIconSet(
"1leftarrow" ) : SmallIconSet(
"1rightarrow" ) );
238 connect( mAddButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotSelect() ) );
239 vb1->addWidget( mAddButton );
241 mRemoveButton =
new TQToolButton(
this );
242 mRemoveButton->setIconSet( TQApplication::reverseLayout() ? SmallIconSet(
"1rightarrow" ) : SmallIconSet(
"1leftarrow" ) );
243 connect( mRemoveButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotUnSelect() ) );
244 vb1->addWidget( mRemoveButton );
247 gl->addLayout( vb1, 1, 1 );
249 TQBoxLayout *vb2 =
new TQBoxLayout( TQBoxLayout::TopToBottom, KDialog::spacingHint() );
252 mUpButton =
new TQToolButton(
this );
253 mUpButton->setIconSet( SmallIconSet(
"1uparrow" ) );
254 connect( mUpButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotMoveUp() ) );
255 vb2->addWidget( mUpButton );
257 mDownButton =
new TQToolButton(
this );
258 mDownButton->setIconSet( SmallIconSet(
"1downarrow" ) );
259 connect( mDownButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotMoveDown() ) );
260 vb2->addWidget( mDownButton );
263 gl->addLayout( vb2, 1, 3 );
265 TQSize sizeHint = mUnSelectedBox->sizeHint();
271 sizeHint = sizeHint.expandedTo( mSelectedBox->sizeHint() );
272 sizeHint.setWidth( mUnSelectedBox->maxItemWidth() );
273 mUnSelectedBox->setMinimumSize( sizeHint );
274 mSelectedBox->setMinimumSize( sizeHint );
278 connect( mUnSelectedBox, TQ_SIGNAL( selectionChanged() ), TQ_SLOT( slotButtonsEnabled() ) );
279 connect( mSelectedBox, TQ_SIGNAL( selectionChanged() ), TQ_SLOT( slotButtonsEnabled() ) );
280 connect( mSelectedBox, TQ_SIGNAL( currentChanged( TQListBoxItem * ) ), TQ_SLOT( slotButtonsEnabled() ) );
282 slotButtonsEnabled();
285 #include "viewconfigurefieldspage.moc"