Initial commit of NovaCoin changes
[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_WS_MAC
21     ui->payToLayout->setSpacing(4);
22 #endif
23
24 #if QT_VERSION >= 0x040700
25     ui->payTo->setPlaceholderText(tr("Enter a NovaCoin address (e.g. 4Zo1ga6xuKuQ7JV7M9rGDoxdbYwV5zgQJ5)"));
26     ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
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     clear();
72 }
73
74 void SendCoinsEntry::setRemoveEnabled(bool enabled)
75 {
76     ui->deleteButton->setEnabled(enabled);
77 }
78
79 void SendCoinsEntry::clear()
80 {
81     ui->payTo->clear();
82     ui->addAsLabel->clear();
83     ui->payAmount->clear();
84     ui->payTo->setFocus();
85     if(model && model->getOptionsModel())
86     {
87         ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
88     }
89 }
90
91 void SendCoinsEntry::on_deleteButton_clicked()
92 {
93     emit removeEntry(this);
94 }
95
96 bool SendCoinsEntry::validate()
97 {
98     // Check input validity
99     bool retval = true;
100
101     if(!ui->payAmount->validate())
102     {
103         retval = false;
104     }
105     else
106     {
107         if(ui->payAmount->value() <= 0)
108         {
109             // Cannot send 0 coins or less
110             ui->payAmount->setValid(false);
111             retval = false;
112         }
113     }
114
115     if(!ui->payTo->hasAcceptableInput() ||
116        (model && !model->validateAddress(ui->payTo->text())))
117     {
118         ui->payTo->setValid(false);
119         retval = false;
120     }
121
122     return retval;
123 }
124
125 SendCoinsRecipient SendCoinsEntry::getValue()
126 {
127     SendCoinsRecipient rv;
128
129     rv.address = ui->payTo->text();
130     rv.label = ui->addAsLabel->text();
131     rv.amount = ui->payAmount->value();
132
133     return rv;
134 }
135
136 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
137 {
138     QWidget::setTabOrder(prev, ui->payTo);
139     QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
140     QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
141     QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
142     QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
143     return ui->payAmount->setupTabChain(ui->addAsLabel);
144 }
145
146 void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
147 {
148     ui->payTo->setText(value.address);
149     ui->addAsLabel->setText(value.label);
150     ui->payAmount->setValue(value.amount);
151 }
152
153 bool SendCoinsEntry::isClear()
154 {
155     return ui->payTo->text().isEmpty();
156 }
157
158 void SendCoinsEntry::setFocus()
159 {
160     ui->payTo->setFocus();
161 }
162