-
Notifications
You must be signed in to change notification settings - Fork 41
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
Initial Template for Request Payment page #435
Open
johnny9
wants to merge
10
commits into
bitcoin-core:main
Choose a base branch
from
johnny9:request-payment-form
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2bcb248
qml: Introduce LabeledTextInput control
johnny9 1f02fab
qml: Add copy icon
johnny9 48eed10
qml: Add flip-vertical icon
johnny9 35e5f21
qml: Add pending icon
johnny9 b502036
qml: Introduce BitcoinAmount
johnny9 9fb3663
qml: Introduce RequestPayment page
johnny9 adff8f2
qml: Introduce RequestConfirmation page
johnny9 8d15b2a
qml: Allow TextFields to be selectable in RequestPayment
johnny9 74e87a9
qml: Merge ReqeustPayment and RequestConfirmation into one form
johnny9 622a114
qml: Remove RequestConfirmation.qml from builds
johnny9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// 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/bitcoinamount.h> | ||
|
||
#include <QRegExp> | ||
#include <QStringList> | ||
|
||
|
||
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) | ||
{ | ||
QString result = text; | ||
|
||
// Remove any invalid characters | ||
result.remove(QRegExp("[^0-9.]")); | ||
|
||
// Ensure only one decimal point | ||
QStringList parts = result.split('.'); | ||
if (parts.size() > 2) { | ||
result = parts[0] + "." + parts[1]; | ||
} | ||
|
||
// Limit decimal places to 8 | ||
if (parts.size() == 2 && parts[1].length() > 8) { | ||
result = parts[0] + "." + parts[1].left(8); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
BitcoinAmount::Unit BitcoinAmount::unit() const | ||
{ | ||
return m_unit; | ||
} | ||
|
||
void BitcoinAmount::setUnit(const Unit unit) | ||
{ | ||
m_unit = unit; | ||
Q_EMIT unitChanged(); | ||
} | ||
|
||
QString BitcoinAmount::unitLabel() const | ||
{ | ||
switch (m_unit) { | ||
case Unit::BTC: return "₿"; | ||
case Unit::SAT: return "Sat"; | ||
} | ||
assert(false); | ||
} | ||
|
||
QString BitcoinAmount::amount() const | ||
{ | ||
return m_amount; | ||
} | ||
|
||
void BitcoinAmount::setAmount(const QString& new_amount) | ||
{ | ||
m_amount = sanitize(new_amount); | ||
Q_EMIT amountChanged(); | ||
} | ||
|
||
long long BitcoinAmount::toSatoshis(QString& amount, const Unit unit) | ||
{ | ||
|
||
int num_decimals = decimals(unit); | ||
|
||
QStringList parts = amount.remove(' ').split("."); | ||
|
||
QString whole = parts[0]; | ||
QString decimals; | ||
|
||
if(parts.size() > 1) | ||
{ | ||
decimals = parts[1]; | ||
} | ||
QString str = whole + decimals.leftJustified(num_decimals, '0', true); | ||
|
||
return str.toLongLong(); | ||
} | ||
|
||
QString BitcoinAmount::convert(const QString &amount, Unit unit) | ||
{ | ||
if (amount == "") { | ||
return amount; | ||
} | ||
|
||
QString result = amount; | ||
int decimalPosition = result.indexOf("."); | ||
|
||
if (decimalPosition == -1) { | ||
decimalPosition = result.length(); | ||
result.append("."); | ||
} | ||
|
||
if (unit == Unit::BTC) { | ||
int numDigitsAfterDecimal = result.length() - decimalPosition - 1; | ||
if (numDigitsAfterDecimal < 8) { | ||
result.append(QString(8 - numDigitsAfterDecimal, '0')); | ||
} | ||
result.remove(decimalPosition, 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, "."); | ||
} | ||
|
||
return result; | ||
} |
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,52 @@ | ||
// 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_BITCOINAMOUNT_H | ||
#define BITCOIN_QML_BITCOINAMOUNT_H | ||
|
||
#include <QObject> | ||
#include <QString> | ||
#include <qobjectdefs.h> | ||
|
||
class BitcoinAmount : public QObject | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(Unit unit READ unit WRITE setUnit NOTIFY unitChanged) | ||
Q_PROPERTY(QString unitLabel READ unitLabel NOTIFY unitChanged) | ||
Q_PROPERTY(QString amount READ amount WRITE setAmount NOTIFY amountChanged) | ||
|
||
public: | ||
enum class Unit { | ||
BTC, | ||
SAT | ||
}; | ||
Q_ENUM(Unit) | ||
|
||
explicit BitcoinAmount(QObject *parent = nullptr); | ||
|
||
Unit unit() const; | ||
void setUnit(Unit unit); | ||
QString unitLabel() const; | ||
QString amount() const; | ||
void setAmount(const QString& new_amount); | ||
|
||
public Q_SLOTS: | ||
QString sanitize(const QString &text); | ||
QString convert(const QString &text, Unit unit); | ||
|
||
Q_SIGNALS: | ||
void unitChanged(); | ||
void unitLabelChanged(); | ||
void amountChanged(); | ||
|
||
private: | ||
long long toSatoshis(QString &amount, const Unit unit); | ||
int decimals(Unit unit); | ||
|
||
Unit m_unit; | ||
QString m_unitLabel; | ||
QString m_amount; | ||
}; | ||
|
||
#endif // BITCOIN_QML_BITCOINAMOUNT_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// 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. | ||
|
||
import QtQuick 2.15 | ||
import QtQuick.Controls 2.15 | ||
import QtQuick.Layouts 1.15 | ||
|
||
Item { | ||
property alias labelText: label.text | ||
property alias text: input.text | ||
property alias placeholderText: input.placeholderText | ||
property alias iconSource: icon.source | ||
property alias customIcon: iconContainer.data | ||
property alias enabled: input.enabled | ||
|
||
signal iconClicked | ||
signal textEdited | ||
|
||
id: root | ||
implicitHeight: label.height + input.height | ||
|
||
CoreText { | ||
id: label | ||
anchors.left: parent.left | ||
anchors.top: parent.top | ||
color: Theme.color.neutral7 | ||
font.pixelSize: 15 | ||
} | ||
|
||
TextField { | ||
id: input | ||
anchors.left: parent.left | ||
anchors.right: iconContainer.left | ||
anchors.bottom: parent.bottom | ||
leftPadding: 0 | ||
font.family: "Inter" | ||
font.styleName: "Regular" | ||
font.pixelSize: 18 | ||
color: Theme.color.neutral9 | ||
placeholderTextColor: Theme.color.neutral7 | ||
background: Item {} | ||
selectByMouse: true | ||
onTextEdited: root.textEdited() | ||
} | ||
|
||
Item { | ||
id: iconContainer | ||
anchors.right: parent.right | ||
anchors.verticalCenter: input.verticalCenter | ||
|
||
Icon { | ||
id: icon | ||
source: "" | ||
color: Theme.color.neutral8 | ||
size: 30 | ||
enabled: source != "" | ||
onClicked: root.iconClicked() | ||
} | ||
} | ||
} |
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.
Is this the right thing to do?
QT gui simply refuses to paste when there are invalid characters.