9 #include <QDoubleSpinBox>
10 #include <QApplication>
14 QWidget(parent), amount(0), currentUnit(-1)
16 amount =
new QDoubleSpinBox(
this);
17 amount->setLocale(QLocale::c());
19 amount->installEventFilter(
this);
20 amount->setMaximumWidth(170);
21 amount->setSingleStep(0.001);
23 QHBoxLayout *layout =
new QHBoxLayout(
this);
27 layout->addWidget(
unit);
28 layout->addStretch(1);
29 layout->setContentsMargins(0,0,0,0);
33 setFocusPolicy(Qt::TabFocus);
38 connect(
unit, SIGNAL(currentIndexChanged(
int)),
this, SLOT(
unitChanged(
int)));
49 amount->setValue(text.toDouble());
55 unit->setCurrentIndex(0);
61 if (
amount->value() == 0.0)
81 if (
amount->text().isEmpty())
89 if (event->type() == QEvent::FocusIn)
94 else if (event->type() == QEvent::KeyPress ||
event->type() == QEvent::KeyRelease)
96 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(event);
97 if (keyEvent->key() == Qt::Key_Comma)
100 QKeyEvent periodKeyEvent(event->type(), Qt::Key_Period, keyEvent->modifiers(),
".", keyEvent->isAutoRepeat(), keyEvent->count());
101 QApplication::sendEvent(
object, &periodKeyEvent);
105 return QWidget::eventFilter(
object, event);
110 QWidget::setTabOrder(prev,
amount);
133 unit->setToolTip(
unit->itemData(idx, Qt::ToolTipRole).toString());
140 qint64 currentValue =
value(&valid);
149 amount->setSingleStep(0.01);
151 amount->setSingleStep(0.001);
bool validate()
Perform input validation, mark field as invalid if entered value is not valid.
Bitcoin unit definitions.
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)
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 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.