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, sales@eyeos.com
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>
14using namespace Akonadi;
15
16PhoneWidgetLister::PhoneWidgetLister(QWidget *parent)
17 : KWidgetLister(1, 8, parent)
18{
19 setNumberOfShownWidgetsTo(widgetsMinimum());
20 updateAddRemoveButton();
21}
22
23PhoneWidgetLister::~PhoneWidgetLister() = default;
24
25void PhoneWidgetLister::loadContact(const KContacts::Addressee &contact)
26{
27 const KContacts::PhoneNumber::List phoneNumbers = contact.phoneNumbers();
28 if (phoneNumbers.isEmpty()) {
29 setNumberOfShownWidgetsTo(1);
30 } else {
31 setNumberOfShownWidgetsTo(phoneNumbers.count());
32 const QList<QWidget *> widgetList = widgets();
33 auto wIt = widgetList.constBegin();
34 auto wEnd = widgetList.constEnd();
35 for (int i = 0; wIt != wEnd; ++wIt, ++i) {
36 auto w = qobject_cast<PhoneWidget *>(*wIt);
37 w->loadPhone(phoneNumbers.at(i));
38 }
39 }
40}
41
42void PhoneWidgetLister::storeContact(KContacts::Addressee &contact) const
43{
45 const QList<QWidget *> widgetList = widgets();
46 for (QWidget *widget : widgetList) {
47 auto w = qobject_cast<PhoneWidget *>(widget);
48 KContacts::PhoneNumber number = w->storePhone();
49 if (!number.isEmpty()) {
50 phoneNumbers << number;
51 }
52 }
53 contact.setPhoneNumbers(phoneNumbers);
54}
55
56void PhoneWidgetLister::setReadOnly(bool readOnly)
57{
58 const QList<QWidget *> widgetList = widgets();
59 for (QWidget *widget : widgetList) {
60 auto w = qobject_cast<PhoneWidget *>(widget);
61 w->setReadOnly(readOnly);
62 }
63}
64
65QWidget *PhoneWidgetLister::createWidget(QWidget *parent)
66{
67 auto w = new PhoneWidget(parent);
68 reconnectWidget(w);
69 return w;
70}
71
72void PhoneWidgetLister::reconnectWidget(PhoneWidget *w)
73{
74 connect(w, &PhoneWidget::addWidget, this, &PhoneWidgetLister::slotAddWidget, Qt::UniqueConnection);
75 connect(w, &PhoneWidget::removeWidget, this, &PhoneWidgetLister::slotRemoveWidget, Qt::UniqueConnection);
76 connect(w, &PhoneWidget::preferredChanged, this, &PhoneWidgetLister::slotPreferredChanged, Qt::UniqueConnection);
77}
78
79void PhoneWidgetLister::slotAddWidget(PhoneWidget *w)
80{
81 addWidgetAfterThisWidget(w);
82 updateAddRemoveButton();
83}
84
85void PhoneWidgetLister::slotRemoveWidget(PhoneWidget *w)
86{
87 if (widgets().count() == 1) {
88 w->clearWidget();
89 } else {
90 removeWidget(w);
91 updateAddRemoveButton();
92 }
93}
94
95void PhoneWidgetLister::slotPreferredChanged(PhoneWidget *w)
96{
97 const QList<QWidget *> widgetList = widgets();
98 for (QWidget *widget : widgetList) {
99 if (widget != w) {
100 (static_cast<PhoneWidget *>(widget))->setPreferred(false);
101 }
102 }
103}
104
105void PhoneWidgetLister::updateAddRemoveButton()
106{
107 const QList<QWidget *> widgetList = widgets();
108 const int numberOfWidget(widgetList.count());
109 bool addButtonEnabled = false;
110 if (numberOfWidget <= widgetsMinimum()) {
111 addButtonEnabled = true;
112 } else if (numberOfWidget >= widgetsMaximum()) {
113 addButtonEnabled = false;
114 } else {
115 addButtonEnabled = true;
116 }
117
118 for (QWidget *widget : widgetList) {
119 auto w = qobject_cast<PhoneWidget *>(widget);
120 w->updateAddRemoveButton(addButtonEnabled);
121 }
122}
123
124#include "moc_phonewidgetlister.cpp"
PhoneNumber::List phoneNumbers() const
A widget for editing the display name of a contact.
KIOCORE_EXPORT QString number(KIO::filesize_t size)
const_reference at(qsizetype i) const const
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() const const
bool isEmpty() const const
bool isEmpty() const const
UniqueConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.