Browse Source

introduce bitcoin amount field with split amount/decimals, to protect against mistakes (https://forum.bitcoin.org/index.php?topic=19168.0)

pull/1/head
Wladimir J. van der Laan 11 years ago
parent
commit
f193c57a63
  1. 6
      bitcoin-qt.pro
  2. 67
      src/qt/bitcoinamountfield.cpp
  3. 33
      src/qt/bitcoinamountfield.h
  4. 39
      src/qt/forms/sendcoinsdialog.ui
  5. 8
      src/qt/optionsdialog.cpp
  6. 1
      src/qt/sendcoinsdialog.cpp

6
bitcoin-qt.pro

@ -68,7 +68,8 @@ HEADERS += src/qt/bitcoingui.h \ @@ -68,7 +68,8 @@ HEADERS += src/qt/bitcoingui.h \
src/qt/monitoreddatamapper.h \
src/externui.h \
src/qt/transactiondesc.h \
src/qt/transactiondescdialog.h
src/qt/transactiondescdialog.h \
src/qt/bitcoinamountfield.h
SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
src/qt/transactiontablemodel.cpp \
src/qt/addresstablemodel.cpp \
@ -98,7 +99,8 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \ @@ -98,7 +99,8 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
src/qt/monitoreddatamapper.cpp \
src/qt/transactiondesc.cpp \
src/qt/transactiondescdialog.cpp \
src/qt/bitcoinstrings.cpp
src/qt/bitcoinstrings.cpp \
src/qt/bitcoinamountfield.cpp
RESOURCES += \
src/qt/bitcoin.qrc

67
src/qt/bitcoinamountfield.cpp

@ -0,0 +1,67 @@ @@ -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;
}

33
src/qt/bitcoinamountfield.h

@ -0,0 +1,33 @@ @@ -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

39
src/qt/forms/sendcoinsdialog.ui

@ -52,22 +52,6 @@ @@ -52,22 +52,6 @@
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="payAmount">
<property name="maximumSize">
<size>
<width>145</width>
<height>16777215</height>
</size>
</property>
<property name="toolTip">
<string>Amount of bitcoins to send (e.g. 0.05)</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="pasteButton">
<property name="toolTip">
@ -106,6 +90,13 @@ @@ -106,6 +90,13 @@
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="BitcoinAmountField" name="payAmount" native="true">
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
@ -173,6 +164,22 @@ @@ -173,6 +164,22 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>BitcoinAmountField</class>
<extends>QWidget</extends>
<header>bitcoinamountfield.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>payTo</tabstop>
<tabstop>payAmount</tabstop>
<tabstop>pasteButton</tabstop>
<tabstop>addressBookButton</tabstop>
<tabstop>sendButton</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources>
<include location="../bitcoin.qrc"/>
</resources>

8
src/qt/optionsdialog.cpp

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#include "optionsdialog.h"
#include "optionsmodel.h"
#include "bitcoinamountfield.h"
#include "monitoreddatamapper.h"
#include "guiutil.h"
@ -31,7 +32,7 @@ private: @@ -31,7 +32,7 @@ private:
QCheckBox *connect_socks4;
QLineEdit *proxy_ip;
QLineEdit *proxy_port;
QLineEdit *fee_edit;
BitcoinAmountField *fee_edit;
signals:
@ -195,12 +196,9 @@ MainOptionsPage::MainOptionsPage(QWidget *parent): @@ -195,12 +196,9 @@ MainOptionsPage::MainOptionsPage(QWidget *parent):
fee_hbox->addSpacing(18);
QLabel *fee_label = new QLabel(tr("Pay transaction &fee"));
fee_hbox->addWidget(fee_label);
fee_edit = new QLineEdit();
fee_edit->setMaximumWidth(100);
fee_edit = new BitcoinAmountField();
fee_edit->setToolTip(tr("Optional transaction fee per KB that helps make sure your transactions are processed quickly. Most transactions are 1KB. Fee 0.01 recommended."));
GUIUtil::setupAmountWidget(fee_edit, this);
fee_label->setBuddy(fee_edit);
fee_hbox->addWidget(fee_edit);
fee_hbox->addStretch(1);

1
src/qt/sendcoinsdialog.cpp

@ -23,7 +23,6 @@ SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) : @@ -23,7 +23,6 @@ SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) :
ui->setupUi(this);
GUIUtil::setupAddressWidget(ui->payTo, this);
GUIUtil::setupAmountWidget(ui->payAmount, this);
// Set initial send-to address if provided
if(!address.isEmpty())

Loading…
Cancel
Save