summaryrefslogtreecommitdiffstats
path: root/kaddressbook/phoneeditwidget.cpp
blob: b1d87f41f2a7e221219460f3b8b76bdba610a0f2 (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
/*
    This file is part of KAddressBook.
    Copyright (c) 2002 Mike Pilone <[email protected]>

    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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

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

#include <tqbuttongroup.h>
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlistbox.h>
#include <tqlistview.h>
#include <tqpushbutton.h>
#include <tqsignalmapper.h>
#include <tqstring.h>
#include <tqtooltip.h>

#include <tdeapplication.h>
#include <kbuttonbox.h>
#include <kcombobox.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <tdelistview.h>
#include <tdelocale.h>

#include <tdeabc/phonenumber.h>

#include "phoneeditwidget.h"

PhoneTypeCombo::PhoneTypeCombo( TQWidget *parent )
  : KComboBox( parent, "TypeCombo" ),
    mType( TDEABC::PhoneNumber::Home ),
    mLastSelected( 0 ),
    mTypeList( TDEABC::PhoneNumber::typeList() )
{
  mTypeList.append( -1 ); // Others...

  update();

  connect( this, TQT_SIGNAL( activated( int ) ),
           this, TQT_SLOT( selected( int ) ) );
  connect( this, TQT_SIGNAL( activated( int ) ),
           this, TQT_SIGNAL( modified() ) );
}

PhoneTypeCombo::~PhoneTypeCombo()
{
}

void PhoneTypeCombo::setType( int type )
{
  if ( !mTypeList.contains( type ) )
    mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), type );

  mType = type;
  update();
}

int PhoneTypeCombo::type() const
{
  return mType;
}

void PhoneTypeCombo::update()
{
  bool blocked = signalsBlocked();
  blockSignals( true );

  clear();
  TQValueList<int>::ConstIterator it;
  for ( it = mTypeList.begin(); it != mTypeList.end(); ++it ) {
    if ( *it == -1 ) { // "Other..." entry
      insertItem( i18n( "Other..." ) );
    } else {
      TDEABC::PhoneNumber number;
      number.setType( *it );
      insertItem( number.typeLabel() );
    }
  }

  setCurrentItem( mLastSelected = mTypeList.findIndex( mType ) );

  blockSignals( blocked );
}

void PhoneTypeCombo::selected( int pos )
{
  if ( mTypeList[ pos ] == -1 )
    otherSelected();
  else {
    mType = mTypeList[ pos ];
    mLastSelected = pos;
  }
}

void PhoneTypeCombo::otherSelected()
{
  PhoneTypeDialog dlg( mType, this );
  if ( dlg.exec() ) {
    mType = dlg.type();
    if ( !mTypeList.contains( mType ) )
      mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), mType );
  } else {
    setType( mTypeList[ mLastSelected ] );
  }

  update();
}

PhoneNumberWidget::PhoneNumberWidget( TQWidget *parent )
  : TQWidget( parent )
{
  TQHBoxLayout *layout = new TQHBoxLayout( this, 6, 11 );

  mTypeCombo = new PhoneTypeCombo( this );
  mNumberEdit = new KLineEdit( this );

  layout->addWidget( mTypeCombo );
  layout->addWidget( mNumberEdit );

  connect( mTypeCombo, TQT_SIGNAL( modified() ), TQT_SIGNAL( modified() ) );
  connect( mNumberEdit, TQT_SIGNAL( textChanged( const TQString& ) ), TQT_SIGNAL( modified() ) );
}

void PhoneNumberWidget::setNumber( const TDEABC::PhoneNumber &number )
{
  mNumber = number;

  mTypeCombo->setType( number.type() );
  mNumberEdit->setText( number.number() );
}

TDEABC::PhoneNumber PhoneNumberWidget::number() const
{
  TDEABC::PhoneNumber number( mNumber );

  number.setType( mTypeCombo->type() );
  number.setNumber( mNumberEdit->text() );

  return number;
}

void PhoneNumberWidget::setReadOnly( bool readOnly )
{
  mTypeCombo->setEnabled( !readOnly );
  mNumberEdit->setReadOnly( readOnly );
}


PhoneEditWidget::PhoneEditWidget( TQWidget *parent, const char *name )
  : TQWidget( parent, name ), mReadOnly( false )
{
  TQGridLayout *layout = new TQGridLayout( this, 2, 2 );
  layout->setSpacing( KDialog::spacingHint() );

  mWidgetLayout = new TQVBoxLayout( layout );
  layout->addMultiCellLayout( mWidgetLayout, 0, 0, 0, 1 );

  mAddButton = new TQPushButton( i18n( "Add" ), this );
  mAddButton->setMaximumSize( mAddButton->sizeHint() );
  layout->addWidget( mAddButton, 1, 0, TQt::AlignRight );

  mRemoveButton = new TQPushButton( i18n( "Remove" ), this );
  mRemoveButton->setMaximumSize( mRemoveButton->sizeHint() );
  layout->addWidget( mRemoveButton, 1, 1 );

  mMapper = new TQSignalMapper( this );
  connect( mMapper, TQT_SIGNAL( mapped( int ) ), TQT_SLOT( changed( int ) ) );

  connect( mAddButton, TQT_SIGNAL( clicked() ), TQT_SLOT( add() ) );
  connect( mRemoveButton, TQT_SIGNAL( clicked() ), TQT_SLOT( remove() ) );
}

PhoneEditWidget::~PhoneEditWidget()
{
}

void PhoneEditWidget::setReadOnly( bool readOnly )
{
  mReadOnly = readOnly;
  mAddButton->setEnabled( !readOnly );
  mRemoveButton->setEnabled( !readOnly && mPhoneNumberList.count() > 3 );

  TQPtrListIterator<PhoneNumberWidget> it( mWidgets );
  while ( it.current() ) {
    it.current()->setReadOnly( readOnly );
    ++it;
  }
}

void PhoneEditWidget::setPhoneNumbers( const TDEABC::PhoneNumber::List &list )
{
  mPhoneNumberList = list;

  TDEABC::PhoneNumber::TypeList types;
  types << TDEABC::PhoneNumber::Home;
  types << TDEABC::PhoneNumber::Work;
  types << TDEABC::PhoneNumber::Cell;

  // add an empty entry per default
  if ( mPhoneNumberList.count() < 3 )
    for ( int i = mPhoneNumberList.count(); i < 3; ++i )
      mPhoneNumberList.append( TDEABC::PhoneNumber( "", types[ i ] ) );

  recreateNumberWidgets();
}

TDEABC::PhoneNumber::List PhoneEditWidget::phoneNumbers() const
{
  TDEABC::PhoneNumber::List list;

  TDEABC::PhoneNumber::List::ConstIterator it;
  for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it )
    if ( !(*it).number().isEmpty() )
      list.append( *it );

  return list;
}

void PhoneEditWidget::changed()
{
  if ( !mReadOnly )
    emit modified();
}

void PhoneEditWidget::add()
{
  mPhoneNumberList.append( TDEABC::PhoneNumber() );

  recreateNumberWidgets();
}

void PhoneEditWidget::remove()
{
  mPhoneNumberList.remove( mPhoneNumberList.last() );
  changed();

  recreateNumberWidgets();
}

void PhoneEditWidget::recreateNumberWidgets()
{
  for ( TQWidget *w = mWidgets.first(); w; w = mWidgets.next() ) {
    mWidgetLayout->remove( w );
    w->deleteLater();
  }
  mWidgets.clear();

  TDEABC::PhoneNumber::List::ConstIterator it;
  int counter = 0;
  for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it ) {
    PhoneNumberWidget *wdg = new PhoneNumberWidget( this );
    wdg->setNumber( *it );

    mMapper->setMapping( wdg, counter );
    connect( wdg, TQT_SIGNAL( modified() ), mMapper, TQT_SLOT( map() ) );

    mWidgetLayout->addWidget( wdg );
    mWidgets.append( wdg );
    wdg->show();

    ++counter;
  }
  setReadOnly(mReadOnly);
}

void PhoneEditWidget::changed( int pos )
{
  mPhoneNumberList[ pos ] = mWidgets.at( pos )->number();
  changed();
}

///////////////////////////////////////////
// PhoneTypeDialog
PhoneTypeDialog::PhoneTypeDialog( int type, TQWidget *parent )
  : KDialogBase( Plain, i18n( "Edit Phone Number" ), Ok | Cancel, Ok,
                 parent, "PhoneTypeDialog", true ),
    mType( type )
{
  TQWidget *page = plainPage();

  TQVBoxLayout *layout = new TQVBoxLayout( page, spacingHint() );

  mPreferredBox = new TQCheckBox( i18n( "This is the preferred phone number" ), page );
  layout->addWidget( mPreferredBox );

  mGroup = new TQButtonGroup( 2, TQt::Horizontal, i18n( "Types" ), page );
  layout->addWidget( mGroup );

  // fill widgets
  mTypeList = TDEABC::PhoneNumber::typeList();
  mTypeList.remove( TDEABC::PhoneNumber::Pref );

  TDEABC::PhoneNumber::TypeList::ConstIterator it;
  for ( it = mTypeList.begin(); it != mTypeList.end(); ++it )
    new TQCheckBox( TDEABC::PhoneNumber::typeLabel( *it ), mGroup );

  for ( int i = 0; i < mGroup->count(); ++i ) {
    TQCheckBox *box = (TQCheckBox*)mGroup->find( i );
    box->setChecked( mType & mTypeList[ i ] );
  }

  mPreferredBox->setChecked( mType & TDEABC::PhoneNumber::Pref );
}

int PhoneTypeDialog::type() const
{
  int type = 0;

  for ( int i = 0; i < mGroup->count(); ++i ) {
    TQCheckBox *box = (TQCheckBox*)mGroup->find( i );
    if ( box->isChecked() )
      type += mTypeList[ i ];
  }

  if ( mPreferredBox->isChecked() )
    type = type | TDEABC::PhoneNumber::Pref;
  else
    type = type & ~TDEABC::PhoneNumber::Pref;

  return type;
}


#include "phoneeditwidget.moc"