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