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