preparations for multiple unit (uBTC, mBTC, BTC) support, fix amount entry issue
[novacoin.git] / src / qt / sendcoinsentry.cpp
1 #include "sendcoinsentry.h"
2 #include "ui_sendcoinsentry.h"
3 #include "guiutil.h"
4 #include "bitcoinunits.h"
5 #include "addressbookpage.h"
6 #include "walletmodel.h"
7 #include "addresstablemodel.h"
8
9 #include "qapplication.h"
10 #include "qclipboard.h"
11
12 #include <QDebug>
13
14 SendCoinsEntry::SendCoinsEntry(QWidget *parent) :
15     QFrame(parent),
16     ui(new Ui::SendCoinsEntry),
17     model(0)
18 {
19     ui->setupUi(this);
20
21 #if QT_VERSION >= 0x040700
22     ui->payTo->setPlaceholderText(tr("Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
23     ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
24 #endif
25     setFocusPolicy(Qt::TabFocus);
26     setFocusProxy(ui->payTo);
27
28     GUIUtil::setupAddressWidget(ui->payTo, this);
29 }
30
31 SendCoinsEntry::~SendCoinsEntry()
32 {
33     delete ui;
34 }
35
36 void SendCoinsEntry::on_pasteButton_clicked()
37 {
38     // Paste text from clipboard into recipient field
39     ui->payTo->setText(QApplication::clipboard()->text());
40 }
41
42 void SendCoinsEntry::on_addressBookButton_clicked()
43 {
44     AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
45     dlg.setModel(model->getAddressTableModel());
46     if(dlg.exec())
47     {
48         ui->payTo->setText(dlg.getReturnValue());
49         ui->payAmount->setFocus();
50     }
51 }
52
53 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
54 {
55     ui->addAsLabel->setText(model->getAddressTableModel()->labelForAddress(address));
56 }
57
58 void SendCoinsEntry::setModel(WalletModel *model)
59 {
60     this->model = model;
61 }
62
63 void SendCoinsEntry::setRemoveEnabled(bool enabled)
64 {
65     ui->deleteButton->setEnabled(enabled);
66 }
67
68 void SendCoinsEntry::clear()
69 {
70     ui->payTo->clear();
71     ui->addAsLabel->clear();
72     ui->payAmount->clear();
73     ui->payTo->setFocus();
74 }
75
76 void SendCoinsEntry::on_deleteButton_clicked()
77 {
78     emit removeEntry(this);
79 }
80
81 bool SendCoinsEntry::validate()
82 {
83     // Check input validity
84     bool retval = true;
85
86     if(!ui->payAmount->validate())
87     {
88         retval = false;
89     }
90
91     if(!ui->payTo->hasAcceptableInput() ||
92        (model && !model->validateAddress(ui->payTo->text())))
93     {
94         ui->payTo->setValid(false);
95         retval = false;
96     }
97
98     return retval;
99 }
100
101 SendCoinsRecipient SendCoinsEntry::getValue()
102 {
103     SendCoinsRecipient rv;
104
105     rv.address = ui->payTo->text();
106     rv.label = ui->addAsLabel->text();
107     BitcoinUnits::parse(BitcoinUnits::BTC, ui->payAmount->text(), &rv.amount);
108
109     return rv;
110 }
111
112 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
113 {
114     QWidget::setTabOrder(prev, ui->payTo);
115     QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
116     QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
117     QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
118     QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
119     return ui->payAmount->setupTabChain(ui->addAsLabel);
120 }