Akonadi Contacts

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