Skip to content

Commit

Permalink
qt: Improve BitcoinAmountField class
Browse files Browse the repository at this point in the history
This adds functions for specifing a min/max value for a
BitcoinAmountField. These options only affect user input, so it's still
possible to use setValue to set values outside of the min/max range. The
existing value will not be changed when calling these functions even if
it's out of range. The min/max range will be reinforced when the field
loses focus.
This also adds `SetAllowEmpty` function which specifies if the field is
allowed to be left empty by the user. If set to false the field will be
set to the minimum allowed value if it's empty when focus is lost.
  • Loading branch information
hebasto committed Oct 30, 2018
1 parent 29f429d commit 8711cc0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
67 changes: 53 additions & 14 deletions src/qt/bitcoinamountfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class AmountSpinBox: public QAbstractSpinBox

public:
explicit AmountSpinBox(QWidget *parent):
QAbstractSpinBox(parent),
currentUnit(BitcoinUnits::BTC),
singleStep(100000) // satoshis
QAbstractSpinBox(parent)
{
setAlignment(Qt::AlignRight);

Expand All @@ -44,10 +42,19 @@ class AmountSpinBox: public QAbstractSpinBox

void fixup(QString &input) const
{
bool valid = false;
CAmount val = parse(input, &valid);
if(valid)
{
bool valid;
CAmount val;

if (input.isEmpty() && !m_allow_empty) {
valid = true;
val = m_min_amount;
} else {
valid = false;
val = parse(input, &valid);
}

if (valid) {
val = qBound(m_min_amount, val, m_max_amount);
input = BitcoinUnits::format(currentUnit, val, false, BitcoinUnits::separatorAlways);
lineEdit()->setText(input);
}
Expand All @@ -64,12 +71,27 @@ class AmountSpinBox: public QAbstractSpinBox
Q_EMIT valueChanged();
}

void SetAllowEmpty(bool allow)
{
m_allow_empty = allow;
}

void SetMinValue(const CAmount& value)
{
m_min_amount = value;
}

void SetMaxValue(const CAmount& value)
{
m_max_amount = value;
}

void stepBy(int steps)
{
bool valid = false;
CAmount val = value(&valid);
val = val + steps * singleStep;
val = qMin(qMax(val, CAmount(0)), BitcoinUnits::maxMoney());
val = qBound(m_min_amount, val, m_max_amount);
setValue(val);
}

Expand Down Expand Up @@ -125,9 +147,12 @@ class AmountSpinBox: public QAbstractSpinBox
}

private:
int currentUnit;
CAmount singleStep;
int currentUnit{BitcoinUnits::BTC};
CAmount singleStep{CAmount(100000)}; // satoshis
mutable QSize cachedMinimumSizeHint;
bool m_allow_empty{true};
CAmount m_min_amount{CAmount(0)};
CAmount m_max_amount{BitcoinUnits::maxMoney()};

/**
* Parse a string into a number of base monetary units and
Expand Down Expand Up @@ -174,11 +199,10 @@ class AmountSpinBox: public QAbstractSpinBox
StepEnabled rv = 0;
bool valid = false;
CAmount val = value(&valid);
if(valid)
{
if(val > 0)
if (valid) {
if (val > m_min_amount)
rv |= StepDownEnabled;
if(val < BitcoinUnits::maxMoney())
if (val < m_max_amount)
rv |= StepUpEnabled;
}
return rv;
Expand Down Expand Up @@ -275,6 +299,21 @@ void BitcoinAmountField::setValue(const CAmount& value)
amount->setValue(value);
}

void BitcoinAmountField::SetAllowEmpty(bool allow)
{
amount->SetAllowEmpty(allow);
}

void BitcoinAmountField::SetMinValue(const CAmount& value)
{
amount->SetMinValue(value);
}

void BitcoinAmountField::SetMaxValue(const CAmount& value)
{
amount->SetMaxValue(value);
}

void BitcoinAmountField::setReadOnly(bool fReadOnly)
{
amount->setReadOnly(fReadOnly);
Expand Down
9 changes: 9 additions & 0 deletions src/qt/bitcoinamountfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ class BitcoinAmountField: public QWidget
CAmount value(bool *value=0) const;
void setValue(const CAmount& value);

/** If allow empty is set to false the field will be set to the minimum allowed value if left empty. **/
void SetAllowEmpty(bool allow);

/** Set the minimum value in satoshis **/
void SetMinValue(const CAmount& value);

/** Set the maximum value in satoshis **/
void SetMaxValue(const CAmount& value);

/** Set single step in satoshis **/
void setSingleStep(const CAmount& step);

Expand Down

0 comments on commit 8711cc0

Please sign in to comment.