Skip to content

Commit 8711cc0

Browse files
committed
qt: Improve BitcoinAmountField class
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.
1 parent 29f429d commit 8711cc0

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

src/qt/bitcoinamountfield.cpp

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ class AmountSpinBox: public QAbstractSpinBox
2323

2424
public:
2525
explicit AmountSpinBox(QWidget *parent):
26-
QAbstractSpinBox(parent),
27-
currentUnit(BitcoinUnits::BTC),
28-
singleStep(100000) // satoshis
26+
QAbstractSpinBox(parent)
2927
{
3028
setAlignment(Qt::AlignRight);
3129

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

4543
void fixup(QString &input) const
4644
{
47-
bool valid = false;
48-
CAmount val = parse(input, &valid);
49-
if(valid)
50-
{
45+
bool valid;
46+
CAmount val;
47+
48+
if (input.isEmpty() && !m_allow_empty) {
49+
valid = true;
50+
val = m_min_amount;
51+
} else {
52+
valid = false;
53+
val = parse(input, &valid);
54+
}
55+
56+
if (valid) {
57+
val = qBound(m_min_amount, val, m_max_amount);
5158
input = BitcoinUnits::format(currentUnit, val, false, BitcoinUnits::separatorAlways);
5259
lineEdit()->setText(input);
5360
}
@@ -64,12 +71,27 @@ class AmountSpinBox: public QAbstractSpinBox
6471
Q_EMIT valueChanged();
6572
}
6673

74+
void SetAllowEmpty(bool allow)
75+
{
76+
m_allow_empty = allow;
77+
}
78+
79+
void SetMinValue(const CAmount& value)
80+
{
81+
m_min_amount = value;
82+
}
83+
84+
void SetMaxValue(const CAmount& value)
85+
{
86+
m_max_amount = value;
87+
}
88+
6789
void stepBy(int steps)
6890
{
6991
bool valid = false;
7092
CAmount val = value(&valid);
7193
val = val + steps * singleStep;
72-
val = qMin(qMax(val, CAmount(0)), BitcoinUnits::maxMoney());
94+
val = qBound(m_min_amount, val, m_max_amount);
7395
setValue(val);
7496
}
7597

@@ -125,9 +147,12 @@ class AmountSpinBox: public QAbstractSpinBox
125147
}
126148

127149
private:
128-
int currentUnit;
129-
CAmount singleStep;
150+
int currentUnit{BitcoinUnits::BTC};
151+
CAmount singleStep{CAmount(100000)}; // satoshis
130152
mutable QSize cachedMinimumSizeHint;
153+
bool m_allow_empty{true};
154+
CAmount m_min_amount{CAmount(0)};
155+
CAmount m_max_amount{BitcoinUnits::maxMoney()};
131156

132157
/**
133158
* Parse a string into a number of base monetary units and
@@ -174,11 +199,10 @@ class AmountSpinBox: public QAbstractSpinBox
174199
StepEnabled rv = 0;
175200
bool valid = false;
176201
CAmount val = value(&valid);
177-
if(valid)
178-
{
179-
if(val > 0)
202+
if (valid) {
203+
if (val > m_min_amount)
180204
rv |= StepDownEnabled;
181-
if(val < BitcoinUnits::maxMoney())
205+
if (val < m_max_amount)
182206
rv |= StepUpEnabled;
183207
}
184208
return rv;
@@ -275,6 +299,21 @@ void BitcoinAmountField::setValue(const CAmount& value)
275299
amount->setValue(value);
276300
}
277301

302+
void BitcoinAmountField::SetAllowEmpty(bool allow)
303+
{
304+
amount->SetAllowEmpty(allow);
305+
}
306+
307+
void BitcoinAmountField::SetMinValue(const CAmount& value)
308+
{
309+
amount->SetMinValue(value);
310+
}
311+
312+
void BitcoinAmountField::SetMaxValue(const CAmount& value)
313+
{
314+
amount->SetMaxValue(value);
315+
}
316+
278317
void BitcoinAmountField::setReadOnly(bool fReadOnly)
279318
{
280319
amount->setReadOnly(fReadOnly);

src/qt/bitcoinamountfield.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ class BitcoinAmountField: public QWidget
3131
CAmount value(bool *value=0) const;
3232
void setValue(const CAmount& value);
3333

34+
/** If allow empty is set to false the field will be set to the minimum allowed value if left empty. **/
35+
void SetAllowEmpty(bool allow);
36+
37+
/** Set the minimum value in satoshis **/
38+
void SetMinValue(const CAmount& value);
39+
40+
/** Set the maximum value in satoshis **/
41+
void SetMaxValue(const CAmount& value);
42+
3443
/** Set single step in satoshis **/
3544
void setSingleStep(const CAmount& step);
3645

0 commit comments

Comments
 (0)