fix clear() (clear red/invalid status)
[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(amount->text().isEmpty())
68     {
69         amount->setValid(false);
70         valid = false;
71     }
72     if(decimals->text().isEmpty())
73     {
74         decimals->setValid(false);
75         valid = false;
76     }
77     return valid;
78 }
79
80 QString BitcoinAmountField::text() const
81 {
82     if(amount->text().isEmpty() || decimals->text().isEmpty())
83     {
84         return QString();
85     }
86     return amount->text() + QString(".") + decimals->text();
87 }
88
89 // Intercept '.' and ',' keys, if pressed focus a specified widget
90 bool BitcoinAmountField::eventFilter(QObject *object, QEvent *event)
91 {
92     Q_UNUSED(object);
93     if(event->type() == QEvent::KeyPress)
94     {
95         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
96         if(keyEvent->key() == Qt::Key_Period || keyEvent->key() == Qt::Key_Comma)
97         {
98             decimals->setFocus();
99             decimals->selectAll();
100         }
101     }
102     return false;
103 }
104
105 QWidget *BitcoinAmountField::setupTabChain(QWidget *prev)
106 {
107     QWidget::setTabOrder(prev, amount);
108     QWidget::setTabOrder(amount, decimals);
109     return decimals;
110 }