Akonadi Contacts

mailwidgetlister.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 "mailwidgetlister.h"
11 #include "mailwidget.h"
12 
13 #include <KContacts/Addressee>
14 using namespace ContactEditor;
15 
16 MailWidgetLister::MailWidgetLister(QWidget *parent)
17  : KWidgetLister(1, 8, parent)
18 {
19  setNumberOfShownWidgetsTo(widgetsMinimum());
20  updateAddRemoveButton();
21 }
22 
23 MailWidgetLister::~MailWidgetLister()
24 {
25 }
26 
27 void MailWidgetLister::loadContact(const KContacts::Addressee &contact)
28 {
29  KContacts::Email::List mailList = contact.emailList();
30  if (mailList.isEmpty()) {
31  setNumberOfShownWidgetsTo(1);
32  } else {
33  setNumberOfShownWidgetsTo(mailList.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<MailWidget *>(*wIt);
39  w->setMail(mailList.at(i));
40  }
41  }
42 }
43 
44 void MailWidgetLister::storeContact(KContacts::Addressee &contact) const
45 {
46  const QList<QWidget *> widgetList = widgets();
47  KContacts::Email::List emailList;
48  emailList.reserve(widgetList.count());
49  for (QWidget *widget : widgetList) {
50  auto w = qobject_cast<MailWidget *>(widget);
51  KContacts::Email newEmail = w->email();
52  if (newEmail.isValid()) {
53  emailList << newEmail;
54  }
55  }
56  contact.setEmailList(emailList);
57 }
58 
59 void MailWidgetLister::setReadOnly(bool readOnly)
60 {
61  const QList<QWidget *> widgetList = widgets();
62  for (QWidget *widget : widgetList) {
63  auto w = qobject_cast<MailWidget *>(widget);
64  w->setReadOnly(readOnly);
65  }
66 }
67 
68 QWidget *MailWidgetLister::createWidget(QWidget *parent)
69 {
70  auto w = new MailWidget(parent);
71  reconnectWidget(w);
72  return w;
73 }
74 
75 void MailWidgetLister::reconnectWidget(MailWidget *w)
76 {
77  connect(w, &MailWidget::addWidget, this, &MailWidgetLister::slotAddWidget, Qt::UniqueConnection);
78  connect(w, &MailWidget::removeWidget, this, &MailWidgetLister::slotRemoveWidget, Qt::UniqueConnection);
79  connect(w, &MailWidget::preferredChanged, this, &MailWidgetLister::slotPreferredChanged, Qt::UniqueConnection);
80 }
81 
82 void MailWidgetLister::slotAddWidget(MailWidget *w)
83 {
84  addWidgetAfterThisWidget(w);
85  updateAddRemoveButton();
86 }
87 
88 void MailWidgetLister::slotRemoveWidget(MailWidget *w)
89 {
90  if (widgets().count() == 1) {
91  w->clearWidget();
92  } else {
93  removeWidget(w);
94  updateAddRemoveButton();
95  }
96 }
97 
98 void MailWidgetLister::slotPreferredChanged(MailWidget *w)
99 {
100  const QList<QWidget *> widgetList = widgets();
101  for (QWidget *widget : widgetList) {
102  if (widget != w) {
103  (static_cast<MailWidget *>(widget))->setPreferred(false);
104  }
105  }
106 }
107 
108 void MailWidgetLister::updateAddRemoveButton()
109 {
110  const QList<QWidget *> widgetList = widgets();
111  const int numberOfWidget(widgetList.count());
112  bool addButtonEnabled = false;
113  if (numberOfWidget <= widgetsMinimum()) {
114  addButtonEnabled = true;
115  } else if (numberOfWidget >= widgetsMaximum()) {
116  addButtonEnabled = false;
117  } else {
118  addButtonEnabled = true;
119  }
120 
121  for (QWidget *widget : widgetList) {
122  auto w = qobject_cast<MailWidget *>(widget);
123  w->updateAddRemoveButton(addButtonEnabled);
124  }
125 }
int count(const T &value) const const
UniqueConnection
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.