b312b9792e674d471055a9a577691be547b4dc7c
[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     return amount->text() + QString(".") + decimals->text();
82 }
83
84 // Intercept '.' and ',' keys, if pressed focus a specified widget
85 bool BitcoinAmountField::eventFilter(QObject *object, QEvent *event)
86 {
87     Q_UNUSED(object);
88     if(event->type() == QEvent::KeyPress)
89     {
90         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
91         if(keyEvent->key() == Qt::Key_Period || keyEvent->key() == Qt::Key_Comma)
92         {
93             decimals->setFocus();
94             decimals->selectAll();
95         }
96     }
97     return false;
98 }
99
100 QWidget *BitcoinAmountField::setupTabChain(QWidget *prev)
101 {
102     QWidget::setTabOrder(prev, amount);
103     QWidget::setTabOrder(amount, decimals);
104     return decimals;
105 }