2020-12-31 05:48:25 -03:00
|
|
|
// Copyright (c) 2011-2020 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 12:20:43 -03:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/bitcoinamountfield.h>
|
2013-01-23 17:51:02 -03:00
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/bitcoinunits.h>
|
|
|
|
#include <qt/guiconstants.h>
|
2019-08-23 13:13:11 -04:00
|
|
|
#include <qt/guiutil.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/qvaluecombobox.h>
|
2011-10-07 08:21:45 -03:00
|
|
|
|
2013-07-23 02:52:24 -04:00
|
|
|
#include <QApplication>
|
2014-07-18 10:31:13 -04:00
|
|
|
#include <QAbstractSpinBox>
|
2011-06-20 15:31:42 -04:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QKeyEvent>
|
2014-07-18 10:31:13 -04:00
|
|
|
#include <QLineEdit>
|
2011-06-20 15:31:42 -04:00
|
|
|
|
2014-07-18 10:31:13 -04:00
|
|
|
/** QSpinBox that uses fixed-point numbers internally and uses our own
|
|
|
|
* formatting/parsing functions.
|
|
|
|
*/
|
|
|
|
class AmountSpinBox: public QAbstractSpinBox
|
2014-05-09 18:50:09 -04:00
|
|
|
{
|
2014-07-18 10:31:13 -04:00
|
|
|
Q_OBJECT
|
2015-01-10 11:02:12 -03:00
|
|
|
|
2014-05-09 18:50:09 -04:00
|
|
|
public:
|
|
|
|
explicit AmountSpinBox(QWidget *parent):
|
2018-10-30 09:02:08 -03:00
|
|
|
QAbstractSpinBox(parent)
|
2014-05-09 18:50:09 -04:00
|
|
|
{
|
2014-07-18 10:31:13 -04:00
|
|
|
setAlignment(Qt::AlignRight);
|
|
|
|
|
2018-06-24 11:18:22 -04:00
|
|
|
connect(lineEdit(), &QLineEdit::textEdited, this, &AmountSpinBox::valueChanged);
|
2014-07-18 10:31:13 -04:00
|
|
|
}
|
|
|
|
|
2020-03-14 03:49:59 -03:00
|
|
|
QValidator::State validate(QString &text, int &pos) const override
|
2014-07-18 10:31:13 -04:00
|
|
|
{
|
|
|
|
if(text.isEmpty())
|
|
|
|
return QValidator::Intermediate;
|
|
|
|
bool valid = false;
|
|
|
|
parse(text, &valid);
|
|
|
|
/* Make sure we return Intermediate so that fixup() is called on defocus */
|
|
|
|
return valid ? QValidator::Intermediate : QValidator::Invalid;
|
|
|
|
}
|
|
|
|
|
2020-03-14 03:49:59 -03:00
|
|
|
void fixup(QString &input) const override
|
2014-07-18 10:31:13 -04:00
|
|
|
{
|
2018-10-30 09:02:08 -03:00
|
|
|
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);
|
2020-06-18 08:30:15 -04:00
|
|
|
input = BitcoinUnits::format(currentUnit, val, false, BitcoinUnits::SeparatorStyle::ALWAYS);
|
2014-07-18 10:31:13 -04:00
|
|
|
lineEdit()->setText(input);
|
|
|
|
}
|
2014-05-09 18:50:09 -04:00
|
|
|
}
|
2014-07-18 10:31:13 -04:00
|
|
|
|
2018-07-30 06:37:09 -04:00
|
|
|
CAmount value(bool *valid_out=nullptr) const
|
2014-05-09 18:50:09 -04:00
|
|
|
{
|
2014-07-18 10:31:13 -04:00
|
|
|
return parse(text(), valid_out);
|
|
|
|
}
|
|
|
|
|
2014-04-22 19:46:19 -03:00
|
|
|
void setValue(const CAmount& value)
|
2014-07-18 10:31:13 -04:00
|
|
|
{
|
2020-06-18 08:30:15 -04:00
|
|
|
lineEdit()->setText(BitcoinUnits::format(currentUnit, value, false, BitcoinUnits::SeparatorStyle::ALWAYS));
|
2015-07-14 08:59:05 -03:00
|
|
|
Q_EMIT valueChanged();
|
2014-07-18 10:31:13 -04:00
|
|
|
}
|
|
|
|
|
2018-10-30 09:02:08 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-14 03:49:59 -03:00
|
|
|
void stepBy(int steps) override
|
2014-07-18 10:31:13 -04:00
|
|
|
{
|
|
|
|
bool valid = false;
|
2014-04-22 19:46:19 -03:00
|
|
|
CAmount val = value(&valid);
|
2014-07-18 10:31:13 -04:00
|
|
|
val = val + steps * singleStep;
|
2018-10-30 09:02:08 -03:00
|
|
|
val = qBound(m_min_amount, val, m_max_amount);
|
2014-07-18 10:31:13 -04:00
|
|
|
setValue(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDisplayUnit(int unit)
|
|
|
|
{
|
|
|
|
bool valid = false;
|
2014-04-22 19:46:19 -03:00
|
|
|
CAmount val = value(&valid);
|
2014-07-18 10:31:13 -04:00
|
|
|
|
|
|
|
currentUnit = unit;
|
2020-06-18 08:30:15 -04:00
|
|
|
lineEdit()->setPlaceholderText(BitcoinUnits::format(currentUnit, m_min_amount, false, BitcoinUnits::SeparatorStyle::ALWAYS));
|
2014-07-18 10:31:13 -04:00
|
|
|
if(valid)
|
|
|
|
setValue(val);
|
2014-05-09 18:50:09 -04:00
|
|
|
else
|
2014-07-18 10:31:13 -04:00
|
|
|
clear();
|
2014-05-09 18:50:09 -04:00
|
|
|
}
|
2014-07-18 10:31:13 -04:00
|
|
|
|
2014-04-22 19:46:19 -03:00
|
|
|
void setSingleStep(const CAmount& step)
|
2014-05-09 18:50:09 -04:00
|
|
|
{
|
2014-07-18 10:31:13 -04:00
|
|
|
singleStep = step;
|
|
|
|
}
|
|
|
|
|
2020-03-14 03:49:59 -03:00
|
|
|
QSize minimumSizeHint() const override
|
2014-07-18 10:31:13 -04:00
|
|
|
{
|
|
|
|
if(cachedMinimumSizeHint.isEmpty())
|
|
|
|
{
|
|
|
|
ensurePolished();
|
|
|
|
|
|
|
|
const QFontMetrics fm(fontMetrics());
|
|
|
|
int h = lineEdit()->minimumSizeHint().height();
|
2020-06-18 08:30:15 -04:00
|
|
|
int w = GUIUtil::TextWidth(fm, BitcoinUnits::format(BitcoinUnits::BTC, BitcoinUnits::maxMoney(), false, BitcoinUnits::SeparatorStyle::ALWAYS));
|
2014-07-18 10:31:13 -04:00
|
|
|
w += 2; // cursor blinking space
|
|
|
|
|
|
|
|
QStyleOptionSpinBox opt;
|
|
|
|
initStyleOption(&opt);
|
|
|
|
QSize hint(w, h);
|
|
|
|
QSize extra(35, 6);
|
|
|
|
opt.rect.setSize(hint + extra);
|
|
|
|
extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &opt,
|
|
|
|
QStyle::SC_SpinBoxEditField, this).size();
|
|
|
|
// get closer to final result by repeating the calculation
|
|
|
|
opt.rect.setSize(hint + extra);
|
|
|
|
extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &opt,
|
|
|
|
QStyle::SC_SpinBoxEditField, this).size();
|
|
|
|
hint += extra;
|
2014-11-15 21:07:09 -03:00
|
|
|
hint.setHeight(h);
|
2014-07-18 10:31:13 -04:00
|
|
|
|
|
|
|
opt.rect = rect();
|
|
|
|
|
|
|
|
cachedMinimumSizeHint = style()->sizeFromContents(QStyle::CT_SpinBox, &opt, hint, this)
|
|
|
|
.expandedTo(QApplication::globalStrut());
|
|
|
|
}
|
|
|
|
return cachedMinimumSizeHint;
|
|
|
|
}
|
2015-01-10 11:02:12 -03:00
|
|
|
|
2014-07-18 10:31:13 -04:00
|
|
|
private:
|
2018-10-30 09:02:08 -03:00
|
|
|
int currentUnit{BitcoinUnits::BTC};
|
|
|
|
CAmount singleStep{CAmount(100000)}; // satoshis
|
2014-07-18 10:31:13 -04:00
|
|
|
mutable QSize cachedMinimumSizeHint;
|
2018-10-30 09:02:08 -03:00
|
|
|
bool m_allow_empty{true};
|
|
|
|
CAmount m_min_amount{CAmount(0)};
|
|
|
|
CAmount m_max_amount{BitcoinUnits::maxMoney()};
|
2014-07-18 10:31:13 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a string into a number of base monetary units and
|
|
|
|
* return validity.
|
|
|
|
* @note Must return 0 if !valid.
|
|
|
|
*/
|
2018-07-30 06:37:09 -04:00
|
|
|
CAmount parse(const QString &text, bool *valid_out=nullptr) const
|
2014-07-18 10:31:13 -04:00
|
|
|
{
|
2014-04-22 19:46:19 -03:00
|
|
|
CAmount val = 0;
|
2014-07-18 10:31:13 -04:00
|
|
|
bool valid = BitcoinUnits::parse(currentUnit, text, &val);
|
|
|
|
if(valid)
|
|
|
|
{
|
|
|
|
if(val < 0 || val > BitcoinUnits::maxMoney())
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
if(valid_out)
|
|
|
|
*valid_out = valid;
|
|
|
|
return valid ? val : 0;
|
2014-05-09 18:50:09 -04:00
|
|
|
}
|
2014-07-18 10:31:13 -04:00
|
|
|
|
|
|
|
protected:
|
2020-03-14 03:49:59 -03:00
|
|
|
bool event(QEvent *event) override
|
2014-05-09 18:50:09 -04:00
|
|
|
{
|
2014-07-18 10:31:13 -04:00
|
|
|
if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
|
|
|
|
{
|
|
|
|
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
|
|
|
|
if (keyEvent->key() == Qt::Key_Comma)
|
|
|
|
{
|
|
|
|
// Translate a comma into a period
|
|
|
|
QKeyEvent periodKeyEvent(event->type(), Qt::Key_Period, keyEvent->modifiers(), ".", keyEvent->isAutoRepeat(), keyEvent->count());
|
|
|
|
return QAbstractSpinBox::event(&periodKeyEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QAbstractSpinBox::event(event);
|
2014-05-09 18:50:09 -04:00
|
|
|
}
|
2014-07-18 10:31:13 -04:00
|
|
|
|
2020-03-14 03:49:59 -03:00
|
|
|
StepEnabled stepEnabled() const override
|
2015-01-10 11:02:12 -03:00
|
|
|
{
|
|
|
|
if (isReadOnly()) // Disable steps when AmountSpinBox is read-only
|
|
|
|
return StepNone;
|
2015-01-13 07:51:45 -03:00
|
|
|
if (text().isEmpty()) // Allow step-up with empty field
|
2015-01-10 11:02:12 -03:00
|
|
|
return StepUpEnabled;
|
2015-01-13 07:51:45 -03:00
|
|
|
|
2018-07-31 14:02:34 -04:00
|
|
|
StepEnabled rv = StepNone;
|
2015-01-10 11:02:12 -03:00
|
|
|
bool valid = false;
|
|
|
|
CAmount val = value(&valid);
|
2018-10-30 09:02:08 -03:00
|
|
|
if (valid) {
|
|
|
|
if (val > m_min_amount)
|
2015-01-10 11:02:12 -03:00
|
|
|
rv |= StepDownEnabled;
|
2018-10-30 09:02:08 -03:00
|
|
|
if (val < m_max_amount)
|
2015-01-10 11:02:12 -03:00
|
|
|
rv |= StepUpEnabled;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-07-14 08:59:05 -03:00
|
|
|
Q_SIGNALS:
|
2014-07-18 10:31:13 -04:00
|
|
|
void valueChanged();
|
2014-05-09 18:50:09 -04:00
|
|
|
};
|
|
|
|
|
2017-08-15 12:31:26 -03:00
|
|
|
#include <qt/bitcoinamountfield.moc>
|
2014-07-18 10:31:13 -04:00
|
|
|
|
2013-12-16 18:54:02 -03:00
|
|
|
BitcoinAmountField::BitcoinAmountField(QWidget *parent) :
|
|
|
|
QWidget(parent),
|
2018-07-31 14:02:34 -04:00
|
|
|
amount(nullptr)
|
2011-06-20 15:31:42 -04:00
|
|
|
{
|
2014-05-09 18:50:09 -04:00
|
|
|
amount = new AmountSpinBox(this);
|
2011-10-07 08:21:45 -03:00
|
|
|
amount->setLocale(QLocale::c());
|
2011-06-20 15:31:42 -04:00
|
|
|
amount->installEventFilter(this);
|
2018-05-20 01:09:16 -04:00
|
|
|
amount->setMaximumWidth(240);
|
2011-06-20 15:31:42 -04:00
|
|
|
|
|
|
|
QHBoxLayout *layout = new QHBoxLayout(this);
|
|
|
|
layout->addWidget(amount);
|
2011-07-29 08:36:35 -04:00
|
|
|
unit = new QValueComboBox(this);
|
2011-07-26 07:08:34 -04:00
|
|
|
unit->setModel(new BitcoinUnits(this));
|
|
|
|
layout->addWidget(unit);
|
2011-06-20 15:31:42 -04:00
|
|
|
layout->addStretch(1);
|
2011-06-21 01:35:59 -04:00
|
|
|
layout->setContentsMargins(0,0,0,0);
|
2011-06-20 15:31:42 -04:00
|
|
|
|
|
|
|
setLayout(layout);
|
2011-07-16 13:01:05 -04:00
|
|
|
|
|
|
|
setFocusPolicy(Qt::TabFocus);
|
2011-06-20 15:31:42 -04:00
|
|
|
setFocusProxy(amount);
|
|
|
|
|
|
|
|
// If one if the widgets changes, the combined content changes as well
|
2018-06-24 11:18:22 -04:00
|
|
|
connect(amount, &AmountSpinBox::valueChanged, this, &BitcoinAmountField::valueChanged);
|
2021-03-24 00:37:56 -03:00
|
|
|
connect(unit, qOverload<int>(&QComboBox::currentIndexChanged), this, &BitcoinAmountField::unitChanged);
|
2011-07-26 07:08:34 -04:00
|
|
|
|
2011-08-08 11:38:17 -04:00
|
|
|
// Set default based on configuration
|
2011-07-26 07:08:34 -04:00
|
|
|
unitChanged(unit->currentIndex());
|
2011-06-20 15:31:42 -04:00
|
|
|
}
|
|
|
|
|
2011-07-22 11:06:37 -04:00
|
|
|
void BitcoinAmountField::clear()
|
|
|
|
{
|
|
|
|
amount->clear();
|
2011-07-26 07:31:59 -04:00
|
|
|
unit->setCurrentIndex(0);
|
2011-07-22 11:06:37 -04:00
|
|
|
}
|
|
|
|
|
2014-11-01 20:14:47 -03:00
|
|
|
void BitcoinAmountField::setEnabled(bool fEnabled)
|
|
|
|
{
|
|
|
|
amount->setEnabled(fEnabled);
|
|
|
|
unit->setEnabled(fEnabled);
|
|
|
|
}
|
|
|
|
|
2011-07-16 13:01:05 -04:00
|
|
|
bool BitcoinAmountField::validate()
|
|
|
|
{
|
2014-07-18 10:31:13 -04:00
|
|
|
bool valid = false;
|
|
|
|
value(&valid);
|
2011-10-07 08:21:45 -03:00
|
|
|
setValid(valid);
|
2011-07-16 13:01:05 -04:00
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2011-07-26 07:08:34 -04:00
|
|
|
void BitcoinAmountField::setValid(bool valid)
|
|
|
|
{
|
2011-10-07 08:21:45 -03:00
|
|
|
if (valid)
|
|
|
|
amount->setStyleSheet("");
|
|
|
|
else
|
|
|
|
amount->setStyleSheet(STYLE_INVALID);
|
2011-07-26 07:08:34 -04:00
|
|
|
}
|
|
|
|
|
2011-06-20 15:31:42 -04:00
|
|
|
bool BitcoinAmountField::eventFilter(QObject *object, QEvent *event)
|
|
|
|
{
|
2011-10-07 08:21:45 -03:00
|
|
|
if (event->type() == QEvent::FocusIn)
|
|
|
|
{
|
|
|
|
// Clear invalid flag on focus
|
|
|
|
setValid(true);
|
|
|
|
}
|
|
|
|
return QWidget::eventFilter(object, event);
|
2011-06-20 15:31:42 -04:00
|
|
|
}
|
2011-07-16 13:01:05 -04:00
|
|
|
|
|
|
|
QWidget *BitcoinAmountField::setupTabChain(QWidget *prev)
|
|
|
|
{
|
|
|
|
QWidget::setTabOrder(prev, amount);
|
2014-01-29 10:41:41 -03:00
|
|
|
QWidget::setTabOrder(amount, unit);
|
|
|
|
return unit;
|
2011-07-16 13:01:05 -04:00
|
|
|
}
|
2011-07-26 07:08:34 -04:00
|
|
|
|
2014-04-22 19:46:19 -03:00
|
|
|
CAmount BitcoinAmountField::value(bool *valid_out) const
|
2011-07-26 07:08:34 -04:00
|
|
|
{
|
2014-07-18 10:31:13 -04:00
|
|
|
return amount->value(valid_out);
|
2011-07-26 07:08:34 -04:00
|
|
|
}
|
|
|
|
|
2014-04-22 19:46:19 -03:00
|
|
|
void BitcoinAmountField::setValue(const CAmount& value)
|
2011-07-26 07:08:34 -04:00
|
|
|
{
|
2014-07-18 10:31:13 -04:00
|
|
|
amount->setValue(value);
|
2011-07-26 07:08:34 -04:00
|
|
|
}
|
|
|
|
|
2018-10-30 09:02:08 -03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-10-15 10:26:22 -03:00
|
|
|
void BitcoinAmountField::setReadOnly(bool fReadOnly)
|
2013-07-22 02:50:39 -04:00
|
|
|
{
|
2013-10-15 10:26:22 -03:00
|
|
|
amount->setReadOnly(fReadOnly);
|
2013-07-22 02:50:39 -04:00
|
|
|
}
|
|
|
|
|
2011-07-26 07:08:34 -04:00
|
|
|
void BitcoinAmountField::unitChanged(int idx)
|
|
|
|
{
|
|
|
|
// Use description tooltip for current unit for the combobox
|
|
|
|
unit->setToolTip(unit->itemData(idx, Qt::ToolTipRole).toString());
|
|
|
|
|
|
|
|
// Determine new unit ID
|
|
|
|
int newUnit = unit->itemData(idx, BitcoinUnits::UnitRole).toInt();
|
|
|
|
|
2014-07-18 10:31:13 -04:00
|
|
|
amount->setDisplayUnit(newUnit);
|
2011-07-29 08:36:35 -04:00
|
|
|
}
|
2011-07-26 07:08:34 -04:00
|
|
|
|
2011-07-29 08:36:35 -04:00
|
|
|
void BitcoinAmountField::setDisplayUnit(int newUnit)
|
|
|
|
{
|
|
|
|
unit->setValue(newUnit);
|
2011-07-26 07:08:34 -04:00
|
|
|
}
|
2014-02-02 00:12:30 -03:00
|
|
|
|
2014-04-22 19:46:19 -03:00
|
|
|
void BitcoinAmountField::setSingleStep(const CAmount& step)
|
2014-02-02 00:12:30 -03:00
|
|
|
{
|
2014-07-18 10:31:13 -04:00
|
|
|
amount->setSingleStep(step);
|
2014-02-02 00:12:30 -03:00
|
|
|
}
|