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

KDE's Doxygen guidelines are available online.