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, 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 "messagingwidgetlister.h"
11#include "messagingwidget.h"
12
13#include <KContacts/Addressee>
14using namespace Akonadi;
15
16MessagingWidgetLister::MessagingWidgetLister(QWidget *parent)
17 : KWidgetLister(1, 8, parent)
18{
19 setNumberOfShownWidgetsTo(widgetsMinimum());
20 updateAddRemoveButton();
21}
22
23MessagingWidgetLister::~MessagingWidgetLister() = default;
24
25void 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
42void 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
53void 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
62QWidget *MessagingWidgetLister::createWidget(QWidget *parent)
63{
64 auto w = new MessagingWidget(parent);
65 reconnectWidget(w);
66 return w;
67}
68
69void 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
76void 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
86void MessagingWidgetLister::slotAddWidget(MessagingWidget *w)
87{
88 addWidgetAfterThisWidget(w);
89 updateAddRemoveButton();
90}
91
92void MessagingWidgetLister::slotRemoveWidget(MessagingWidget *w)
93{
94 if (widgets().count() == 1) {
95 w->clearWidget();
96 } else {
97 removeWidget(w);
98 updateAddRemoveButton();
99 }
100}
101
102void 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 }
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}
121
122#include "moc_messagingwidgetlister.cpp"
A widget for editing the display name of a contact.
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() 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 Fri Nov 22 2024 12:08:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.