Allow ammount field to be empty so that one can specify .05 instead of
[novacoin.git] / src / qt / bitcoinamountfield.cpp
1 #include "bitcoinamountfield.h"
2 #include "qvalidatedlineedit.h"
3
4 #include <QLabel>
5 #include <QLineEdit>
6 #include <QRegExpValidator>
7 #include <QHBoxLayout>
8 #include <QKeyEvent>
9
10 BitcoinAmountField::BitcoinAmountField(QWidget *parent):
11         QWidget(parent), amount(0), decimals(0)
12 {
13     amount = new QValidatedLineEdit(this);
14     amount->setValidator(new QRegExpValidator(QRegExp("[0-9]?"), this));
15     amount->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
16     amount->installEventFilter(this);
17     amount->setMaximumWidth(100);
18     decimals = new QValidatedLineEdit(this);
19     decimals->setValidator(new QRegExpValidator(QRegExp("[0-9]+"), this));
20     decimals->setMaxLength(8);
21     decimals->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
22     decimals->setMaximumWidth(75);
23
24     QHBoxLayout *layout = new QHBoxLayout(this);
25     layout->setSpacing(0);
26     layout->addWidget(amount);
27     layout->addWidget(new QLabel(QString(".")));
28     layout->addWidget(decimals);
29     layout->addWidget(new QLabel(QString(" BTC")));
30     layout->addStretch(1);
31     layout->setContentsMargins(0,0,0,0);
32
33     setLayout(layout);
34
35     setFocusPolicy(Qt::TabFocus);
36     setFocusProxy(amount);
37
38     // If one if the widgets changes, the combined content changes as well
39     connect(amount, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged()));
40     connect(decimals, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged()));
41 }
42
43 void BitcoinAmountField::setText(const QString &text)
44 {
45     const QStringList parts = text.split(QString("."));
46     if(parts.size() == 2)
47     {
48         amount->setText(parts[0]);
49         decimals->setText(parts[1]);
50     }
51     else
52     {
53         amount->setText(QString());
54         decimals->setText(QString());
55     }
56 }
57
58 void BitcoinAmountField::clear()
59 {
60     amount->clear();
61     decimals->clear();
62 }
63
64 bool BitcoinAmountField::validate()
65 {
66     bool valid = true;
67     if(decimals->text().isEmpty())
68     {
69         decimals->setValid(false);
70         valid = false;
71     }
72     return valid;
73 }
74
75 QString BitcoinAmountField::text() const
76 {
77     if(decimals->text().isEmpty())
78     {
79         return QString();
80     }
81     if(amount->text().isEmpty())
82     {
83         return QString("0.") + decimals->text();
84     }
85     return amount->text() + QString(".") + decimals->text();
86 }
87
88 // Intercept '.' and ',' keys, if pressed focus a specified widget
89 bool BitcoinAmountField::eventFilter(QObject *object, QEvent *event)
90 {
91     Q_UNUSED(object);
92     if(event->type() == QEvent::KeyPress)
93     {
94         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
95         if(keyEvent->key() == Qt::Key_Period || keyEvent->key() == Qt::Key_Comma)
96         {
97             decimals->setFocus();
98             decimals->selectAll();
99         }
100     }
101     return false;
102 }
103
104 QWidget *BitcoinAmountField::setupTabChain(QWidget *prev)
105 {
106     QWidget::setTabOrder(prev, amount);
107     QWidget::setTabOrder(amount, decimals);
108     return decimals;
109 }