12 #include <QApplication> 13 #include <QAbstractSpinBox> 14 #include <QHBoxLayout> 30 QAbstractSpinBox(parent)
32 setAlignment(Qt::AlignRight);
40 return QValidator::Intermediate;
44 return valid ? QValidator::Intermediate : QValidator::Invalid;
47 void fixup(QString &input)
const override 57 val =
parse(input, &valid);
63 lineEdit()->setText(input);
69 return parse(text(), valid_out);
126 const QFontMetrics fm(fontMetrics());
127 int h = lineEdit()->minimumSizeHint().height();
131 QStyleOptionSpinBox opt;
132 initStyleOption(&opt);
135 opt.rect.setSize(hint + extra);
136 extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &opt,
137 QStyle::SC_SpinBoxEditField,
this).size();
139 opt.rect.setSize(hint + extra);
140 extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &opt,
141 QStyle::SC_SpinBoxEditField,
this).size();
176 return valid ? val : 0;
182 if (
event->type() == QEvent::KeyPress ||
event->type() == QEvent::KeyRelease)
184 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
185 if (keyEvent->key() == Qt::Key_Comma)
188 QKeyEvent periodKeyEvent(
event->type(), Qt::Key_Period, keyEvent->modifiers(),
".", keyEvent->isAutoRepeat(), keyEvent->count());
189 return QAbstractSpinBox::event(&periodKeyEvent);
192 return QAbstractSpinBox::event(
event);
199 if (text().isEmpty())
200 return StepUpEnabled;
202 StepEnabled rv = StepNone;
207 rv |= StepDownEnabled;
218 #include <qt/bitcoinamountfield.moc> 224 amount->setLocale(QLocale::c());
225 amount->installEventFilter(
this);
226 amount->setMaximumWidth(240);
228 QHBoxLayout *layout =
new QHBoxLayout(
this);
229 layout->addWidget(
amount);
232 layout->addWidget(
unit);
233 layout->addStretch(1);
234 layout->setContentsMargins(0,0,0,0);
238 setFocusPolicy(Qt::TabFocus);
252 unit->setCurrentIndex(0);
257 amount->setEnabled(fEnabled);
258 unit->setEnabled(fEnabled);
272 amount->setStyleSheet(
"");
279 if (event->type() == QEvent::FocusIn)
284 return QWidget::eventFilter(
object, event);
289 QWidget::setTabOrder(prev,
amount);
321 amount->setReadOnly(fReadOnly);
327 unit->setToolTip(
unit->itemData(idx, Qt::ToolTipRole).toString());
331 assert(new_unit.isValid());
bool validate()
Perform input validation, mark field as invalid if entered value is not valid.
Bitcoin unit definitions.
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 setDisplayUnit(BitcoinUnit unit)
StepEnabled stepEnabled() const override
static bool parse(Unit unit, const QString &value, CAmount *val_out)
Parse string to coin amount.
State
The various states a (txhash,peer) pair can be in.
CAmount value(bool *valid_out=nullptr) const
void fixup(QString &input) const override
QSize cachedMinimumSizeHint
BitcoinAmountField(QWidget *parent=nullptr)
QValidator::State validate(QString &text, int &pos) const override
void setValue(const CAmount &value)
int64_t CAmount
Amount in satoshis (Can be negative)
void setEnabled(bool fEnabled)
Enable/Disable.
static QString format(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
bool event(QEvent *event) override
void unitChanged(int idx)
void SetMinValue(const CAmount &value)
Set the minimum value in satoshis.
bool eventFilter(QObject *object, QEvent *event) override
Intercept focus-in event and ',' key presses.
void setSingleStep(const CAmount &step)
Set single step in satoshis.
void setValue(const QVariant &value)
void SetMaxValue(const CAmount &value)
void clear()
Make field empty and ready for new input.
void setValid(bool valid)
Mark current value as invalid in UI.
void setSingleStep(const CAmount &step)
void SetMaxValue(const CAmount &value)
Set the maximum value in satoshis.
void SetMinValue(const CAmount &value)
void stepBy(int steps) override
CAmount parse(const QString &text, bool *valid_out=nullptr) const
Parse a string into a number of base monetary units and return validity.
void setDisplayUnit(BitcoinUnit new_unit)
Change unit used to display amount.
void setValue(const CAmount &value)
static CAmount maxMoney()
Return maximum number of base units (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...