12 #include <QAbstractSpinBox> 13 #include <QApplication> 14 #include <QHBoxLayout> 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);
107 const QFontMetrics fm(fontMetrics());
108 int h = lineEdit()->minimumSizeHint().height();
116 QStyleOptionSpinBox opt;
117 initStyleOption(&opt);
120 opt.rect.setSize(hint + extra);
123 ->subControlRect(QStyle::CC_SpinBox, &opt,
124 QStyle::SC_SpinBoxEditField,
this)
127 opt.rect.setSize(hint + extra);
130 ->subControlRect(QStyle::CC_SpinBox, &opt,
131 QStyle::SC_SpinBoxEditField,
this)
140 ->sizeFromContents(QStyle::CT_SpinBox, &opt, hint,
this)
141 .expandedTo(QApplication::globalStrut());
159 Amount parse(
const QString &text,
bool *valid_out =
nullptr)
const {
175 if (event->type() == QEvent::KeyPress ||
176 event->type() == QEvent::KeyRelease) {
177 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
178 if (keyEvent->key() == Qt::Key_Comma) {
180 QKeyEvent periodKeyEvent(
181 event->type(), Qt::Key_Period, keyEvent->modifiers(),
".",
182 keyEvent->isAutoRepeat(), keyEvent->count());
183 return QAbstractSpinBox::event(&periodKeyEvent);
186 return QAbstractSpinBox::event(event);
194 if (text().isEmpty()) {
196 return StepUpEnabled;
199 StepEnabled rv = StepNone;
204 rv |= StepDownEnabled;
217 #include <qt/bitcoinamountfield.moc> 220 : QWidget(parent), amount(nullptr) {
222 amount->setLocale(QLocale::c());
223 amount->installEventFilter(
this);
224 amount->setMaximumWidth(240);
226 QHBoxLayout *layout =
new QHBoxLayout(
this);
227 layout->addWidget(
amount);
230 layout->addWidget(
unit);
231 layout->addStretch(1);
232 layout->setContentsMargins(0, 0, 0, 0);
236 setFocusPolicy(Qt::TabFocus);
244 static_cast<void (QComboBox::*)(
int)
>(&QComboBox::currentIndexChanged),
253 unit->setCurrentIndex(0);
257 amount->setEnabled(fEnabled);
258 unit->setEnabled(fEnabled);
270 amount->setStyleSheet(
"");
277 if (event->type() == QEvent::FocusIn) {
281 return QWidget::eventFilter(
object, event);
285 QWidget::setTabOrder(prev,
amount);
310 amount->setReadOnly(fReadOnly);
315 unit->setToolTip(
unit->itemData(idx, Qt::ToolTipRole).toString());
bool validate()
Perform input validation, mark field as invalid if entered value is not valid.
Bitcoin unit definitions.
static constexpr Amount zero()
void setReadOnly(bool fReadOnly)
Make read-only.
QSpinBox that uses fixed-point numbers internally and uses our own formatting/parsing functions...
int TextWidth(const QFontMetrics &fm, const QString &text)
Returns the distance in pixels appropriate for drawing a subsequent character after text...
void SetAllowEmpty(bool allow)
QSize minimumSizeHint() const override
AmountSpinBox(QWidget *parent)
QWidget * setupTabChain(QWidget *prev)
Qt messes up the tab chain by default in some cases (issue https://bugreports.qt-project.org/browse/QTBUG-10907), in these cases we have to set it up manually.
void setSingleStep(const Amount step)
Set single step in satoshis.
static constexpr Amount SATOSHI
StepEnabled stepEnabled() const override
void setDisplayUnit(int unit)
void fixup(QString &input) const override
QSize cachedMinimumSizeHint
BitcoinAmountField(QWidget *parent=nullptr)
QValidator::State validate(QString &text, int &pos) const override
void setEnabled(bool fEnabled)
Enable/Disable.
bool event(QEvent *event) override
void unitChanged(int idx)
bool eventFilter(QObject *object, QEvent *event) override
Intercept focus-in event and ',' key presses.
Amount value(bool *valid_out=nullptr) const
static Amount maxMoney()
Return maximum number of base units (Satoshis)
static QString format(int unit, const Amount amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string.
void setValue(const Amount value)
void SetMaxValue(const Amount &value)
void setValue(const Amount value)
void setValue(const QVariant &value)
void SetMinValue(const Amount &value)
Set the minimum value in satoshis.
void clear()
Make field empty and ready for new input.
void setValid(bool valid)
Mark current value as invalid in UI.
void SetMinValue(const Amount &value)
Amount parse(const QString &text, bool *valid_out=nullptr) const
Parse a string into a number of base monetary units and return validity.
void setSingleStep(const Amount step)
void stepBy(int steps) override
static bool parse(int unit, const QString &value, Amount *val_out)
Parse string to coin amount.
void setDisplayUnit(int unit)
Change unit used to display amount.
void SetMaxValue(const Amount &value)
Set the maximum value in satoshis.
void SetAllowEmpty(bool allow)
If allow empty is set to false the field will be set to the minimum allowed value if left empty...