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

KDE's Doxygen guidelines are available online.