-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce WalletModel and loadWallet functionality #417
Open
johnny9
wants to merge
1
commit into
bitcoin-core:main
Choose a base branch
from
johnny9:wallet-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,36 @@ | ||
// 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 <qt/bitcoinunits.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) | ||
{ | ||
} | ||
|
||
QString WalletQmlModel::balance() const | ||
{ | ||
if (!m_wallet) { | ||
return "0"; | ||
} | ||
return BitcoinUnits::format(BitcoinUnits::Unit::BTC, m_wallet->getBalance()); | ||
} | ||
|
||
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_MODELS_WALLETQMLMODEL_H | ||
#define BITCOIN_QML_MODELS_WALLETQMLMODEL_H | ||
|
||
#include <interfaces/wallet.h> | ||
|
||
#include <QObject> | ||
|
||
class WalletQmlModel : public QObject | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(QString name READ name NOTIFY nameChanged) | ||
Q_PROPERTY(QString 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; | ||
QString balance() const; | ||
|
||
Q_SIGNALS: | ||
void nameChanged(); | ||
void balanceChanged(); | ||
|
||
private: | ||
std::unique_ptr<interfaces::Wallet> m_wallet; | ||
}; | ||
|
||
#endif // BITCOIN_QML_MODELS_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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,70 +13,6 @@ import "../../controls" | |
Button { | ||
id: root | ||
|
||
function formatSatoshis(satoshis) { | ||
var highlightColor = Theme.color.neutral9 | ||
var zeroColor = Theme.color.neutral7 | ||
|
||
if (root.checked || root.hovered) { | ||
highlightColor = zeroColor = Theme.color.orange | ||
} | ||
|
||
// Convert satoshis to bitcoins | ||
var bitcoins = satoshis / 100000000; | ||
|
||
// Format bitcoins to a fixed 8 decimal places string | ||
var bitcoinStr = bitcoins.toFixed(8); | ||
|
||
// Split the bitcoin string into integer and fractional parts | ||
var parts = bitcoinStr.split('.'); | ||
|
||
// Add spaces for every 3 digits in the integer part | ||
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ' '); | ||
|
||
// Highlight the first significant digit and all following digits in the integer part | ||
var significantFound = false; | ||
parts[0] = parts[0].replace(/(\d)/g, function(match) { | ||
if (!significantFound && match !== '0') { | ||
significantFound = true; | ||
} | ||
if (significantFound) { | ||
return '<font color="' + highlightColor + '">' + match + '</font>'; | ||
} | ||
return match; | ||
}); | ||
|
||
// Add spaces for every 3 digits in the decimal part | ||
parts[1] = parts[1].replace(/\B(?=(\d{3})+(?!\d))/g, ' '); | ||
if (significantFound) { | ||
parts[1] = '<font color="' + highlightColor + '">' + parts[1] + '</font>'; | ||
} else { | ||
// Highlight the first significant digit and all following digits in the fractional part | ||
significantFound = false; | ||
parts[1] = parts[1].replace(/(\d)/g, function(match) { | ||
if (!significantFound && match !== '0') { | ||
significantFound = true; | ||
} | ||
if (significantFound) { | ||
return '<font color="' + highlightColor + '">' + match + '</font>'; | ||
} | ||
return match; | ||
}); | ||
} | ||
|
||
// Concatenate the parts back together | ||
var formattedBitcoins = parts.join('.'); | ||
|
||
// Format the text with the Bitcoin symbol | ||
var formattedText = `<font color="${highlightColor}">₿</font> ${formattedBitcoins}`; | ||
|
||
// Highlight zero in a different color if satoshis are zero | ||
if (satoshis === 0) { | ||
formattedText = `<font color="${zeroColor}">₿ 0.00</font>`; | ||
} | ||
|
||
return formattedText; | ||
} | ||
|
||
property color bgActiveColor: Theme.color.neutral2 | ||
property color textColor: Theme.color.neutral7 | ||
property color textHoverColor: Theme.color.orange | ||
|
@@ -85,17 +21,17 @@ Button { | |
property string iconSource: "" | ||
property bool showBalance: true | ||
property bool showIcon: true | ||
property string balance: "0.0 000 000" | ||
|
||
checkable: true | ||
hoverEnabled: AppMode.isDesktop | ||
implicitHeight: 60 | ||
implicitWidth: 220 | ||
implicitWidth: contentItem.width | ||
bottomPadding: 0 | ||
topPadding: 0 | ||
clip: true | ||
|
||
contentItem: RowLayout { | ||
anchors.fill: parent | ||
anchors.leftMargin: 5 | ||
anchors.rightMargin: 5 | ||
clip: true | ||
|
@@ -126,7 +62,7 @@ Button { | |
CoreText { | ||
id: balanceText | ||
visible: root.showBalance | ||
text: formatSatoshis(12300) | ||
text: "₿ " + root.balance | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we'll also need the sat symbol :P |
||
color: Theme.color.neutral7 | ||
} | ||
} | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this leaves the wallet dropdown in a weird looking state, we'd want to revisit balance anyways on subsequent updates, just noting that this should all change.