feather/src/ContactsWidget.cpp

159 lines
5.2 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2020-12-26 19:56:06 +00:00
// Copyright (c) 2020-2021, The Monero Project.
2021-06-28 17:48:23 +00:00
#include "ContactsWidget.h"
#include "ui_ContactsWidget.h"
#include <QMessageBox>
2021-06-27 11:46:32 +00:00
#include "dialog/ContactsDialog.h"
2020-12-23 04:54:57 +00:00
#include "libwalletqt/AddressBook.h"
2021-06-28 17:48:23 +00:00
#include "model/ModelUtils.h"
2021-05-02 18:22:38 +00:00
#include "utils/Icons.h"
2021-05-18 15:59:18 +00:00
ContactsWidget::ContactsWidget(QSharedPointer<AppContext> ctx, QWidget *parent)
2021-06-28 17:48:23 +00:00
: QWidget(parent)
, ui(new Ui::ContactsWidget)
, m_ctx(std::move(ctx))
{
ui->setupUi(this);
2021-05-18 15:59:18 +00:00
m_model = m_ctx->wallet->addressBookModel();
m_proxyModel = new AddressBookProxyModel;
m_proxyModel->setSourceModel(m_model);
ui->contacts->setModel(m_proxyModel);
ui->contacts->setSortingEnabled(true);
ui->contacts->header()->setSectionResizeMode(AddressBookModel::Address, QHeaderView::ResizeToContents);
ui->contacts->header()->setSectionResizeMode(AddressBookModel::Description, QHeaderView::Stretch);
2021-05-18 15:59:18 +00:00
ui->contacts->header()->setMinimumSectionSize(200);
// header context menu
ui->contacts->header()->setContextMenuPolicy(Qt::CustomContextMenu);
m_headerMenu = new QMenu(this);
m_showFullAddressesAction = m_headerMenu->addAction("Show full addresses", this, &ContactsWidget::setShowFullAddresses);
m_showFullAddressesAction->setCheckable(true);
connect(ui->contacts->header(), &QHeaderView::customContextMenuRequested, this, &ContactsWidget::showHeaderMenu);
2021-06-04 18:32:18 +00:00
connect(ui->contacts, &QTreeView::doubleClicked, [this](QModelIndex index){
if (!(m_model->flags(index) & Qt::ItemIsEditable)) {
this->payTo();
}
});
// context menu
ui->contacts->setContextMenuPolicy(Qt::CustomContextMenu);
m_contextMenu = new QMenu(ui->contacts);
2021-05-02 18:22:38 +00:00
m_contextMenu->addAction(icons()->icon("person.svg"), "New contact", [this]{
2020-12-23 04:54:57 +00:00
this->newContact();
});
// row context menu
m_rowMenu = new QMenu(ui->contacts);
2021-06-04 18:32:18 +00:00
m_rowMenu->addAction("Copy address", this, &ContactsWidget::copyAddress);
2020-12-23 04:54:57 +00:00
m_rowMenu->addAction("Pay to", this, &ContactsWidget::payTo);
m_rowMenu->addAction("Delete", this, &ContactsWidget::deleteContact);
connect(ui->contacts, &QTreeView::customContextMenuRequested, [=](const QPoint & point){
QModelIndex index = ui->contacts->indexAt(point);
if (index.isValid()) {
m_rowMenu->exec(ui->contacts->viewport()->mapToGlobal(point));
}
else {
m_contextMenu->exec(ui->contacts->viewport()->mapToGlobal(point));
}
});
connect(ui->search, &QLineEdit::textChanged, this, &ContactsWidget::setSearchFilter);
}
2021-05-23 14:58:18 +00:00
void ContactsWidget::setSearchbarVisible(bool visible) {
ui->search->setVisible(visible);
}
void ContactsWidget::focusSearchbar() {
ui->search->setFocusPolicy(Qt::StrongFocus);
ui->search->setFocus();
}
void ContactsWidget::copyAddress() {
QModelIndex index = ui->contacts->currentIndex();
ModelUtils::copyColumn(&index, AddressBookModel::Address);
}
void ContactsWidget::copyName() {
QModelIndex index = ui->contacts->currentIndex();
ModelUtils::copyColumn(&index, AddressBookModel::Description);
}
void ContactsWidget::payTo() {
QModelIndex index = ui->contacts->currentIndex();
QString address = index.model()->data(index.siblingAtColumn(AddressBookModel::Address), Qt::UserRole).toString();
emit fillAddress(address);
}
void ContactsWidget::setShowFullAddresses(bool show) {
m_model->setShowFullAddresses(show);
}
void ContactsWidget::setSearchFilter(const QString &filter) {
if(!m_proxyModel) return;
m_proxyModel->setSearchFilter(filter);
}
void ContactsWidget::showHeaderMenu(const QPoint& position)
{
m_showFullAddressesAction->setChecked(m_model->isShowFullAddresses());
m_headerMenu->exec(QCursor::pos());
}
2020-12-23 04:54:57 +00:00
void ContactsWidget::newContact(QString address, QString name)
{
2021-05-10 15:05:10 +00:00
ContactsDialog dialog{this, address, name};
int ret = dialog.exec();
if (ret != QDialog::Accepted) {
return;
}
2021-05-10 15:05:10 +00:00
address = dialog.getAddress();
name = dialog.getName();
2020-12-23 04:54:57 +00:00
2021-05-18 15:59:18 +00:00
bool addressValid = WalletManager::addressValid(address, m_ctx->wallet->nettype());
2020-12-23 04:54:57 +00:00
if (!addressValid) {
QMessageBox::warning(this, "Invalid address", "Invalid address");
return;
}
2021-05-18 15:59:18 +00:00
int num_addresses = m_ctx->wallet->addressBook()->count();
2020-12-23 04:54:57 +00:00
QString address_entry;
QString name_entry;
for (int i=0; i<num_addresses; i++) {
2021-05-18 15:59:18 +00:00
m_ctx->wallet->addressBook()->getRow(i, [&address_entry, &name_entry](const AddressBookInfo &entry){
2020-12-23 04:54:57 +00:00
address_entry = entry.address();
name_entry = entry.description();
});
if (address == address_entry) {
QMessageBox::warning(this, "Unable to add contact", "Duplicate address");
ui->contacts->setCurrentIndex(m_model->index(i,0)); // Highlight duplicate address
return;
}
if (name == name_entry) {
QMessageBox::warning(this, "Unable to add contact", "Duplicate label");
this->newContact(address, name);
return;
}
}
2021-05-18 15:59:18 +00:00
m_ctx->wallet->addressBook()->addRow(address, "", name);
}
void ContactsWidget::deleteContact()
{
QModelIndex index = ui->contacts->currentIndex();
m_model->deleteRow(m_proxyModel->mapToSource(index).row());
}
2021-06-27 12:13:05 +00:00
ContactsWidget::~ContactsWidget() = default;