Changeset View
Changeset View
Standalone View
Standalone View
src/qt/bitcoinamountfield.cpp
Show All 25 Lines | explicit AmountSpinBox(QWidget *parent) | ||||
: QAbstractSpinBox(parent), currentUnit(BitcoinUnits::BCH), | : QAbstractSpinBox(parent), currentUnit(BitcoinUnits::BCH), | ||||
singleStep(100000 /* satoshis */) { | singleStep(100000 /* satoshis */) { | ||||
setAlignment(Qt::AlignRight); | setAlignment(Qt::AlignRight); | ||||
connect(lineEdit(), SIGNAL(textEdited(QString)), this, | connect(lineEdit(), SIGNAL(textEdited(QString)), this, | ||||
SIGNAL(valueChanged())); | SIGNAL(valueChanged())); | ||||
} | } | ||||
QValidator::State validate(QString &text, int &pos) const { | QValidator::State validate(QString &text, int &pos) const override { | ||||
if (text.isEmpty()) return QValidator::Intermediate; | if (text.isEmpty()) { | ||||
return QValidator::Intermediate; | |||||
} | |||||
bool valid = false; | bool valid = false; | ||||
parse(text, &valid); | parse(text, &valid); | ||||
// Make sure we return Intermediate so that fixup() is called on | // Make sure we return Intermediate so that fixup() is called on | ||||
// defocus. | // defocus. | ||||
return valid ? QValidator::Intermediate : QValidator::Invalid; | return valid ? QValidator::Intermediate : QValidator::Invalid; | ||||
} | } | ||||
void fixup(QString &input) const { | void fixup(QString &input) const override { | ||||
bool valid = false; | bool valid = false; | ||||
CAmount val = parse(input, &valid); | CAmount val = parse(input, &valid); | ||||
if (valid) { | if (valid) { | ||||
input = BitcoinUnits::format(currentUnit, val, false, | input = BitcoinUnits::format(currentUnit, val, false, | ||||
BitcoinUnits::separatorAlways); | BitcoinUnits::separatorAlways); | ||||
lineEdit()->setText(input); | lineEdit()->setText(input); | ||||
} | } | ||||
} | } | ||||
CAmount value(bool *valid_out = 0) const { | CAmount value(bool *valid_out = 0) const { | ||||
return parse(text(), valid_out); | return parse(text(), valid_out); | ||||
} | } | ||||
void setValue(const CAmount &value) { | void setValue(const CAmount &value) { | ||||
lineEdit()->setText(BitcoinUnits::format( | lineEdit()->setText(BitcoinUnits::format( | ||||
currentUnit, value, false, BitcoinUnits::separatorAlways)); | currentUnit, value, false, BitcoinUnits::separatorAlways)); | ||||
Q_EMIT valueChanged(); | Q_EMIT valueChanged(); | ||||
} | } | ||||
void stepBy(int steps) { | void stepBy(int steps) override { | ||||
bool valid = false; | bool valid = false; | ||||
Amount val(value(&valid)); | Amount val(value(&valid)); | ||||
val = val + steps * singleStep; | val = val + steps * singleStep; | ||||
val = qMin(qMax(val, Amount(0)), BitcoinUnits::maxMoney()); | val = qMin(qMax(val, Amount(0)), BitcoinUnits::maxMoney()); | ||||
setValue(val.GetSatoshis()); | setValue(val.GetSatoshis()); | ||||
} | } | ||||
void setDisplayUnit(int unit) { | void setDisplayUnit(int unit) { | ||||
bool valid = false; | bool valid = false; | ||||
Amount val(value(&valid)); | Amount val(value(&valid)); | ||||
currentUnit = unit; | currentUnit = unit; | ||||
if (valid) | if (valid) { | ||||
setValue(val.GetSatoshis()); | setValue(val.GetSatoshis()); | ||||
else | } else { | ||||
clear(); | clear(); | ||||
} | } | ||||
} | |||||
void setSingleStep(const CAmount &step) { singleStep = step; } | void setSingleStep(const CAmount &step) { singleStep = step; } | ||||
QSize minimumSizeHint() const { | QSize minimumSizeHint() const override { | ||||
if (cachedMinimumSizeHint.isEmpty()) { | if (cachedMinimumSizeHint.isEmpty()) { | ||||
ensurePolished(); | ensurePolished(); | ||||
const QFontMetrics fm(fontMetrics()); | const QFontMetrics fm(fontMetrics()); | ||||
int h = lineEdit()->minimumSizeHint().height(); | int h = lineEdit()->minimumSizeHint().height(); | ||||
int w = fm.width(BitcoinUnits::format( | int w = fm.width(BitcoinUnits::format( | ||||
BitcoinUnits::BCH, BitcoinUnits::maxMoney(), false, | BitcoinUnits::BCH, BitcoinUnits::maxMoney(), false, | ||||
BitcoinUnits::separatorAlways)); | BitcoinUnits::separatorAlways)); | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | CAmount parse(const QString &text, bool *valid_out = 0) const { | ||||
} | } | ||||
if (valid_out) { | if (valid_out) { | ||||
*valid_out = valid; | *valid_out = valid; | ||||
} | } | ||||
return valid ? val.GetSatoshis() : 0; | return valid ? val.GetSatoshis() : 0; | ||||
} | } | ||||
protected: | protected: | ||||
bool event(QEvent *event) { | bool event(QEvent *event) override { | ||||
if (event->type() == QEvent::KeyPress || | if (event->type() == QEvent::KeyPress || | ||||
event->type() == QEvent::KeyRelease) { | event->type() == QEvent::KeyRelease) { | ||||
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); | QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); | ||||
if (keyEvent->key() == Qt::Key_Comma) { | if (keyEvent->key() == Qt::Key_Comma) { | ||||
// Translate a comma into a period. | // Translate a comma into a period. | ||||
QKeyEvent periodKeyEvent( | QKeyEvent periodKeyEvent( | ||||
event->type(), Qt::Key_Period, keyEvent->modifiers(), ".", | event->type(), Qt::Key_Period, keyEvent->modifiers(), ".", | ||||
keyEvent->isAutoRepeat(), keyEvent->count()); | keyEvent->isAutoRepeat(), keyEvent->count()); | ||||
return QAbstractSpinBox::event(&periodKeyEvent); | return QAbstractSpinBox::event(&periodKeyEvent); | ||||
} | } | ||||
} | } | ||||
return QAbstractSpinBox::event(event); | return QAbstractSpinBox::event(event); | ||||
} | } | ||||
StepEnabled stepEnabled() const { | StepEnabled stepEnabled() const override { | ||||
if (isReadOnly()) { | if (isReadOnly()) { | ||||
// Disable steps when AmountSpinBox is read-only. | // Disable steps when AmountSpinBox is read-only. | ||||
return StepNone; | return StepNone; | ||||
} | } | ||||
if (text().isEmpty()) { | if (text().isEmpty()) { | ||||
// Allow step-up with empty field. | // Allow step-up with empty field. | ||||
return StepUpEnabled; | return StepUpEnabled; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 118 Lines • Show Last 20 Lines |