Akonadi Contacts

addresseslocationwidget.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 "addresseslocationwidget.h"
11 #include "addressdelegate.h"
12 #include "addresslocationwidget.h"
13 #include "addressmodel.h"
14 #include "contacteditor_debug.h"
15 
16 #include <KLocalizedString>
17 #include <KMessageBox>
18 #include <QMenu>
19 #include <QTreeView>
20 
21 using namespace ContactEditor;
22 
23 AddressesLocationWidget::AddressesLocationWidget(QWidget *parent)
24  : QSplitter(parent)
25  , mAddressLocationWidget(new AddressLocationWidget(this))
26  , mAddressesLocationView(new QTreeView(this))
27  , mAddressModel(new AddressModel(this))
28 {
29  mAddressLocationWidget->setObjectName(QStringLiteral("addresslocationwidget"));
30  addWidget(mAddressLocationWidget);
31 
32  mAddressesLocationView->setRootIsDecorated(false);
33  mAddressesLocationView->setHeaderHidden(true);
34  mAddressesLocationView->setModel(mAddressModel);
35  mAddressesLocationView->setItemDelegate(new AddressDelegate(this));
36  mAddressesLocationView->setContextMenuPolicy(Qt::CustomContextMenu);
37  mAddressesLocationView->setObjectName(QStringLiteral("addresseslocationviewer"));
38  addWidget(mAddressesLocationView);
39 
40  connect(mAddressesLocationView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this](const QItemSelection &selection) {
41  mAddressLocationWidget->clear();
42  if (selection.size() != 1) {
43  return;
44  }
45  const auto idx = selection.at(0).topLeft();
46  if (!idx.isValid()) {
47  return;
48  }
49  mAddressLocationWidget->slotModifyAddress(idx.data(Qt::UserRole).value<KContacts::Address>(), idx.row());
50  });
51  connect(mAddressLocationWidget, &AddressLocationWidget::addNewAddress, mAddressModel, &AddressModel::addAddress);
52  connect(mAddressLocationWidget, &AddressLocationWidget::removeAddress, mAddressModel, &AddressModel::removeAddress);
53  connect(mAddressLocationWidget,
54  &AddressLocationWidget::updateAddressCanceled,
55  mAddressesLocationView->selectionModel(),
57  connect(mAddressLocationWidget, &AddressLocationWidget::updateAddress, mAddressModel, &AddressModel::replaceAddress);
58  connect(mAddressesLocationView, &QWidget::customContextMenuRequested, this, [this](QPoint pos) {
59  const auto idx = mAddressesLocationView->indexAt(pos);
60  if (!idx.isValid()) {
61  return;
62  }
63  QMenu menu;
64  auto action = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove Address"));
65  action->setEnabled(!mReadOnly);
66  connect(action, &QAction::triggered, this, [this, idx]() {
67  const auto result = KMessageBox::questionYesNo(this, i18n("Do you really want to delete this address?"));
68  if (result == KMessageBox::Yes) {
69  mAddressModel->removeAddress(idx.row());
70  }
71  });
72  menu.exec(mAddressesLocationView->viewport()->mapToGlobal(pos));
73  });
74 
75  setChildrenCollapsible(false);
76  setSizes({400, 300});
77 }
78 
79 AddressesLocationWidget::~AddressesLocationWidget() = default;
80 
81 void AddressesLocationWidget::loadContact(const KContacts::Addressee &contact)
82 {
83  mAddressModel->setAddresses(contact.addresses());
84 }
85 
86 void AddressesLocationWidget::storeContact(KContacts::Addressee &contact) const
87 {
88  // delete all previous addresses
89  const KContacts::Address::List oldAddresses = contact.addresses();
90  const int numberOfOldAddresses(oldAddresses.count());
91  for (int i = 0; i < numberOfOldAddresses; ++i) {
92  contact.removeAddress(oldAddresses.at(i));
93  }
94 
95  const KContacts::Address::List addressList = mAddressModel->addresses();
96  // insert the new ones
97  const int numberOfAddress(addressList.count());
98  for (int i = 0; i < numberOfAddress; ++i) {
99  const KContacts::Address address(addressList.at(i));
100  if (!address.isEmpty()) {
101  contact.insertAddress(address);
102  }
103  }
104 }
105 
106 void AddressesLocationWidget::setReadOnly(bool readOnly)
107 {
108  mReadOnly = readOnly;
109  mAddressLocationWidget->setReadOnly(readOnly);
110 }
111 
112 bool AddressesLocationWidget::hasNoSavedData() const
113 {
114  return mAddressLocationWidget->wasChanged();
115 }
void customContextMenuRequested(const QPoint &pos)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void triggered(bool checked)
void removeAddress(const Address &address)
QAction * addAction(const QString &text)
CustomContextMenu
PostalAddress address(const QVariant &location)
void setObjectName(const QString &name)
UserRole
QAction * exec()
ButtonCode questionYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
QString i18n(const char *text, const TYPE &arg...)
const T & at(int i) const const
int count(const T &value) const const
QIcon fromTheme(const QString &name)
void result(KJob *job)
Address::List addresses() const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setEnabled(bool)
void insertAddress(const Address &address)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.