allow multiple units in bitcoin amount widget (for example, for sending) using a...
[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     else
91     {
92         if(ui->payAmount->value() <= 0)
93         {
94             // Cannot send 0 coins or less
95             ui->payAmount->setValid(false);
96             retval = false;
97         }
98     }
99
100
101     if(!ui->payTo->hasAcceptableInput() ||
102        (model && !model->validateAddress(ui->payTo->text())))
103     {
104         ui->payTo->setValid(false);
105         retval = false;
106     }
107
108     return retval;
109 }
110
111 SendCoinsRecipient SendCoinsEntry::getValue()
112 {
113     SendCoinsRecipient rv;
114
115     rv.address = ui->payTo->text();
116     rv.label = ui->addAsLabel->text();
117     rv.amount = ui->payAmount->value();
118
119     return rv;
120 }
121
122 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
123 {
124     QWidget::setTabOrder(prev, ui->payTo);
125     QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
126     QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
127     QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
128     QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
129     return ui->payAmount->setupTabChain(ui->addAsLabel);
130 }