Akonadi Contacts

messagingwidgetlister.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 "messagingwidgetlister.h"
11 #include "../../utils/utils.h"
12 #include "messagingwidget.h"
13 
14 #include <KContacts/Addressee>
15 using namespace ContactEditor;
16 
17 MessagingWidgetLister::MessagingWidgetLister(QWidget *parent)
18  : KWidgetLister(1, 8, parent)
19 {
20  setNumberOfShownWidgetsTo(widgetsMinimum());
21  updateAddRemoveButton();
22 }
23 
24 MessagingWidgetLister::~MessagingWidgetLister()
25 {
26 }
27 
28 void MessagingWidgetLister::loadContact(const KContacts::Addressee &contact)
29 {
30  const auto imaddresses = contact.imppList();
31  if (imaddresses.isEmpty()) {
32  setNumberOfShownWidgetsTo(1);
33  } else {
34  setNumberOfShownWidgetsTo(imaddresses.count());
35  const QList<QWidget *> widgetList = widgets();
36  auto wIt = widgetList.constBegin();
37  auto wEnd = widgetList.constEnd();
38  for (int i = 0; wIt != wEnd; ++wIt, ++i) {
39  auto w = qobject_cast<MessagingWidget *>(*wIt);
40  w->setIMAddress(imaddresses.at(i));
41  }
42  }
43 }
44 
45 void MessagingWidgetLister::storeContact(KContacts::Addressee &contact) const
46 {
47  KContacts::Impp::List imaddresses;
48  const QList<QWidget *> widgetList = widgets();
49  for (QWidget *widget : widgetList) {
50  auto w = qobject_cast<MessagingWidget *>(widget);
51  imaddresses << w->imAddress();
52  }
53  contact.setImppList(imaddresses);
54 }
55 
56 void MessagingWidgetLister::setReadOnly(bool readOnly)
57 {
58  const QList<QWidget *> widgetList = widgets();
59  for (QWidget *widget : widgetList) {
60  auto w = qobject_cast<MessagingWidget *>(widget);
61  w->setReadOnly(readOnly);
62  }
63 }
64 
65 QWidget *MessagingWidgetLister::createWidget(QWidget *parent)
66 {
67  auto w = new MessagingWidget(parent);
68  reconnectWidget(w);
69  return w;
70 }
71 
72 void MessagingWidgetLister::reconnectWidget(MessagingWidget *w)
73 {
74  connect(w, &MessagingWidget::addWidget, this, &MessagingWidgetLister::slotAddWidget, Qt::UniqueConnection);
75  connect(w, &MessagingWidget::removeWidget, this, &MessagingWidgetLister::slotRemoveWidget, Qt::UniqueConnection);
76  connect(w, &MessagingWidget::preferredChanged, this, &MessagingWidgetLister::slotPreferredChanged, Qt::UniqueConnection);
77 }
78 
79 void MessagingWidgetLister::slotPreferredChanged(MessagingWidget *w)
80 {
81  const QList<QWidget *> widgetList = widgets();
82  for (QWidget *widget : widgetList) {
83  if (widget != w) {
84  (static_cast<MessagingWidget *>(widget))->setPreferred(false);
85  }
86  }
87 }
88 
89 void MessagingWidgetLister::slotAddWidget(MessagingWidget *w)
90 {
91  addWidgetAfterThisWidget(w);
92  updateAddRemoveButton();
93 }
94 
95 void MessagingWidgetLister::slotRemoveWidget(MessagingWidget *w)
96 {
97  if (widgets().count() == 1) {
98  w->clearWidget();
99  } else {
100  removeWidget(w);
101  updateAddRemoveButton();
102  }
103 }
104 
105 void MessagingWidgetLister::updateAddRemoveButton()
106 {
107  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  QList<QWidget *>::ConstIterator wIt = widgetList.constBegin();
118  QList<QWidget *>::ConstIterator wEnd = widgetList.constEnd();
119  for (; wIt != wEnd; ++wIt) {
120  auto w = qobject_cast<MessagingWidget *>(*wIt);
121  w->updateAddRemoveButton(addButtonEnabled);
122  }
123 }
int count(const T &value) const const
QList::const_iterator constEnd() const const
QList::const_iterator constBegin() 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.