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