summaryrefslogtreecommitdiffabout
path: root/kaddressbook/viewconfigurefieldspage.cpp
blob: 7366a0b3e95fed00f3eb29b95e59320060d8e207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
    This file is part of KAddressBook.
    Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    As a special exception, permission is given to link this program
    with any edition of Qt, and distribute the resulting executable,
    without including the source code for Qt in the source distribution.
*/

#ifndef KAB_EMBEDDED
#include <kcombobox.h>
#include <qpushbutton.h>
#include <kdebug.h>

#else //KAB_EMBEDDED
#include <qcombobox.h>
#endif //KAB_EMBEDDED

#include <kiconloader.h>
#include <kdialog.h>
#include <klocale.h>

#include <qtoolbutton.h>
#include <qapplication.h>
#include <QDesktopWidget>
#include <qlabel.h>
#include <q3listbox.h>
#include <qlayout.h>
//Added by qt3to4:
#include <Q3GridLayout>
#include <Q3BoxLayout>

#include "viewconfigurefieldspage.h"

class FieldItem : public Q3ListBoxText
{
  public:
    FieldItem( Q3ListBox *parent, KABC::Field *field )
      : Q3ListBoxText( parent, field->label() ), mField( field ) {}
    
    FieldItem( Q3ListBox *parent, KABC::Field *field, int index )
      : Q3ListBoxText( parent, field->label(), parent->item( index ) ),
        mField( field ) {}
    
    KABC::Field *field() { return mField; }
    
  private:
    KABC::Field *mField;
};


ViewConfigureFieldsPage::ViewConfigureFieldsPage( KABC::AddressBook *ab,
                                                  QWidget *parent,
                                                  const char *name )
  : QWidget( parent, name ), mAddressBook( ab )
{
  initGUI();
}

void ViewConfigureFieldsPage::restoreSettings( KConfig *config )
{
  KABC::Field::List fields = KABC::Field::restoreFields( config, "KABCFields" );

  if ( fields.isEmpty() )
    fields = KABC::Field::defaultFields();
  
  KABC::Field::List::ConstIterator it;
  for( it = fields.begin(); it != fields.end(); ++it )
    new FieldItem( mSelectedBox, *it );

  slotShowFields( mCategoryCombo->currentItem() );
}

void ViewConfigureFieldsPage::saveSettings( KConfig *config )
{
  KABC::Field::List fields;

  for( uint i = 0; i < mSelectedBox->count(); ++i ) {
    FieldItem *fieldItem = static_cast<FieldItem *>( mSelectedBox->item( i ) );
    fields.append( fieldItem->field() );
  }

  KABC::Field::saveFields( config, "KABCFields", fields );
}

void ViewConfigureFieldsPage::slotShowFields( int index )
{
  int currentPos = mUnSelectedBox->currentItem();
  mUnSelectedBox->clear();

  int category;
  if ( index == 0 ) category = KABC::Field::All;
  else category = 1 << ( index - 1 );

  KABC::Field::List allFields = mAddressBook->fields( category );

  KABC::Field::List::ConstIterator it;
  for ( it = allFields.begin(); it != allFields.end(); ++it ) {
    Q3ListBoxItem *item = mSelectedBox->firstItem();
    while( item ) {
      FieldItem *fieldItem = static_cast<FieldItem *>( item );
      if ( (*it)->equals( fieldItem->field() ) )
        break;
      item = item->next();
    }

    if ( !item )
      new FieldItem( mUnSelectedBox, *it );
  }

  mUnSelectedBox->sort();
  mUnSelectedBox->setCurrentItem( currentPos );
}

void ViewConfigureFieldsPage::slotSelect()
{
  // insert selected items in the unselected list to the selected list,
  // directoy under the current item if selected, or at the bottonm if
  // nothing is selected in the selected list
  int where = mSelectedBox->currentItem();
//US QListBoxItem::isSelected()) is not available in QT 2.x. Use selected instead.
  if ( !(where > -1 && mSelectedBox->item( where )->selected()) )
    where = mSelectedBox->count() - 1;

  for ( uint i = 0; i < mUnSelectedBox->count(); ++i )
    if ( mUnSelectedBox->isSelected( mUnSelectedBox->item( i ) ) ) {
      FieldItem *fieldItem = static_cast<FieldItem *>( mUnSelectedBox->item( i ) );
      new FieldItem( mSelectedBox, fieldItem->field(), where );
      where++;
    }

  slotShowFields( mCategoryCombo->currentItem() );
}

void ViewConfigureFieldsPage::slotUnSelect()
{
  for ( uint i = 0; i < mSelectedBox->count(); ++i )
    if ( mSelectedBox->isSelected( mSelectedBox->item( i ) ) ) {
      mSelectedBox->removeItem( i );
      --i;
    }

  slotShowFields( mCategoryCombo->currentItem() );
}

void ViewConfigureFieldsPage::slotButtonsEnabled()
{
  bool state = false;
  // add button: enabled if any items are selected in the unselected list
  for( uint i = 0; i < mUnSelectedBox->count(); ++i )
//US QListBoxItem::isSelected()) is not available in QT 2.x. Use selected instead.
    if ( mUnSelectedBox->item( i )->selected() ) {
      state = true;
      break;
    }
  mAddButton->setEnabled( state );

  int j = mSelectedBox->currentItem();
  state = ( j > -1 && mSelectedBox->isSelected( j ) );

  // up button: enabled if there is a current item > 0 and that is selected
  mUpButton->setEnabled( ( j > 0 && state ) );

  // down button: enabled if there is a current item < count - 2 and that is selected
  mDownButton->setEnabled( ( j > -1 && j < (int)mSelectedBox->count() - 1 && state ) );

  // remove button: enabled if any items are selected in the selected list
  state = false;
  for ( uint i = 0; i < mSelectedBox->count(); ++i )
//US QListBoxItem::isSelected()) is not available in QT 2.x. Use selected instead.
    if ( mSelectedBox->item( i )->selected() ) {
      state = true;
      break;
    }
  mRemoveButton->setEnabled( state );
}

void ViewConfigureFieldsPage::slotMoveUp()
{
  int i = mSelectedBox->currentItem();
  if ( i > 0 ) {
    Q3ListBoxItem *item = mSelectedBox->item( i );
    mSelectedBox->takeItem( item );
    mSelectedBox->insertItem( item, i - 1 );
    mSelectedBox->setCurrentItem( item );
    mSelectedBox->setSelected( i - 1, true );
  }
}

void ViewConfigureFieldsPage::slotMoveDown()
{
  int i = mSelectedBox->currentItem();
  if ( i > -1 && i < (int)mSelectedBox->count() - 1 ) {
    Q3ListBoxItem *item = mSelectedBox->item( i );
    mSelectedBox->takeItem( item );
    mSelectedBox->insertItem( item, i + 1 );
    mSelectedBox->setCurrentItem( item );
    mSelectedBox->setSelected( i + 1, true );
  }
}

void ViewConfigureFieldsPage::initGUI()
{
  //US change the orientation dependent on the following flag
  // right the flag is set only staticly.
  // 0 = 640x480 ; 1 = 240x320
  bool orientation = 1;
  

  setCaption( i18n("Select Fields to Display") );

  Q3GridLayout *gl = 0;
  if (orientation == 0) 
    gl = new Q3GridLayout( this , 6, 4, 0, KDialog::spacingHint() );
  else
    gl = new Q3GridLayout( this , 4, 6, 0, KDialog::spacingHint() );

  #ifndef KAB_EMBEDDED
  mCategoryCombo = new KComboBox( false, this );
  #else //KAB_EMBEDDED
  mCategoryCombo = new QComboBox( false, this );
  #endif //KAB_EMBEDDED
  if ( QApplication::desktop()->width() < 320 )
      mCategoryCombo->setMaximumWidth( 70 );
  mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::All ) );
  mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Frequent ) );
  mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Address ) );
  mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Email ) );
  mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Personal ) );
  mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Organization ) );
  mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::CustomCategory ) );
  connect( mCategoryCombo, SIGNAL( activated(int) ), SLOT( slotShowFields(int) ) );
  gl->addWidget( mCategoryCombo, 0, 0 );

  QLabel *label = new QLabel( i18n( "&Selected\nfields:" ), this );
  if (orientation == 0) 
    gl->addWidget( label, 0, 2 );
  else
    gl->addWidget( label, 2, 0 );
    

  mUnSelectedBox = new Q3ListBox( this );
  mUnSelectedBox->setSelectionMode( Q3ListBox::Extended );
  mUnSelectedBox->setMinimumHeight( 80 );
  if (orientation == 0) 
    gl->addWidget( mUnSelectedBox, 1, 0 );
  else
    gl->addWidget( mUnSelectedBox, 0, 1 );

  mSelectedBox = new Q3ListBox( this );
  //if ( QApplication::desktop()->width() < 320 ) {
  //  mUnSelectedBox->setMaximumWidth( 134 );
  //  mSelectedBox->setMaximumWidth( 134 );
  //}
  mSelectedBox->setSelectionMode( Q3ListBox::Extended );
  mSelectedBox->setMinimumHeight( 80 );
  label->setBuddy( mSelectedBox );
  if (orientation == 0) 
    gl->addWidget( mSelectedBox, 1, 2 );
  else
    gl->addWidget( mSelectedBox, 2, 1 );
    
  Q3BoxLayout *vb1 = 0;
  if (orientation == 0) 
    vb1 = new Q3BoxLayout( Q3BoxLayout::TopToBottom, KDialog::spacingHint() );
  else
    vb1 = new Q3BoxLayout( Q3BoxLayout::LeftToRight, KDialog::spacingHint() );
  
  vb1->addStretch();

  mAddButton = new QToolButton( this );
  if (orientation == 0) 
    mAddButton->setIconSet( SmallIconSet( "1rightarrow" ) );
  else
    mAddButton->setIconSet( SmallIconSet( "1downarrow" ) );
  connect( mAddButton, SIGNAL( clicked() ), SLOT( slotSelect() ) );
  vb1->addWidget( mAddButton );

  mRemoveButton = new QToolButton( this );
  if (orientation == 0) 
    mRemoveButton->setIconSet( SmallIconSet( "1leftarrow" ) );
  else
    mRemoveButton->setIconSet( SmallIconSet( "1uparrow" ) );
  
  connect( mRemoveButton, SIGNAL( clicked() ), SLOT( slotUnSelect() ) );
  vb1->addWidget( mRemoveButton );

  vb1->addStretch();
  if (orientation == 0) 
    gl->addLayout( vb1, 1, 1 );
  else
    gl->addLayout( vb1, 1, 1 );

  Q3BoxLayout *vb2 = 0;
  if (orientation == 0) 
    vb2 = new Q3BoxLayout( Q3BoxLayout::TopToBottom, KDialog::spacingHint() );
  else
    vb2 = new Q3BoxLayout( Q3BoxLayout::LeftToRight, KDialog::spacingHint() );
  
  vb2->addStretch();

  mUpButton = new QToolButton( this );
    mUpButton->setIconSet( SmallIconSet( "1uparrow" ) );
  connect( mUpButton, SIGNAL( clicked() ), SLOT( slotMoveUp() ) );
  vb2->addWidget( mUpButton );

  mDownButton = new QToolButton( this );
  mDownButton->setIconSet( SmallIconSet( "1downarrow" ) );
  connect( mDownButton, SIGNAL( clicked() ), SLOT( slotMoveDown() ) );
  vb2->addWidget( mDownButton );

  vb2->addStretch();
  if (orientation == 0) 
    gl->addLayout( vb2, 1, 3 );
  else
    gl->addLayout( vb2, 3, 1 );
  
  QSize sizeHint = mUnSelectedBox->sizeHint();

  // make sure we fill the list with all items, so that we can
  // get the maxItemWidth we need to not truncate the view
  slotShowFields( 0 );

  //sizeHint = sizeHint.expandedTo( mSelectedBox->sizeHint() );
  //sizeHint.setWidth( mUnSelectedBox->maxItemWidth() );
  // mUnSelectedBox->setMinimumSize( sizeHint );
  //mSelectedBox->setMinimumSize( sizeHint );

  gl->activate();

  connect( mUnSelectedBox, SIGNAL( selectionChanged() ), SLOT( slotButtonsEnabled() ) );
  connect( mSelectedBox, SIGNAL( selectionChanged() ), SLOT( slotButtonsEnabled() ) );
  connect( mSelectedBox, SIGNAL( currentChanged( Q3ListBoxItem * ) ), SLOT( slotButtonsEnabled() ) );

  slotButtonsEnabled();
}


#ifndef KAB_EMBEDDED_
#include "moc_viewconfigurefieldspage.cpp"
#endif //KAB_EMBEDDED