2014-02-08 22:50:24 +01:00
|
|
|
// Copyright (c) 2011-2014 The Bitcoin developers
|
2013-11-04 16:20:43 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2011-06-20 21:31:42 +02:00
|
|
|
#include "bitcoinamountfield.h"
|
2013-01-23 21:51:02 +01:00
|
|
|
|
2011-07-25 21:35:45 +02:00
|
|
|
#include "bitcoinunits.h"
|
2011-10-07 13:21:45 +02:00
|
|
|
#include "guiconstants.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "qvaluecombobox.h"
|
2011-10-07 13:21:45 +02:00
|
|
|
|
2013-07-23 08:52:24 +02:00
|
|
|
#include <QApplication>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <QDoubleSpinBox>
|
2011-06-20 21:31:42 +02:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QKeyEvent>
|
2013-01-23 21:51:02 +01:00
|
|
|
#include <qmath.h> // for qPow()
|
2011-06-20 21:31:42 +02:00
|
|
|
|
2013-12-16 22:54:02 +01:00
|
|
|
BitcoinAmountField::BitcoinAmountField(QWidget *parent) :
|
|
|
|
QWidget(parent),
|
|
|
|
amount(0),
|
|
|
|
currentUnit(-1)
|
2011-06-20 21:31:42 +02:00
|
|
|
{
|
2014-02-02 04:12:30 +01:00
|
|
|
nSingleStep = 100000; // satoshis
|
|
|
|
|
2011-10-07 13:21:45 +02:00
|
|
|
amount = new QDoubleSpinBox(this);
|
|
|
|
amount->setLocale(QLocale::c());
|
2011-06-20 21:31:42 +02:00
|
|
|
amount->installEventFilter(this);
|
2011-10-07 13:21:45 +02:00
|
|
|
amount->setMaximumWidth(170);
|
2011-06-20 21:31:42 +02:00
|
|
|
|
|
|
|
QHBoxLayout *layout = new QHBoxLayout(this);
|
|
|
|
layout->addWidget(amount);
|
2011-07-29 14:36:35 +02:00
|
|
|
unit = new QValueComboBox(this);
|
2011-07-26 13:08:34 +02:00
|
|
|
unit->setModel(new BitcoinUnits(this));
|
|
|
|
layout->addWidget(unit);
|
2011-06-20 21:31:42 +02:00
|
|
|
layout->addStretch(1);
|
2011-06-21 07:35:59 +02:00
|
|
|
layout->setContentsMargins(0,0,0,0);
|
2011-06-20 21:31:42 +02:00
|
|
|
|
|
|
|
setLayout(layout);
|
2011-07-16 19:01:05 +02:00
|
|
|
|
|
|
|
setFocusPolicy(Qt::TabFocus);
|
2011-06-20 21:31:42 +02:00
|
|
|
setFocusProxy(amount);
|
|
|
|
|
|
|
|
// If one if the widgets changes, the combined content changes as well
|
2011-10-07 13:21:45 +02:00
|
|
|
connect(amount, SIGNAL(valueChanged(QString)), this, SIGNAL(textChanged()));
|
2011-07-26 13:08:34 +02:00
|
|
|
connect(unit, SIGNAL(currentIndexChanged(int)), this, SLOT(unitChanged(int)));
|
|
|
|
|
2011-08-08 17:38:17 +02:00
|
|
|
// Set default based on configuration
|
2011-07-26 13:08:34 +02:00
|
|
|
unitChanged(unit->currentIndex());
|
2011-06-20 21:31:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BitcoinAmountField::setText(const QString &text)
|
|
|
|
{
|
2011-10-07 13:21:45 +02:00
|
|
|
if (text.isEmpty())
|
|
|
|
amount->clear();
|
2011-07-07 17:33:15 +02:00
|
|
|
else
|
2011-10-07 13:21:45 +02:00
|
|
|
amount->setValue(text.toDouble());
|
2011-06-20 21:31:42 +02:00
|
|
|
}
|
|
|
|
|
2011-07-22 17:06:37 +02:00
|
|
|
void BitcoinAmountField::clear()
|
|
|
|
{
|
|
|
|
amount->clear();
|
2011-07-26 13:31:59 +02:00
|
|
|
unit->setCurrentIndex(0);
|
2011-07-22 17:06:37 +02:00
|
|
|
}
|
|
|
|
|
2011-07-16 19:01:05 +02:00
|
|
|
bool BitcoinAmountField::validate()
|
|
|
|
{
|
|
|
|
bool valid = true;
|
2011-10-07 13:21:45 +02:00
|
|
|
if (amount->value() == 0.0)
|
2011-07-16 19:01:05 +02:00
|
|
|
valid = false;
|
2013-05-22 00:19:18 +02:00
|
|
|
else if (!BitcoinUnits::parse(currentUnit, text(), 0))
|
|
|
|
valid = false;
|
|
|
|
else if (amount->value() > BitcoinUnits::maxAmount(currentUnit))
|
2011-07-25 21:35:45 +02:00
|
|
|
valid = false;
|
2011-10-07 13:21:45 +02:00
|
|
|
|
|
|
|
setValid(valid);
|
2011-07-25 21:35:45 +02:00
|
|
|
|
2011-07-16 19:01:05 +02:00
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
void BitcoinAmountField::setValid(bool valid)
|
|
|
|
{
|
2011-10-07 13:21:45 +02:00
|
|
|
if (valid)
|
|
|
|
amount->setStyleSheet("");
|
|
|
|
else
|
|
|
|
amount->setStyleSheet(STYLE_INVALID);
|
2011-07-26 13:08:34 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 21:31:42 +02:00
|
|
|
QString BitcoinAmountField::text() const
|
|
|
|
{
|
2011-10-07 13:21:45 +02:00
|
|
|
if (amount->text().isEmpty())
|
2011-06-30 17:32:19 +02:00
|
|
|
return QString();
|
2011-10-07 13:21:45 +02:00
|
|
|
else
|
|
|
|
return amount->text();
|
2011-06-20 21:31:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BitcoinAmountField::eventFilter(QObject *object, QEvent *event)
|
|
|
|
{
|
2011-10-07 13:21:45 +02:00
|
|
|
if (event->type() == QEvent::FocusIn)
|
|
|
|
{
|
|
|
|
// Clear invalid flag on focus
|
|
|
|
setValid(true);
|
|
|
|
}
|
|
|
|
else if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
|
2011-06-20 21:31:42 +02:00
|
|
|
{
|
|
|
|
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
|
2011-10-07 13:21:45 +02:00
|
|
|
if (keyEvent->key() == Qt::Key_Comma)
|
2011-06-20 21:31:42 +02:00
|
|
|
{
|
2011-10-07 13:21:45 +02:00
|
|
|
// Translate a comma into a period
|
|
|
|
QKeyEvent periodKeyEvent(event->type(), Qt::Key_Period, keyEvent->modifiers(), ".", keyEvent->isAutoRepeat(), keyEvent->count());
|
2013-04-02 17:30:14 +02:00
|
|
|
QApplication::sendEvent(object, &periodKeyEvent);
|
2011-10-07 13:21:45 +02:00
|
|
|
return true;
|
2011-06-20 21:31:42 +02:00
|
|
|
}
|
|
|
|
}
|
2011-10-07 13:21:45 +02:00
|
|
|
return QWidget::eventFilter(object, event);
|
2011-06-20 21:31:42 +02:00
|
|
|
}
|
2011-07-16 19:01:05 +02:00
|
|
|
|
|
|
|
QWidget *BitcoinAmountField::setupTabChain(QWidget *prev)
|
|
|
|
{
|
|
|
|
QWidget::setTabOrder(prev, amount);
|
2014-01-29 14:41:41 +01:00
|
|
|
QWidget::setTabOrder(amount, unit);
|
|
|
|
return unit;
|
2011-07-16 19:01:05 +02:00
|
|
|
}
|
2011-07-26 13:08:34 +02:00
|
|
|
|
|
|
|
qint64 BitcoinAmountField::value(bool *valid_out) const
|
|
|
|
{
|
|
|
|
qint64 val_out = 0;
|
|
|
|
bool valid = BitcoinUnits::parse(currentUnit, text(), &val_out);
|
2013-05-22 00:19:18 +02:00
|
|
|
if (valid_out)
|
2011-07-26 13:08:34 +02:00
|
|
|
{
|
|
|
|
*valid_out = valid;
|
|
|
|
}
|
|
|
|
return val_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitcoinAmountField::setValue(qint64 value)
|
|
|
|
{
|
|
|
|
setText(BitcoinUnits::format(currentUnit, value));
|
|
|
|
}
|
|
|
|
|
2013-10-15 15:26:22 +02:00
|
|
|
void BitcoinAmountField::setReadOnly(bool fReadOnly)
|
2013-07-22 08:50:39 +02:00
|
|
|
{
|
2013-10-15 15:26:22 +02:00
|
|
|
amount->setReadOnly(fReadOnly);
|
|
|
|
unit->setEnabled(!fReadOnly);
|
2013-07-22 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
void BitcoinAmountField::unitChanged(int idx)
|
|
|
|
{
|
|
|
|
// Use description tooltip for current unit for the combobox
|
|
|
|
unit->setToolTip(unit->itemData(idx, Qt::ToolTipRole).toString());
|
|
|
|
|
|
|
|
// Determine new unit ID
|
|
|
|
int newUnit = unit->itemData(idx, BitcoinUnits::UnitRole).toInt();
|
|
|
|
|
|
|
|
// Parse current value and convert to new unit
|
|
|
|
bool valid = false;
|
|
|
|
qint64 currentValue = value(&valid);
|
|
|
|
|
|
|
|
currentUnit = newUnit;
|
|
|
|
|
|
|
|
// Set max length after retrieving the value, to prevent truncation
|
2011-10-07 13:21:45 +02:00
|
|
|
amount->setDecimals(BitcoinUnits::decimals(currentUnit));
|
|
|
|
amount->setMaximum(qPow(10, BitcoinUnits::amountDigits(currentUnit)) - qPow(10, -amount->decimals()));
|
2014-02-02 04:12:30 +01:00
|
|
|
amount->setSingleStep((double)nSingleStep / (double)BitcoinUnits::factor(currentUnit));
|
2013-03-23 05:20:51 +01:00
|
|
|
|
2013-05-22 00:19:18 +02:00
|
|
|
if (valid)
|
2011-07-26 13:08:34 +02:00
|
|
|
{
|
2011-08-31 16:08:31 +02:00
|
|
|
// If value was valid, re-place it in the widget with the new unit
|
2011-07-26 13:08:34 +02:00
|
|
|
setValue(currentValue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If current value is invalid, just clear field
|
|
|
|
setText("");
|
|
|
|
}
|
|
|
|
setValid(true);
|
2011-07-29 14:36:35 +02:00
|
|
|
}
|
2011-07-26 13:08:34 +02:00
|
|
|
|
2011-07-29 14:36:35 +02:00
|
|
|
void BitcoinAmountField::setDisplayUnit(int newUnit)
|
|
|
|
{
|
|
|
|
unit->setValue(newUnit);
|
2011-07-26 13:08:34 +02:00
|
|
|
}
|
2014-02-02 04:12:30 +01:00
|
|
|
|
|
|
|
void BitcoinAmountField::setSingleStep(qint64 step)
|
|
|
|
{
|
|
|
|
nSingleStep = step;
|
|
|
|
unitChanged(unit->currentIndex());
|
|
|
|
}
|