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

akonadi/contact

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 <QButtonGroup>
29 #include <QCheckBox>
30 #include <QGridLayout>
31 #include <QGroupBox>
32 #include <QHBoxLayout>
33 #include <QPushButton>
34 #include <QScrollArea>
35 #include <QScrollBar>
36 #include <QVBoxLayout>
37 
38 #include <kabc/phonenumber.h>
39 #include <kcombobox.h>
40 #include <kdebug.h>
41 #include <klineedit.h>
42 #include <klocalizedstring.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 
53  mTypeList.append( -1 ); // Others...
54 
55  update();
56 
57  connect( this, SIGNAL(activated(int)),
58  this, SLOT(selected(int)) );
59 }
60 
61 PhoneTypeCombo::~PhoneTypeCombo()
62 {
63 }
64 
65 void PhoneTypeCombo::setType( KABC::PhoneNumber::Type type )
66 {
67  if ( !mTypeList.contains( type ) ) {
68  mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), type );
69  }
70 
71  mType = type;
72  update();
73 }
74 
75 KABC::PhoneNumber::Type PhoneTypeCombo::type() const
76 {
77  return mType;
78 }
79 
80 void PhoneTypeCombo::update()
81 {
82  clear();
83 
84  for ( int i = 0; i < mTypeList.count(); ++i ) {
85  if ( mTypeList.at( i ) == -1 ) { // "Other..." entry
86  addItem( i18nc( "@item:inlistbox Category of contact info field", "Other..." ) );
87  } else {
88  addItem( KABC::PhoneNumber::typeLabel( KABC::PhoneNumber::Type( mTypeList.at( i ) ) ) );
89  }
90  }
91 
92  setCurrentIndex( mLastSelected = mTypeList.indexOf( mType ) );
93 }
94 
95 void PhoneTypeCombo::selected( int pos )
96 {
97  if ( mTypeList.at( pos ) == -1 ) {
98  otherSelected();
99  } else {
100  mType = KABC::PhoneNumber::Type( mTypeList.at( pos ) );
101  mLastSelected = pos;
102  }
103 }
104 
105 void PhoneTypeCombo::otherSelected()
106 {
107  AutoQPointer<PhoneTypeDialog> dlg = new PhoneTypeDialog( mType, this );
108  if ( dlg->exec() ) {
109  mType = dlg->type();
110  if ( !mTypeList.contains( mType ) ) {
111  mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), mType );
112  }
113  } else {
114  setType( KABC::PhoneNumber::Type( mTypeList.at( mLastSelected ) ) );
115  }
116 
117  update();
118 }
119 
120 PhoneNumberWidget::PhoneNumberWidget( QWidget *parent )
121  : QWidget( parent )
122 {
123  QHBoxLayout *layout = new QHBoxLayout( this );
124  layout->setSpacing( 11 );
125  layout->setMargin( 0 );
126 
127  mTypeCombo = new PhoneTypeCombo( this );
128  mNumberEdit = new KLineEdit( this );
129  QFontMetrics fm(font());
130  mNumberEdit->setMinimumWidth(fm.width(QLatin1String("MMMMMMMMMM")));
131 
132  layout->addWidget( mTypeCombo );
133  layout->addWidget( mNumberEdit );
134 
135  connect( mTypeCombo, SIGNAL(activated(int)), SIGNAL(modified()) );
136  connect( mNumberEdit, SIGNAL(textChanged(QString)), SIGNAL(modified()) );
137 }
138 
139 void PhoneNumberWidget::setNumber( const KABC::PhoneNumber &number )
140 {
141  mNumber = number;
142 
143  disconnect( mTypeCombo, SIGNAL(activated(int)), this, SIGNAL(modified()) );
144  mTypeCombo->setType( number.type() );
145  connect( mTypeCombo, SIGNAL(activated(int)), SIGNAL(modified()) );
146 
147  mNumberEdit->setText( number.number() );
148 }
149 
150 KABC::PhoneNumber PhoneNumberWidget::number() const
151 {
152  KABC::PhoneNumber number( mNumber );
153 
154  number.setType( mTypeCombo->type() );
155  number.setNumber( mNumberEdit->text() );
156 
157  return number;
158 }
159 
160 void PhoneNumberWidget::setReadOnly( bool readOnly )
161 {
162  mTypeCombo->setEnabled( !readOnly );
163  mNumberEdit->setReadOnly( readOnly );
164 }
165 
166 PhoneNumberListWidget::PhoneNumberListWidget( QWidget *parent )
167  : QWidget( parent ), mReadOnly( false )
168 {
169  mWidgetLayout = new QVBoxLayout( this );
170 
171  mMapper = new QSignalMapper( this );
172  connect( mMapper, SIGNAL(mapped(int)), SLOT(changed(int)) );
173 
174  setPhoneNumbers( KABC::PhoneNumber::List() );
175 }
176 
177 PhoneNumberListWidget::~PhoneNumberListWidget()
178 {
179 }
180 
181 void PhoneNumberListWidget::setReadOnly( bool readOnly )
182 {
183  mReadOnly = readOnly;
184 
185  foreach ( PhoneNumberWidget *const widget, mWidgets ) {
186  widget->setReadOnly( readOnly );
187  }
188 }
189 
190 int PhoneNumberListWidget::phoneNumberCount() const
191 {
192  return mPhoneNumberList.count();
193 }
194 
195 void PhoneNumberListWidget::setPhoneNumbers( const KABC::PhoneNumber::List &list )
196 {
197  mPhoneNumberList = list;
198 
199  KABC::PhoneNumber::TypeList types;
200  types << KABC::PhoneNumber::Home;
201  types << KABC::PhoneNumber::Work;
202  types << KABC::PhoneNumber::Cell;
203 
204  // add an empty entry per default
205  if ( mPhoneNumberList.count() < 3 ) {
206  for ( int i = mPhoneNumberList.count(); i < 3; ++i ) {
207  mPhoneNumberList.append( KABC::PhoneNumber( QString(), types[ i ] ) );
208  }
209  }
210 
211  recreateNumberWidgets();
212 }
213 
214 KABC::PhoneNumber::List PhoneNumberListWidget::phoneNumbers() const
215 {
216  KABC::PhoneNumber::List list;
217 
218  KABC::PhoneNumber::List::ConstIterator it;
219  for ( it = mPhoneNumberList.constBegin(); it != mPhoneNumberList.constEnd(); ++it ) {
220  if ( !( *it ).number().isEmpty() ) {
221  list.append( *it );
222  }
223  }
224 
225  return list;
226 }
227 
228 void PhoneNumberListWidget::add()
229 {
230  mPhoneNumberList.append( KABC::PhoneNumber() );
231 
232  recreateNumberWidgets();
233 }
234 
235 void PhoneNumberListWidget::remove()
236 {
237  mPhoneNumberList.removeLast();
238 
239  recreateNumberWidgets();
240 }
241 
242 void PhoneNumberListWidget::recreateNumberWidgets()
243 {
244  foreach ( QWidget *const widget, mWidgets ) {
245  mWidgetLayout->removeWidget( widget );
246  delete widget;
247  }
248  mWidgets.clear();
249 
250  KABC::PhoneNumber::List::ConstIterator it;
251  int counter = 0;
252  for ( it = mPhoneNumberList.constBegin(); it != mPhoneNumberList.constEnd(); ++it ) {
253  PhoneNumberWidget *wdg = new PhoneNumberWidget( this );
254  wdg->setNumber( *it );
255 
256  mMapper->setMapping( wdg, counter );
257  connect( wdg, SIGNAL(modified()), mMapper, SLOT(map()) );
258 
259  mWidgetLayout->addWidget( wdg );
260  mWidgets.append( wdg );
261  wdg->show();
262 
263  ++counter;
264  }
265 
266  setReadOnly( mReadOnly );
267 }
268 
269 void PhoneNumberListWidget::changed( int pos )
270 {
271  mPhoneNumberList[ pos ] = mWidgets.at( pos )->number();
272 }
273 
274 PhoneEditWidget::PhoneEditWidget( QWidget *parent )
275  : QWidget( parent ), mReadOnly( false )
276 {
277  QGridLayout *layout = new QGridLayout( this );
278  layout->setSpacing( KDialog::spacingHint() );
279 
280  mListScrollArea = new QScrollArea( this );
281  mPhoneNumberListWidget = new PhoneNumberListWidget;
282  mListScrollArea->setWidget( mPhoneNumberListWidget );
283  mListScrollArea->setWidgetResizable( true );
284 
285  // ugly but size policies seem to be messed up dialog (parent) wide
286  const int scrollAreaMinHeight = mPhoneNumberListWidget->sizeHint().height() +
287  mListScrollArea->horizontalScrollBar()->sizeHint().height();
288  mListScrollArea->setMinimumHeight( scrollAreaMinHeight );
289  layout->addWidget( mListScrollArea, 0, 0, 1, 2 );
290 
291  mAddButton = new QPushButton( i18n( "Add" ), this );
292  mAddButton->setMaximumSize( mAddButton->sizeHint() );
293  layout->addWidget( mAddButton, 1, 0, Qt::AlignRight );
294 
295  mRemoveButton = new QPushButton( i18n( "Remove" ), this );
296  mRemoveButton->setMaximumSize( mRemoveButton->sizeHint() );
297  layout->addWidget( mRemoveButton, 1, 1 );
298 
299  connect( mAddButton, SIGNAL(clicked()), mPhoneNumberListWidget, SLOT(add()) );
300  connect( mRemoveButton, SIGNAL(clicked()), mPhoneNumberListWidget, SLOT(remove()) );
301  connect( mAddButton, SIGNAL(clicked()), SLOT(changed()) );
302  connect( mRemoveButton, SIGNAL(clicked()), SLOT(changed()) );
303 }
304 
305 PhoneEditWidget::~PhoneEditWidget()
306 {
307 }
308 
309 void PhoneEditWidget::setReadOnly( bool readOnly )
310 {
311  mReadOnly = readOnly;
312  mAddButton->setEnabled( !readOnly );
313  mRemoveButton->setEnabled( !readOnly && mPhoneNumberListWidget->phoneNumberCount() > 3 );
314 
315  mPhoneNumberListWidget->setReadOnly( readOnly );
316 }
317 
318 void PhoneEditWidget::changed()
319 {
320  mRemoveButton->setEnabled( !mReadOnly && mPhoneNumberListWidget->phoneNumberCount() > 3 );
321 }
322 
323 void PhoneEditWidget::loadContact( const KABC::Addressee &contact )
324 {
325  mPhoneNumberListWidget->setPhoneNumbers( contact.phoneNumbers() );
326  changed();
327 }
328 
329 void PhoneEditWidget::storeContact( KABC::Addressee &contact ) const
330 {
331  const KABC::PhoneNumber::List oldNumbers = contact.phoneNumbers();
332  for ( int i = 0; i < oldNumbers.count(); ++i ) {
333  contact.removePhoneNumber( oldNumbers.at( i ) );
334  }
335 
336  const KABC::PhoneNumber::List newNumbers = mPhoneNumberListWidget->phoneNumbers();
337  for ( int i = 0; i < newNumbers.count(); ++i ) {
338  contact.insertPhoneNumber( newNumbers.at( i ) );
339  }
340 }
341 
343 // PhoneTypeDialog
344 PhoneTypeDialog::PhoneTypeDialog( KABC::PhoneNumber::Type type, QWidget *parent )
345  : KDialog( parent),
346  mType( type )
347 {
348  setCaption( i18n( "Edit Phone Number" ) );
349  setButtons( Ok | Cancel );
350  setDefaultButton( Ok );
351  showButtonSeparator( true );
352 
353  QWidget *page = new QWidget( this );
354  setMainWidget( page );
355 
356  QVBoxLayout *layout = new QVBoxLayout( page );
357  layout->setSpacing( spacingHint() );
358  layout->setMargin( 0 );
359 
360  mPreferredBox = new QCheckBox( i18n( "This is the preferred phone number" ), page );
361  layout->addWidget( mPreferredBox );
362 
363  QGroupBox *box = new QGroupBox( i18n( "Types" ), page );
364  layout->addWidget( box );
365 
366  QGridLayout *buttonLayout = new QGridLayout( box );
367 
368  // fill widgets
369  mTypeList = KABC::PhoneNumber::typeList();
370  mTypeList.removeAll( KABC::PhoneNumber::Pref );
371 
372  KABC::PhoneNumber::TypeList::ConstIterator it;
373  mGroup = new QButtonGroup( box );
374  mGroup->setExclusive( false );
375  int row, column, counter;
376  row = column = counter = 0;
377  for ( it = mTypeList.constBegin(); it != mTypeList.constEnd(); ++it, ++counter ) {
378  QCheckBox *cb = new QCheckBox( KABC::PhoneNumber::typeLabel( *it ), box );
379  cb->setChecked( type & mTypeList[ counter ] );
380  buttonLayout->addWidget( cb, row, column );
381  mGroup->addButton( cb );
382 
383  column++;
384  if ( column == 5 ) {
385  column = 0;
386  ++row;
387  }
388  }
389 
390  mPreferredBox->setChecked( mType & KABC::PhoneNumber::Pref );
391 }
392 
393 KABC::PhoneNumber::Type PhoneTypeDialog::type() const
394 {
395  KABC::PhoneNumber::Type type = 0;
396 
397  for ( int i = 0; i < mGroup->buttons().count(); ++i ) {
398  QCheckBox *box = dynamic_cast<QCheckBox*>( mGroup->buttons().at( i ) ) ;
399  if ( box && box->isChecked() ) {
400  type |= mTypeList[ i ];
401  }
402  }
403 
404  if ( mPreferredBox->isChecked() ) {
405  type = type | KABC::PhoneNumber::Pref;
406  } else {
407  type = type & ~KABC::PhoneNumber::Pref;
408  }
409 
410  return type;
411 }
412 
PhoneTypeDialog::PhoneTypeDialog
PhoneTypeDialog(KABC::PhoneNumber::Type type, QWidget *parent=0)
Creates a new phone type dialog.
Definition: phoneeditwidget.cpp:344
PhoneNumberListWidget::~PhoneNumberListWidget
~PhoneNumberListWidget()
Destroys the phone number list widget.
Definition: phoneeditwidget.cpp:177
PhoneNumberWidget::number
KABC::PhoneNumber number() const
Returns the phone number of the widget.
Definition: phoneeditwidget.cpp:150
PhoneNumberWidget
A widget that provides selectors for the type and number of a phone number entry. ...
Definition: phoneeditwidget.h:85
PhoneNumberListWidget::setReadOnly
void setReadOnly(bool readOnly)
Sets the widget to readOnly mode.
Definition: phoneeditwidget.cpp:181
PhoneNumberWidget::setReadOnly
void setReadOnly(bool readOnly)
Sets the widget to readOnly mode.
Definition: phoneeditwidget.cpp:160
PhoneNumberListWidget::remove
void remove()
Removes the last phone number widget from this widget.
Definition: phoneeditwidget.cpp:235
PhoneNumberListWidget::PhoneNumberListWidget
PhoneNumberListWidget(QWidget *parent=0)
Creates a new phone number list widget.
Definition: phoneeditwidget.cpp:166
PhoneEditWidget::setReadOnly
void setReadOnly(bool readOnly)
Sets the widget to readOnly mode.
Definition: phoneeditwidget.cpp:309
PhoneTypeDialog
A dialog for editing phone number types.
Definition: phoneeditwidget.h:238
PhoneEditWidget::storeContact
void storeContact(KABC::Addressee &contact) const
Stores the data from the widget to the contact.
Definition: phoneeditwidget.cpp:329
PhoneNumberListWidget::phoneNumberCount
int phoneNumberCount() const
Returns the number of phone numbers available.
Definition: phoneeditwidget.cpp:190
PhoneTypeCombo
A combobox to select a phone number type.
Definition: phoneeditwidget.h:42
PhoneTypeCombo::~PhoneTypeCombo
~PhoneTypeCombo()
Destroys the phone type combo.
Definition: phoneeditwidget.cpp:61
PhoneEditWidget::PhoneEditWidget
PhoneEditWidget(QWidget *parent=0)
Creates a new phone edit widget.
Definition: phoneeditwidget.cpp:274
AutoQPointer
A QPointer which when destructed, deletes the object it points to.
Definition: autoqpointer_p.h:34
PhoneEditWidget::loadContact
void loadContact(const KABC::Addressee &contact)
Loads the data from contact to the widget.
Definition: phoneeditwidget.cpp:323
PhoneNumberListWidget::phoneNumbers
KABC::PhoneNumber::List phoneNumbers() const
Returns the list of phone numbers.
Definition: phoneeditwidget.cpp:214
PhoneTypeDialog::type
KABC::PhoneNumber::Type type() const
Returns the selected type.
Definition: phoneeditwidget.cpp:393
PhoneTypeCombo::PhoneTypeCombo
PhoneTypeCombo(QWidget *parent=0)
Creates a phone type combo.
Definition: phoneeditwidget.cpp:44
PhoneTypeCombo::setType
void setType(KABC::PhoneNumber::Type type)
Sets the phone number type that shall be selected.
Definition: phoneeditwidget.cpp:65
PhoneNumberWidget::setNumber
void setNumber(const KABC::PhoneNumber &number)
Sets the phone number of the widget.
Definition: phoneeditwidget.cpp:139
PhoneNumberListWidget::add
void add()
Adds a new phone number widget to this widget.
Definition: phoneeditwidget.cpp:228
PhoneNumberWidget::PhoneNumberWidget
PhoneNumberWidget(QWidget *parent=0)
Creates a new phone number widget.
Definition: phoneeditwidget.cpp:120
PhoneNumberListWidget::setPhoneNumbers
void setPhoneNumbers(const KABC::PhoneNumber::List &list)
Sets the list of phone numbers the widget shall show.
Definition: phoneeditwidget.cpp:195
PhoneTypeCombo::type
KABC::PhoneNumber::Type type() const
Returns the selected phone number type.
Definition: phoneeditwidget.cpp:75
PhoneEditWidget::~PhoneEditWidget
~PhoneEditWidget()
Destroys the phone edit widget.
Definition: phoneeditwidget.cpp:305
PhoneNumberListWidget
A widgets that groups together a list of PhoneNumberWidgets.
Definition: phoneeditwidget.h:124
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:49 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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