convert to full tab-based ui
[novacoin.git] / src / qt / bitcoinamountfield.cpp
1 #include "bitcoinamountfield.h"
2
3 #include <QLabel>
4 #include <QLineEdit>
5 #include <QRegExpValidator>
6 #include <QHBoxLayout>
7 #include <QKeyEvent>
8
9 BitcoinAmountField::BitcoinAmountField(QWidget *parent):
10         QWidget(parent), amount(0), decimals(0)
11 {
12     amount = new QLineEdit(this);
13     amount->setValidator(new QRegExpValidator(QRegExp("[0-9]+"), this));
14     amount->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
15     amount->installEventFilter(this);
16     amount->setMaximumWidth(100);
17     decimals = new QLineEdit(this);
18     decimals->setValidator(new QRegExpValidator(QRegExp("[0-9]+"), this));
19     decimals->setMaxLength(8);
20     decimals->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
21     decimals->setMaximumWidth(75);
22
23     QHBoxLayout *layout = new QHBoxLayout(this);
24     layout->setSpacing(0);
25     layout->addWidget(amount);
26     layout->addWidget(new QLabel(QString(".")));
27     layout->addWidget(decimals);
28     layout->addWidget(new QLabel(QString(" BTC")));
29     layout->addStretch(1);
30     layout->setContentsMargins(0,0,0,0);
31
32     setFocusPolicy(Qt::TabFocus);
33     setLayout(layout);
34     setFocusProxy(amount);
35
36     // If one if the widgets changes, the combined content changes as well
37     connect(amount, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged()));
38     connect(decimals, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged()));
39 }
40
41 void BitcoinAmountField::setText(const QString &text)
42 {
43     const QStringList parts = text.split(QString("."));
44     if(parts.size() == 2)
45     {
46         amount->setText(parts[0]);
47         decimals->setText(parts[1]);
48     }
49     else
50     {
51         amount->setText(QString());
52         decimals->setText(QString());
53     }
54 }
55
56 QString BitcoinAmountField::text() const
57 {
58     if(amount->text().isEmpty() || decimals->text().isEmpty())
59         return QString();
60     return amount->text() + QString(".") + decimals->text();
61 }
62
63 // Intercept '.' and ',' keys, if pressed focus a specified widget
64 bool BitcoinAmountField::eventFilter(QObject *object, QEvent *event)
65 {
66     Q_UNUSED(object);
67     if(event->type() == QEvent::KeyPress)
68     {
69         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
70         if(keyEvent->key() == Qt::Key_Period || keyEvent->key() == Qt::Key_Comma)
71         {
72             decimals->setFocus();
73             decimals->selectAll();
74         }
75     }
76     return false;
77 }