Bugfix: Replace "URL" with "URI" where we aren't actually working with URLs
[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 Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
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 no label is filled in yet
63     if(ui->addAsLabel->text().isEmpty())
64         ui->addAsLabel->setText(model->getAddressTableModel()->labelForAddress(address));}
65
66 void SendCoinsEntry::setModel(WalletModel *model)
67 {
68     this->model = model;
69     clear();
70 }
71
72 void SendCoinsEntry::setRemoveEnabled(bool enabled)
73 {
74     ui->deleteButton->setEnabled(enabled);
75 }
76
77 void SendCoinsEntry::clear()
78 {
79     ui->payTo->clear();
80     ui->addAsLabel->clear();
81     ui->payAmount->clear();
82     ui->payTo->setFocus();
83     if(model && model->getOptionsModel())
84     {
85         ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
86     }
87 }
88
89 void SendCoinsEntry::on_deleteButton_clicked()
90 {
91     emit removeEntry(this);
92 }
93
94 bool SendCoinsEntry::validate()
95 {
96     // Check input validity
97     bool retval = true;
98
99     if(!ui->payAmount->validate())
100     {
101         retval = false;
102     }
103     else
104     {
105         if(ui->payAmount->value() <= 0)
106         {
107             // Cannot send 0 coins or less
108             ui->payAmount->setValid(false);
109             retval = false;
110         }
111     }
112
113     if(!ui->payTo->hasAcceptableInput() ||
114        (model && !model->validateAddress(ui->payTo->text())))
115     {
116         ui->payTo->setValid(false);
117         retval = false;
118     }
119
120     return retval;
121 }
122
123 SendCoinsRecipient SendCoinsEntry::getValue()
124 {
125     SendCoinsRecipient rv;
126
127     rv.address = ui->payTo->text();
128     rv.label = ui->addAsLabel->text();
129     rv.amount = ui->payAmount->value();
130
131     return rv;
132 }
133
134 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
135 {
136     QWidget::setTabOrder(prev, ui->payTo);
137     QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
138     QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
139     QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
140     QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
141     return ui->payAmount->setupTabChain(ui->addAsLabel);
142 }
143
144 void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
145 {
146     ui->payTo->setText(value.address);
147     ui->addAsLabel->setText(value.label);
148     ui->payAmount->setValue(value.amount);
149 }
150
151 bool SendCoinsEntry::isClear()
152 {
153     return ui->payTo->text().isEmpty();
154 }
155
156 void SendCoinsEntry::setFocus()
157 {
158     ui->payTo->setFocus();
159 }
160