]> Nutra Git (v1) - gamesguru/feather.git/commitdiff
SubaddressAccount: cleanup
authortobtoht <tob@featherwallet.org>
Tue, 11 Mar 2025 14:50:15 +0000 (15:50 +0100)
committertobtoht <tob@featherwallet.org>
Tue, 11 Mar 2025 15:05:43 +0000 (16:05 +0100)
src/dialog/AccountSwitcherDialog.cpp
src/dialog/AccountSwitcherDialog.h
src/libwalletqt/SubaddressAccount.cpp
src/libwalletqt/SubaddressAccount.h
src/libwalletqt/Wallet.cpp
src/libwalletqt/rows/AccountRow.cpp [deleted file]
src/libwalletqt/rows/AccountRow.h
src/model/SubaddressAccountModel.cpp
src/model/SubaddressAccountModel.h

index 347e2074885a103877510908e48a69dd9f4f9269..15028dd7fae4d52bb57f98c35eadc03fd5d0468f 100644 (file)
@@ -67,21 +67,25 @@ void AccountSwitcherDialog::switchAccount(const QModelIndex &index) {
 }
 
 void AccountSwitcherDialog::copyLabel() {
-    auto row = this->currentEntry();
-    if (!row) {
+    QModelIndex index = m_proxyModel->mapToSource(ui->accounts->currentIndex());
+    if (!index.isValid()) {
         return;
     }
 
-    Utils::copyToClipboard(row->getLabel());
+    auto& row = m_wallet->subaddressAccountModel()->entryFromIndex(index);
+
+    Utils::copyToClipboard(row.label);
 }
 
 void AccountSwitcherDialog::copyBalance() {
-    auto row = this->currentEntry();
-    if (!row) {
+    QModelIndex index = m_proxyModel->mapToSource(ui->accounts->currentIndex());
+    if (!index.isValid()) {
         return;
     }
 
-    Utils::copyToClipboard(row->getBalance());
+    auto& row = m_wallet->subaddressAccountModel()->entryFromIndex(index);
+
+    Utils::copyToClipboard(WalletManager::displayAmount(row.balance));
 }
 
 void AccountSwitcherDialog::editLabel() {
@@ -115,9 +119,4 @@ void AccountSwitcherDialog::showContextMenu(const QPoint &point) {
     menu->popup(ui->accounts->viewport()->mapToGlobal(point));
 }
 
-AccountRow* AccountSwitcherDialog::currentEntry() {
-    QModelIndex index = m_proxyModel->mapToSource(ui->accounts->currentIndex());
-    return m_wallet->subaddressAccountModel()->entryFromIndex(index);
-}
-
 AccountSwitcherDialog::~AccountSwitcherDialog() = default;
index 1adfbc7792fdcd88e3f1308e3113b84b66775dc1..b1e9114cc96e0fe47ebe08bf689d3e9cd6c83c70 100644 (file)
@@ -35,8 +35,6 @@ private:
     void copyBalance();
     void editLabel();
 
-    AccountRow* currentEntry();
-
     QScopedPointer<Ui::AccountSwitcherDialog> ui;
     Wallet *m_wallet;
     SubaddressAccountModel *m_model;
index 44d1c7247ea11183f3f0e3ed6b68cfbbc2fa4533..7d5f0bbcc04fc0b10333eadb4da835279d437cc0 100644 (file)
@@ -4,24 +4,12 @@
 #include "SubaddressAccount.h"
 #include <wallet/wallet2.h>
 
-SubaddressAccount::SubaddressAccount(Wallet *wallet, tools::wallet2 *wallet2, QObject *parent)
+SubaddressAccount::SubaddressAccount(tools::wallet2 *wallet2, QObject *parent)
     : QObject(parent)
-    , m_wallet(wallet)
     , m_wallet2(wallet2)
 {
 }
 
-bool SubaddressAccount::getRow(int index, std::function<void (AccountRow &row)> callback) const
-{
-    if (index < 0 || index >= m_rows.size())
-    {
-        return false;
-    }
-
-    callback(*m_rows.value(index));
-    return true;
-}
-
 void SubaddressAccount::addRow(const QString &label)
 {
     m_wallet2->add_subaddress_account(label.toStdString());
@@ -39,16 +27,14 @@ void SubaddressAccount::refresh()
     emit refreshStarted();
 
     this->clearRows();
+
     for (uint32_t i = 0; i < m_wallet2->get_num_subaddress_accounts(); ++i)
     {
-        auto *row = new AccountRow{this,
-                                   i,
-                                   QString::fromStdString(m_wallet2->get_subaddress_as_str({i,0})),
-                                   QString::fromStdString(m_wallet2->get_subaddress_label({i,0})),
-                                   m_wallet2->balance(i, false),
-                                   m_wallet2->unlocked_balance(i, false)};
-
-        m_rows.append(row);
+        m_rows.emplace_back(
+            QString::fromStdString(m_wallet2->get_subaddress_as_str({i,0})),
+            QString::fromStdString(m_wallet2->get_subaddress_label({i,0})),
+            m_wallet2->balance(i, false),
+            m_wallet2->unlocked_balance(i, false));
     }
 
     emit refreshFinished();
@@ -61,11 +47,18 @@ qsizetype SubaddressAccount::count() const
 
 void SubaddressAccount::clearRows()
 {
-    qDeleteAll(m_rows);
     m_rows.clear();
 }
 
-AccountRow* SubaddressAccount::row(int index) const
+const QList<AccountRow>& SubaddressAccount::getRows()
+{
+    return m_rows;
+}
+
+const AccountRow& SubaddressAccount::row(const int index) const
 {
-    return m_rows.value(index);
-}
\ No newline at end of file
+    if (index < 0 || index >= m_rows.size()) {
+        throw std::out_of_range("Index out of range");
+    }
+    return m_rows[index];
+}
index 3052c1cd7bfffa51e35e8edce97d4e2a7b62fb28..65e2cea4b68adf9c286030660ed79a0222f5628d 100644 (file)
@@ -4,14 +4,9 @@
 #ifndef SUBADDRESSACCOUNT_H
 #define SUBADDRESSACCOUNT_H
 
-#include <functional>
-
 #include <QObject>
-#include <QReadWriteLock>
 #include <QList>
-#include <QDateTime>
 
-#include "Wallet.h"
 #include "rows/AccountRow.h"
 
 namespace tools {
@@ -23,30 +18,26 @@ class SubaddressAccount : public QObject
     Q_OBJECT
 
 public:
-    void getAll() const;
-    bool getRow(int index, std::function<void (AccountRow &row)> callback) const;
-    void addRow(const QString &label);
+    const QList<AccountRow>& getRows();
+    const AccountRow& row(int index) const;
 
+    void addRow(const QString &label);
     void setLabel(quint32 accountIndex, const QString &label);
+    qsizetype count() const;
 
     void refresh();
-
-    qsizetype count() const;
     void clearRows();
 
-    AccountRow* row(int index) const;
-
 signals:
     void refreshStarted() const;
     void refreshFinished() const;
 
 private:
-    explicit SubaddressAccount(Wallet *wallet, tools::wallet2 *wallet2, QObject *parent);
+    explicit SubaddressAccount(tools::wallet2 *wallet2, QObject *parent);
     friend class Wallet;
 
-    Wallet *m_wallet;
     tools::wallet2 *m_wallet2;
-    QList<AccountRow*> m_rows;
+    QList<AccountRow> m_rows;
 };
 
 #endif // SUBADDRESSACCOUNT_H
index 26b4461faff3c77ce9a5badadf62d24aa9436c2d..73a7dd1222cbbafa0589dcf48cc0bb278a6847e3 100644 (file)
@@ -45,7 +45,7 @@ Wallet::Wallet(Monero::Wallet *wallet, QObject *parent)
         , m_connectionStatus(Wallet::ConnectionStatus_Disconnected)
         , m_currentSubaddressAccount(0)
         , m_subaddress(new Subaddress(this, wallet->getWallet(), this))
-        , m_subaddressAccount(new SubaddressAccount(this, wallet->getWallet(), this))
+        , m_subaddressAccount(new SubaddressAccount(wallet->getWallet(), this))
         , m_refreshNow(false)
         , m_refreshEnabled(false)
         , m_scheduler(this)
diff --git a/src/libwalletqt/rows/AccountRow.cpp b/src/libwalletqt/rows/AccountRow.cpp
deleted file mode 100644 (file)
index d49d348..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-// SPDX-License-Identifier: BSD-3-Clause
-// SPDX-FileCopyrightText: The Monero Project
-
-#include "AccountRow.h"
-#include "WalletManager.h"
-
-qsizetype AccountRow::getRow() const {
-    return m_row;
-}
-
-const QString& AccountRow::getAddress() const {
-    return m_address;
-}
-
-const QString& AccountRow::getLabel() const {
-    return m_label;
-}
-
-QString AccountRow::getBalance() const {
-    return WalletManager::displayAmount(m_balance);
-}
-
-QString AccountRow::getUnlockedBalance() const {
-    return WalletManager::displayAmount(m_unlockedBalance);
-}
\ No newline at end of file
index bd783977d8dcca5f5927403d31cadd43d2b25d34..499ddacc8caaa8f6402a6bd1659fb8d3e32a5ef8 100644 (file)
@@ -4,33 +4,20 @@
 #ifndef FEATHER_ACCOUNTROW_H
 #define FEATHER_ACCOUNTROW_H
 
-#include <QObject>
+#include <QString>
 
-class AccountRow : public QObject
+struct AccountRow
 {
-Q_OBJECT
+    QString address;
+    QString label;
+    quint64 balance;
+    quint64 unlockedBalance;
 
-public:
-    AccountRow(QObject *parent, qsizetype row, const QString& address, const QString &label, uint64_t balance, uint64_t unlockedBalance)
-            : QObject(parent)
-            , m_row(row)
-            , m_address(address)
-            , m_label(label)
-            , m_balance(balance)
-            , m_unlockedBalance(unlockedBalance) {}
-
-    qsizetype getRow() const;
-    const QString& getAddress() const;
-    const QString& getLabel() const;
-    QString getBalance() const;
-    QString getUnlockedBalance() const;
-
-private:
-    qsizetype m_row;
-    QString m_address;
-    QString m_label;
-    uint64_t m_balance;
-    uint64_t m_unlockedBalance;
+    AccountRow(const QString& address_, const QString &label_, uint64_t balance_, uint64_t unlockedBalance_)
+            : address(address_)
+            , label(label_)
+            , balance(balance_)
+            , unlockedBalance(unlockedBalance_) {}
 };
 
 #endif //FEATHER_ACCOUNTROW_H
index 482cadc02bc0b2d2ee94240c145cd144df9883b9..0628c980aa35272a72fde64db3312320901126cd 100644 (file)
@@ -45,32 +45,28 @@ int SubaddressAccountModel::columnCount(const QModelIndex &parent) const
 
 QVariant SubaddressAccountModel::data(const QModelIndex &index, int role) const
 {
-    if (!index.isValid() || index.row() < 0 || static_cast<quint64>(index.row()) >= m_subaddressAccount->count())
+    const QList<AccountRow>& rows = m_subaddressAccount->getRows();
+    if (index.row() < 0 || index.row() >= rows.size()) {
         return {};
+    }
 
-    QVariant result;
+    const AccountRow& row = rows[index.row()];
 
-    bool found = m_subaddressAccount->getRow(index.row(), [this, &index, &result, &role](const AccountRow &row) {
-        if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole) {
-            result = parseSubaddressAccountRow(row, index, role);
-        }
-        else if (role == Qt::FontRole) {
-            if (index.column() == Column::Balance || index.column() == Column::UnlockedBalance) {
-                result = Utils::getMonospaceFont();
-            }
+    if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole) {
+        return parseSubaddressAccountRow(row, index, role);
+    }
+    else if (role == Qt::FontRole) {
+        if (index.column() == Column::Balance || index.column() == Column::UnlockedBalance) {
+            return Utils::getMonospaceFont();
         }
-        else if (role == Qt::TextAlignmentRole) {
-            if (index.column() == Column::Balance || index.column() == Column::UnlockedBalance) {
-                result = Qt::AlignRight;
-            }
+    }
+    else if (role == Qt::TextAlignmentRole) {
+        if (index.column() == Column::Balance || index.column() == Column::UnlockedBalance) {
+            return Qt::AlignRight;
         }
-    });
-
-    if (!found) {
-        qCritical("%s: internal error: invalid index %d", __FUNCTION__, index.row());
     }
 
-    return result;
+    return {};
 }
 
 QVariant SubaddressAccountModel::parseSubaddressAccountRow(const AccountRow &row,
@@ -83,19 +79,19 @@ QVariant SubaddressAccountModel::parseSubaddressAccountRow(const AccountRow &row
             }
             return QString("#%1").arg(QString::number(index.row()));
         case Address:
-            return row.getAddress();
+            return row.address;
         case Label:
-            return row.getLabel();
+            return row.label;
         case Balance:
             if (role == Qt::UserRole) {
-                return WalletManager::amountFromString(row.getBalance());
+                return row.balance;
             }
-            return row.getBalance();
+            return WalletManager::displayAmount(row.balance);
         case UnlockedBalance:
             if (role == Qt::UserRole) {
-                return WalletManager::amountFromString(row.getUnlockedBalance());
+                return row.unlockedBalance;
             }
-            return row.getUnlockedBalance();
+            return WalletManager::displayAmount(row.unlockedBalance);
         default:
             return QVariant();
     }
@@ -143,7 +139,6 @@ bool SubaddressAccountModel::setData(const QModelIndex &index, const QVariant &v
     return false;
 }
 
-
 Qt::ItemFlags SubaddressAccountModel::flags(const QModelIndex &index) const
 {
     if (!index.isValid())
@@ -155,7 +150,7 @@ Qt::ItemFlags SubaddressAccountModel::flags(const QModelIndex &index) const
     return QAbstractTableModel::flags(index);
 }
 
-AccountRow* SubaddressAccountModel::entryFromIndex(const QModelIndex &index) const {
+const AccountRow& SubaddressAccountModel::entryFromIndex(const QModelIndex &index) const {
     return m_subaddressAccount->row(index.row());
 }
 
@@ -163,4 +158,4 @@ SubaddressAccountProxyModel::SubaddressAccountProxyModel(QObject *parent)
     : QSortFilterProxyModel(parent)
 {
     setSortRole(Qt::UserRole);
-}
\ No newline at end of file
+}
index 62135b3110a3b7e86cbf4d5dca0e1347e92e4496..cd32bb1978440816d33470b0ef19b7a943a3315c 100644 (file)
@@ -36,7 +36,7 @@ public:
 
     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
 
-    AccountRow* entryFromIndex(const QModelIndex &index) const;
+    const AccountRow& entryFromIndex(const QModelIndex &index) const;
 
 public slots:
     void startReset();