Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce bitcoin amount field with split amount/decimals, to protect…
… against mistakes (https://forum.bitcoin.org/index.php?topic=19168.0)
- Loading branch information
Showing
6 changed files
with
130 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "bitcoinamountfield.h" | ||
|
||
#include <QLabel> | ||
#include <QLineEdit> | ||
#include <QRegExpValidator> | ||
#include <QHBoxLayout> | ||
#include <QKeyEvent> | ||
|
||
BitcoinAmountField::BitcoinAmountField(QWidget *parent): | ||
QWidget(parent), amount(0), decimals(0) | ||
{ | ||
amount = new QLineEdit(this); | ||
amount->setValidator(new QRegExpValidator(QRegExp("[0-9]+"), this)); | ||
amount->setAlignment(Qt::AlignRight|Qt::AlignVCenter); | ||
amount->installEventFilter(this); | ||
amount->setMaximumWidth(80); | ||
decimals = new QLineEdit(this); | ||
decimals->setValidator(new QRegExpValidator(QRegExp("[0-9]+"), this)); | ||
decimals->setMaxLength(8); | ||
decimals->setAlignment(Qt::AlignLeft|Qt::AlignVCenter); | ||
decimals->setMaximumWidth(75); | ||
|
||
QHBoxLayout *layout = new QHBoxLayout(this); | ||
layout->setSpacing(0); | ||
layout->addWidget(amount); | ||
layout->addWidget(new QLabel(QString("."))); | ||
layout->addWidget(decimals); | ||
layout->addStretch(1); | ||
|
||
setFocusPolicy(Qt::TabFocus); | ||
setLayout(layout); | ||
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())); | ||
} | ||
|
||
void BitcoinAmountField::setText(const QString &text) | ||
{ | ||
const QStringList parts = text.split(QString(".")); | ||
if(parts.size() == 2) | ||
{ | ||
amount->setText(parts[0]); | ||
decimals->setText(parts[1]); | ||
} | ||
} | ||
|
||
QString BitcoinAmountField::text() const | ||
{ | ||
return amount->text() + QString(".") + decimals->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) | ||
{ | ||
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); | ||
if(keyEvent->key() == Qt::Key_Period || keyEvent->key() == Qt::Key_Comma) | ||
{ | ||
decimals->setFocus(); | ||
} | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef BITCOINFIELD_H | ||
#define BITCOINFIELD_H | ||
|
||
#include <QWidget> | ||
|
||
QT_BEGIN_NAMESPACE | ||
class QLineEdit; | ||
QT_END_NAMESPACE | ||
|
||
class BitcoinAmountField: public QWidget | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged USER true); | ||
public: | ||
explicit BitcoinAmountField(QWidget *parent = 0); | ||
|
||
void setText(const QString &text); | ||
QString text() const; | ||
|
||
signals: | ||
void textChanged(); | ||
|
||
protected: | ||
// Intercept '.' and ',' keys, if pressed focus a specified widget | ||
bool eventFilter(QObject *object, QEvent *event); | ||
|
||
private: | ||
QLineEdit *amount; | ||
QLineEdit *decimals; | ||
}; | ||
|
||
|
||
#endif // BITCOINFIELD_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters