Skip to content

Connect Receive page to WalletQmlModel and Cleanup Layouts #464

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ QT_MOC_CPP = \
qml/models/moc_networktraffictower.cpp \
qml/models/moc_nodemodel.cpp \
qml/models/moc_options_model.cpp \
qml/models/moc_paymentrequest.cpp \
qml/models/moc_peerdetailsmodel.cpp \
qml/models/moc_peerlistsortproxy.cpp \
qml/models/moc_peerlistsortproxy.cpp \\
qml/models/moc_sendrecipient.cpp \
qml/models/moc_sendrecipientslistmodel.cpp \
qml/models/moc_transaction.cpp \
qml/models/moc_sendrecipient.cpp \
qml/models/moc_walletlistmodel.cpp \
Expand Down Expand Up @@ -134,10 +137,12 @@ BITCOIN_QT_H = \
qml/models/networktraffictower.h \
qml/models/nodemodel.h \
qml/models/options_model.h \
qml/models/paymentrequest.h \
qml/models/peerdetailsmodel.h \
qml/models/peerlistsortproxy.h \
qml/models/transaction.h \
qml/models/sendrecipient.h \
qml/models/sendrecipientslistmodel.h \
qml/models/walletlistmodel.h \
qml/models/walletqmlmodel.h \
qml/models/walletqmlmodeltransaction.h \
Expand Down Expand Up @@ -335,10 +340,12 @@ BITCOIN_QML_BASE_CPP = \
qml/models/networktraffictower.cpp \
qml/models/nodemodel.cpp \
qml/models/options_model.cpp \
qml/models/paymentrequest.cpp \
qml/models/peerdetailsmodel.cpp \
qml/models/peerlistsortproxy.cpp \
qml/models/transaction.cpp \
qml/models/sendrecipient.cpp \
qml/models/sendrecipientslistmodel.cpp \
qml/models/walletlistmodel.cpp \
qml/models/walletqmlmodel.cpp \
qml/models/walletqmlmodeltransaction.cpp \
Expand All @@ -353,6 +360,7 @@ QML_RES_FONTS = \

QML_RES_ICONS = \
qml/res/icons/add-wallet-dark.png \
qml/res/icons/alert-filled.png \
qml/res/icons/arrow-down.png \
qml/res/icons/arrow-up.png \
qml/res/icons/bitcoin-circle.png \
Expand All @@ -379,6 +387,7 @@ QML_RES_ICONS = \
qml/res/icons/network-dark.png \
qml/res/icons/network-light.png \
qml/res/icons/plus.png \
qml/res/icons/plus-big-filled.png \
qml/res/icons/pending.png \
qml/res/icons/shutdown.png \
qml/res/icons/singlesig-wallet.png \
Expand All @@ -398,6 +407,7 @@ QML_RES_QML = \
qml/components/BlockClock.qml \
qml/components/BlockClockDisplayMode.qml \
qml/components/BlockCounter.qml \
qml/components/BitcoinAmountInputField.qml \
qml/components/ConnectionOptions.qml \
qml/components/ConnectionSettings.qml \
qml/components/DeveloperOptions.qml \
Expand All @@ -420,12 +430,12 @@ QML_RES_QML = \
qml/controls/CoreCheckBox.qml \
qml/controls/CoreText.qml \
qml/controls/CoreTextField.qml \
qml/controls/EllipsisMenuButton.qml \
qml/controls/EllipsisMenuToggleItem.qml \
qml/controls/ExternalLink.qml \
qml/controls/FocusBorder.qml \
qml/controls/Header.qml \
qml/controls/Icon.qml \
qml/controls/IconButton.qml \
qml/controls/InformationPage.qml \
qml/controls/IPAddressValueInput.qml \
qml/controls/KeyValueRow.qml \
Expand Down Expand Up @@ -484,6 +494,7 @@ QML_RES_QML = \
qml/pages/wallet/CreatePassword.qml \
qml/pages/wallet/CreateWalletWizard.qml \
qml/pages/wallet/DesktopWallets.qml \
qml/pages/wallet/MultipleSendReview.qml \
qml/pages/wallet/RequestPayment.qml \
qml/pages/wallet/Send.qml \
qml/pages/wallet/SendResult.qml \
Expand Down
2 changes: 2 additions & 0 deletions src/qml/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <qml/models/networktraffictower.h>
#include <qml/models/nodemodel.h>
#include <qml/models/options_model.h>
#include <qml/models/paymentrequest.h>
#include <qml/models/peerdetailsmodel.h>
#include <qml/models/peerlistsortproxy.h>
#include <qml/models/sendrecipient.h>
Expand Down Expand Up @@ -340,6 +341,7 @@ int QmlGuiMain(int argc, char* argv[])
qmlRegisterType<LineGraph>("org.bitcoincore.qt", 1, 0, "LineGraph");
qmlRegisterUncreatableType<PeerDetailsModel>("org.bitcoincore.qt", 1, 0, "PeerDetailsModel", "");
qmlRegisterType<BitcoinAmount>("org.bitcoincore.qt", 1, 0, "BitcoinAmount");
qmlRegisterType<PaymentRequest>("org.bitcoincore.qt", 1, 0, "PaymentRequest");
qmlRegisterUncreatableType<Transaction>("org.bitcoincore.qt", 1, 0, "Transaction", "");
qmlRegisterUncreatableType<SendRecipient>("org.bitcoincore.qt", 1, 0, "SendRecipient", "");

Expand Down
6 changes: 5 additions & 1 deletion src/qml/bitcoin_qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<file>components/BlockClock.qml</file>
<file>components/BlockClockDisplayMode.qml</file>
<file>components/BlockCounter.qml</file>
<file>components/BitcoinAmountInputField.qml</file>
<file>components/ConnectionOptions.qml</file>
<file>components/ConnectionSettings.qml</file>
<file>components/DeveloperOptions.qml</file>
Expand All @@ -30,12 +31,12 @@
<file>controls/FocusBorder.qml</file>
<file>controls/Header.qml</file>
<file>controls/Icon.qml</file>
<file>controls/IconButton.qml</file>
<file>controls/InformationPage.qml</file>
<file>controls/IPAddressValueInput.qml</file>
<file>controls/KeyValueRow.qml</file>
<file>controls/LabeledTextInput.qml</file>
<file>controls/LabeledCoinControlButton.qml</file>
<file>controls/EllipsisMenuButton.qml</file>
<file>controls/EllipsisMenuToggleItem.qml</file>
<file>controls/NavButton.qml</file>
<file>controls/NavigationBar.qml</file>
Expand Down Expand Up @@ -90,6 +91,7 @@
<file>pages/wallet/CreatePassword.qml</file>
<file>pages/wallet/CreateWalletWizard.qml</file>
<file>pages/wallet/DesktopWallets.qml</file>
<file>pages/wallet/MultipleSendReview.qml</file>
<file>pages/wallet/RequestPayment.qml</file>
<file>pages/wallet/Send.qml</file>
<file>pages/wallet/SendResult.qml</file>
Expand All @@ -99,6 +101,7 @@
</qresource>
<qresource prefix="/icons">
<file alias="add-wallet-dark">res/icons/add-wallet-dark.png</file>
<file alias="alert-filled">res/icons/alert-filled.png</file>
<file alias="arrow-down">res/icons/arrow-down.png</file>
<file alias="arrow-up">res/icons/arrow-up.png</file>
<file alias="bitcoin-circle">res/icons/bitcoin-circle.png</file>
Expand Down Expand Up @@ -126,6 +129,7 @@
<file alias="network-dark">res/icons/network-dark.png</file>
<file alias="network-light">res/icons/network-light.png</file>
<file alias="plus">res/icons/plus.png</file>
<file alias="plus-big-filled">res/icons/plus-big-filled.png</file>
<file alias="pending">res/icons/pending.png</file>
<file alias="shutdown">res/icons/shutdown.png</file>
<file alias="singlesig-wallet">res/icons/singlesig-wallet.png</file>
Expand Down
155 changes: 77 additions & 78 deletions src/qml/bitcoinamount.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 The Bitcoin Core developers
// Copyright (c) 2024-2025 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand All @@ -7,19 +7,9 @@
#include <QRegExp>
#include <QStringList>


BitcoinAmount::BitcoinAmount(QObject *parent) : QObject(parent)
BitcoinAmount::BitcoinAmount(QObject* parent)
: QObject(parent)
{
m_unit = Unit::BTC;
}

int BitcoinAmount::decimals(Unit unit)
{
switch (unit) {
case Unit::BTC: return 8;
case Unit::SAT: return 0;
} // no default case, so the compiler can warn about missing cases
assert(false);
}

QString BitcoinAmount::sanitize(const QString &text)
Expand All @@ -43,6 +33,30 @@ QString BitcoinAmount::sanitize(const QString &text)
return result;
}

qint64 BitcoinAmount::satoshi() const
{
return m_satoshi;
}

void BitcoinAmount::setSatoshi(qint64 new_amount)
{
m_isSet = true;
if (m_satoshi != new_amount) {
m_satoshi = new_amount;
Q_EMIT amountChanged();
}
}

void BitcoinAmount::clear()
{
if (!m_isSet && m_satoshi == 0) {
return;
}
m_satoshi = 0;
m_isSet = false;
Q_EMIT amountChanged();
}

BitcoinAmount::Unit BitcoinAmount::unit() const
{
return m_unit;
Expand All @@ -58,97 +72,82 @@ QString BitcoinAmount::unitLabel() const
{
switch (m_unit) {
case Unit::BTC: return "₿";
case Unit::SAT: return "Sat";
case Unit::SAT: return "sat";
}
assert(false);
}

QString BitcoinAmount::amount() const
void BitcoinAmount::flipUnit()
{
return m_amount;
if (m_unit == Unit::BTC) {
m_unit = Unit::SAT;
} else {
m_unit = Unit::BTC;
}
Q_EMIT unitChanged();
Q_EMIT amountChanged();
}

QString BitcoinAmount::satoshiAmount() const
QString BitcoinAmount::satsToBtcString(qint64 sat)
{
return toSatoshis(m_amount);
}
const bool negative = sat < 0;
qint64 absSat = negative ? -sat : sat;

void BitcoinAmount::setAmount(const QString& new_amount)
{
m_amount = sanitize(new_amount);
Q_EMIT amountChanged();
const qint64 wholePart = absSat / COIN;
const qint64 fracInt = absSat % COIN;
QString fracPart = QString("%1").arg(fracInt, 8, 10, QLatin1Char('0'));

QString result = QString::number(wholePart) + '.' + fracPart;
if (negative) {
result.prepend('-');
}
return result;
}

QString BitcoinAmount::toSatoshis(const QString& text) const
QString BitcoinAmount::toDisplay() const
{
if (!m_isSet) {
return "";
}
if (m_unit == Unit::SAT) {
return text;
return QString::number(m_satoshi);
} else {
return convert(text, m_unit);
return satsToBtcString(m_satoshi);
}
}

long long BitcoinAmount::toSatoshis(QString& amount, const Unit unit)
qint64 BitcoinAmount::btcToSats(const QString& btcSanitized)
{
int num_decimals = decimals(unit);

QStringList parts = amount.remove(' ').split(".");
if (btcSanitized.isEmpty() || btcSanitized == ".") return 0;

QString whole = parts[0];
QString decimals;
QString cleaned = btcSanitized;
if (cleaned.startsWith('.')) cleaned.prepend('0');

if(parts.size() > 1)
{
decimals = parts[1];
QStringList parts = cleaned.split('.');
const qint64 whole = parts[0].isEmpty() ? 0 : parts[0].toLongLong();
qint64 frac = 0;
if (parts.size() == 2) {
frac = parts[1].leftJustified(8, '0').toLongLong();
}
QString str = whole + decimals.leftJustified(num_decimals, '0', true);

return str.toLongLong();
return whole * COIN + frac;
}

QString BitcoinAmount::convert(const QString& amount, Unit unit) const
void BitcoinAmount::fromDisplay(const QString& text)
{
if (amount == "") {
return amount;
}

QString result = amount;
int decimalPosition = result.indexOf(".");

if (decimalPosition == -1) {
decimalPosition = result.length();
result.append(".");
if (text.trimmed().isEmpty()) {
clear();
return;
}

if (unit == Unit::BTC) {
int numDigitsAfterDecimal = result.length() - decimalPosition - 1;
if (numDigitsAfterDecimal < 8) {
result.append(QString(8 - numDigitsAfterDecimal, '0'));
}
result.remove(decimalPosition, 1);

while (result.startsWith('0') && result.length() > 1) {
result.remove(0, 1);
}
} else if (unit == Unit::SAT) {
result.remove(decimalPosition, 1);
int newDecimalPosition = decimalPosition - 8;
if (newDecimalPosition < 1) {
result = QString("0").repeated(-newDecimalPosition) + result;
newDecimalPosition = 0;
}
result.insert(newDecimalPosition, ".");

while (result.endsWith('0') && result.contains('.')) {
result.chop(1);
}
if (result.endsWith('.')) {
result.chop(1);
}
if (result.startsWith('.')) {
result.insert(0, "0");
}
qint64 newSat = 0;
if (m_unit == Unit::BTC) {
QString sanitized = sanitize(text);
newSat = btcToSats(sanitized);
} else {
QString digitsOnly = text;
digitsOnly.remove(QRegExp("[^0-9]"));
newSat = digitsOnly.trimmed().isEmpty() ? 0 : digitsOnly.toLongLong();
}

return result;
setSatoshi(newSat);
}
Loading
Loading