Skip to content

Commit

Permalink
introduce bitcoin amount field with split amount/decimals, to protect…
Browse files Browse the repository at this point in the history
  • Loading branch information
laanwj committed Jun 20, 2011
1 parent 18b99e3 commit f193c57
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 24 deletions.
6 changes: 4 additions & 2 deletions bitcoin-qt.pro
Expand Up @@ -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 \
Expand Down Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions src/qt/bitcoinamountfield.cpp
@@ -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 changes: 33 additions & 0 deletions src/qt/bitcoinamountfield.h
@@ -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 changes: 23 additions & 16 deletions src/qt/forms/sendcoinsdialog.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
8 changes: 3 additions & 5 deletions src/qt/optionsdialog.cpp
@@ -1,5 +1,6 @@
#include "optionsdialog.h"
#include "optionsmodel.h"
#include "bitcoinamountfield.h"
#include "monitoreddatamapper.h"
#include "guiutil.h"

Expand Down Expand Up @@ -31,7 +32,7 @@ class MainOptionsPage : public QWidget
QCheckBox *connect_socks4;
QLineEdit *proxy_ip;
QLineEdit *proxy_port;
QLineEdit *fee_edit;
BitcoinAmountField *fee_edit;

signals:

Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/qt/sendcoinsdialog.cpp
Expand Up @@ -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())
Expand Down

0 comments on commit f193c57

Please sign in to comment.