feather/src/dialog/AccountSwitcherDialog.cpp

117 lines
4.2 KiB
C++
Raw Normal View History

2021-05-22 13:42:26 +00:00
// SPDX-License-Identifier: BSD-3-Clause
2023-01-02 19:30:11 +00:00
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
2021-05-22 13:42:26 +00:00
#include "AccountSwitcherDialog.h"
#include "ui_AccountSwitcherDialog.h"
2021-06-27 14:33:18 +00:00
#include <QMenu>
2021-05-22 13:42:26 +00:00
#include "libwalletqt/SubaddressAccount.h"
#include "libwalletqt/WalletManager.h"
2021-05-25 22:08:32 +00:00
#include "model/ModelUtils.h"
2021-06-27 14:33:18 +00:00
#include "utils/Icons.h"
#include "utils/Utils.h"
2021-05-22 13:42:26 +00:00
AccountSwitcherDialog::AccountSwitcherDialog(Wallet *wallet, QWidget *parent)
2021-10-22 17:19:56 +00:00
: WindowModalDialog(parent)
2021-05-22 13:42:26 +00:00
, ui(new Ui::AccountSwitcherDialog)
, m_wallet(wallet)
, m_model(wallet->subaddressAccountModel())
2021-05-25 22:08:32 +00:00
, m_proxyModel(new SubaddressAccountProxyModel(this))
2021-05-22 13:42:26 +00:00
{
ui->setupUi(this);
m_wallet->subaddressAccount()->refresh();
2021-05-25 22:08:32 +00:00
m_proxyModel->setSourceModel(m_model);
ui->label_totalBalance->setFont(ModelUtils::getMonospaceFont());
ui->label_totalBalance->setText(WalletManager::displayAmount(m_wallet->balanceAll()));
2021-05-25 22:08:32 +00:00
2021-10-22 17:19:56 +00:00
this->setWindowModality(Qt::WindowModal);
2021-05-25 22:08:32 +00:00
ui->accounts->setModel(m_proxyModel);
ui->accounts->setContextMenuPolicy(Qt::CustomContextMenu);
2021-05-22 13:42:26 +00:00
ui->accounts->setSelectionMode(QAbstractItemView::SingleSelection);
ui->accounts->setSelectionBehavior(QAbstractItemView::SelectRows);
2021-05-25 22:08:32 +00:00
ui->accounts->setSortingEnabled(true);
ui->accounts->sortByColumn(SubaddressAccountModel::Column::Number, Qt::AscendingOrder);
2021-05-22 13:42:26 +00:00
ui->accounts->hideColumn(SubaddressAccountModel::Column::Address);
ui->accounts->hideColumn(SubaddressAccountModel::Column::UnlockedBalance);
ui->accounts->setColumnWidth(SubaddressAccountModel::Column::Label, 200);
ui->accounts->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->accounts->header()->setSectionResizeMode(SubaddressAccountModel::Label, QHeaderView::Stretch);
connect(ui->accounts->selectionModel(), &QItemSelectionModel::currentChanged, this, &AccountSwitcherDialog::switchAccount);
connect(ui->accounts, &QTreeView::customContextMenuRequested, this, &AccountSwitcherDialog::showContextMenu);
connect(ui->btn_newAccount, &QPushButton::clicked, [this]{
m_wallet->addSubaddressAccount("New account");
m_wallet->subaddressAccount()->refresh();
2021-05-22 13:42:26 +00:00
});
connect(m_wallet, &Wallet::currentSubaddressAccountChanged, this, &AccountSwitcherDialog::updateSelection);
connect(m_wallet->subaddressAccount(), &SubaddressAccount::refreshFinished, this, &AccountSwitcherDialog::updateSelection);
2021-05-22 13:42:26 +00:00
this->updateSelection();
}
void AccountSwitcherDialog::switchAccount() {
2023-01-17 20:25:01 +00:00
auto row = this->currentEntry();
if (!row) {
return;
}
m_wallet->switchSubaddressAccount(row->getRowId());
2021-05-22 13:42:26 +00:00
}
void AccountSwitcherDialog::copyLabel() {
auto row = this->currentEntry();
2023-01-17 20:25:01 +00:00
if (!row) {
2021-05-22 13:42:26 +00:00
return;
2023-01-17 20:25:01 +00:00
}
2021-05-22 13:42:26 +00:00
Utils::copyToClipboard(QString::fromStdString(row->getLabel()));
}
void AccountSwitcherDialog::copyBalance() {
auto row = this->currentEntry();
2023-01-17 20:25:01 +00:00
if (!row) {
2021-05-22 13:42:26 +00:00
return;
2023-01-17 20:25:01 +00:00
}
2021-05-22 13:42:26 +00:00
Utils::copyToClipboard(QString::fromStdString(row->getBalance()));
}
void AccountSwitcherDialog::editLabel() {
QModelIndex index = ui->accounts->currentIndex().siblingAtColumn(m_wallet->subaddressAccountModel()->Column::Label);
2021-05-22 13:42:26 +00:00
ui->accounts->setCurrentIndex(index);
ui->accounts->edit(index);
}
void AccountSwitcherDialog::updateSelection() {
QModelIndex index = m_model->index(m_wallet->currentSubaddressAccount(), 0);
2021-05-22 13:42:26 +00:00
ui->accounts->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
void AccountSwitcherDialog::showContextMenu(const QPoint &point) {
QModelIndex index = ui->accounts->currentIndex();
if (!index.isValid()) {
return;
}
auto *menu = new QMenu(ui->accounts);
menu->addAction("Copy label", this, &AccountSwitcherDialog::copyLabel);
menu->addAction("Copy balance", this, &AccountSwitcherDialog::copyBalance);
menu->addAction("Edit label", this, &AccountSwitcherDialog::editLabel);
menu->popup(ui->accounts->viewport()->mapToGlobal(point));
}
Monero::SubaddressAccountRow* AccountSwitcherDialog::currentEntry() {
2023-01-17 20:25:01 +00:00
QModelIndex index = m_proxyModel->mapToSource(ui->accounts->currentIndex());
return m_wallet->subaddressAccountModel()->entryFromIndex(index);
2021-05-22 13:42:26 +00:00
}
2021-06-27 12:13:05 +00:00
AccountSwitcherDialog::~AccountSwitcherDialog() = default;