• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.5 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
  • contact
  • editor
phoneeditwidget.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "phoneeditwidget.h"
23 
24 #include "autoqpointer_p.h"
25 
26 #include <QtCore/QSignalMapper>
27 #include <QtCore/QString>
28 #include <QtGui/QButtonGroup>
29 #include <QtGui/QCheckBox>
30 #include <QtGui/QGridLayout>
31 #include <QtGui/QGroupBox>
32 #include <QtGui/QHBoxLayout>
33 #include <QtGui/QPushButton>
34 #include <QtGui/QScrollArea>
35 #include <QtGui/QScrollBar>
36 #include <QtGui/QVBoxLayout>
37 
38 #include <kabc/phonenumber.h>
39 #include <kcombobox.h>
40 #include <kdebug.h>
41 #include <klineedit.h>
42 #include <klocale.h>
43 
44 PhoneTypeCombo::PhoneTypeCombo( QWidget *parent )
45  : KComboBox( parent ),
46  mType( KABC::PhoneNumber::Home ),
47  mLastSelected( 0 )
48 {
49  for ( int i = 0; i < KABC::PhoneNumber::typeList().count(); ++i )
50  mTypeList.append( KABC::PhoneNumber::typeList().at( i ) );
51 
52  mTypeList.append( -1 ); // Others...
53 
54  update();
55 
56  connect( this, SIGNAL(activated(int)),
57  this, SLOT(selected(int)) );
58 }
59 
60 PhoneTypeCombo::~PhoneTypeCombo()
61 {
62 }
63 
64 void PhoneTypeCombo::setType( KABC::PhoneNumber::Type type )
65 {
66  if ( !mTypeList.contains( type ) )
67  mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), type );
68 
69  mType = type;
70  update();
71 }
72 
73 KABC::PhoneNumber::Type PhoneTypeCombo::type() const
74 {
75  return mType;
76 }
77 
78 void PhoneTypeCombo::update()
79 {
80  clear();
81 
82  for ( int i = 0; i < mTypeList.count(); ++i ) {
83  if ( mTypeList.at( i ) == -1 ) // "Other..." entry
84  addItem( i18nc( "@item:inlistbox Category of contact info field", "Other..." ) );
85  else
86  addItem( KABC::PhoneNumber::typeLabel( KABC::PhoneNumber::Type( mTypeList.at( i ) ) ) );
87  }
88 
89  setCurrentIndex( mLastSelected = mTypeList.indexOf( mType ) );
90 }
91 
92 void PhoneTypeCombo::selected( int pos )
93 {
94  if ( mTypeList.at( pos ) == -1 )
95  otherSelected();
96  else {
97  mType = KABC::PhoneNumber::Type( mTypeList.at( pos ) );
98  mLastSelected = pos;
99  }
100 }
101 
102 void PhoneTypeCombo::otherSelected()
103 {
104  AutoQPointer<PhoneTypeDialog> dlg = new PhoneTypeDialog( mType, this );
105  if ( dlg->exec() ) {
106  mType = dlg->type();
107  if ( !mTypeList.contains( mType ) )
108  mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), mType );
109  } else {
110  setType( KABC::PhoneNumber::Type( mTypeList.at( mLastSelected ) ) );
111  }
112 
113  update();
114 }
115 
116 PhoneNumberWidget::PhoneNumberWidget( QWidget *parent )
117  : QWidget( parent )
118 {
119  QHBoxLayout *layout = new QHBoxLayout( this );
120  layout->setSpacing( 11 );
121  layout->setMargin( 0 );
122 
123  mTypeCombo = new PhoneTypeCombo( this );
124  mNumberEdit = new KLineEdit( this );
125  QFontMetrics fm(font());
126  mNumberEdit->setMinimumWidth(fm.width(QLatin1String("MMMMMMMMMM")));
127 
128  layout->addWidget( mTypeCombo );
129  layout->addWidget( mNumberEdit );
130 
131  connect( mTypeCombo, SIGNAL(activated(int)), SIGNAL(modified()) );
132  connect( mNumberEdit, SIGNAL(textChanged(QString)), SIGNAL(modified()) );
133 }
134 
135 void PhoneNumberWidget::setNumber( const KABC::PhoneNumber &number )
136 {
137  mNumber = number;
138 
139  disconnect( mTypeCombo, SIGNAL(activated(int)), this, SIGNAL(modified()) );
140  mTypeCombo->setType( number.type() );
141  connect( mTypeCombo, SIGNAL(activated(int)), SIGNAL(modified()) );
142 
143  mNumberEdit->setText( number.number() );
144 }
145 
146 KABC::PhoneNumber PhoneNumberWidget::number() const
147 {
148  KABC::PhoneNumber number( mNumber );
149 
150  number.setType( mTypeCombo->type() );
151  number.setNumber( mNumberEdit->text() );
152 
153  return number;
154 }
155 
156 void PhoneNumberWidget::setReadOnly( bool readOnly )
157 {
158  mTypeCombo->setEnabled( !readOnly );
159  mNumberEdit->setReadOnly( readOnly );
160 }
161 
162 PhoneNumberListWidget::PhoneNumberListWidget( QWidget *parent )
163  : QWidget( parent ), mReadOnly( false )
164 {
165  mWidgetLayout = new QVBoxLayout( this );
166 
167  mMapper = new QSignalMapper( this );
168  connect( mMapper, SIGNAL(mapped(int)), SLOT(changed(int)) );
169 
170  setPhoneNumbers( KABC::PhoneNumber::List() );
171 }
172 
173 PhoneNumberListWidget::~PhoneNumberListWidget()
174 {
175 }
176 
177 void PhoneNumberListWidget::setReadOnly( bool readOnly )
178 {
179  mReadOnly = readOnly;
180 
181  foreach ( PhoneNumberWidget *const widget, mWidgets )
182  widget->setReadOnly( readOnly );
183 }
184 
185 int PhoneNumberListWidget::phoneNumberCount() const
186 {
187  return mPhoneNumberList.count();
188 }
189 
190 void PhoneNumberListWidget::setPhoneNumbers( const KABC::PhoneNumber::List &list )
191 {
192  mPhoneNumberList = list;
193 
194  KABC::PhoneNumber::TypeList types;
195  types << KABC::PhoneNumber::Home;
196  types << KABC::PhoneNumber::Work;
197  types << KABC::PhoneNumber::Cell;
198 
199  // add an empty entry per default
200  if ( mPhoneNumberList.count() < 3 )
201  for ( int i = mPhoneNumberList.count(); i < 3; ++i )
202  mPhoneNumberList.append( KABC::PhoneNumber( QString(), types[ i ] ) );
203 
204  recreateNumberWidgets();
205 }
206 
207 KABC::PhoneNumber::List PhoneNumberListWidget::phoneNumbers() const
208 {
209  KABC::PhoneNumber::List list;
210 
211  KABC::PhoneNumber::List::ConstIterator it;
212  for ( it = mPhoneNumberList.constBegin(); it != mPhoneNumberList.constEnd(); ++it )
213  if ( !(*it).number().isEmpty() )
214  list.append( *it );
215 
216  return list;
217 }
218 
219 void PhoneNumberListWidget::add()
220 {
221  mPhoneNumberList.append( KABC::PhoneNumber() );
222 
223  recreateNumberWidgets();
224 }
225 
226 void PhoneNumberListWidget::remove()
227 {
228  mPhoneNumberList.removeLast();
229 
230  recreateNumberWidgets();
231 }
232 
233 void PhoneNumberListWidget::recreateNumberWidgets()
234 {
235  foreach ( QWidget *const widget, mWidgets ) {
236  mWidgetLayout->removeWidget( widget );
237  delete widget;
238  }
239  mWidgets.clear();
240 
241  KABC::PhoneNumber::List::ConstIterator it;
242  int counter = 0;
243  for ( it = mPhoneNumberList.constBegin(); it != mPhoneNumberList.constEnd(); ++it ) {
244  PhoneNumberWidget *wdg = new PhoneNumberWidget( this );
245  wdg->setNumber( *it );
246 
247  mMapper->setMapping( wdg, counter );
248  connect( wdg, SIGNAL(modified()), mMapper, SLOT(map()) );
249 
250  mWidgetLayout->addWidget( wdg );
251  mWidgets.append( wdg );
252  wdg->show();
253 
254  ++counter;
255  }
256 
257  setReadOnly( mReadOnly );
258 }
259 
260 void PhoneNumberListWidget::changed( int pos )
261 {
262  mPhoneNumberList[ pos ] = mWidgets.at( pos )->number();
263 }
264 
265 PhoneEditWidget::PhoneEditWidget( QWidget *parent )
266  : QWidget( parent ), mReadOnly( false )
267 {
268  QGridLayout *layout = new QGridLayout( this );
269  layout->setSpacing( KDialog::spacingHint() );
270 
271  mListScrollArea = new QScrollArea( this );
272  mPhoneNumberListWidget = new PhoneNumberListWidget;
273  mListScrollArea->setWidget( mPhoneNumberListWidget );
274  mListScrollArea->setWidgetResizable( true );
275 
276  // ugly but size policies seem to be messed up dialog (parent) wide
277  const int scrollAreaMinHeight = mPhoneNumberListWidget->sizeHint().height() +
278  mListScrollArea->horizontalScrollBar()->sizeHint().height();
279  mListScrollArea->setMinimumHeight( scrollAreaMinHeight );
280  layout->addWidget( mListScrollArea, 0, 0, 1, 2 );
281 
282  mAddButton = new QPushButton( i18n( "Add" ), this );
283  mAddButton->setMaximumSize( mAddButton->sizeHint() );
284  layout->addWidget( mAddButton, 1, 0, Qt::AlignRight );
285 
286  mRemoveButton = new QPushButton( i18n( "Remove" ), this );
287  mRemoveButton->setMaximumSize( mRemoveButton->sizeHint() );
288  layout->addWidget( mRemoveButton, 1, 1 );
289 
290  connect( mAddButton, SIGNAL(clicked()), mPhoneNumberListWidget, SLOT(add()) );
291  connect( mRemoveButton, SIGNAL(clicked()), mPhoneNumberListWidget, SLOT(remove()) );
292  connect( mAddButton, SIGNAL(clicked()), SLOT(changed()) );
293  connect( mRemoveButton, SIGNAL(clicked()), SLOT(changed()) );
294 }
295 
296 PhoneEditWidget::~PhoneEditWidget()
297 {
298 }
299 
300 void PhoneEditWidget::setReadOnly( bool readOnly )
301 {
302  mReadOnly = readOnly;
303  mAddButton->setEnabled( !readOnly );
304  mRemoveButton->setEnabled( !readOnly && mPhoneNumberListWidget->phoneNumberCount() > 3 );
305 
306  mPhoneNumberListWidget->setReadOnly( readOnly );
307 }
308 
309 void PhoneEditWidget::changed()
310 {
311  mRemoveButton->setEnabled( !mReadOnly && mPhoneNumberListWidget->phoneNumberCount() > 3 );
312 }
313 
314 void PhoneEditWidget::loadContact( const KABC::Addressee &contact )
315 {
316  mPhoneNumberListWidget->setPhoneNumbers( contact.phoneNumbers() );
317  changed();
318 }
319 
320 void PhoneEditWidget::storeContact( KABC::Addressee &contact ) const
321 {
322  const KABC::PhoneNumber::List oldNumbers = contact.phoneNumbers();
323  for ( int i = 0; i < oldNumbers.count(); ++i )
324  contact.removePhoneNumber( oldNumbers.at( i ) );
325 
326  const KABC::PhoneNumber::List newNumbers = mPhoneNumberListWidget->phoneNumbers();
327  for ( int i = 0; i < newNumbers.count(); ++i )
328  contact.insertPhoneNumber( newNumbers.at( i ) );
329 }
330 
332 // PhoneTypeDialog
333 PhoneTypeDialog::PhoneTypeDialog( KABC::PhoneNumber::Type type, QWidget *parent )
334  : KDialog( parent),
335  mType( type )
336 {
337  setCaption( i18n( "Edit Phone Number" ) );
338  setButtons( Ok | Cancel );
339  setDefaultButton( Ok );
340  showButtonSeparator( true );
341 
342  QWidget *page = new QWidget( this );
343  setMainWidget( page );
344 
345  QVBoxLayout *layout = new QVBoxLayout( page );
346  layout->setSpacing( spacingHint() );
347  layout->setMargin( 0 );
348 
349  mPreferredBox = new QCheckBox( i18n( "This is the preferred phone number" ), page );
350  layout->addWidget( mPreferredBox );
351 
352  QGroupBox *box = new QGroupBox( i18n( "Types" ), page );
353  layout->addWidget( box );
354 
355  QGridLayout *buttonLayout = new QGridLayout( box );
356 
357  // fill widgets
358  mTypeList = KABC::PhoneNumber::typeList();
359  mTypeList.removeAll( KABC::PhoneNumber::Pref );
360 
361  KABC::PhoneNumber::TypeList::ConstIterator it;
362  mGroup = new QButtonGroup( box );
363  mGroup->setExclusive( false );
364  int row, column, counter;
365  row = column = counter = 0;
366  for ( it = mTypeList.constBegin(); it != mTypeList.constEnd(); ++it, ++counter ) {
367  QCheckBox *cb = new QCheckBox( KABC::PhoneNumber::typeLabel( *it ), box );
368  cb->setChecked( type & mTypeList[ counter ] );
369  buttonLayout->addWidget( cb, row, column );
370  mGroup->addButton( cb );
371 
372  column++;
373  if ( column == 5 ) {
374  column = 0;
375  ++row;
376  }
377  }
378 
379  mPreferredBox->setChecked( mType & KABC::PhoneNumber::Pref );
380 }
381 
382 KABC::PhoneNumber::Type PhoneTypeDialog::type() const
383 {
384  KABC::PhoneNumber::Type type = 0;
385 
386  for ( int i = 0; i < mGroup->buttons().count(); ++i ) {
387  QCheckBox *box = dynamic_cast<QCheckBox*>( mGroup->buttons().at( i ) ) ;
388  if ( box && box->isChecked() )
389  type |= mTypeList[ i ];
390  }
391 
392  if ( mPreferredBox->isChecked() )
393  type = type | KABC::PhoneNumber::Pref;
394  else
395  type = type & ~KABC::PhoneNumber::Pref;
396 
397  return type;
398 }
399 
400 #include "phoneeditwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:46:06 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.9.5 API Reference

Skip menu "kdepimlibs-4.9.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal