File indexing completed on 2024-11-24 04:39:28
0001 /* 0002 This file is part of Contact Editor. 0003 0004 SPDX-FileCopyrightText: 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com 0005 SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel.org> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #include "addresseslocationwidget.h" 0011 #include "addressdelegate.h" 0012 #include "addresslocationwidget.h" 0013 #include "addressmodel.h" 0014 #include "contacteditor_debug.h" 0015 0016 #include <KLocalizedString> 0017 #include <KMessageBox> 0018 #include <QMenu> 0019 #include <QTreeView> 0020 0021 using namespace Akonadi; 0022 0023 AddressesLocationWidget::AddressesLocationWidget(QWidget *parent) 0024 : QSplitter(parent) 0025 , mAddressLocationWidget(new AddressLocationWidget(this)) 0026 , mAddressesLocationView(new QTreeView(this)) 0027 , mAddressModel(new AddressModel(this)) 0028 { 0029 mAddressLocationWidget->setObjectName(QLatin1StringView("addresslocationwidget")); 0030 addWidget(mAddressLocationWidget); 0031 0032 mAddressesLocationView->setRootIsDecorated(false); 0033 mAddressesLocationView->setHeaderHidden(true); 0034 mAddressesLocationView->setModel(mAddressModel); 0035 mAddressesLocationView->setItemDelegate(new AddressDelegate(this)); 0036 mAddressesLocationView->setContextMenuPolicy(Qt::CustomContextMenu); 0037 mAddressesLocationView->setObjectName(QLatin1StringView("addresseslocationviewer")); 0038 addWidget(mAddressesLocationView); 0039 0040 connect(mAddressesLocationView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this](const QItemSelection &selection) { 0041 mAddressLocationWidget->clear(); 0042 if (selection.size() != 1) { 0043 return; 0044 } 0045 const auto idx = selection.at(0).topLeft(); 0046 if (!idx.isValid()) { 0047 return; 0048 } 0049 mAddressLocationWidget->slotModifyAddress(idx.data(Qt::UserRole).value<KContacts::Address>(), idx.row()); 0050 }); 0051 connect(mAddressLocationWidget, &AddressLocationWidget::addNewAddress, mAddressModel, &AddressModel::addAddress); 0052 connect(mAddressLocationWidget, &AddressLocationWidget::removeAddress, mAddressModel, &AddressModel::removeAddress); 0053 connect(mAddressLocationWidget, 0054 &AddressLocationWidget::updateAddressCanceled, 0055 mAddressesLocationView->selectionModel(), 0056 &QItemSelectionModel::clearSelection); 0057 connect(mAddressLocationWidget, &AddressLocationWidget::updateAddress, mAddressModel, &AddressModel::replaceAddress); 0058 connect(mAddressesLocationView, &QWidget::customContextMenuRequested, this, [this](QPoint pos) { 0059 const auto idx = mAddressesLocationView->indexAt(pos); 0060 if (!idx.isValid()) { 0061 return; 0062 } 0063 QMenu menu; 0064 auto action = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove Address")); 0065 action->setEnabled(!mReadOnly); 0066 connect(action, &QAction::triggered, this, [this, idx]() { 0067 const auto result = KMessageBox::questionTwoActions(this, 0068 i18n("Do you really want to delete this address?"), 0069 QString(), 0070 KStandardGuiItem::del(), 0071 KStandardGuiItem::cancel()); 0072 if (result == KMessageBox::ButtonCode::PrimaryAction) { 0073 mAddressModel->removeAddress(idx.row()); 0074 } 0075 }); 0076 menu.exec(mAddressesLocationView->viewport()->mapToGlobal(pos)); 0077 }); 0078 0079 setChildrenCollapsible(false); 0080 setSizes({400, 300}); 0081 } 0082 0083 AddressesLocationWidget::~AddressesLocationWidget() = default; 0084 0085 void AddressesLocationWidget::loadContact(const KContacts::Addressee &contact) 0086 { 0087 mAddressModel->setAddresses(contact.addresses()); 0088 } 0089 0090 void AddressesLocationWidget::storeContact(KContacts::Addressee &contact) const 0091 { 0092 // delete all previous addresses 0093 const KContacts::Address::List oldAddresses = contact.addresses(); 0094 const int numberOfOldAddresses(oldAddresses.count()); 0095 for (int i = 0; i < numberOfOldAddresses; ++i) { 0096 contact.removeAddress(oldAddresses.at(i)); 0097 } 0098 0099 const KContacts::Address::List addressList = mAddressModel->addresses(); 0100 // insert the new ones 0101 const int numberOfAddress(addressList.count()); 0102 for (int i = 0; i < numberOfAddress; ++i) { 0103 const KContacts::Address address(addressList.at(i)); 0104 if (!address.isEmpty()) { 0105 contact.insertAddress(address); 0106 } 0107 } 0108 } 0109 0110 void AddressesLocationWidget::setReadOnly(bool readOnly) 0111 { 0112 mReadOnly = readOnly; 0113 mAddressLocationWidget->setReadOnly(readOnly); 0114 } 0115 0116 bool AddressesLocationWidget::hasNoSavedData() const 0117 { 0118 return mAddressLocationWidget->wasChanged(); 0119 } 0120 0121 #include "moc_addresseslocationwidget.cpp"