Akonadi Contacts

phonewidgetlister.cpp
1 /*
2  This file is part of Contact Editor.
3 
4  SPDX-FileCopyrightText: 2016 eyeOS S.L.U., a Telefonica company, [email protected]
5  SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel.org>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "phonewidgetlister.h"
11 #include "phonewidget.h"
12 
13 #include <KContacts/Addressee>
14 using namespace ContactEditor;
15 
16 PhoneWidgetLister::PhoneWidgetLister(QWidget *parent)
17  : KWidgetLister(1, 8, parent)
18 {
19  setNumberOfShownWidgetsTo(widgetsMinimum());
20  updateAddRemoveButton();
21 }
22 
23 PhoneWidgetLister::~PhoneWidgetLister()
24 {
25 }
26 
27 void PhoneWidgetLister::loadContact(const KContacts::Addressee &contact)
28 {
29  const KContacts::PhoneNumber::List phoneNumbers = contact.phoneNumbers();
30  if (phoneNumbers.isEmpty()) {
31  setNumberOfShownWidgetsTo(1);
32  } else {
33  setNumberOfShownWidgetsTo(phoneNumbers.count());
34  const QList<QWidget *> widgetList = widgets();
35  auto wIt = widgetList.constBegin();
36  auto wEnd = widgetList.constEnd();
37  for (int i = 0; wIt != wEnd; ++wIt, ++i) {
38  auto w = qobject_cast<PhoneWidget *>(*wIt);
39  w->loadPhone(phoneNumbers.at(i));
40  }
41  }
42 }
43 
44 void PhoneWidgetLister::storeContact(KContacts::Addressee &contact) const
45 {
46  KContacts::PhoneNumber::List phoneNumbers;
47  const QList<QWidget *> widgetList = widgets();
48  for (QWidget *widget : widgetList) {
49  auto w = qobject_cast<PhoneWidget *>(widget);
50  KContacts::PhoneNumber number = w->storePhone();
51  if (!number.isEmpty()) {
52  phoneNumbers << number;
53  }
54  }
55  contact.setPhoneNumbers(phoneNumbers);
56 }
57 
58 void PhoneWidgetLister::setReadOnly(bool readOnly)
59 {
60  const QList<QWidget *> widgetList = widgets();
61  for (QWidget *widget : widgetList) {
62  auto w = qobject_cast<PhoneWidget *>(widget);
63  w->setReadOnly(readOnly);
64  }
65 }
66 
67 QWidget *PhoneWidgetLister::createWidget(QWidget *parent)
68 {
69  auto w = new PhoneWidget(parent);
70  reconnectWidget(w);
71  return w;
72 }
73 
74 void PhoneWidgetLister::reconnectWidget(PhoneWidget *w)
75 {
76  connect(w, &PhoneWidget::addWidget, this, &PhoneWidgetLister::slotAddWidget, Qt::UniqueConnection);
77  connect(w, &PhoneWidget::removeWidget, this, &PhoneWidgetLister::slotRemoveWidget, Qt::UniqueConnection);
78  connect(w, &PhoneWidget::preferredChanged, this, &PhoneWidgetLister::slotPreferredChanged, Qt::UniqueConnection);
79 }
80 
81 void PhoneWidgetLister::slotAddWidget(PhoneWidget *w)
82 {
83  addWidgetAfterThisWidget(w);
84  updateAddRemoveButton();
85 }
86 
87 void PhoneWidgetLister::slotRemoveWidget(PhoneWidget *w)
88 {
89  if (widgets().count() == 1) {
90  w->clearWidget();
91  } else {
92  removeWidget(w);
93  updateAddRemoveButton();
94  }
95 }
96 
97 void PhoneWidgetLister::slotPreferredChanged(PhoneWidget *w)
98 {
99  const QList<QWidget *> widgetList = widgets();
100  for (QWidget *widget : widgetList) {
101  if (widget != w) {
102  (static_cast<PhoneWidget *>(widget))->setPreferred(false);
103  }
104  }
105 }
106 
107 void PhoneWidgetLister::updateAddRemoveButton()
108 {
109  const QList<QWidget *> widgetList = widgets();
110  const int numberOfWidget(widgetList.count());
111  bool addButtonEnabled = false;
112  if (numberOfWidget <= widgetsMinimum()) {
113  addButtonEnabled = true;
114  } else if (numberOfWidget >= widgetsMaximum()) {
115  addButtonEnabled = false;
116  } else {
117  addButtonEnabled = true;
118  }
119 
120  for (QWidget *widget : widgetList) {
121  auto w = qobject_cast<PhoneWidget *>(widget);
122  w->updateAddRemoveButton(addButtonEnabled);
123  }
124 }
KIOCORE_EXPORT QString number(KIO::filesize_t size)
int count(const T &value) const const
const T & at(int i) const const
PhoneNumber::List phoneNumbers() const
bool isEmpty() const const
int count(const T &value) const const
UniqueConnection
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.