feather/src/HistoryWidget.cpp

206 lines
6.8 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2024-01-01 17:07:58 +00:00
// SPDX-FileCopyrightText: 2020-2024 The Monero Project
2021-06-28 17:48:23 +00:00
#include "HistoryWidget.h"
#include "ui_HistoryWidget.h"
#include <QMessageBox>
2021-06-27 12:22:54 +00:00
#include "dialog/TxInfoDialog.h"
2021-03-08 20:03:20 +00:00
#include "dialog/TxProofDialog.h"
#include "libwalletqt/WalletManager.h"
2021-05-02 18:22:38 +00:00
#include "utils/config.h"
2021-06-28 17:48:23 +00:00
#include "utils/Icons.h"
#include "WebsocketNotifier.h"
HistoryWidget::HistoryWidget(Wallet *wallet, QWidget *parent)
2021-06-28 17:48:23 +00:00
: QWidget(parent)
, ui(new Ui::HistoryWidget)
, m_wallet(wallet)
2021-06-28 17:48:23 +00:00
, m_contextMenu(new QMenu(this))
, m_copyMenu(new QMenu("Copy", this))
, m_model(wallet->historyModel())
{
ui->setupUi(this);
m_contextMenu->addMenu(m_copyMenu);
2021-05-02 18:22:38 +00:00
m_contextMenu->addAction(icons()->icon("info2.svg"), "Show details", this, &HistoryWidget::showTxDetails);
m_contextMenu->addAction("View on block explorer", this, &HistoryWidget::onViewOnBlockExplorer);
// copy menu
m_copyMenu->addAction("Transaction ID", this, [this]{copy(copyField::TxID);});
m_copyMenu->addAction("Date", this, [this]{copy(copyField::Date);});
2021-10-22 12:07:06 +00:00
m_copyMenu->addAction("Description", this, [this]{copy(copyField::Description);});
m_copyMenu->addAction("Amount", this, [this]{copy(copyField::Amount);});
ui->history->setContextMenuPolicy(Qt::CustomContextMenu);
2020-12-14 22:07:23 +00:00
connect(ui->history, &QTreeView::customContextMenuRequested, this, &HistoryWidget::showContextMenu);
connect(ui->search, &QLineEdit::textChanged, this, &HistoryWidget::setSearchFilter);
connect(ui->history, &QTreeView::doubleClicked, [this](QModelIndex index){
if (m_model == nullptr) return;
if (!(m_model->flags(index) & Qt::ItemIsEditable)) {
this->showTxDetails();
}
});
2020-12-30 03:58:17 +00:00
connect(ui->btn_moreInfo, &QPushButton::clicked, this, &HistoryWidget::showSyncNoticeMsg);
connect(ui->btn_close, &QPushButton::clicked, [this]{
conf()->set(Config::showHistorySyncNotice, false);
2020-12-30 03:58:17 +00:00
ui->syncNotice->hide();
});
2021-05-18 15:59:18 +00:00
connect(m_wallet, &Wallet::walletRefreshed, this, &HistoryWidget::onWalletRefreshed);
2021-05-18 15:59:18 +00:00
ui->syncNotice->setVisible(conf()->get(Config::showHistorySyncNotice).toBool());
2021-05-18 15:59:18 +00:00
ui->history->setHistoryModel(m_model);
// Load view state
QByteArray historyViewState = QByteArray::fromBase64(conf()->get(Config::GUI_HistoryViewState).toByteArray());
2021-05-18 15:59:18 +00:00
if (!historyViewState.isEmpty()) {
ui->history->setViewState(historyViewState);
}
}
2021-05-23 14:58:18 +00:00
void HistoryWidget::setSearchbarVisible(bool visible) {
ui->search->setVisible(visible);
}
void HistoryWidget::focusSearchbar() {
ui->search->setFocusPolicy(Qt::StrongFocus);
ui->search->setFocus();
}
void HistoryWidget::setWebsocketEnabled(bool enabled) {
ui->history->setColumnHidden(TransactionHistoryModel::FiatAmount, !enabled);
}
2020-12-14 22:07:23 +00:00
void HistoryWidget::showContextMenu(const QPoint &point) {
QModelIndex index = ui->history->indexAt(point);
if (!index.isValid()) {
return;
}
QMenu menu(this);
2021-03-08 20:03:20 +00:00
auto *tx = ui->history->currentEntry();
if (!tx) return;
bool unconfirmed = tx->isFailed() || tx->isPending();
if (unconfirmed && tx->direction() != TransactionRow::Direction_In) {
menu.addAction("Resend transaction", this, &HistoryWidget::onResendTransaction);
2020-12-14 22:07:23 +00:00
}
if (tx->isFailed()) {
menu.addAction("Remove from history", this, &HistoryWidget::onRemoveFromHistory);
}
2020-12-14 22:07:23 +00:00
menu.addMenu(m_copyMenu);
2021-10-21 20:05:30 +00:00
menu.addAction("Show details", this, &HistoryWidget::showTxDetails);
menu.addAction("View on block explorer", this, &HistoryWidget::onViewOnBlockExplorer);
2021-10-22 12:07:06 +00:00
menu.addAction("Create Tx Proof", this, &HistoryWidget::createTxProof);
2020-12-14 22:07:23 +00:00
menu.exec(ui->history->viewport()->mapToGlobal(point));
}
void HistoryWidget::onResendTransaction() {
2021-03-08 20:03:20 +00:00
auto *tx = ui->history->currentEntry();
if (tx) {
QString txid = tx->hash();
emit resendTransaction(txid);
}
2020-12-14 22:07:23 +00:00
}
void HistoryWidget::onRemoveFromHistory() {
auto *tx = ui->history->currentEntry();
if (!tx) return;
auto result = QMessageBox::question(this, "Remove transaction from history", "Are you sure you want to remove this transaction from the history?");
if (result == QMessageBox::Yes) {
m_wallet->removeFailedTx(tx->hash());
}
}
2020-12-30 04:45:00 +00:00
void HistoryWidget::resetModel()
{
2021-03-08 20:03:20 +00:00
// Save view state
conf()->set(Config::GUI_HistoryViewState, ui->history->viewState().toBase64());
conf()->sync();
2021-03-08 20:03:20 +00:00
2020-12-30 04:45:00 +00:00
ui->history->setModel(nullptr);
}
void HistoryWidget::showTxDetails() {
2021-03-08 20:03:20 +00:00
auto *tx = ui->history->currentEntry();
if (!tx) return;
auto *dialog = new TxInfoDialog(m_wallet, tx, this);
2021-06-27 12:22:54 +00:00
connect(dialog, &TxInfoDialog::resendTranscation, [this](const QString &txid){
2021-03-14 21:12:02 +00:00
emit resendTransaction(txid);
});
2021-03-08 20:03:20 +00:00
dialog->show();
dialog->setAttribute(Qt::WA_DeleteOnClose);
}
void HistoryWidget::onViewOnBlockExplorer() {
2021-03-08 20:03:20 +00:00
auto *tx = ui->history->currentEntry();
if (!tx) return;
2021-03-08 20:03:20 +00:00
QString txid = tx->hash();
emit viewOnBlockExplorer(txid);
}
void HistoryWidget::setSearchText(const QString &text) {
ui->search->setText(text);
}
void HistoryWidget::setSearchFilter(const QString &filter) {
m_model->setSearchFilter(filter);
2021-03-08 20:03:20 +00:00
ui->history->setSearchMode(!filter.isEmpty());
}
void HistoryWidget::createTxProof() {
auto *tx = ui->history->currentEntry();
if (!tx) return;
TxProofDialog dialog{this, m_wallet, tx};
dialog.getTxKey();
dialog.exec();
}
void HistoryWidget::copy(copyField field) {
2021-03-08 20:03:20 +00:00
auto *tx = ui->history->currentEntry();
if (!tx) return;
2021-03-08 20:03:20 +00:00
QString data = [field, tx]{
switch(field) {
case copyField::TxID:
2021-03-08 20:03:20 +00:00
return tx->hash();
2021-10-21 19:49:01 +00:00
case copyField::Description:
return tx->description();
case copyField::Date:
return tx->timestamp().toString(QString("%1 %2").arg(conf()->get(Config::dateFormat).toString(),
conf()->get(Config::timeFormat).toString()));
case copyField::Amount:
2024-01-03 21:30:23 +00:00
return WalletManager::displayAmount(abs(tx->balanceDelta()));
2021-03-08 20:03:20 +00:00
default:
return QString("");
}
2021-03-08 20:03:20 +00:00
}();
Utils::copyToClipboard(data);
}
2020-12-30 03:58:17 +00:00
void HistoryWidget::onWalletRefreshed() {
ui->syncNotice->hide();
}
void HistoryWidget::showSyncNoticeMsg() {
QMessageBox::information(this, "Sync notice",
"The wallet needs to scan the blockchain to find your transactions. "
"The status bar will show you how many blocks are still remaining.\n"
"\n"
"The history page will update once synchronization has finished. "
"To update the history page during synchronization press Ctrl+R.");
}
2021-06-27 12:13:05 +00:00
HistoryWidget::~HistoryWidget() = default;