11 #include <QApplication>
12 #include <QDoubleSpinBox>
13 #include <QHBoxLayout>
24 amount =
new QDoubleSpinBox(
this);
25 amount->setLocale(QLocale::c());
26 amount->installEventFilter(
this);
27 amount->setMaximumWidth(170);
29 QHBoxLayout *layout =
new QHBoxLayout(
this);
33 layout->addWidget(
unit);
34 layout->addStretch(1);
35 layout->setContentsMargins(0,0,0,0);
39 setFocusPolicy(Qt::TabFocus);
44 connect(
unit, SIGNAL(currentIndexChanged(
int)),
this, SLOT(
unitChanged(
int)));
55 amount->setValue(text.toDouble());
61 unit->setCurrentIndex(0);
67 if (
amount->value() == 0.0)
89 if (
amount->text().isEmpty())
97 if (event->type() == QEvent::FocusIn)
102 else if (event->type() == QEvent::KeyPress ||
event->type() == QEvent::KeyRelease)
104 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(event);
105 if (keyEvent->key() == Qt::Key_Comma)
108 QKeyEvent periodKeyEvent(event->type(), Qt::Key_Period, keyEvent->modifiers(),
".", keyEvent->isAutoRepeat(), keyEvent->count());
109 QApplication::sendEvent(
object, &periodKeyEvent);
113 return QWidget::eventFilter(
object, event);
118 QWidget::setTabOrder(prev,
amount);
141 amount->setReadOnly(fReadOnly);
142 unit->setEnabled(!fReadOnly);
148 unit->setToolTip(
unit->itemData(idx, Qt::ToolTipRole).toString());
155 qint64 currentValue =
value(&valid);
static qint64 maxAmount(int unit)
Max amount per unit.
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.
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.
bool eventFilter(QObject *object, QEvent *event)
Intercept focus-in event and ',' key presses.
BitcoinAmountField(QWidget *parent=0)
static qint64 factor(int unit)
Number of Satoshis (1e-8) per unit.
void unitChanged(int idx)
void setValue(const QVariant &value)
void clear()
Make field empty and ready for new input.
static int amountDigits(int unit)
Number of amount digits (to represent max number of coins)
static bool parse(int unit, const QString &value, qint64 *val_out)
Parse string to coin amount.
void setSingleStep(qint64 step)
Set single step in satoshis.
void setValid(bool valid)
Mark current value as invalid in UI.
void setText(const QString &text)
void setDisplayUnit(int unit)
Change unit used to display amount.
void setValue(qint64 value)
static QString format(int unit, qint64 amount, bool plussign=false)
Format as string.
static int decimals(int unit)
Number of decimals left.