X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fqt%2Fbitcoinamountfield.cpp;h=7248cf78d95c54fc3fe99f2a54401be275015833;hb=128b5cec3c7f20b34a40541be2236c11fe204df6;hp=a9c89334eaf2aafb2364271ee1065e30782b4340;hpb=1aafe34a0839153d7027fdd0251edc32bd8001aa;p=novacoin.git diff --git a/src/qt/bitcoinamountfield.cpp b/src/qt/bitcoinamountfield.cpp index a9c8933..7248cf7 100644 --- a/src/qt/bitcoinamountfield.cpp +++ b/src/qt/bitcoinamountfield.cpp @@ -1,34 +1,31 @@ #include "bitcoinamountfield.h" -#include "qvalidatedlineedit.h" #include "qvaluecombobox.h" #include "bitcoinunits.h" +#include "guiconstants.h" + #include #include #include #include #include +#include #include -#include +#include +#include BitcoinAmountField::BitcoinAmountField(QWidget *parent): - QWidget(parent), amount(0), decimals(0), currentUnit(-1) + QWidget(parent), amount(0), currentUnit(-1) { - amount = new QValidatedLineEdit(this); - amount->setValidator(new QRegExpValidator(QRegExp("[0-9]*"), this)); - amount->setAlignment(Qt::AlignRight|Qt::AlignVCenter); + amount = new QDoubleSpinBox(this); + amount->setLocale(QLocale::c()); + amount->setDecimals(8); amount->installEventFilter(this); - amount->setMaximumWidth(100); - decimals = new QValidatedLineEdit(this); - decimals->setValidator(new QRegExpValidator(QRegExp("[0-9]+"), this)); - decimals->setAlignment(Qt::AlignLeft|Qt::AlignVCenter); - decimals->setMaximumWidth(75); + amount->setMaximumWidth(240); + amount->setSingleStep(0.001); QHBoxLayout *layout = new QHBoxLayout(this); - layout->setSpacing(0); layout->addWidget(amount); - layout->addWidget(new QLabel(QString("."))); - layout->addWidget(decimals); unit = new QValueComboBox(this); unit->setModel(new BitcoinUnits(this)); layout->addWidget(unit); @@ -41,90 +38,81 @@ BitcoinAmountField::BitcoinAmountField(QWidget *parent): setFocusProxy(amount); // If one if the widgets changes, the combined content changes as well - connect(amount, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged())); - connect(decimals, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged())); + connect(amount, SIGNAL(valueChanged(QString)), this, SIGNAL(textChanged())); connect(unit, SIGNAL(currentIndexChanged(int)), this, SLOT(unitChanged(int))); - // TODO: set default based on configuration + // Set default based on configuration unitChanged(unit->currentIndex()); } void BitcoinAmountField::setText(const QString &text) { - const QStringList parts = text.split(QString(".")); - if(parts.size() == 2) - { - amount->setText(parts[0]); - decimals->setText(parts[1]); - } + if (text.isEmpty()) + amount->clear(); else - { - amount->setText(QString()); - decimals->setText(QString()); - } + amount->setValue(text.toDouble()); } void BitcoinAmountField::clear() { amount->clear(); - decimals->clear(); - // TODO: set default based on configuration unit->setCurrentIndex(0); } bool BitcoinAmountField::validate() { bool valid = true; - if(decimals->text().isEmpty()) - { - decimals->setValid(false); + if (amount->value() == 0.0) valid = false; - } - if(!BitcoinUnits::parse(currentUnit, text(), 0)) - { - setValid(false); + if (valid && !BitcoinUnits::parse(currentUnit, text(), 0)) valid = false; - } + + setValid(valid); return valid; } void BitcoinAmountField::setValid(bool valid) { - amount->setValid(valid); - decimals->setValid(valid); + if (valid) + amount->setStyleSheet(""); + else + amount->setStyleSheet(STYLE_INVALID); } QString BitcoinAmountField::text() const { - if(decimals->text().isEmpty() && amount->text().isEmpty()) - { + if (amount->text().isEmpty()) return QString(); - } - return amount->text() + QString(".") + decimals->text(); + else + return amount->text(); } -// Intercept '.' and ',' keys, if pressed focus a specified widget bool BitcoinAmountField::eventFilter(QObject *object, QEvent *event) { - Q_UNUSED(object); - if(event->type() == QEvent::KeyPress) + if (event->type() == QEvent::FocusIn) + { + // Clear invalid flag on focus + setValid(true); + } + else if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { QKeyEvent *keyEvent = static_cast(event); - if(keyEvent->key() == Qt::Key_Period || keyEvent->key() == Qt::Key_Comma) + if (keyEvent->key() == Qt::Key_Comma) { - decimals->setFocus(); - decimals->selectAll(); + // Translate a comma into a period + QKeyEvent periodKeyEvent(event->type(), Qt::Key_Period, keyEvent->modifiers(), ".", keyEvent->isAutoRepeat(), keyEvent->count()); + qApp->sendEvent(object, &periodKeyEvent); + return true; } } - return false; + return QWidget::eventFilter(object, event); } QWidget *BitcoinAmountField::setupTabChain(QWidget *prev) { QWidget::setTabOrder(prev, amount); - QWidget::setTabOrder(amount, decimals); - return decimals; + return amount; } qint64 BitcoinAmountField::value(bool *valid_out) const @@ -158,11 +146,12 @@ void BitcoinAmountField::unitChanged(int idx) currentUnit = newUnit; // Set max length after retrieving the value, to prevent truncation - amount->setMaxLength(BitcoinUnits::amountDigits(currentUnit)); - decimals->setMaxLength(BitcoinUnits::decimals(currentUnit)); + amount->setDecimals(BitcoinUnits::decimals(currentUnit)); + amount->setMaximum(qPow(10, BitcoinUnits::amountDigits(currentUnit)) - qPow(10, -amount->decimals())); if(valid) { + // If value was valid, re-place it in the widget with the new unit setValue(currentValue); } else