Akonadi Contacts

phonewidgetlister.cpp
1 /*
2  This file is part of Contact Editor.
3 
4  Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, [email protected]
5  Copyright (C) 2016-2020 Laurent Montel <montel.org>
6 
7  This library is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Library General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but WITHOUT
13  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15  License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to the
19  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  02110-1301, USA.
21 */
22 
23 #include "phonewidgetlister.h"
24 #include "phonewidget.h"
25 
26 #include <KContacts/Addressee>
27 using namespace ContactEditor;
28 
29 PhoneWidgetLister::PhoneWidgetLister(QWidget *parent)
30  : KWidgetLister(1, 8, parent)
31 {
32  setNumberOfShownWidgetsTo(widgetsMinimum());
33  updateAddRemoveButton();
34 }
35 
36 PhoneWidgetLister::~PhoneWidgetLister()
37 {
38 }
39 
40 void PhoneWidgetLister::loadContact(const KContacts::Addressee &contact)
41 {
42  const KContacts::PhoneNumber::List phoneNumbers = contact.phoneNumbers();
43  if (phoneNumbers.isEmpty()) {
44  setNumberOfShownWidgetsTo(1);
45  } else {
46  setNumberOfShownWidgetsTo(phoneNumbers.count());
47  const QList<QWidget *> widgetList = widgets();
48  auto wIt = widgetList.constBegin();
49  auto wEnd = widgetList.constEnd();
50  for (int i = 0; wIt != wEnd; ++wIt, ++i) {
51  PhoneWidget *w = qobject_cast<PhoneWidget *>(*wIt);
52  w->loadPhone(phoneNumbers.at(i));
53  }
54  }
55 }
56 
57 void PhoneWidgetLister::storeContact(KContacts::Addressee &contact) const
58 {
59  KContacts::PhoneNumber::List phoneNumbers;
60  const QList<QWidget *> widgetList = widgets();
61  for (QWidget *widget : widgetList) {
62  PhoneWidget *w = qobject_cast<PhoneWidget *>(widget);
63  KContacts::PhoneNumber number = w->storePhone();
64  if (!number.isEmpty()) {
65  phoneNumbers << number;
66  }
67  }
68  contact.setPhoneNumbers(phoneNumbers);
69 }
70 
71 void PhoneWidgetLister::setReadOnly(bool readOnly)
72 {
73  const QList<QWidget *> widgetList = widgets();
74  for (QWidget *widget : widgetList) {
75  PhoneWidget *w = qobject_cast<PhoneWidget *>(widget);
76  w->setReadOnly(readOnly);
77  }
78 }
79 
80 QWidget *PhoneWidgetLister::createWidget(QWidget *parent)
81 {
82  PhoneWidget *w = new PhoneWidget(parent);
83  reconnectWidget(w);
84  return w;
85 }
86 
87 void PhoneWidgetLister::reconnectWidget(PhoneWidget *w)
88 {
89  connect(w, &PhoneWidget::addWidget, this, &PhoneWidgetLister::slotAddWidget, Qt::UniqueConnection);
90  connect(w, &PhoneWidget::removeWidget, this, &PhoneWidgetLister::slotRemoveWidget, Qt::UniqueConnection);
91  connect(w, &PhoneWidget::preferredChanged, this, &PhoneWidgetLister::slotPreferredChanged, Qt::UniqueConnection);
92 }
93 
94 void PhoneWidgetLister::slotAddWidget(PhoneWidget *w)
95 {
96  addWidgetAfterThisWidget(w);
97  updateAddRemoveButton();
98 }
99 
100 void PhoneWidgetLister::slotRemoveWidget(PhoneWidget *w)
101 {
102  if (widgets().count() == 1) {
103  w->clearWidget();
104  } else {
105  removeWidget(w);
106  updateAddRemoveButton();
107  }
108 }
109 
110 void PhoneWidgetLister::slotPreferredChanged(PhoneWidget *w)
111 {
112  const QList<QWidget *> widgetList = widgets();
113  for (QWidget *widget : widgetList) {
114  if (widget != w) {
115  (static_cast<PhoneWidget *>(widget))->setPreferred(false);
116  }
117  }
118 }
119 
120 void PhoneWidgetLister::updateAddRemoveButton()
121 {
122  const QList<QWidget *> widgetList = widgets();
123  const int numberOfWidget(widgetList.count());
124  bool addButtonEnabled = false;
125  if (numberOfWidget <= widgetsMinimum()) {
126  addButtonEnabled = true;
127  } else if (numberOfWidget >= widgetsMaximum()) {
128  addButtonEnabled = false;
129  } else {
130  addButtonEnabled = true;
131  }
132 
133  for (QWidget *widget : widgetList) {
134  PhoneWidget *w = qobject_cast<PhoneWidget *>(widget);
135  w->updateAddRemoveButton(addButtonEnabled);
136  }
137 }
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-2020 The KDE developers.
Generated on Sat Jul 11 2020 23:07:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.