12#include <QAbstractSpinBox>
13#include <QApplication>
27 setAlignment(Qt::AlignRight);
29 connect(lineEdit(), &QLineEdit::textEdited,
this,
33 QValidator::State
validate(QString &text,
int &pos)
const override {
35 return QValidator::Intermediate;
41 return valid ? QValidator::Intermediate : QValidator::Invalid;
44 void fixup(QString &input)
const override {
53 val =
parse(input, &valid);
60 lineEdit()->setText(input);
65 return parse(text(), valid_out);
92 lineEdit()->setPlaceholderText(
108 const QFontMetrics fm(fontMetrics());
109 int h = lineEdit()->minimumSizeHint().height();
117 QStyleOptionSpinBox opt;
118 initStyleOption(&opt);
121 opt.rect.setSize(hint + extra);
124 ->subControlRect(QStyle::CC_SpinBox, &opt,
125 QStyle::SC_SpinBoxEditField,
this)
128 opt.rect.setSize(hint + extra);
131 ->subControlRect(QStyle::CC_SpinBox, &opt,
132 QStyle::SC_SpinBoxEditField,
this)
140 style()->sizeFromContents(QStyle::CT_SpinBox, &opt, hint,
this);
158 Amount parse(
const QString &text,
bool *valid_out =
nullptr)
const {
174 if (
event->type() == QEvent::KeyPress ||
175 event->type() == QEvent::KeyRelease) {
176 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
177 if (keyEvent->key() == Qt::Key_Comma) {
179 QKeyEvent periodKeyEvent(
180 event->type(), Qt::Key_Period, keyEvent->modifiers(),
".",
181 keyEvent->isAutoRepeat(), keyEvent->count());
182 return QAbstractSpinBox::event(&periodKeyEvent);
185 return QAbstractSpinBox::event(
event);
193 if (text().isEmpty()) {
195 return StepUpEnabled;
198 StepEnabled rv = StepNone;
203 rv |= StepDownEnabled;
216#include <qt/bitcoinamountfield.moc>
219 : QWidget(parent), amount(nullptr) {
221 amount->setLocale(QLocale::c());
222 amount->installEventFilter(
this);
223 amount->setMaximumWidth(240);
225 QHBoxLayout *layout =
new QHBoxLayout(
this);
226 layout->addWidget(
amount);
229 layout->addWidget(
unit);
230 layout->addStretch(1);
231 layout->setContentsMargins(0, 0, 0, 0);
235 setFocusPolicy(Qt::TabFocus);
243 static_cast<void (QComboBox::*)(
int)
>(&QComboBox::currentIndexChanged),
252 unit->setCurrentIndex(0);
256 amount->setEnabled(fEnabled);
257 unit->setEnabled(fEnabled);
269 amount->setStyleSheet(
"");
276 if (event->type() == QEvent::FocusIn) {
280 return QWidget::eventFilter(
object, event);
284 QWidget::setTabOrder(prev,
amount);
309 amount->setReadOnly(fReadOnly);
314 unit->setToolTip(
unit->itemData(idx, Qt::ToolTipRole).toString());
static constexpr Amount SATOSHI
QSpinBox that uses fixed-point numbers internally and uses our own formatting/parsing functions.
void SetMinValue(const Amount &value)
bool event(QEvent *event) override
void setValue(const Amount value)
AmountSpinBox(QWidget *parent)
QValidator::State validate(QString &text, int &pos) const override
StepEnabled stepEnabled() const override
QSize cachedMinimumSizeHint
void fixup(QString &input) const override
void setSingleStep(const Amount step)
void stepBy(int steps) override
QSize minimumSizeHint() const override
Amount value(bool *valid_out=nullptr) const
void SetAllowEmpty(bool allow)
void SetMaxValue(const Amount &value)
void setDisplayUnit(int unit)
Amount parse(const QString &text, bool *valid_out=nullptr) const
Parse a string into a number of base monetary units and return validity.
void setEnabled(bool fEnabled)
Enable/Disable.
void SetMaxValue(const Amount &value)
Set the maximum value in satoshis.
bool eventFilter(QObject *object, QEvent *event) override
Intercept focus-in event and ',' key presses.
void setReadOnly(bool fReadOnly)
Make read-only.
BitcoinAmountField(QWidget *parent=nullptr)
void unitChanged(int idx)
void setDisplayUnit(int unit)
Change unit used to display amount.
void clear()
Make field empty and ready for new input.
bool validate()
Perform input validation, mark field as invalid if entered value is not valid.
QWidget * setupTabChain(QWidget *prev)
Qt messes up the tab chain by default in some cases (issue https://bugreports.qt-project....
void setValue(const Amount value)
void setValid(bool valid)
Mark current value as invalid in UI.
void SetAllowEmpty(bool allow)
If allow empty is set to false the field will be set to the minimum allowed value if left empty.
void setSingleStep(const Amount step)
Set single step in satoshis.
void SetMinValue(const Amount &value)
Set the minimum value in satoshis.
Bitcoin unit definitions.
@ UnitRole
Unit identifier.
static Amount maxMoney()
Return maximum number of base units (Satoshis)
static QString format(int unit, const Amount amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
static bool parse(int unit, const QString &value, Amount *val_out)
Parse string to coin amount.
void setValue(const QVariant &value)
int TextWidth(const QFontMetrics &fm, const QString &text)
Returns the distance in pixels appropriate for drawing a subsequent character after text.
static constexpr Amount zero() noexcept