kaddressbook

viewconfigurefieldspage.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2002 Tobias Koenig <tokoe@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 #include <tqlabel.h>
25 #include <tqlayout.h>
26 #include <tqlistbox.h>
27 #include <tqpushbutton.h>
28 #include <tqtoolbutton.h>
29 #include <tqapplication.h>
30 
31 #include <kcombobox.h>
32 #include <kdebug.h>
33 #include <kdialog.h>
34 #include <kiconloader.h>
35 #include <tdelocale.h>
36 
37 #include "viewconfigurefieldspage.h"
38 
39 class FieldItem : public TQListBoxText
40 {
41  public:
42  FieldItem( TQListBox *parent, TDEABC::Field *field )
43  : TQListBoxText( parent, field->label() ), mField( field ) {}
44 
45  FieldItem( TQListBox *parent, TDEABC::Field *field, int index )
46  : TQListBoxText( parent, field->label(), parent->item( index ) ),
47  mField( field ) {}
48 
49  TDEABC::Field *field() { return mField; }
50 
51  private:
52  TDEABC::Field *mField;
53 };
54 
55 
56 ViewConfigureFieldsPage::ViewConfigureFieldsPage( TDEABC::AddressBook *ab,
57  TQWidget *parent,
58  const char *name )
59  : TQWidget( parent, name ), mAddressBook( ab )
60 {
61  initGUI();
62 }
63 
64 void ViewConfigureFieldsPage::restoreSettings( TDEConfig *config )
65 {
66  TDEABC::Field::List fields = TDEABC::Field::restoreFields( config, "KABCFields" );
67 
68  if ( fields.isEmpty() )
69  fields = TDEABC::Field::defaultFields();
70 
71  TDEABC::Field::List::ConstIterator it;
72  for ( it = fields.begin(); it != fields.end(); ++it )
73  new FieldItem( mSelectedBox, *it );
74 
75  slotShowFields( mCategoryCombo->currentItem() );
76 }
77 
78 void ViewConfigureFieldsPage::saveSettings( TDEConfig *config )
79 {
80  TDEABC::Field::List fields;
81 
82  for ( uint i = 0; i < mSelectedBox->count(); ++i ) {
83  FieldItem *fieldItem = static_cast<FieldItem *>( mSelectedBox->item( i ) );
84  fields.append( fieldItem->field() );
85  }
86 
87  TDEABC::Field::saveFields( config, "KABCFields", fields );
88 }
89 
90 void ViewConfigureFieldsPage::slotShowFields( int index )
91 {
92  int currentPos = mUnSelectedBox->currentItem();
93  mUnSelectedBox->clear();
94 
95  int category;
96  if ( index == 0 ) category = TDEABC::Field::All;
97  else category = 1 << ( index - 1 );
98 
99  TDEABC::Field::List allFields = mAddressBook->fields( category );
100 
101  TDEABC::Field::List::ConstIterator it;
102  for ( it = allFields.begin(); it != allFields.end(); ++it ) {
103  TQListBoxItem *item = mSelectedBox->firstItem();
104  while( item ) {
105  FieldItem *fieldItem = static_cast<FieldItem *>( item );
106  if ( (*it)->equals( fieldItem->field() ) )
107  break;
108  item = item->next();
109  }
110 
111  if ( !item )
112  new FieldItem( mUnSelectedBox, *it );
113  }
114 
115  mUnSelectedBox->sort();
116  mUnSelectedBox->setCurrentItem( currentPos );
117 }
118 
119 void ViewConfigureFieldsPage::slotSelect()
120 {
121  // insert selected items in the unselected list to the selected list,
122  // directoy under the current item if selected, or at the bottonm if
123  // nothing is selected in the selected list
124  int where = mSelectedBox->currentItem();
125  if ( !(where > -1 && mSelectedBox->item( where )->isSelected()) )
126  where = mSelectedBox->count() - 1;
127 
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 );
132  where++;
133  }
134 
135  slotShowFields( mCategoryCombo->currentItem() );
136 }
137 
138 void ViewConfigureFieldsPage::slotUnSelect()
139 {
140  for ( uint i = 0; i < mSelectedBox->count(); ++i )
141  if ( mSelectedBox->isSelected( mSelectedBox->item( i ) ) ) {
142  mSelectedBox->removeItem( i );
143  --i;
144  }
145 
146  slotShowFields( mCategoryCombo->currentItem() );
147 }
148 
149 void ViewConfigureFieldsPage::slotButtonsEnabled()
150 {
151  bool state = false;
152  // add button: enabled if any items are selected in the unselected list
153  for ( uint i = 0; i < mUnSelectedBox->count(); ++i )
154  if ( mUnSelectedBox->item( i )->isSelected() ) {
155  state = true;
156  break;
157  }
158  mAddButton->setEnabled( state );
159 
160  int j = mSelectedBox->currentItem();
161  state = ( j > -1 && mSelectedBox->isSelected( j ) );
162 
163  // up button: enabled if there is a current item > 0 and that is selected
164  mUpButton->setEnabled( ( j > 0 && state ) );
165 
166  // down button: enabled if there is a current item < count - 2 and that is selected
167  mDownButton->setEnabled( ( j > -1 && j < (int)mSelectedBox->count() - 1 && state ) );
168 
169  // remove button: enabled if any items are selected in the selected list
170  state = false;
171  for ( uint i = 0; i < mSelectedBox->count(); ++i )
172  if ( mSelectedBox->item( i )->isSelected() ) {
173  state = true;
174  break;
175  }
176  mRemoveButton->setEnabled( state );
177 }
178 
179 void ViewConfigureFieldsPage::slotMoveUp()
180 {
181  int i = mSelectedBox->currentItem();
182  if ( i > 0 ) {
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 );
188  }
189 }
190 
191 void ViewConfigureFieldsPage::slotMoveDown()
192 {
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 );
200  }
201 }
202 
203 void ViewConfigureFieldsPage::initGUI()
204 {
205  setCaption( i18n("Select Fields to Display") );
206 
207  TQGridLayout *gl = new TQGridLayout( this , 6, 4, 0, KDialog::spacingHint() );
208 
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 );
219 
220  TQLabel *label = new TQLabel( i18n( "&Selected fields:" ), this );
221  gl->addWidget( label, 0, 2 );
222 
223  mUnSelectedBox = new TQListBox( this );
224  mUnSelectedBox->setSelectionMode( TQListBox::Extended );
225  mUnSelectedBox->setMinimumHeight( 100 );
226  gl->addWidget( mUnSelectedBox, 1, 0 );
227 
228  mSelectedBox = new TQListBox( this );
229  mSelectedBox->setSelectionMode( TQListBox::Extended );
230  label->setBuddy( mSelectedBox );
231  gl->addWidget( mSelectedBox, 1, 2 );
232 
233  TQBoxLayout *vb1 = new TQBoxLayout( TQBoxLayout::TopToBottom, KDialog::spacingHint() );
234  vb1->addStretch();
235 
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 );
240 
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 );
245 
246  vb1->addStretch();
247  gl->addLayout( vb1, 1, 1 );
248 
249  TQBoxLayout *vb2 = new TQBoxLayout( TQBoxLayout::TopToBottom, KDialog::spacingHint() );
250  vb2->addStretch();
251 
252  mUpButton = new TQToolButton( this );
253  mUpButton->setIconSet( SmallIconSet( "1uparrow" ) );
254  connect( mUpButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotMoveUp() ) );
255  vb2->addWidget( mUpButton );
256 
257  mDownButton = new TQToolButton( this );
258  mDownButton->setIconSet( SmallIconSet( "1downarrow" ) );
259  connect( mDownButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotMoveDown() ) );
260  vb2->addWidget( mDownButton );
261 
262  vb2->addStretch();
263  gl->addLayout( vb2, 1, 3 );
264 
265  TQSize sizeHint = mUnSelectedBox->sizeHint();
266 
267  // make sure we fill the list with all items, so that we can
268  // get the maxItemWidth we need to not truncate the view
269  slotShowFields( 0 );
270 
271  sizeHint = sizeHint.expandedTo( mSelectedBox->sizeHint() );
272  sizeHint.setWidth( mUnSelectedBox->maxItemWidth() );
273  mUnSelectedBox->setMinimumSize( sizeHint );
274  mSelectedBox->setMinimumSize( sizeHint );
275 
276  gl->activate();
277 
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() ) );
281 
282  slotButtonsEnabled();
283 }
284 
285 #include "viewconfigurefieldspage.moc"