From c296eab19164b47bbecda19495bb98a213298f9a Mon Sep 17 00:00:00 2001 From: tobtoht Date: Thu, 4 Feb 2021 00:14:20 +0100 Subject: [PATCH] Coins: improve freeze/thaw performance and fix indexing bug --- src/coinswidget.cpp | 12 +++++++----- src/coinswidget.h | 6 ++---- src/libwalletqt/Coins.cpp | 4 ---- src/mainwindow.cpp | 19 ++++--------------- src/model/CoinsProxyModel.cpp | 15 ++++++++++----- src/model/CoinsProxyModel.h | 4 +++- 6 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/coinswidget.cpp b/src/coinswidget.cpp index f6dac9d..21934a5 100644 --- a/src/coinswidget.cpp +++ b/src/coinswidget.cpp @@ -61,7 +61,7 @@ CoinsWidget::CoinsWidget(QWidget *parent) void CoinsWidget::setModel(CoinsModel * model, Coins * coins) { m_coins = coins; m_model = model; - m_proxyModel = new CoinsProxyModel(this); + m_proxyModel = new CoinsProxyModel(this, m_coins); m_proxyModel->setSourceModel(m_model); ui->coins->setModel(m_proxyModel); ui->coins->setColumnHidden(CoinsModel::Spent, true); @@ -135,7 +135,8 @@ void CoinsWidget::setShowSpent(bool show) void CoinsWidget::freezeOutput() { QModelIndex index = ui->coins->currentIndex(); - emit freeze(m_proxyModel->mapToSource(index).row()); + QVector indexes = {m_proxyModel->mapToSource(index).row()}; + emit freeze(indexes); } void CoinsWidget::freezeAllSelected() { @@ -145,12 +146,13 @@ void CoinsWidget::freezeAllSelected() { for (QModelIndex index: list) { indexes.push_back(m_proxyModel->mapToSource(index).row()); // todo: will segfault if index get invalidated } - emit freezeMulti(indexes); + emit freeze(indexes); } void CoinsWidget::thawOutput() { QModelIndex index = ui->coins->currentIndex(); - emit thaw(m_proxyModel->mapToSource(index).row()); + QVector indexes = {m_proxyModel->mapToSource(index).row()}; + emit thaw(indexes); } void CoinsWidget::thawAllSelected() { @@ -160,7 +162,7 @@ void CoinsWidget::thawAllSelected() { for (QModelIndex index: list) { indexes.push_back(m_proxyModel->mapToSource(index).row()); } - emit thawMulti(indexes); + emit thaw(indexes); } void CoinsWidget::viewOutput() { diff --git a/src/coinswidget.h b/src/coinswidget.h index a2b3365..4ee451f 100644 --- a/src/coinswidget.h +++ b/src/coinswidget.h @@ -39,10 +39,8 @@ private slots: void onSweepOutput(); signals: - void freeze(int index); - void freezeMulti(QVector); - void thaw(int index); - void thawMulti(QVector); + void freeze(QVector indexes); + void thaw(QVector indexes); void sweepOutput(const QString &keyImage, const QString &address, bool isChurn, int outputs); private: diff --git a/src/libwalletqt/Coins.cpp b/src/libwalletqt/Coins.cpp index a99f971..677c700 100644 --- a/src/libwalletqt/Coins.cpp +++ b/src/libwalletqt/Coins.cpp @@ -36,10 +36,6 @@ void Coins::refresh(quint32 accountIndex) m_pimpl->refresh(); for (const auto i : m_pimpl->getAll()) { - if (i->subaddrAccount() != accountIndex) { - continue; - } - m_tinfo.append(new CoinsInfo(i, this)); } } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4191ef6..af84b79 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -289,30 +289,19 @@ MainWindow::MainWindow(AppContext *ctx, QWidget *parent) : connect(m_ctx, &AppContext::openAliasResolved, ui->sendWidget, &SendWidget::onOpenAliasResolved); // Coins - connect(ui->coinsWidget, &CoinsWidget::freeze, [=](int index) { - m_ctx->currentWallet->coins()->freeze(index); - m_ctx->currentWallet->coins()->refresh(m_ctx->currentWallet->currentSubaddressAccount()); - m_ctx->updateBalance(); - // subaddress account filtering should be done in Model maybe, so we can update data in coins() directly - }); - connect(ui->coinsWidget, &CoinsWidget::freezeMulti, [&](const QVector& indexes) { + connect(ui->coinsWidget, &CoinsWidget::freeze, [&](const QVector& indexes) { for (int i : indexes) { m_ctx->currentWallet->coins()->freeze(i); - m_ctx->currentWallet->coins()->refresh(m_ctx->currentWallet->currentSubaddressAccount()); - m_ctx->updateBalance(); } - }); - connect(ui->coinsWidget, &CoinsWidget::thaw, [=](int index) { - m_ctx->currentWallet->coins()->thaw(index); m_ctx->currentWallet->coins()->refresh(m_ctx->currentWallet->currentSubaddressAccount()); m_ctx->updateBalance(); }); - connect(ui->coinsWidget, &CoinsWidget::thawMulti, [&](const QVector& indexes) { + connect(ui->coinsWidget, &CoinsWidget::thaw, [&](const QVector& indexes) { for (int i : indexes) { m_ctx->currentWallet->coins()->thaw(i); - m_ctx->currentWallet->coins()->refresh(m_ctx->currentWallet->currentSubaddressAccount()); - m_ctx->updateBalance(); } + m_ctx->currentWallet->coins()->refresh(m_ctx->currentWallet->currentSubaddressAccount()); + m_ctx->updateBalance(); }); connect(ui->coinsWidget, &CoinsWidget::sweepOutput, m_ctx, &AppContext::onSweepOutput); diff --git a/src/model/CoinsProxyModel.cpp b/src/model/CoinsProxyModel.cpp index b90e33d..d376813 100644 --- a/src/model/CoinsProxyModel.cpp +++ b/src/model/CoinsProxyModel.cpp @@ -3,17 +3,22 @@ #include "CoinsProxyModel.h" #include "CoinsModel.h" +#include "libwalletqt/CoinsInfo.h" -CoinsProxyModel::CoinsProxyModel(QObject *parent) - : QSortFilterProxyModel(parent) +CoinsProxyModel::CoinsProxyModel(QObject *parent, Coins *coins) + : QSortFilterProxyModel(parent), m_coins(coins) { setSortRole(Qt::UserRole); } bool CoinsProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { - QModelIndex spentIndex = sourceModel()->index(sourceRow, CoinsModel::Spent, sourceParent); - bool isSpent = sourceModel()->data(spentIndex).toBool(); + bool isSpent; + int accountIndex; + m_coins->coin(sourceRow, [&isSpent, &accountIndex](const CoinsInfo &c){ + isSpent = c.spent(); + accountIndex = c.subaddrAccount(); + }); - return !(!m_showSpent && isSpent); + return !(!m_showSpent && isSpent) && accountIndex == 0; } \ No newline at end of file diff --git a/src/model/CoinsProxyModel.h b/src/model/CoinsProxyModel.h index a8383c5..43bff13 100644 --- a/src/model/CoinsProxyModel.h +++ b/src/model/CoinsProxyModel.h @@ -5,12 +5,13 @@ #define FEATHER_COINSPROXYMODEL_H #include +#include "libwalletqt/Coins.h" class CoinsProxyModel : public QSortFilterProxyModel { Q_OBJECT public: - explicit CoinsProxyModel(QObject* parent = nullptr); + explicit CoinsProxyModel(QObject* parent, Coins *coins); bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; public slots: @@ -21,6 +22,7 @@ public slots: private: bool m_showSpent = false; + Coins *m_coins; }; #endif //FEATHER_COINSPROXYMODEL_H