fccef232bb3a87a66dcc21ffaa9f2d5d5ef92178
[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>
11 #include <QClipboard>
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     if(model)
74     {
75         ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
76     }
77 }
78
79 void SendCoinsEntry::on_deleteButton_clicked()
80 {
81     emit removeEntry(this);
82 }
83
84 bool SendCoinsEntry::validate()
85 {
86     // Check input validity
87     bool retval = true;
88
89     if(!ui->payAmount->validate())
90     {
91         retval = false;
92     }
93     else
94     {
95         if(ui->payAmount->value() <= 0)
96         {
97             // Cannot send 0 coins or less
98             ui->payAmount->setValid(false);
99             retval = false;
100         }
101     }
102
103     if(!ui->payTo->hasAcceptableInput() ||
104        (model && !model->validateAddress(ui->payTo->text())))
105     {
106         ui->payTo->setValid(false);
107         retval = false;
108     }
109
110     return retval;
111 }
112
113 SendCoinsRecipient SendCoinsEntry::getValue()
114 {
115     SendCoinsRecipient rv;
116
117     rv.address = ui->payTo->text();
118     rv.label = ui->addAsLabel->text();
119     rv.amount = ui->payAmount->value();
120
121     return rv;
122 }
123
124 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
125 {
126     QWidget::setTabOrder(prev, ui->payTo);
127     QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
128     QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
129     QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
130     QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
131     return ui->payAmount->setupTabChain(ui->addAsLabel);
132 }
133
134 void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
135 {
136     ui->payTo->setText(value.address);
137     ui->addAsLabel->setText(value.label);
138     ui->payAmount->setValue(value.amount);
139 }
140
141 bool SendCoinsEntry::isClear()
142 {
143     return ui->payTo->text().isEmpty();
144 }
145