Skip to content

Commit

Permalink
qt: Move wallet preloading out of model constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
promag committed Sep 3, 2021
1 parent c09fd3a commit a845f63
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 17 deletions.
17 changes: 11 additions & 6 deletions src/qt/addresstablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ class AddressTablePriv
{
public:
QList<AddressTableEntry> cachedAddressTable;
AddressTableModel *parent;
AddressTableModel* const parent;
const bool pk_hash_only;

explicit AddressTablePriv(AddressTableModel *_parent):
parent(_parent) {}
explicit AddressTablePriv(AddressTableModel *_parent, bool pk_hash_only):
parent(_parent), pk_hash_only(pk_hash_only) {}

void refreshAddressTable(interfaces::Wallet& wallet, bool pk_hash_only = false)
void refreshAddressTable(interfaces::Wallet& wallet)
{
cachedAddressTable.clear();
{
Expand Down Expand Up @@ -166,15 +167,19 @@ AddressTableModel::AddressTableModel(WalletModel *parent, bool pk_hash_only) :
QAbstractTableModel(parent), walletModel(parent)
{
columns << tr("Label") << tr("Address");
priv = new AddressTablePriv(this);
priv->refreshAddressTable(parent->wallet(), pk_hash_only);
priv = new AddressTablePriv(this, pk_hash_only);
}

AddressTableModel::~AddressTableModel()
{
delete priv;
}

void AddressTableModel::preload()
{
priv->refreshAddressTable(walletModel->wallet());
}

int AddressTableModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
Expand Down
2 changes: 2 additions & 0 deletions src/qt/addresstablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class AddressTableModel : public QAbstractTableModel
static const QString Send; /**< Specifies send address */
static const QString Receive; /**< Specifies receive address */

void preload();

/** @name Methods overridden from QAbstractTableModel
@{*/
int rowCount(const QModelIndex &parent) const override;
Expand Down
13 changes: 8 additions & 5 deletions src/qt/recentrequeststablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
RecentRequestsTableModel::RecentRequestsTableModel(WalletModel *parent) :
QAbstractTableModel(parent), walletModel(parent)
{
// Load entries from wallet
for (const std::string& request : parent->wallet().getAddressReceiveRequests()) {
addNewRequest(request);
}

/* These columns must match the indices in the ColumnIndex enumeration */
columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();

Expand All @@ -39,6 +34,14 @@ RecentRequestsTableModel::~RecentRequestsTableModel()
/* Intentionally left empty */
}

void RecentRequestsTableModel::preload()
{
// Load entries from wallet
for (const std::string& request : walletModel->wallet().getAddressReceiveRequests()) {
addNewRequest(request);
}
}

int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
Expand Down
2 changes: 2 additions & 0 deletions src/qt/recentrequeststablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class RecentRequestsTableModel: public QAbstractTableModel
NUMBER_OF_COLUMNS
};

void preload();

/** @name Methods overridden from QAbstractTableModel
@{*/
int rowCount(const QModelIndex &parent) const override;
Expand Down
1 change: 1 addition & 0 deletions src/qt/test/addressbooktests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
WalletContext& context = *node.walletClient().context();
AddWallet(context, wallet);
WalletModel walletModel(interfaces::MakeWallet(context, wallet), clientModel, platformStyle.get());
walletModel.preload();
RemoveWallet(context, wallet, /* load_on_startup= */ std::nullopt);
EditAddressDialog editAddressDialog(EditAddressDialog::NewSendingAddress);
editAddressDialog.setModel(walletModel.getAddressTableModel());
Expand Down
1 change: 1 addition & 0 deletions src/qt/test/wallettests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ void TestGUI(interfaces::Node& node)
WalletContext& context = *node.walletClient().context();
AddWallet(context, wallet);
WalletModel walletModel(interfaces::MakeWallet(context, wallet), clientModel, platformStyle.get());
walletModel.preload();
RemoveWallet(context, wallet, /* load_on_startup= */ std::nullopt);
sendCoinsDialog.setModel(&walletModel);
transactionView.setModel(&walletModel);
Expand Down
10 changes: 6 additions & 4 deletions src/qt/transactiontablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,7 @@ TransactionTableModel::TransactionTableModel(const PlatformStyle *_platformStyle
fProcessingQueuedTransactions(false),
platformStyle(_platformStyle)
{
subscribeToCoreSignals();

columns << QString() << QString() << tr("Date") << tr("Type") << tr("Label") << BitcoinUnits::getAmountColumnTitle(walletModel->getOptionsModel()->getDisplayUnit());
priv->refreshWallet(walletModel->wallet());

connect(walletModel->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &TransactionTableModel::updateDisplayUnit);
}

Expand All @@ -269,6 +265,12 @@ TransactionTableModel::~TransactionTableModel()
delete priv;
}

void TransactionTableModel::preload()
{
subscribeToCoreSignals();
priv->refreshWallet(walletModel->wallet());
}

/** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
void TransactionTableModel::updateAmountColumnTitle()
{
Expand Down
2 changes: 2 additions & 0 deletions src/qt/transactiontablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class TransactionTableModel : public QAbstractTableModel
RawDecorationRole,
};

void preload();

int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
Expand Down
2 changes: 2 additions & 0 deletions src/qt/walletcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wal
wallet_model = new WalletModel(std::move(wallet), m_client_model, m_platform_style, this);
}, GUIUtil::blockingGUIThreadConnection());

wallet_model->preload();

m_wallets.push_back(wallet_model);

// WalletModel::startPollBalance needs to be called in a thread managed by
Expand Down
10 changes: 8 additions & 2 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,21 @@ WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet, ClientModel
addressTableModel = new AddressTableModel(this);
transactionTableModel = new TransactionTableModel(platformStyle, this);
recentRequestsTableModel = new RecentRequestsTableModel(this);

subscribeToCoreSignals();
}

WalletModel::~WalletModel()
{
unsubscribeFromCoreSignals();
}

void WalletModel::preload()
{
addressTableModel->preload();
transactionTableModel->preload();
recentRequestsTableModel->preload();
subscribeToCoreSignals();
}

void WalletModel::startPollBalance()
{
// This timer will be fired repeatedly to update the balance
Expand Down
2 changes: 2 additions & 0 deletions src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class WalletModel : public QObject
Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
};

void preload();

OptionsModel *getOptionsModel();
AddressTableModel *getAddressTableModel();
TransactionTableModel *getTransactionTableModel();
Expand Down

0 comments on commit a845f63

Please sign in to comment.