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 "messagingwidget.h"
12 
13 #include <KContacts/Addressee>
14 using namespace ContactEditor;
15 
16 MessagingWidgetLister::MessagingWidgetLister(QWidget *parent)
17  : KWidgetLister(1, 8, parent)
18 {
19  setNumberOfShownWidgetsTo(widgetsMinimum());
20  updateAddRemoveButton();
21 }
22 
23 MessagingWidgetLister::~MessagingWidgetLister() = default;
24 
25 void MessagingWidgetLister::loadContact(const KContacts::Addressee &contact)
26 {
27  const auto imaddresses = contact.imppList();
28  if (imaddresses.isEmpty()) {
29  setNumberOfShownWidgetsTo(1);
30  } else {
31  setNumberOfShownWidgetsTo(imaddresses.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<MessagingWidget *>(*wIt);
37  w->setIMAddress(imaddresses.at(i));
38  }
39  }
40 }
41 
42 void MessagingWidgetLister::storeContact(KContacts::Addressee &contact) const
43 {
44  KContacts::Impp::List imaddresses;
45  const QList<QWidget *> widgetList = widgets();
46  for (QWidget *widget : widgetList) {
47  auto w = qobject_cast<MessagingWidget *>(widget);
48  imaddresses << w->imAddress();
49  }
50  contact.setImppList(imaddresses);
51 }
52 
53 void MessagingWidgetLister::setReadOnly(bool readOnly)
54 {
55  const QList<QWidget *> widgetList = widgets();
56  for (QWidget *widget : widgetList) {
57  auto w = qobject_cast<MessagingWidget *>(widget);
58  w->setReadOnly(readOnly);
59  }
60 }
61 
62 QWidget *MessagingWidgetLister::createWidget(QWidget *parent)
63 {
64  auto w = new MessagingWidget(parent);
65  reconnectWidget(w);
66  return w;
67 }
68 
69 void MessagingWidgetLister::reconnectWidget(MessagingWidget *w)
70 {
71  connect(w, &MessagingWidget::addWidget, this, &MessagingWidgetLister::slotAddWidget, Qt::UniqueConnection);
72  connect(w, &MessagingWidget::removeWidget, this, &MessagingWidgetLister::slotRemoveWidget, Qt::UniqueConnection);
73  connect(w, &MessagingWidget::preferredChanged, this, &MessagingWidgetLister::slotPreferredChanged, Qt::UniqueConnection);
74 }
75 
76 void MessagingWidgetLister::slotPreferredChanged(MessagingWidget *w)
77 {
78  const QList<QWidget *> widgetList = widgets();
79  for (QWidget *widget : widgetList) {
80  if (widget != w) {
81  (static_cast<MessagingWidget *>(widget))->setPreferred(false);
82  }
83  }
84 }
85 
86 void MessagingWidgetLister::slotAddWidget(MessagingWidget *w)
87 {
88  addWidgetAfterThisWidget(w);
89  updateAddRemoveButton();
90 }
91 
92 void MessagingWidgetLister::slotRemoveWidget(MessagingWidget *w)
93 {
94  if (widgets().count() == 1) {
95  w->clearWidget();
96  } else {
97  removeWidget(w);
98  updateAddRemoveButton();
99  }
100 }
101 
102 void MessagingWidgetLister::updateAddRemoveButton()
103 {
104  QList<QWidget *> widgetList = widgets();
105  const int numberOfWidget(widgetList.count());
106  bool addButtonEnabled = false;
107  if (numberOfWidget <= widgetsMinimum()) {
108  addButtonEnabled = true;
109  } else if (numberOfWidget >= widgetsMaximum()) {
110  addButtonEnabled = false;
111  } else {
112  addButtonEnabled = true;
113  }
114  QList<QWidget *>::ConstIterator wIt = widgetList.constBegin();
115  QList<QWidget *>::ConstIterator wEnd = widgetList.constEnd();
116  for (; wIt != wEnd; ++wIt) {
117  auto w = qobject_cast<MessagingWidget *>(*wIt);
118  w->updateAddRemoveButton(addButtonEnabled);
119  }
120 }
int count(const T &value) const const
QList::const_iterator constBegin() const const
UniqueConnection
QList::const_iterator constEnd() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.