Merge coin control features
[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 #ifdef Q_OS_MAC
21     ui->payToLayout->setSpacing(4);
22 #endif
23 #if QT_VERSION >= 0x040700
24     /* Do not move this to the XML file, Qt before 4.7 will choke on it */
25     ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
26     ui->payTo->setPlaceholderText(tr("Enter a NovaCoin address (e.g. 4Zo1ga6xuKuQ7JV7M9rGDoxdbYwV5zgQJ5)"));
27 #endif
28     setFocusPolicy(Qt::TabFocus);
29     setFocusProxy(ui->payTo);
30
31     GUIUtil::setupAddressWidget(ui->payTo, this);
32 }
33
34 SendCoinsEntry::~SendCoinsEntry()
35 {
36     delete ui;
37 }
38
39 void SendCoinsEntry::on_pasteButton_clicked()
40 {
41     // Paste text from clipboard into recipient field
42     ui->payTo->setText(QApplication::clipboard()->text());
43 }
44
45 void SendCoinsEntry::on_addressBookButton_clicked()
46 {
47     if(!model)
48         return;
49     AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
50     dlg.setModel(model->getAddressTableModel());
51     if(dlg.exec())
52     {
53         ui->payTo->setText(dlg.getReturnValue());
54         ui->payAmount->setFocus();
55     }
56 }
57
58 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
59 {
60     if(!model)
61         return;
62     // Fill in label from address book, if address has an associated label
63     QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
64     if(!associatedLabel.isEmpty())
65         ui->addAsLabel->setText(associatedLabel);
66 }
67
68 void SendCoinsEntry::setModel(WalletModel *model)
69 {
70     this->model = model;
71
72     if(model && model->getOptionsModel())
73         connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
74
75     connect(ui->payAmount, SIGNAL(textChanged()), this, SIGNAL(payAmountChanged()));
76
77     clear();
78 }
79
80 void SendCoinsEntry::setRemoveEnabled(bool enabled)
81 {
82     ui->deleteButton->setEnabled(enabled);
83 }
84
85 void SendCoinsEntry::clear()
86 {
87     ui->payTo->clear();
88     ui->addAsLabel->clear();
89     ui->payAmount->clear();
90     ui->payTo->setFocus();
91     // update the display unit, to not use the default ("BTC")
92     updateDisplayUnit();
93 }
94
95 void SendCoinsEntry::on_deleteButton_clicked()
96 {
97     emit removeEntry(this);
98 }
99
100 bool SendCoinsEntry::validate()
101 {
102     // Check input validity
103     bool retval = true;
104
105     if(!ui->payAmount->validate())
106     {
107         retval = false;
108     }
109     else
110     {
111         if(ui->payAmount->value() <= 0)
112         {
113             // Cannot send 0 coins or less
114             ui->payAmount->setValid(false);
115             retval = false;
116         }
117     }
118
119     if(!ui->payTo->hasAcceptableInput() ||
120        (model && !model->validateAddress(ui->payTo->text())))
121     {
122         ui->payTo->setValid(false);
123         retval = false;
124     }
125
126     return retval;
127 }
128
129 SendCoinsRecipient SendCoinsEntry::getValue()
130 {
131     SendCoinsRecipient rv;
132
133     rv.address = ui->payTo->text();
134     rv.label = ui->addAsLabel->text();
135     rv.amount = ui->payAmount->value();
136
137     return rv;
138 }
139
140 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
141 {
142     QWidget::setTabOrder(prev, ui->payTo);
143     QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
144     QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
145     QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
146     QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
147     return ui->payAmount->setupTabChain(ui->addAsLabel);
148 }
149
150 void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
151 {
152     ui->payTo->setText(value.address);
153     ui->addAsLabel->setText(value.label);
154     ui->payAmount->setValue(value.amount);
155 }
156
157 bool SendCoinsEntry::isClear()
158 {
159     return ui->payTo->text().isEmpty();
160 }
161
162 void SendCoinsEntry::setFocus()
163 {
164     ui->payTo->setFocus();
165 }
166
167 void SendCoinsEntry::updateDisplayUnit()
168 {
169     if(model && model->getOptionsModel())
170     {
171         // Update payAmount with the current unit
172         ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
173     }
174 }