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

akonadi

  • home
  • ichiro
  • data
  • ssd
  • Momonga
  • trunk
  • pkgs
  • kdepimlibs
  • BUILD
  • kdepimlibs-4.14.3
  • 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)
168  , mReadOnly(false)
169 {
170  mWidgetLayout = new QVBoxLayout(this);
171 
172  mMapper = new QSignalMapper(this);
173  connect(mMapper, SIGNAL(mapped(int)), SLOT(changed(int)));
174 
175  setPhoneNumbers(KABC::PhoneNumber::List());
176 }
177 
178 PhoneNumberListWidget::~PhoneNumberListWidget()
179 {
180 }
181 
182 void PhoneNumberListWidget::setReadOnly(bool readOnly)
183 {
184  mReadOnly = readOnly;
185 
186  foreach (PhoneNumberWidget *const widget, mWidgets) {
187  widget->setReadOnly(readOnly);
188  }
189 }
190 
191 int PhoneNumberListWidget::phoneNumberCount() const
192 {
193  return mPhoneNumberList.count();
194 }
195 
196 void PhoneNumberListWidget::setPhoneNumbers(const KABC::PhoneNumber::List &list)
197 {
198  mPhoneNumberList = list;
199 
200  KABC::PhoneNumber::TypeList types;
201  types << KABC::PhoneNumber::Home;
202  types << KABC::PhoneNumber::Work;
203  types << KABC::PhoneNumber::Cell;
204 
205  // add an empty entry per default
206  if (mPhoneNumberList.count() < 3) {
207  for (int i = mPhoneNumberList.count(); i < 3; ++i) {
208  mPhoneNumberList.append(KABC::PhoneNumber(QString(), types[i]));
209  }
210  }
211 
212  recreateNumberWidgets();
213 }
214 
215 KABC::PhoneNumber::List PhoneNumberListWidget::phoneNumbers() const
216 {
217  KABC::PhoneNumber::List list;
218 
219  KABC::PhoneNumber::List::ConstIterator it;
220  for (it = mPhoneNumberList.constBegin(); it != mPhoneNumberList.constEnd(); ++it) {
221  if (!(*it).number().isEmpty()) {
222  list.append(*it);
223  }
224  }
225 
226  return list;
227 }
228 
229 void PhoneNumberListWidget::add()
230 {
231  mPhoneNumberList.append(KABC::PhoneNumber());
232 
233  recreateNumberWidgets();
234 }
235 
236 void PhoneNumberListWidget::remove()
237 {
238  mPhoneNumberList.removeLast();
239 
240  recreateNumberWidgets();
241 }
242 
243 void PhoneNumberListWidget::recreateNumberWidgets()
244 {
245  foreach (QWidget *const widget, mWidgets) {
246  mWidgetLayout->removeWidget(widget);
247  delete widget;
248  }
249  mWidgets.clear();
250 
251  KABC::PhoneNumber::List::ConstIterator it;
252  int counter = 0;
253  for (it = mPhoneNumberList.constBegin(); it != mPhoneNumberList.constEnd(); ++it) {
254  PhoneNumberWidget *wdg = new PhoneNumberWidget(this);
255  wdg->setNumber(*it);
256 
257  mMapper->setMapping(wdg, counter);
258  connect(wdg, SIGNAL(modified()), mMapper, SLOT(map()));
259 
260  mWidgetLayout->addWidget(wdg);
261  mWidgets.append(wdg);
262  wdg->show();
263 
264  ++counter;
265  }
266 
267  setReadOnly(mReadOnly);
268 }
269 
270 void PhoneNumberListWidget::changed(int pos)
271 {
272  mPhoneNumberList[pos] = mWidgets.at(pos)->number();
273 }
274 
275 PhoneEditWidget::PhoneEditWidget(QWidget *parent)
276  : QWidget(parent)
277  , mReadOnly(false)
278 {
279  QGridLayout *layout = new QGridLayout(this);
280  layout->setSpacing(KDialog::spacingHint());
281 
282  mListScrollArea = new QScrollArea(this);
283  mPhoneNumberListWidget = new PhoneNumberListWidget;
284  mListScrollArea->setWidget(mPhoneNumberListWidget);
285  mListScrollArea->setWidgetResizable(true);
286 
287  // ugly but size policies seem to be messed up dialog (parent) wide
288  const int scrollAreaMinHeight = mPhoneNumberListWidget->sizeHint().height() +
289  mListScrollArea->horizontalScrollBar()->sizeHint().height();
290  mListScrollArea->setMinimumHeight(scrollAreaMinHeight);
291  layout->addWidget(mListScrollArea, 0, 0, 1, 2);
292 
293  mAddButton = new QPushButton(i18n("Add"), this);
294  mAddButton->setMaximumSize(mAddButton->sizeHint());
295  layout->addWidget(mAddButton, 1, 0, Qt::AlignRight);
296 
297  mRemoveButton = new QPushButton(i18n("Remove"), this);
298  mRemoveButton->setMaximumSize(mRemoveButton->sizeHint());
299  layout->addWidget(mRemoveButton, 1, 1);
300 
301  connect(mAddButton, SIGNAL(clicked()), mPhoneNumberListWidget, SLOT(add()));
302  connect(mRemoveButton, SIGNAL(clicked()), mPhoneNumberListWidget, SLOT(remove()));
303  connect(mAddButton, SIGNAL(clicked()), SLOT(changed()));
304  connect(mRemoveButton, SIGNAL(clicked()), SLOT(changed()));
305 }
306 
307 PhoneEditWidget::~PhoneEditWidget()
308 {
309 }
310 
311 void PhoneEditWidget::setReadOnly(bool readOnly)
312 {
313  mReadOnly = readOnly;
314  mAddButton->setEnabled(!readOnly);
315  mRemoveButton->setEnabled(!readOnly && mPhoneNumberListWidget->phoneNumberCount() > 3);
316 
317  mPhoneNumberListWidget->setReadOnly(readOnly);
318 }
319 
320 void PhoneEditWidget::changed()
321 {
322  mRemoveButton->setEnabled(!mReadOnly && mPhoneNumberListWidget->phoneNumberCount() > 3);
323 }
324 
325 void PhoneEditWidget::loadContact(const KABC::Addressee &contact)
326 {
327  mPhoneNumberListWidget->setPhoneNumbers(contact.phoneNumbers());
328  changed();
329 }
330 
331 void PhoneEditWidget::storeContact(KABC::Addressee &contact) const
332 {
333  const KABC::PhoneNumber::List oldNumbers = contact.phoneNumbers();
334  for (int i = 0; i < oldNumbers.count(); ++i) {
335  contact.removePhoneNumber(oldNumbers.at(i));
336  }
337 
338  const KABC::PhoneNumber::List newNumbers = mPhoneNumberListWidget->phoneNumbers();
339  for (int i = 0; i < newNumbers.count(); ++i) {
340  contact.insertPhoneNumber(newNumbers.at(i));
341  }
342 }
343 
345 // PhoneTypeDialog
346 PhoneTypeDialog::PhoneTypeDialog(KABC::PhoneNumber::Type type, QWidget *parent)
347  : KDialog(parent)
348  , mType(type)
349 {
350  setCaption(i18n("Edit Phone Number"));
351  setButtons(Ok | Cancel);
352  setDefaultButton(Ok);
353  showButtonSeparator(true);
354 
355  QWidget *page = new QWidget(this);
356  setMainWidget(page);
357 
358  QVBoxLayout *layout = new QVBoxLayout(page);
359  layout->setSpacing(spacingHint());
360  layout->setMargin(0);
361 
362  mPreferredBox = new QCheckBox(i18n("This is the preferred phone number"), page);
363  layout->addWidget(mPreferredBox);
364 
365  QGroupBox *box = new QGroupBox(i18n("Types"), page);
366  layout->addWidget(box);
367 
368  QGridLayout *buttonLayout = new QGridLayout(box);
369 
370  // fill widgets
371  mTypeList = KABC::PhoneNumber::typeList();
372  mTypeList.removeAll(KABC::PhoneNumber::Pref);
373 
374  KABC::PhoneNumber::TypeList::ConstIterator it;
375  mGroup = new QButtonGroup(box);
376  mGroup->setExclusive(false);
377  int row, column, counter;
378  row = column = counter = 0;
379  for (it = mTypeList.constBegin(); it != mTypeList.constEnd(); ++it, ++counter) {
380  QCheckBox *cb = new QCheckBox(KABC::PhoneNumber::typeLabel(*it), box);
381  cb->setChecked(type & mTypeList[counter]);
382  buttonLayout->addWidget(cb, row, column);
383  mGroup->addButton(cb);
384 
385  column++;
386  if (column == 5) {
387  column = 0;
388  ++row;
389  }
390  }
391 
392  mPreferredBox->setChecked(mType & KABC::PhoneNumber::Pref);
393 }
394 
395 KABC::PhoneNumber::Type PhoneTypeDialog::type() const
396 {
397  KABC::PhoneNumber::Type type = 0;
398 
399  for (int i = 0; i < mGroup->buttons().count(); ++i) {
400  QCheckBox *box = dynamic_cast<QCheckBox *>(mGroup->buttons().at(i)) ;
401  if (box && box->isChecked()) {
402  type |= mTypeList[i];
403  }
404  }
405 
406  if (mPreferredBox->isChecked()) {
407  type = type | KABC::PhoneNumber::Pref;
408  } else {
409  type = type & ~KABC::PhoneNumber::Pref;
410  }
411 
412  return type;
413 }
PhoneNumberWidget::number
KABC::PhoneNumber number() const
Returns the phone number of the widget.
Definition: phoneeditwidget.cpp:150
PhoneTypeDialog::PhoneTypeDialog
PhoneTypeDialog(KABC::PhoneNumber::Type type, QWidget *parent=0)
Creates a new phone type dialog.
Definition: phoneeditwidget.cpp:346
PhoneNumberListWidget::~PhoneNumberListWidget
~PhoneNumberListWidget()
Destroys the phone number list widget.
Definition: phoneeditwidget.cpp:178
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:182
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:236
PhoneEditWidget::storeContact
void storeContact(KABC::Addressee &contact) const
Stores the data from the widget to the contact.
Definition: phoneeditwidget.cpp:331
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:311
PhoneTypeDialog
A dialog for editing phone number types.
Definition: phoneeditwidget.h:238
PhoneTypeCombo
A combobox to select a phone number type.
Definition: phoneeditwidget.h:42
PhoneTypeCombo::type
KABC::PhoneNumber::Type type() const
Returns the selected phone number type.
Definition: phoneeditwidget.cpp:75
PhoneTypeDialog::type
KABC::PhoneNumber::Type type() const
Returns the selected type.
Definition: phoneeditwidget.cpp:395
PhoneTypeCombo::~PhoneTypeCombo
~PhoneTypeCombo()
Destroys the phone type combo.
Definition: phoneeditwidget.cpp:61
PhoneNumberListWidget::phoneNumberCount
int phoneNumberCount() const
Returns the number of phone numbers available.
Definition: phoneeditwidget.cpp:191
PhoneEditWidget::PhoneEditWidget
PhoneEditWidget(QWidget *parent=0)
Creates a new phone edit widget.
Definition: phoneeditwidget.cpp:275
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:325
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:229
PhoneNumberWidget::PhoneNumberWidget
PhoneNumberWidget(QWidget *parent=0)
Creates a new phone number widget.
Definition: phoneeditwidget.cpp:120
KABC
Definition: abstractcontacteditorwidget_p.h:27
PhoneNumberListWidget::setPhoneNumbers
void setPhoneNumbers(const KABC::PhoneNumber::List &list)
Sets the list of phone numbers the widget shall show.
Definition: phoneeditwidget.cpp:196
PhoneEditWidget::~PhoneEditWidget
~PhoneEditWidget()
Destroys the phone edit widget.
Definition: phoneeditwidget.cpp:307
PhoneNumberListWidget
A widgets that groups together a list of PhoneNumberWidgets.
Definition: phoneeditwidget.h:124
PhoneNumberListWidget::phoneNumbers
KABC::PhoneNumber::List phoneNumbers() const
Returns the list of phone numbers.
Definition: phoneeditwidget.cpp:215
This file is part of the KDE documentation.
Documentation copyright © 1996-2018 The KDE developers.
Generated on Fri Oct 19 2018 17:57:19 by doxygen 1.8.13 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.14.3 API Reference

Skip menu "kdepimlibs-4.14.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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