-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
qml: Introduce WalletModel and loadWallet functionality
When a user selects a wallet from the WAlletSelect menu the wallet controller can now load the wallet data in and the name and balance will appear in the WalletBadge
- Loading branch information
Showing
12 changed files
with
213 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2024 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <qml/models/walletqmlmodel.h> | ||
|
||
#include <QTimer> | ||
|
||
WalletQmlModel::WalletQmlModel(std::unique_ptr<interfaces::Wallet> wallet, QObject *parent) | ||
: QObject(parent) | ||
{ | ||
m_wallet = std::move(wallet); | ||
} | ||
|
||
WalletQmlModel::WalletQmlModel(QObject *parent) | ||
: QObject(parent) | ||
{ | ||
} | ||
|
||
qint64 WalletQmlModel::balance() const | ||
{ | ||
if (!m_wallet) { | ||
return 0; | ||
} | ||
return m_wallet->getBalances().balance; | ||
} | ||
|
||
QString WalletQmlModel::name() const | ||
{ | ||
if (!m_wallet) { | ||
return QString(); | ||
} | ||
return QString::fromStdString(m_wallet->getWalletName()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2024 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_QML_WALLETQMLMODEL_H | ||
#define BITCOIN_QML_WALLETQMLMODEL_H | ||
|
||
#include <interfaces/wallet.h> | ||
|
||
#include <QObject> | ||
|
||
class WalletQmlModel : public QObject | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(QString name READ name NOTIFY nameChanged) | ||
Q_PROPERTY(qint64 balance READ balance NOTIFY balanceChanged) | ||
|
||
public: | ||
WalletQmlModel(std::unique_ptr<interfaces::Wallet> wallet, QObject *parent = nullptr); | ||
WalletQmlModel(QObject *parent = nullptr); | ||
~WalletQmlModel() = default; | ||
|
||
QString name() const; | ||
qint64 balance() const; | ||
|
||
Q_SIGNALS: | ||
void nameChanged(); | ||
void balanceChanged(); | ||
|
||
private: | ||
std::unique_ptr<interfaces::Wallet> m_wallet; | ||
}; | ||
|
||
#endif // BITCOIN_QML_WALLETQMLMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) 2024 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <qml/walletqmlcontroller.h> | ||
|
||
#include <interfaces/node.h> | ||
|
||
WalletQmlController::WalletQmlController(interfaces::Node& node, QObject *parent) | ||
: QObject(parent) | ||
, m_node(node) | ||
{ | ||
m_selected_wallet = new WalletQmlModel(parent); | ||
} | ||
|
||
WalletQmlController::~WalletQmlController() | ||
{ | ||
if (m_handler_load_wallet) { | ||
m_handler_load_wallet->disconnect(); | ||
} | ||
} | ||
|
||
void WalletQmlController::setSelectedWallet(QString path) | ||
{ | ||
std::vector<bilingual_str> warning_message; | ||
auto wallet{m_node.walletLoader().loadWallet(path.toStdString(), warning_message)}; | ||
if (wallet.has_value()) { | ||
m_selected_wallet = new WalletQmlModel(std::move(wallet.value())); | ||
m_wallets.push_back(m_selected_wallet); | ||
Q_EMIT selectedWalletChanged(); | ||
} | ||
} | ||
|
||
WalletQmlModel* WalletQmlController::selectedWallet() const | ||
{ | ||
return m_selected_wallet; | ||
} | ||
|
||
void WalletQmlController::unloadWallets() | ||
{ | ||
m_handler_load_wallet->disconnect(); | ||
for (WalletQmlModel* wallet : m_wallets) { | ||
delete wallet; | ||
} | ||
m_wallets.clear(); | ||
} | ||
|
||
void WalletQmlController::handleLoadWallet(std::unique_ptr<interfaces::Wallet> wallet) | ||
{ | ||
if (!m_wallets.empty()) { | ||
QString name = QString::fromStdString(wallet->getWalletName()); | ||
for (WalletQmlModel* wallet_model : m_wallets) { | ||
if (wallet_model->name() == name) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
m_selected_wallet = new WalletQmlModel(std::move(wallet)); | ||
m_wallets.push_back(m_selected_wallet); | ||
Q_EMIT selectedWalletChanged(); | ||
} | ||
|
||
void WalletQmlController::initialize() | ||
{ | ||
m_handler_load_wallet = m_node.walletLoader().handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) { | ||
handleLoadWallet(std::move(wallet)); | ||
}); | ||
|
||
auto wallets = m_node.walletLoader().getWallets(); | ||
for (auto& wallet : wallets) { | ||
handleLoadWallet(std::move(wallet)); | ||
} | ||
} |
Oops, something went wrong.