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)
141 ->sizeFromContents(QStyle::CT_SpinBox, &opt, hint,
this)
142 .expandedTo(QApplication::globalStrut());
160 Amount parse(
const QString &text,
bool *valid_out =
nullptr)
const {
176 if (
event->type() == QEvent::KeyPress ||
177 event->type() == QEvent::KeyRelease) {
178 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
179 if (keyEvent->key() == Qt::Key_Comma) {
181 QKeyEvent periodKeyEvent(
182 event->type(), Qt::Key_Period, keyEvent->modifiers(),
".",
183 keyEvent->isAutoRepeat(), keyEvent->count());
184 return QAbstractSpinBox::event(&periodKeyEvent);
187 return QAbstractSpinBox::event(
event);
195 if (text().isEmpty()) {
197 return StepUpEnabled;
200 StepEnabled rv = StepNone;
205 rv |= StepDownEnabled;
218#include <qt/bitcoinamountfield.moc>
221 : QWidget(parent), amount(nullptr) {
223 amount->setLocale(QLocale::c());
224 amount->installEventFilter(
this);
225 amount->setMaximumWidth(240);
227 QHBoxLayout *layout =
new QHBoxLayout(
this);
228 layout->addWidget(
amount);
231 layout->addWidget(
unit);
232 layout->addStretch(1);
233 layout->setContentsMargins(0, 0, 0, 0);
237 setFocusPolicy(Qt::TabFocus);
245 static_cast<void (QComboBox::*)(
int)
>(&QComboBox::currentIndexChanged),
254 unit->setCurrentIndex(0);
258 amount->setEnabled(fEnabled);
259 unit->setEnabled(fEnabled);
271 amount->setStyleSheet(
"");
278 if (event->type() == QEvent::FocusIn) {
282 return QWidget::eventFilter(
object, event);
286 QWidget::setTabOrder(prev,
amount);
311 amount->setReadOnly(fReadOnly);
316 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